在 Rails 上安全地转义用于连接、限制、选择等(而非条件)的 SQL 片段的字符串
在 Ruby on Rails 中,对于条件,很容易进行 SQL 注入防护查询:
:conditions => ["title = ?", title]
其中标题来自外部、来自 Web 表单或类似的东西。
但是,如果您在查询的其他部分使用 SQL 片段,例如:
:select => "\"#{title}\" AS title" # I do have something like this in one instance
:joins => ["LEFT JOIN blah AS blah2 ON blah2.title = \"#{title}\""]
有没有办法正确转义这些字符串?
In Ruby on Rails, for conditions, it's easy to make SQL-injection-proof queries:
:conditions => ["title = ?", title]
where title comes from the outside, from a web form or something like that.
But what if you are using SQL fragments in other parts of the query, like:
:select => "\"#{title}\" AS title" # I do have something like this in one instance
:joins => ["LEFT JOIN blah AS blah2 ON blah2.title = \"#{title}\""]
Is there a way to properly escape those strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常在 Rails 中,连接是作为表示 id 连接的符号(或二阶连接的散列)完成的,并且您可以使用条件对其进行过滤。如果您需要如图所示执行此操作,则可以使用 ActiveRecord 的
sanitize_sql_array< /code>
清理 SQL 字符串,如下所示:
Typically in Rails, joins are done as a symbol (or as a hash for second-order joins) representing an id join, and you use the conditions to filter it down. If you need to do it as shown, then you can use ActiveRecord's
sanitize_sql_array
to clean a SQL string, like this: