Introduction

It represents a serious threat because SQL Injection allows evil attacker code to change the structure of a web application’s SQL statement in a way that can steal data, modify data, or potentially facilitate command injection to the underlying OS.

This cheat sheet is a derivative work of the .

Parameterized Query Examples

Using Java with Hibernate

  1. @Entity // declare as entity;
  2. @NamedQuery(
  3. name="findByDescription",
  4. query="FROM Inventory i WHERE i.productDescription = :productDescription"
  5. )
  6. public class Inventory implements Serializable {
  7. @Id
  8. private String productDescription;
  9. }
  10. // use case
  11. // This should REALLY be validated too
  12. String userSuppliedParameter = request.getParameter("Product-Description");
  13. // perform input validation to detect attacks
  14. List<Inventory> list =
  15. session.getNamedQuery("findByDescription")
  16. .setParameter("productDescription", userSuppliedParameter).list();
  17. // This should REALLY be validated too
  18. String userSuppliedParameter = request.getParameter("Product-Description");
  19. // perform input validation to detect attacks
  20. Inventory inv = (Inventory) session.createCriteria(Inventory.class).add
  21. (Restrictions.eq("productDescription", userSuppliedParameter)).uniqueResult();

Using .NET built-in feature

  1. String query = "SELECT account_balance FROM user_data WHERE user_name = ?";
  2. try {
  3. OleDbCommand command = new OleDbCommand(query, connection);
  4. command.Parameters.Add(new OleDbParameter("customerName", CustomerName Name.Text));
  5. OleDbDataReader reader = command.ExecuteReader();
  6. // …
  7. } catch (OleDbException se) {
  8. // error handling
  9. }
  1. string sql = "SELECT * FROM Customers WHERE CustomerId = @CustomerId";
  2. command.Parameters.Add(new SqlParameter("@CustomerId", System.Data.SqlDbType.Int));
  3. command.Parameters["@CustomerId"].Value = 1;

Using Ruby with ActiveRecord

Using Ruby built-in feature

  1. insert_new_user = db.prepare "INSERT INTO users (name, age, gender) VALUES (?, ? ,?)"
  1. $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
  2. $stmt->bindParam(':name', $name);
  3. $stmt->bindParam(':value', $value);

Using Cold Fusion built-in feature

  1. <cfquery name = "getFirst" dataSource = "cfsnippets">
  2. SELECT * FROM #strDatabasePrefix#_courses WHERE intCourseID =
  3. <cfqueryparam value = #intCourseID# CFSQLType = "CF_SQL_INTEGER">
  4. </cfquery>

Using PERL with Database Independent Interface

Stored Procedure Examples

The SQL you write in your web application isn’t the only place that SQL injection vulnerabilities can be introduced. If you are using Stored Procedures, and you are dynamically constructing SQL inside them, you can also introduce SQL injection vulnerabilities.

To ensure this dynamic SQL is secure, you can parameterize this dynamic SQL too using bind variables.

Normal Stored Procedure

No dynamic SQL being created. Parameters passed in to stored procedures are naturally bound to their location within the query without anything special being required:

  1. PROCEDURE SafeGetBalanceQuery(UserID varchar, Dept varchar) AS BEGIN
  2. SELECT balance FROM accounts_table WHERE user_ID = UserID AND department = Dept;
  3. END;

Stored Procedure Using Bind Variables in SQL Run with EXECUTE

Bind variables are used to tell the database that the inputs to this dynamic SQL are ‘data’ and not possibly code:

  1. PROCEDURE AnotherSafeGetBalanceQuery(UserID varchar, Dept varchar)
  2. AS stmt VARCHAR(400); result NUMBER;
  3. BEGIN
  4. stmt := 'SELECT balance FROM accounts_table WHERE user_ID = :1
  5. AND department = :2';
  6. EXECUTE IMMEDIATE stmt INTO result USING UserID, Dept;
  7. RETURN result;
  8. END;

SQL Server using Transact-SQL

Normal Stored Procedure

  1. PROCEDURE SafeGetBalanceQuery(@UserID varchar(20), @Dept varchar(10)) AS BEGIN
  2. END

Stored Procedure Using Bind Variables in SQL Run with EXEC

Bind variables are used to tell the database that the inputs to this dynamic SQL are ‘data’ and not possibly code:

References