在 CakePHP3 全文索引匹配中防止 SQL 注入

Preventing SQL injections in CakePHP3 full text index match

我需要在 CakePHP3 中对 table 进行全文搜索。我这样搜索:

$ids = $this->I18n->find('list', [
    'valueField' => 'foreign_key',
    'conditions' => [
        'field IN' => ['name', 'description_search', 'description_short_search'],
        'model' => 'Products',
        'locale' => $lang,
        'MATCH (content) AGAINST ("'.$filteredValue.'")',
    ],
])->toArray();

这有效,但不安全 - 这是 SQL 注入的完美位置。我尝试用参数 (MATCH (content) AGAINST (?)' => $filteredValue) 替换它,但这会生成错误 Invalid parameter number: mixed named and positional parameters.

我该如何防范?

(是的,这是与标准 i18n table 的匹配。有点 hack,但与问题无关。)

使用绑定

这不再是绑定的工作方式,在 CakePHP 3.x 中,您必须使用 Query::bind() 方法(或 StatementInterface::bindValue() 使用自定义语句时)。

$ids = $this->I18n
    ->find('list', [
        'valueField' => 'foreign_key',
        'conditions' => [
            'field IN' => ['name', 'description_search', 'description_short_search'],
            'model' => 'Products',
            'locale' => $lang,
            'MATCH (content) AGAINST (:against)',
        ],
    ])
    ->bind(':against', $filteredValue, 'string')
    ->toArray();

另见