This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org

Difference between revisions of "Query Parameterization Cheat Sheet"

From OWASP
Jump to: navigation, search
(Parameterized Query Examples)
(Parameterized Query Examples)
Line 13: Line 13:
  
 
SQL Injection is best prevented through the use of [[SQL_Injection_Prevention_Cheat_Sheet#Defense_Option_1:_Prepared_Statements_.28Parameterized_Queries.29|''parameterized queries'']]. The following chart demonstrates, with real-world code samples, how to build parameterized queries in most of the common web languages. The purpose of these code samples is to demonstrate to the web developer how to avoid SQL Injection when building database queries within a web application.
 
SQL Injection is best prevented through the use of [[SQL_Injection_Prevention_Cheat_Sheet#Defense_Option_1:_Prepared_Statements_.28Parameterized_Queries.29|''parameterized queries'']]. The following chart demonstrates, with real-world code samples, how to build parameterized queries in most of the common web languages. The purpose of these code samples is to demonstrate to the web developer how to avoid SQL Injection when building database queries within a web application.
 +
 +
== Prepared Statement Examples ==
  
 
{| class="wikitable nowraplinks"
 
{| class="wikitable nowraplinks"
Line 95: Line 97:
 
  '''$sth->execute( $bar, $baz );'''
 
  '''$sth->execute( $bar, $baz );'''
 
|}
 
|}
 +
 +
== 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. Here are some examples of using bind variables in stored procedures in different databases:
 
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. Here are some examples of using bind variables in stored procedures in different databases:
Line 105: Line 109:
 
| Oracle - PL/SQL
 
| Oracle - PL/SQL
 
|
 
|
Normal Oracle Stored Procedure - no dynamic SQL being created. Parameters passed in to stored procedure are naturally bound to their location within the query without anything special being required.
+
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.
  
   PROCEDURE usp_SafeGetBalanceQuery(
+
   PROCEDURE SafeGetBalanceQuery(
 
     UserID varchar, Dept varchar) AS BEGIN
 
     UserID varchar, Dept varchar) AS BEGIN
 
    
 
    
     SELECT balance FROM accounts_table WHERE user_ID = UserID AND
+
     SELECT balance FROM accounts_table WHERE user_ID = UserID AND department = Dept;
      department = Dept;
 
 
   END;
 
   END;
  
Line 117: Line 120:
 
| Oracle - PL/SQL  
 
| Oracle - PL/SQL  
 
|   
 
|   
Stored Procedure Using Bind Variables in Dynamic SQL Run with EXECUTE IMMEDIATE. Bind variables are used to tell the database that the inputs to this dynamic SQL are 'data' and not possibly 'code'.
+
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.
  
   PROCEDURE usp_AnotherSafeGetBalanceQuery(
+
   PROCEDURE AnotherSafeGetBalanceQuery(
 
     UserID varchar, Dept varchar) AS  
 
     UserID varchar, Dept varchar) AS  
 
     stmt VARCHAR(400); result NUMBER;
 
     stmt VARCHAR(400); result NUMBER;
Line 129: Line 132:
 
     RETURN result;
 
     RETURN result;
 
   END;
 
   END;
 +
|-
 +
| SQL Server- Transact-SQL
 +
|
 +
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.
 +
 +
  PROCEDURE SafeGetBalanceQuery(
 +
    @UserID varchar(20),
 +
    @Dept varchar(10)) AS BEGIN
 +
 
 +
    SELECT balance FROM accounts_table WHERE user_ID = @UserID AND department = @Dept
 +
  END
 +
|-
 +
| SQL Server- Transact-SQL
 +
|
 +
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.
 +
 +
  PROCEDURE SafeGetBalanceQuery(@UserID varchar(20),
 +
      @Dept varchar(10)) AS BEGIN
 +
    DECLARE @sql VARCHAR(200)
 +
    SELECT @sql = 'SELECT balance FROM accounts_table WHERE '
 +
      + 'user_ID = @UID AND department = @DPT'
 +
    EXEC sp_executesql @sql,
 +
      '@UID VARCHAR(20), @DPT VARCHAR(10)',
 +
      @UID=@UserID, @DPT=@Dept
 +
  END
 
|}
 
|}
  

Revision as of 22:23, 6 June 2014

Cheatsheets-header.jpg

Last revision (mm/dd/yy): 06/6/2014

Introduction

SQL Injection is one of the most dangerous web vulnerabilities. So much so that it's the #1 item in the OWASP Top 10. 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 SQL Injection Prevention Cheat Sheet.

Parameterized Query Examples

SQL Injection is best prevented through the use of parameterized queries. The following chart demonstrates, with real-world code samples, how to build parameterized queries in most of the common web languages. The purpose of these code samples is to demonstrate to the web developer how to avoid SQL Injection when building database queries within a web application.

Prepared Statement Examples

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. Here are some examples of using bind variables in stored procedures in different databases:

Authors and Primary Editors

Jim Manico - jim [at] owasp.org
Dave Wichers - dave.wichers [at] aspectsecurity.com
Neil Matatal - neil [at] owasp.org

Other Cheatsheets