duanbeng6709 2018-04-26 09:38
浏览 228
已采纳

如何在Yii 1.1中保护原始sql CDbCriteria条件(反sql注入)?

I'm dealing with an Yii 1.1 app.

Part of the search method use CDbCriteria and raw sql.

I was wondering how can I still use the raw sql code and make it more secure from sql injections?

Here is a code example:

if (!empty($this->textToSearch)) {
    $text_condition = <<<EOC
(
    topic LIKE "%{$this->textToSearch}%" OR
    main LIKE "%{$this->textToSearch}%" OR  
)
EOC;
    $criteria->addCondition($text_condition);
}

Any suggestions?

  • 写回答

1条回答 默认 最新

  • dsbruqxgt820011351 2018-04-26 10:00
    关注

    You should use params to pass untrusted data to query. Note that %, _ and \ chars has special meaning in SQL query, so you need to escape it too.

    $criteria = new CDbCriteria();
    if (!empty($this->textToSearch)) {
        $text_condition = <<<EOC
    (
        topic LIKE :text_to_search OR
        main LIKE :text_to_search  
    )
    EOC;
        $criteria->addCondition($text_condition);
        $textToSearch = strtr($this->textToSearch, [
            '%' => '\%',
            '_' => '\_',
            '\\' => '\\\\',
        ]);
        $criteria->params[':text_to_search'] = "%{$textToSearch}%";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入