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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Q: So, I need to encode input to the database <strong>and</strong> from it as well? Why?<br />

A: If you’re using dynamic <strong>SQL</strong> anywhere, you need to ensure that the content you are<br />

submitting to the database will not cause an <strong>SQL</strong> injection issue. This doesn’t mean that<br />

malicious content has been rendered safe, though. It could be dangerous when queried<br />

from the database <strong>and</strong> used in dynamic <strong>SQL</strong> elsewhere.<br />

Q: At what point should I encode?<br />

A: You should encode close to where you use the information. Therefore, you should<br />

encode when submitting information to the database before it goes to the database.<br />

You should encode information that comes from the database as close to where it is<br />

used as possible; for example, before being presented to the user (encode for cross-site<br />

scripting), or before it is used in dynamic <strong>SQL</strong> (encode for <strong>SQL</strong> injection).<br />

Q: How do I perform canonicalization/normalization on input I’ve received in technology X?<br />

A: Refer to your documentation of the framework in which you’re developing for<br />

canonicalization <strong>and</strong> normalization support. Alternatively, you could consider using an<br />

external framework such as icu for normalization or iconv to convert the input to ASCII<br />

if no other support is available.<br />

Q: Why is Unicode so complex with canonicalization?<br />

A: Unicode allows a character to be represented in a multiple-byte form. Because of the way<br />

Unicode is generated, it is possible for there to be multiple representations of the same<br />

character. It is also possible in cases where an out-of-date or badly implemented Unicode<br />

interpreter is used, that additional invalid presentations of a character may also work.<br />

Q: I can use dynamic <strong>SQL</strong> in a stored procedure, can’t I?<br />

A: Yes. But be aware that you can have <strong>SQL</strong> injection in stored procedures as well. If you<br />

have user-controllable information being included in a dynamic <strong>SQL</strong> query in a stored<br />

procedure, you will be vulnerable.<br />

Q: I use Hibernate, so I’m safe from <strong>SQL</strong> injection, right?<br />

A: Wrong. Hibernate does encourage secure database access behavior, but it is still<br />

possible to create <strong>SQL</strong> injectable code in Hibernate, especially where you’re using<br />

native queries. Avoid dynamic <strong>SQL</strong>, <strong>and</strong> make sure you’re using parameterized<br />

statements with bound variables.

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

Saved successfully!

Ooh no, something went wrong!