28.10.2014 Views

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

END;<br />

Execute immediate 'SELECT count(*) FROM users where username=:1 <strong>and</strong><br />

password=:2' into result using username,password;<br />

Validating Input<br />

Code-Level <strong>Defense</strong>s • Chapter 8 349<br />

In the previous section, we discussed avoiding the use of dynamic <strong>SQL</strong> to prevent <strong>SQL</strong><br />

injection. However, this should not be the only control you put in place to address <strong>SQL</strong><br />

injection. One of the most powerful controls you can use, if done well, is validation of the<br />

input that an application receives.<br />

Input validation is the process of testing input received by the application for compliance<br />

against a st<strong>and</strong>ard defined within the application. It can be as simple as strictly typing a<br />

parameter <strong>and</strong> as complex as using regular expressions or business logic to validate input.<br />

There are two different types of input validation approaches: whitelist validation (sometimes<br />

referred to as inclusion or positive validation) <strong>and</strong> blacklist validation (sometimes known as<br />

exclusion or negative validation). These two approaches, <strong>and</strong> examples of validating input in<br />

Java, C#, <strong>and</strong> PHP to prevent <strong>SQL</strong> injection, are detailed in the following subsections.<br />

Tip<br />

When performing input validation you should always ensure that the input<br />

is in its canonical (simplest) form before making any input validation<br />

decisions. This may involve decoding the input into a simpler format, or just<br />

rejecting input that isn’t already in canonical format where non-canonical<br />

input isn’t expected. We’ll cover canonicalization in a separate solution<br />

later in this chapter.<br />

Whitelisting<br />

Whitelist validation is the practice of only accepting input that is known to be good.<br />

This can involve validating compliance with the expected type, length or size, numeric range,<br />

or other format st<strong>and</strong>ards before accepting the input for further processing. For example,<br />

validating that an input value is a credit card number may involve validating that the input<br />

value contains only numbers, is between 13 <strong>and</strong> 16 digits long, <strong>and</strong> passes the business logic<br />

check of correctly passing the Luhn formula (the formula for calculating the validity of<br />

a number based on the last “check” digit of the card).

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

Saved successfully!

Ooh no, something went wrong!