我想对我的Rails 4应用程序使用以下查询,但我担心SQL注入攻击:
@persons = People.where("persons.name LIKE ?", "%#{params[:search]}%")有人能告诉我写上述声明的安全方法吗?我尝试过以下几种方法,但不确定它是否具有SQL注入的安全性:
search = "%" + params[:search] + "%"
@persons = People.where("persons.name LIKE ?", search)谢谢!
发布于 2014-08-21 01:23:37
正如zishe所说,你的例子很好。
每当向方法使用问号并将另一个参数作为搜索查询传递时,它就会清除查询字符串。
当您手动执行字符串连接以创建查询时,这是危险的,例如:
Project.where("name = '#{params[:name]}'")Click here for more information
发布于 2014-08-21 01:00:24
你们的陈述都是安全的。你也可以这样写:
@persons = People.where("persons.name LIKE concat('%', ?, '%')", params[:search])https://stackoverflow.com/questions/25416667
复制相似问题