来自 Rails 的这段代码片段容易受到 sqli 的攻击吗?如果是这样,有效载荷是多少

Is this code snippet from Rails vulnerable to sqli ? if so what is the payload

我习惯于使用 PHP 和 Prepared 语句,现在当我查看来自 rails 的以下代码时(因为我是 rails 的新手并且不确定语法和东西),我想知道代码是否容易被 SQLI 注入

代码片段(控制器),参数 q 是来自搜索框的值:

def index
    query = %w(% %).join params[:q].to_s.gsub('%', '\%').gsub('_', '\_')
    @posts = Post.where("name LIKE ? OR body LIKE ?", query, query).order(params[:order])
  end

谢谢

你所拥有的是为了安全。如果不是,那就是 Rails.

中的错误

.where 接受多种格式的条件。一个是原始字符串。如果您自己构建该字符串,则所有赌注都将取消并且您很容易受到攻击。

正如最近的一些文档所说:

Note that building your own string from user input may expose your application to injection attacks if not done properly. As an alternative, it is recommended to use one of the following methods.

换句话说,所有 "following"(所有其他支持的方式)做事的方式都可以。

因此,如果您 .where 使用字符串参数以外的任何参数,您应该没问题。

只要您不在 where 子句中插入,它就应该是安全的。 SQL注入代码有一些很好的例子here