Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized SQL statements vs. very simple method

When I started to write the first SQL-Statements in my programs I felt quite comfortable with protecting myself against SQL-Injection with a very simple method that a colleague showed me. It replaced all single quotes with two single quotes.

So for example there is a searchfield in which you can enter a customername to search in the customertable. If you would enter

Peter's Barbershop

The SELECT Statement would look like

SELECT *
FROM Customers
WHERE Customername = 'Peter''s Barbershop'

If now an attacker would insert this:

';DROP TABLE FOO; --

The statement would look like:

SELECT *
FROM Customers
WHERE Customername = ''';DROP TABLE FOO;--'

It would not drop any table, but search the customertable for the customername ';DROP TABLE FOO;-- which, I suppose, won't be found ;-)

Now after a while of writing statements and protecting myself against SQL-Injection with this method, I read that many developers use parameterized statements, but I never read an article where "our" method was used. So definitely there is a good reason for it.

What scenarios would parameterized statements cover but our method doesn't? What are the advantages of parameterized statements compared to our method?

Thanks
Philipp

like image 204
Philipp Grathwohl Avatar asked Apr 28 '10 15:04

Philipp Grathwohl


People also ask

Why you should prefer parameterized query?

A parameterized query is a query in which placeholders are used for parameters and the parameter values are supplied at execution time. The most important reason to use parameterized queries is to avoid SQL injection attacks.

Are parameterized queries faster?

In this example, parameterized queries are shown to run about 33% faster than the dynamic SQL option. A more important and wider performance gap is seen on the amount of CPU time used. The dynamic SQL uses roughly 3.3 times the amount of CPU on the database server as the parameterized query option.

What is parameterized SQL statements?

Parameterized SQL queries allow you to place parameters in an SQL query instead of a constant value. A parameter takes a value only when the query is executed, which allows the query to be reused with different values and for different purposes.

Why is it important to parameterized database queries?

One major reason for using parameterized queries is that they make queries more readable. The second and most compelling reason is that parameterized queries help to protect the database from SQL injection attacks.


4 Answers

The parametrized queries has more proc than the defence to sql-injection.

  1. It solves problem with date & time formating & parsing.
  2. You can prepare execution plan for parametrized query.
  3. The sql-injection protection.

I can't remember now for another pros :).

However the way "double every quotes" has problem with fields with limited character length.

For example:

  • The page has box for "nickname" which can be 10 character long.
  • The user insert "Don't care" - the exact 10 characters.

Now if you double the quotes, the value has 11 characters and the database will "cut" it, and you got another value in db than user typed.

So I recommend the parameters.

like image 108
TcKs Avatar answered Oct 14 '22 14:10

TcKs


One big dis-advantage is that your solution relies on a developer remembering to add the character, obviously the compiler won't complain. That is dangerous.

Secondly, performance should be enhanced with parameterized SQL statements, as Jeff points out here (in 2005!!!).

like image 34
Joe Ratzer Avatar answered Oct 14 '22 15:10

Joe Ratzer


One advantage is that the driver itself will determine what he has to escape and what doesn't need to be escaped. Your method could be broken with an input like this:

  \'; DROP TABLE foo;--

Which would result in

  SELECT *
  FROM Customers
  WHERE Customername = '\'';DROP TABLE FOO;--'

The first quote gets escaped, the second doesn't and closes the string.

like image 29
Bobby Avatar answered Oct 14 '22 14:10

Bobby


Short answer:
You should use parameterized queries simply because the database server knows better than you do which characters need to be escaped.

Long answer:
' is not necessarily the only special character that needs escaping. These special characters differ from DB server to DB server. MySQL, for example, uses \ as an escape character as well (unless sql_mode=NO_BACKSLASH_ESCAPES is set). Therefore, '' and \' mean the same thing.

This is not true of, say, Oracle.

like image 28
Powerlord Avatar answered Oct 14 '22 14:10

Powerlord