在自定义条件下清理 SQL
我需要创建一个简单的搜索,但我无法使用 Sphinx。
这是我写的:
keywords = input.split(/\s+/)
queries = []
keywords.each do |keyword|
queries << sanitize_sql_for_conditions(
"(classifications.species LIKE '%#{keyword}%' OR
classifications.family LIKE '%#{keyword}%' OR
classifications.trivial_names LIKE '%#{keyword}%' OR
place LIKE '%#{keyword}%')")
end
options[:conditions] = queries.join(' AND ')
现在,sanitize_sql_for_conditions 不起作用! 它返回只是返回原始字符串。
如何重写这段代码来逃避恶意代码?
I need to create a simple search but I can't afford to use Sphinx.
Here's what I wrote:
keywords = input.split(/\s+/)
queries = []
keywords.each do |keyword|
queries << sanitize_sql_for_conditions(
"(classifications.species LIKE '%#{keyword}%' OR
classifications.family LIKE '%#{keyword}%' OR
classifications.trivial_names LIKE '%#{keyword}%' OR
place LIKE '%#{keyword}%')")
end
options[:conditions] = queries.join(' AND ')
Now, sanitize_sql_for_conditions does NOT work! It returns simply returns the original string.
How can I rewrite this code to escape malicious code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将“#{keyword}”替换为“?”,则可以执行类似的操作。 使用问号将自动清理 SQL。
If you replace the "#{keyword}" with a "?", you can do something like this. Using the question mark will automatically sanitize SQL.
我在 ActiveRecord 中使用了很多自定义条件,但我喜欢将它们打包在条件数组数组中,然后使用 ? 组合它们。 value 让 AR 自动对它们进行消毒:
I use a lot of custom conditions in ActiveRecord, but I like to package them in an array of condition arrays, then combine 'em, using the ? value lets AR santize them automatically: