2017-01-15 82 views
0

如何防止SQL注入,而从数据库中获取数据时从用户输入收到的使用参数:如何防止SQL注入的参数使用CakePHP

if(isset($_GET['cityval']) && $_GET['cityval'] !=''){ 

    $city = $this->request->query('cityval'); 

     $searching .= " and college_city in ($city) "; 
    } else { 
     $searching .= ""; 
    } 
    if(isset($_GET['scholarship']) && $_GET['scholarship'] !=''){ 
     $searching .= " and college_scholarship = '".$_GET['scholarship']."' "; 
    } else { 
     $searching .= ""; 
    } 

我的主查询低于

$search = $this->Search->query("select * from colleges where college_id!='' and status='active' $searching order by $order desc limit $start, 10 "); 

回答

0

Cake ORM似乎使用PDO:

在封面下方,查询生成器使用PDO预处理语句 可防止SQL注入攻击。

参考:https://book.cakephp.org/3.0/en/orm/query-builder.html

不幸的是因为你不使用ORM蛋糕既不PDO准备语句要创建的查询方式是脆弱的。

如果要使用原始查询,你可以做这样的事情来保护你的代码:

// Add this in the beginning of the file/code: 
use Cake\Datasource\ConnectionManager; 

// Replace connection_name with the name of your connection (maybe it's "default") 
$connection = ConnectionManager::get('connection_name'); 

$bindList = []; 

$city = $this->request->query('cityval'); 

// PDO as a limitation for the parameter markers. See in the comments below 
$cityList = array_filter(explode(',', $city), function($item) { 
    return preg_match('/^\d+$/', $item); 
}); 

$csvCity = implode(',', $cityList); 


$scholarship = $this->request->query('scholarship'); 

if (!empty($csvCity)) { 
    $searching .= " and college_city in ($csvCity)"; 
} 

if (!empty($scholarship)) { 
    $searching .= " and college_scholarship = :scholarship"; 
    $bindList['scholarship'] = $scholarship; 
} 

$stmt = $connection->prepare($searching); 
$stmt->bind($bindList); 
$stmt->execute(); 

// Read all rows. 
$rows = $stmt->fetchAll('assoc'); 

// Read rows through iteration. 
foreach ($rows as $row) { 
    // Do work 
} 

PDO有一个限制,因为这有使用SQL IN()子句中没有编写有道语句(将值绑定到它),所以我们需要像在代码中那样手动解析该子句中的值。

PDO Prepare手册页:

注:参数标记可以代表一个完整的数据只包含文字。 任何部分的文字,也不是关键字,也不是标识符,也不是任何可以使用参数绑定的任意查询部分。例如,您 无法将多个值绑定到SQL语句的IN()子句 中的单个参数。

参考

+0

嗨,这将返回一个数组,并且如果(!empty($ city)){PDO作为参数标记的限制,如何将数组放入查询 – user3198563

+0

中。 $ cityList = array_filter(explode(',',$ city),function($ item){ return preg_match('/^\ d + $ /',$ item); }); $ searching。=“and college_city in($ cityList)”; } – user3198563

+0

我刚更新了代码。对不起。我忘了添加implode(),因为我没有运行/测试代码 – wcomnisky

2

不要使用原始查询。只需使用CakePHP提供的查询生成器,它将阻止注入。请参阅在线CakePHP书籍以获取更多信息。

在CakePHP中需要使用原始查询是非常罕见的。

4

开始实际使用框架或根本不使用它。你没有使用请求对象,你没有使用ORM,你正在和框架一起工作。从您的代码中可以清楚地看出,您没有花时间阅读手册。

wcomniskys答案有一吨不是必需的代码,也不是这个答案使用框架的目标方式。

如果你关心创建一些写得很好的代码,那么你真的应该开始做官方文档​​的博客教程。如果不停止阅读,并且做wcomniskys建议。它可能有效,但它不是好的代码,也不是使用框架的正确方式,也不是最简单的解决方案。如果你没有兴趣正确地做事,你现在可以停止阅读。


你试图做的显然是通过获取参数进行搜索。有一个美好的插件,使得它很容易https://github.com/FriendsOfCake/search

这可能是实际上是用插件很简单:

$query = $this->Colleges->find('search', [ 
    'search' => $this->request->query 
]); 
$this->set('results', $this->Paginator->paginate($query)); 

PARAMS本身搜索将在模型层处理,检查该插件文件。框架将负责消毒输入。

上述要求,你实际上了解框架非常基本的和你知道你做了什么。如果你对任何原因没有兴趣不使用框架提供的内容,那么而不是会更好,因为你只会堆积不可维护的代码并导致开销。