我知道每个人都在使用DB::raw()执行原始sql。
我自己使用以下方法:
DB::select("query string?", [$var]);
DB::insert("query string?", [$var]);
DB::update("query string?", [$var]);
DB::delete("query string?", [$var]);Laravel是否已经为SQL注入提供了防御,或者我仍然需要逃避我的变量?
发布于 2018-05-15 08:59:13
是的,laravel为sql注入提供了防御。
这是因为查询生成器在后台使用PDO,因此我们知道有一种将参数绑定到查询的方法,因此它将清理绑定的变量。
现在,正如您所看到的,任意(原始)查询是在查询生成器中使用例如DB::select()方法完成的。让我们看看select()方法在Illuminate\Database\Connection中,看看它是否有任何方法绑定我们的参数。让我们深入研究:
public function select($query, $bindings = [], $useReadPdo = true)
{
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
if ($this->pretending()) {
return [];
}
// For select statements, we'll simply execute the query and return an array
// of the database result set. Each element in the array will be a single
// row from the database table, and will either be an array or objects.
$statement = $this->prepared($this->getPdoForSelect($useReadPdo)
->prepare($query));
$this->bindValues($statement, $this->prepareBindings($bindings));
$statement->execute();
return $statement->fetchAll();
});
}您的查询可以如下所示:
$someVariable = Input::get("some_variable");
DB::select("SELECT * FROM some_table WHERE some_col = :somevariable", array(
'somevariable' => $someVariable,
));https://stackoverflow.com/questions/50345442
复制相似问题