Why should I use prepared statements if escaping is safe?
It's not escaping, it's people. Escaping, when used properly, is all right. But people tend to oversimplify things, reducing them to a single blatant statement, such as "escape everything". And it's such statements is where escaping fails. Given two oversimplified suggestions
- escape all user input
- prepare SQL and use parameters for all variables
-- the latter indisputably wins.
$id = mysqli_real_escape_string($mysqli,$_POST['id']);
$username = mysqli_real_escape_string($mysqli,$_POST['username']);
$sql = "SELECT * FROM users WHERE id='$id' AND username='$username'";
$result = $mysqli->query($sql);
This oversimplified example, provided --
- the connection character set is properly set
- the processed value is surrounded by single quotes
- no weird SQL modes such as NO_BACKSLASH_ESCAPES used
-- is technically safe.
It would be a nonsense, if some mechanism would fail to provide the functionality it is intended for, when used properly.
However
The problem here is not technical but sort of a methodological one.
As you can see, formatting a value for a database is effectively split into two parts, escaping variables and quoting values in the query. And this is where all the magic happens lies the cause for innumerable real life cases of SQL injections.
With your simplified example, where all the code is bound together, it is hard to overlook the proper routine. But in the real life the code is much more complex, consisting of large distinct modules. And escaping is done in one in one module while quoting in another. Or not. Nobody can tell actually. I'd just trust that this value has been escaped already. Or I will escape it just to be sure, and introduce extra escaping characters in the data. Or I am a new dev, who don't understand the example you posted here, and I was watching a youtube video that said escaping prevents SQL injection. I know the value has been escaped already, so I can put in the query safely. And as it is an integer, why would I waste quotes on it?
Or I know that the data has been escaped already when it was entering the application, so I won't have to escape it during some internal manipulations sometime later (when moving into another table for example). And have a first class second order SQL injection as a result.
Do you see where I am getting at?
Trust me, I've seen all these cases in the wild. Such a separated formatting introduces a total mess and a wast opportunity for injections.
This is why prepared statements are the whole world better. Not because escaping is somewhat "unsafe". But because prepared statement does the complete formatting, in a single place.
Related articles:
- Relative and absolute paths, in the file system and on the web server.
- PHP error reporting
- Do you abuse the null coalescing operator (and isset/empty as well)?
- Do you really need to check for both isset() and empty() at the same time?
- What's wrong with popular articles telling you that foo is faster than bar?
- Articles
- Numerical strings comparison
- MVC in simpler terms or the structure of a modern web-application
- How to get a single player's rank based on the score
- Operator precedence or how does 'or die()' work.
- Do generators really reduce the memory usage?
- How to make Stack Overflow a nice place
- How to debug small PHP programs
- Iterating over array getting look-ahead items along
Comments:
Unless I have a misconfiguration somewhere, my system is correct and it is expected for ", &, etc to be converted to
",&, etc, yes?So then you would have to remember when querying for char/text columns to do the conversions of <, >, &, ', ", to use their respective entities?
I know it's most certainly a somewhat rare occurrence but just due to that fact (not remembering because it's rare) when management says run a query letting is know where we said "More than 2" of rain is forecasted" having to remember to make it "More than 2
"of rain is expected" is... meh. 😂Reply:
It's not a miscunfiguration, it's just a call to htmlspecialchars somewhere in your code. Premature and superfluous.
Like I said, you shouldn't convert your data for a database. Any conversions should be done right before use. For example, given you are going to echo your HTML for editing between
<textarea>tags you must encode it using htmlspecialchars() as HTML standard demands. But no sooner.So having entities in your database is a bug. Check your code.
Hi Colonel,
Im curious what your view is on html being saved to the database. Im not really talking about <div> or <script>, I'm talking about single-quotes and double-quotes primarily. Seeing the entities in the db has always turned me off, but I think using html_entity_decode on all post fields is not really recommended?
For the record, I have no problem storing full-blown HTML in the database if it would just harmlessly render out those script tags, html tags, etc.
Reply:
Hello Chris!
My view is plain and simple: HTML is absolutely irrelevant to database so there should be no such question at all.
Any treatment for the data going to be stored in the database is premature and superfluous. Any data should be stored as is, no matter if it's HTML, binary or whatever.