是的。每次将用户的输入插入查询字符串时,它都是易受攻击的。如果month
将是:
5' AND '8'; DROP TABLE timeslots;--
你可能会遇到严重的麻烦。更不用说删除数据库等。
我没有完全复制这个查询,但是由于使用了acts_as_paranoid插件,在我的查询中类似的[我必须添加]:
SomeModel.pluck(:id)
=> [1, 2, 4, 3, 5, 6]
abc = 'a\');delete from some_models where id=6;--'
User.where("name = '#{abc}'")
=> []
SomeModel.pluck(:id)
=> [1, 2, 4, 3, 5] # please note that record with id 6 was deleted!
攻击可能的原因是我可以提供'
和--
(开始评论)。当你使用建议的方式时,即使用 .where("name = ?", "my_name"),那么攻击就不可能了。看一下这个:
abc = 'a\');delete from some_models where id=5;--'
User.where("name = ?", abc)
=> []
SomeModel.pluck(:id)
=> [1, 2, 4, 3, 5] # this time record with id 5 was not deleted
这是第一个查询:
User Load (1.5ms) SELECT "users".* FROM "users" WHERE ("users"."deleted_at" IS NULL) AND (name = 'a');delete from some_models where id=6;--')
这是第二个
User Load (1.0ms) SELECT "users".* FROM "users" WHERE ("users"."deleted_at" IS NULL) AND (name = 'a'');delete from some_models where id=5;--')
请注意'
第二个中的附加内容-query(name = 'a'')