21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Obviously, the best way to avoid SQLinjection attacks is to not create SQLcommand<br />

strings that include any user input. In some small number of applications, this<br />

may be feasible, but more frequently it is not. Avoid including user input in SQL<br />

commands as much as you can, but where it cannot be avoided, you should escape<br />

dangerous characters.<br />

Discussion<br />

SQLinjection attacks are really just general input validation problems. Unfortunately,<br />

there is no perfect solution to preventing these types of attacks. Your best<br />

defense is to apply strict checking of input—even going so far as to refuse questionable<br />

input rather than attempt to escape it—and hope that that is a strong enough<br />

defense.<br />

There are two main approaches that can be taken to avoid SQL injection attacks:<br />

Restrict user input to the smallest character set possible, and refuse any input that contains<br />

character outside of that set.<br />

In many cases, user input needs to be used in queries such as looking up a username<br />

or a message number, or some other relatively simple piece of information.<br />

It is rare to need any character in a user name other than the set of<br />

alphanumeric characters. Similarly, message numbers or other similar identifiers<br />

can safely be restricted to digits.<br />

With SQL, problems start to occur when symbol characters that have special<br />

meaning are allowed. Examples of such characters are quotes (both double and<br />

single), semicolons, percent symbols, hyphens, and underscores. Avoid these<br />

characters wherever possible; they are often unnecessary, and allowing them at<br />

all just makes things more difficult for everyone except an attacker.<br />

Escape characters that have special significant to SQL command processors.<br />

In SQLparlance, anything that is not a keyword or an identifier is a literal. Keywords<br />

are portions of a SQLcommand such as SELECT or WHERE, and an identifier<br />

would typically be the name of a table or the name of a field. In some cases, SQL<br />

syntax allows literals to appear without enclosing quotes, but as a general rule<br />

you should always enclose literals with quotes.<br />

Literals should always be enclosed in single quotes ('), but some SQLimplementations<br />

allow you to use either single or double quotes ("). Whichever you<br />

choose to use, always close the literal with the same character with which you<br />

opened it.<br />

Within literals, most characters are safe to leave unescaped, and in many cases,<br />

it is not possible to escape them. Certainly, with whichever quoting character<br />

you choose to use with your literals, you may need to allow that character inside<br />

the literal. Escaping quotes is done by doubling up on the quote character. Other<br />

characters that should always be escaped are control characters and the escape<br />

character itself (a backslash).<br />

108 | Chapter 3: Input Validation<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!