I would like to find an optimal way how to mitigate SQL injection in the web application developed in CodeIgniter framework (web application uses MS SQL backend DB with ODBC connection).
Let's assume I have a simple vulnerable code like this:
$this->db->query("SELECT * FROM users WHERE Login = '".$_GET['name']."'");
This query is clearly vulnerable to SQL injection through HTTP GET parameter "name".
I have read CodeIgniter documentation and everything I could find online to see how to mitigate this simple SQL injection in CodeIgniter and I have tried all of the following options:
Option 1:
$this->db->query("SELECT * FROM users WHERE Login = ".$this->db->escape($_GET['name']));
Option 2:
$this->db->select("*")->from("users")->where('Login', $_GET['name'])->get();
Option 3:
$query = "SELECT * FROM users WHERE Login = '?'";
$this->db->query($query, array($_GET['name']));
Option 4:
$query = "SELECT * FROM users WHERE Login = ?";
$this->db->query($query, array($_GET['name']));
I was shocked to find out that all four aforementioned options are just as vulnerable to SQL injection as the initial query. I was wondering whether CodeIgniter is so poorly designed from the security perspective or if I am missing some important piece of configuration.
Is there any conceptual way how to prevent SQL injection in CodeIgniter in this case?