你能否告诉这两个代码片段在 yii 中是否安全。香精1:
$numberOfRows = $this->updateAll(array('full_path' => $target, 'title' => $name, 'machine_name' => $name), 'full_path = :path', array(':path' => $path));
我应该在这个查询中转义 $target 和 $name 吗?
片段 2:
$sql = "UPDATE folders";
$sql .= " SET full_path = CONCAT('" . $target . "',SUBSTR(full_path, " . (strlen($path) + 1) . ", LENGTH(full_path)-1))";
$sql .= " WHERE full_path LIKE '" . $path . "%'";
$command = $this->dbConnection->createCommand($sql);
$command->execute();
我应该在这两个片段中使用 CDbConnection::quoteValue() 或类似的东西来逃避 $target 和 full_path 吗?我还介绍了如何在 Fragment 2 中转义路径以避免与 LIKE (%, _) 一起使用的特殊符号的问题。
我使用绑定和转义 %_ 对片段 2 进行了更改:
$sql = "UPDATE folders";
$sql .= " SET full_path = CONCAT(:target, SUBSTR(full_path, " . (strlen($path) + 1) . ", LENGTH(full_path)-1))";
$sql .= " WHERE full_path LIKE :pathFilter";
$command = $this->dbConnection->createCommand($sql);
//escape %_ that can be used in SQL LIKE expression
$pathFilter = addcslashes($path, '%_') . '%';
$command->bindParam(":pathFilter", $pathFilter, PDO::PARAM_STR);
$command->bindParam(":target", $target, PDO::PARAM_STR);
$command->execute();
这是对的吗?有没有更优雅的方法呢?