views:

2066

answers:

10

Why do we need a DB-specific functions like mysql_real_escape_string()? What can it do that addslashes() doesn't?

Ignoring for the moment the superior alternative of parameterized queries, is a webapp that uses addslashes() exclusively still vulnerable to SQL injection, and if yes, how?

A: 

SQL needs only to escape the ' character. It does this by doubling it. ' -> ''

Therefore adding slashes has no effect at all.

Georg
MySQL uses \' to escape single quotes. If you use the PHP function mentioned, it returns "\'" for "'".
Rob
*ANSI* SQL only needs that. MySQL doesn't do ANSI unless you kick it in its config file shins hard enough.
Ant P.
It is not a difficult option to choose (on the default server or connection level) or very uncommon. Code should be expected to work in ANSI sql_mode.
bobince
using mysql_real_escape_string() should account for it being in ANSI mode (I believe), where using addslashes() or just escaping the single quote will not.
Rob
A: 

It's supposed to escape strings for MySQL in a way that other quoting facilities don't.

Much preferable, however, is to use the mysqli interface, and use parametrized prepared queries instead of trying to make sure all your strings are properly escaped. Using parametrized queries obviates the need for such messy string work and strongly mitigates the risk of SQL injection.

Edit: I'll clarify a little on why I consider quoting a bad idea: It's easy to forget when and where you need to quote - whether your variable is to be a string or number, whether it has already been quoted, etc. A parametrized query has none of these issues, and the need for quoting is completely obviated.

greyfade
I guess I should clarify that I'm perfectly aware of the superiority of parameterized queries :)
Michael Borgwardt
+1  A: 

somedb_real_escape_string() is database specific, addslashes() is not.

In the case of MySQL this means:

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

(From the manual.)

tharkun
+1  A: 

The only real difference that I know of is that mysql_real_escape_string() will take the database's character set into consideration when escaping the input string. Neither function will escape wild card characters % and _ which still leaves the script open to some SQL injection.

Brandon
% and _ have nothing to do with SQL injection.
Bill Karwin
In MySQL, % can be used as a wild card in GRANT statements. Also MySQL treats _ as a single character wild card in statements.
Brandon
+10  A: 

It adds slashes to:

\x00, \n, \r, \, ', " and \x1a. characters.

Where addslashes only adds slashes to

' \ and NUL

Ilias article is also pretty detailed on its functionality

Ólafur Waage
+1  A: 

According to the PHP manual:

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

Rob
+13  A: 

Addslashes is generally not good enough when dealing with multibyte encoded strings.

Chris KL
Any encoding with a valid multibyte character ending in 0x5c can sneak quotes past `addslashes()`. Chris Shiflett has an excellent example on his blog using GBK.http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string
Matthew
+4  A: 

gs's harshly downvoted answer is actually kinda right.

Standard SQL uses doubling to escape a literal apostrophe. MySQL's non-standard use of backslashes for escaping is the default setting, but it can be disabled and often is, in particular in sql_mode ANSI.

In this case only the doubled syntax will work, and any app you have using addslashes (or other ad-hoc escaping method) will break. mysql_real_escape_string will use whichever escaping method is best for the connection's sql_mode.

The multibyte encoding issue is also important if you're still using those nasty East Asian encodings that re-use the lower 128 characters, but then really you want to be using UTF-8 instead. \n-escaping, on the other hand, is of no concern since MySQL can perfectly happily cope with a raw newline in a statement.

bobince
+2  A: 

PHP's mysql_real_escape_string function will, more or less, ask mysql what character(s) needs to be escaped, where the addslashses function will just add a backslash in front of and any single quote ('), double quote ("), backslash () or NUL (the NULL byte) character.

The two practical effects are, addslashes tends not to work well with multibyte characters, and, more importantly, by asking mysql what characters need to be escaped, you avoid a possible future compatibility. Using assslashes is kind of like hardcoding a couple of specific characters into the escape sequence.

Alan Storm
A: 

Is it ok to use addslashes and mysql_real_escape_string at the same time?

Zai Zai
Why don't you try it and see what happens? I suspect that the result will not be good.
Michael Borgwardt