【问题标题】:Is this little Doctrine2 dynamic SQL enough safe of injection?这个小Doctrine2动态SQL注入足够安全吗?
【发布时间】:2012-03-20 12:04:24
【问题描述】:

我知道使用像 Doctrine2 这样的 ORM 来构建查询是安全的,这意味着默认情况下会转义参数。

但我猜这并不是那么明显当使用文字时并且当这个文字直接来自查询字符串时:

    $builder = $this->getRepository()->createQueryBuilder('e');
    $request = $this->getRequest();

    // Loop each allowed filter field and check if exists in $request
    foreach($this->getFilterFields() as $filter) :

        // Skip falsy values in $request
        if(!$value = $request->get($filter)) continue;

        // Add OR LIKE %$value% where $value is GET paramter
        $like = $builder->expr()->literal("%$value%");
        $builder->orWhere($builder->expr()->like("e.$filter", $like));

    endforeach;

是否应该以某种方式提高安全性?

【问题讨论】:

    标签: sql doctrine symfony doctrine-orm


    【解决方案1】:

    $queryBuilder->expr 返回一个 ExpressionBuilder 对象。在 ExpressionBuilder 中,我们发现:

    public function literal($input, $type = null)
    {
        return $this->connection->quote($input, $type);
    }
    

    所以文字确实会被引用并且应该可以使用。

    我们还发现:

    public function like($x, $y)
    {
        return $this->comparison($x, 'LIKE', $y);
    }
    public function comparison($x, $operator, $y)
    {
        return $x . ' ' . $operator . ' ' . $y;
    }
    

    $y 很好,因为它首先通过文字。确实要小心$x。只要您的 filterFields 是内部的,就没有问题。如果它们来自用户,那么您需要确保它们是有效的。

    【讨论】:

    • 谢谢。是的,$this->getFilterFields() 列出所有有效的搜索字段,因此任何无效的参数都将被忽略。
    猜你喜欢
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多