2012-11-12 55 views
2

我對FuelPHP框架頗爲陌生。現在我正在爲位置列表實現「自動完成」。在FuelPHP中清理用戶輸入

我的代碼如下所示:

public function action_search($term=null){ 
    $clean_query = Security::clean($term); 
    $data["locations"] = array(); 
    if ($clean_query != "") { 
     $data["locations"] = Model_Orm_Location::query() 
          ->where("title", "like", $clean_query."%") 
          ->get(); 
    } 

    $response = Response::forge(View::forge("location/search", $data)); 
    $response->set_header("Content-Type","application/json"); 
    return $response; 
} 

正如你所看到的,我一個串聯聲明LIKE,它有點感覺對我不好。此代碼是否對SQL注入安全?如果是,那是因爲:

  • Security::clean將刪除所有的爛攤子;
  • where()在ORM查詢中會做過濾嗎?

回答

4

看着implementation of Security::clean in the source code of core/class/security.php,在你的情況下,應用的過濾器取決於configuration security.input_filter, which is empty by default。所以沒有應用過濾器。

但是當你深入挖掘數據庫抽象,你會看到,當查詢compiled剛好在執行之前,這是在哪裏條件which will then apply escape on string values提供的query builder will apply quote on the value。該方法escape的實現取決於DBMS連接上:

這反映了今天的最佳實踐。所以,是的,這對SQL注入是安全的。

+0

一個很好的答案!感謝您深入挖掘框架! – naivists