11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14: Building Secure Data Access 377Use Type Safe SQL ParametersThe Parameters collection in SQL provides type checking <strong>and</strong> length validation. Ifyou use the Parameters collection, input is treated as a literal value <strong>and</strong> SQL does nottreat it as executable code. An additional benefit of using the Parameters collection isthat you can enforce type <strong>and</strong> length checks. Values outside of the range trigger anexception. This is a healthy example of defense in depth.Important SSL does not protect you from SQL injection. Any application that accesses a databasewithout proper input validation <strong>and</strong> appropriate data access techniques is susceptible to SQLinjection attacks.Use stored procedures where you can, <strong>and</strong> call them with the Parameters collection.Using the Parameters Collection with Stored ProceduresThe following code fragment illustrates the use of the Parameters collection:SqlDataAdapter myComm<strong>and</strong> = new SqlDataAdapter("AuthorLogin", conn);myComm<strong>and</strong>.SelectComm<strong>and</strong>.Comm<strong>and</strong>Type = Comm<strong>and</strong>Type.StoredProcedure;SqlParameter parm = myComm<strong>and</strong>.SelectComm<strong>and</strong>.Parameters.Add("@au_id", SqlDbType.VarChar, 11);parm.Value = Login.Text;In this case, the @au_id parameter is treated as a literal value <strong>and</strong> not as executablecode. Also, the parameter is type <strong>and</strong> length checked. In the sample above, the inputvalue cannot be longer than 11 characters. If the data does not conform to the type orlength defined by the parameter, an exception is generated.Note that using stored procedures does not necessarily prevent SQL injection. Theimportant thing to do is use parameters with stored procedures. If you do not useparameters, your stored procedures can be susceptible to SQL injection if they useunfiltered input. For example, the following code fragment is vulnerable:SqlDataAdapter myComm<strong>and</strong> = new SqlDataAdapter("LoginStoredProcedure '" +Login.Text + "'", conn);Important If you use stored procedures, make sure you use parameters.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!