Does CodeIgniter automatically prevent SQL injection?

CodeigniterSql Injection

Codeigniter Problem Overview


I just inherited a project because the last developer left. The project is built off of Code Igniter. I've never worked with Code Igniter before.

I took a quick look at the code and I see database calls in the controller like this:

$dbResult = $this->db->query("SELECT * FROM users WHERE username = '".$_POST['user_name']."'");

or calls like this:

$dbResult = $this->db->query("SELECT * FROM users WHERE username = '".$this->input->post('username')."'");

Does code igniter automatically sanitize these queries to prevent sql injection?

Codeigniter Solutions


Solution 1 - Codeigniter

CodeIgniter DOES ESCAPE the variables you pass by when using the $this->db->query method. But ONLY when you pass the variables as binds, here's an example:

$dbResult = $this->db->query("SELECT * FROM users WHERE username = ?", array($this->input->post('username')));

Also remember that $_POST shouldn't be preferred over $this->input->post since what it does is check if the variables exists to prevent errors.

Solution 2 - Codeigniter

CodeIgniter provides a few string escaping functions in its database layer.

Excerpt from CI Manual:

> It's a very good security practice to escape your data before submitting it into your database. CodeIgniter has three methods that help you do this:

> 1. $this->db->escape() This function determines the data type so that it can escape only string data. It also automatically adds single quotes around the data so you don't have to:

> $sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")";

I'd post the other two examples, but I wouldn't want to take all the fun out of reading the manual.

Solution 3 - Codeigniter

No, the code you posted is susceptible to SQL injection. You need to use query binding to construct your SQL queries. If you're using the CI DB library, you would code it something like this (example from the user guide):

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";

$this->db->query($sql, array(3, 'live', 'Rick')); 

Solution 4 - Codeigniter

No, CodeIgniter will not magically sanitize queries which have been built like this.

Solution 5 - Codeigniter

According to CI's docs here, the framework filters POST on controller construction. It also optionally does XSS filtering either by manually calling the function or setting a global config.

I've never used CI either except just to play with it, so I'm not sure how far I'd trust this.

Solution 6 - Codeigniter

That doesn't escape anything. You are better off changing it to the bind syntax or the active record syntax

Solution 7 - Codeigniter

You should use $this->input->post, query binding and active record to have the safer data and then still, test test test to be sure.

Solution 8 - Codeigniter

Use active record for safety and easier coding:

Rather than:

  $dbResult = $this->db->query("SELECT * FROM users WHERE username'".$_POST['user_name']."'");

Use (same result):

$this->db->where('username',$this->input->post('user_name'));
$dbResult = $this->db->get('users');

Solution 9 - Codeigniter

It may be a pain but you should convert your queries to active record.

I'm copying from the CodeIgniter manual: "Beyond simplicity, a major benefit to using the Active Record features is that it allows you to create database independent applications, since the query syntax is generated by each database adapter. It also allows for safer queries, since the values are escaped automatically by the system."

And like some people already said, yes this code is susceptible to SQL injection

Solution 10 - Codeigniter

Optimized with a second post param (TRUE) to filter XSS on the input level:

$this->db->where('username',$this->input->post('user_name', TRUE);
$dbResult = $this->db->get('users');

libraries/input.html

Solution 11 - Codeigniter

The docs for (at least) 2.2 state, in a big red box:

> Although Active Record will try its best to properly quote any field and table names that you feed it, note that it is NOT designed to work with arbitrary user input. DO NOT feed it with unsanitized user data.

Which to this programmer means "do not rely on Active Record to quote anything".

Solution 12 - Codeigniter

Using escape function to injection of CI

<?php $username = $this->input->post('username');
$query = 'SELECT * FROM subscribers_tbl WHERE user_name = '.$this->db->escape($email);
$this->db->query($query);?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnView Question on Stackoverflow
Solution 1 - CodeigniterMarioRicaldeView Answer on Stackoverflow
Solution 2 - CodeigniterJohn HimmelmanView Answer on Stackoverflow
Solution 3 - CodeigniterFunkatronView Answer on Stackoverflow
Solution 4 - CodeigniterBen JamesView Answer on Stackoverflow
Solution 5 - CodeigniterJosh LindseyView Answer on Stackoverflow
Solution 6 - CodeigniterTeejView Answer on Stackoverflow
Solution 7 - CodeigniterstefView Answer on Stackoverflow
Solution 8 - CodeigniterRid IculousView Answer on Stackoverflow
Solution 9 - CodeigniterVangelisBView Answer on Stackoverflow
Solution 10 - CodeigniterBeKaView Answer on Stackoverflow
Solution 11 - CodeigniterMadbreaksView Answer on Stackoverflow
Solution 12 - CodeigniterAkborView Answer on Stackoverflow