How to prevent SQL injection in PhalconPHP when using sql in model?

Let's say I'm building a search that finds all teachers and gets an input where the user can enter a search term. I've tried reading the phalcon documentation, but I only see things like binding options. I read another thread that I need instructions for preparation, do I need Phalcon too?

And my function in the model would be something like this:

public function findTeachers($q, $userId, $isUser, $page, $limit, $sort)
{
  $sql =  'SELECT id FROM tags WHERE name LIKE "%' . $q . '%"';
  $result = new Resultset(null, $this,
  $this->getReadConnection()->query($sql, array()));
  $tagResult = $result->toArray();
  $tagList = array();
  foreach ($tagResult as $key => $value) {
     $tagList[] = $value['id'];
  ....

  }
}

      

My question is for Phalcon framework: are there any settings or formats that I should encode for this line $sql = 'SELECT id FROM tags WHERE name LIKE "%' . $q . '%"';

Any general recommendation to prevent SQL Injection in PhalconPHP controllers and index will also be appreciated.

For reference:

My controller:

public function searchAction()
{
    $this->view->disable();
    $q = $this->request->get("q");
    $sort = $this->request->get("sort");
    $searchUserModel = new SearchUsers();
    $loginUser = $this->component->user->getSessionUser();
    if (!$loginUser) {
      $loginUser = new stdClass;
      $loginUser->id = '';
    }
    $page = $this->request->get("page");
    $limit = 2;
    if (!$page){
        $page = 1;
    }

    $list = $searchUserModel->findTeachers($q, $loginUser->id, ($loginUser->id)?true:false, $page, $limit, $sort);

    if ($list){
        $list['status'] = true;
    }
    echo json_encode($list);
}

      

My Ajax:

function(cb){
    $.ajax({
        url: '/search/search?q=' + mapObject.q + '&sort=<?php echo $sort;?>' +  '&page=' + mapObject.page,
        data:{},
        success: function(res) {
            //console.log(res);
            var result = JSON.parse(res);
            if (!result.status){
                return cb(null, result.list);
            }else{
                return cb(null, []);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            cb(null, []);
         }
});

      

where q is the user's search query.

+3


source to share


2 answers


You must bind the query parameter to avoid SQL injection. From what I remember, Phalcon can be a little funny putting "%" in the value conditions

to put them in bind

.

It will be better than just filtering the request.



$tags = Tags::find([
    'conditions' => 'name LIKE :name:',
    'bind' => [
        'name' => "%" . $q . "%"
    ]
])

      

+4


source


Phalcon \ Filter is useful when interacting with the database.

In your controller, you can say remove everything except letters and numbers from $ q.



$q = $this->request->get("q");
$q = $this->filter->sanitize($q, 'alphanum');

      

+3


source







All Articles