SQL 注入和 Postgres“CREATE SCHEMA”
在 postgres 中使用 CREATE SCHEMA schema_name
时,如果需要用户输入 schema_name
参数,如何防止 SQL 注入?
schema_name
不能是带引号的值,因此 ActiveRecord::Base.sanitize()
将不起作用。 (如果您使用导轨)。
When using CREATE SCHEMA schema_name
in postgres, how do you prevent SQL injection of the schema_name
parameter, if it needs to be user-input?
schema_name
can't be a quoted value, so for example ActiveRecord::Base.sanitize()
won't work. (if your using rails).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
允许用户创建模式(通常是执行 DDL)是相当不寻常的(甚至是可怕的)。您确定要这样做吗?
在任何情况下,您都可以做通常的事情:要么清理您的输入(我猜如果您允许用户创建模式,至少您可以限制允许的名称 - 例如仅字母数字等),和/或使用准备好的语句,绑定架构名称(这取决于您的语言/环境)。
It's rather unusual (even scary) to allow users to create schemas (in general to do DDL). Are you sure you want to do that?
In any case, you can do the usual things: either sanitize you input (and I guess that if you allow the user to create schames at least you can restrict the allowed names - eg only alphanumeric, etc), and/or use a prepared statement, binding the schema name (this depends on your language/environment).
用户永远不应该拥有 DDL 权限,他们不需要它,而且它给了他们太多的权力。
您可以使用带有 quote_ident 的 SQL 函数来创建模式并避免 SQL 注入:
Users should never have DDL permissions, they don't need it and it gives them too much power.
You could use a SQL function using quote_ident, to create a schema and avoid SQL injection:
我知道这可能不是您正在寻找的答案,但是如果您的设计让用户决定您的架构名称,但您不信任您的用户,那么您确实需要重新设计。让用户决定架构名称类似于要求用户决定类和函数名称。
如果你真的真的想这样做,我会使用正则表达式来限制为仅 16 个字母数字字符,以字母字符开头,在强制小写后 (/^[az][a-z0-9]{ 1,15}$/)。您还需要显式过滤掉“public”作为模式名称、information_schema(如果也允许使用下划线)和其他内置模式名称。
再次,我愿意打赌您真的不想这样做,并且有更好的方法来解决您的根本问题。
I know this probably isn't the answer you are looking for, but if your design has users deciding your schema names, but you don't trust your users, you really need to do a re-design. Having your users decide on a schema name is akin to asking users to decide on class and function names.
If you really, really want to do this, I would use a regular expression to limit to something like 16 alphanumeric characters only, starting with an alphabetic character, after being forced lowercase (/^[a-z][a-z0-9]{1,15}$/). You will also need to explicitly filter out "public" as a schema name, information_schema (if you allow underscores as well), and other built-in schema names.
Again, I'll be willing to bet that you really don't want to do this, and that there's a better way to solve your underlying problem.