How to cleanse (prevent SQL injection) dynamic SQL in SQL Server?

I believe there are three different cases that you have to worry about: strings (anything that requires quotes): ”” + replace(@string, ””, ”””) + ”” names (anything where quotes aren’t allowed): quotename(@string) things that cannot be quoted: this requires whitelisting Note: Everything in a string variable (char, varchar, nchar, nvarchar, etc.) that comes from user-controlled … Read more

Do I have to use mysql_real_escape_string if I bind parameters?

No, you don’t have to escape value yourself (i.e. no you don’t need to call mysqli_real_escape_string), when you are using prepared statements : the DB engine will do that itself. (Actually, if you were calling mysql_real_escape_string and using bound parameters, your strings would get escaped twice — which would not be great : you’d end … Read more

Python SQLite3 SQL Injection Vulnerable Code

An example SQL injection using your first SQL statement: cursor.execute(“insert into user(username, password) values(‘{0}’, ‘{1}’)”.format(username, password)) If username and password are “blah” the resulting SQL statement is: insert into user(username, password) values(‘blah’, ‘blah’) and there is no problem with this particular statement. However, if a user is able to enter a value for password, perhaps … Read more

rails 3 activerecord order – what is the proper sql injection work around?

Ryan Bates’ method: in your controller: def index @users = User.order(sort_by + ” ” + direction) end private def sort_by %w{email name}.include?(params[:sort_by]) ? params[:sort_by] : ‘name’ end def direction %w{asc desc}.include?(params[:direction]) ? params[:direction] : ‘asc’ end Essentially you’re making a whitelist, but it’s easy to do and insusceptible to injection.

Is mysqli_real_escape_string safe?

Is this correct? Yes. This isolated handpicked example is safe. It doesn’t mean, though, that mysqli_real_escape_string should be viewed as a function that’s purpose is to prevent SQL injections. Because in this example it protects you only by accident. Is this a good example of how to use mysqli_real_escape_string? Not at all This function should … Read more

How to avoid SQL injection in CodeIgniter?

CodeIgniter’s Active Record methods automatically escape queries for you, to prevent sql injection. $this->db->select(‘*’)->from(‘tablename’)->where(‘var’, $val1); $this->db->get(); or $this->db->insert(‘tablename’, array(‘var1’=>$val1, ‘var2’=>$val2)); If you don’t want to use Active Records, you can use query bindings to prevent against injection. $sql=”SELECT * FROM tablename WHERE var = ?”; $this->db->query($sql, array($val1)); Or for inserting you can use the insert_string() … Read more