Using Multi-Byte Characters to Nullify SQL Injection Sanitizing
howto.hackallthethings.com
howto.hackallthethings.com
Point #4 makes it really hard to envision this ever being a problem, since if you're using a CJK charset on your platform already, you're quite likely to notice very quickly that something is horribly, horribly wrong.
Seems like this bad practice should be dead by now, not only does it open up for injection attacks but it also prevents the database from optimizing the query by precompiling, data aggregation and building smarter execution plans.
[1]: https://developer.wordpress.org/reference/classes/wpdb/prepa...
I really don't get why anyone in this day and age doesn't use parameterised queries.
It's such a big security win and comes with such little programming overhead that it boggles my mind to think people still use manual string escaping.
[1] http://blog.endpoint.com/2014/04/custom-plans-prepared-state...
And the most recent LTS, 16.04, has 9.5.
People care more about convenience and fast iteration than security - concatenation is faster and easier, and insecure sites can still make money, so the incentives are on the side of fast, dirty code that works now rather than secure code that works later.
People (this is especially true in the PHP world) may not even know parameterized queries exist.
I see this sort of thing on a daily basis, but it's not from PHP developers but our "lead" Delphi developer. I've tried to convince him of the benefits of parameter based queries, but believes that it would be a performance hit (performance for him maybe, he'd have to struggle to learn something new).
A particular irritant is that most (all?) db APIs make it impossible to use parametrized queries with IN clauses. I find myself having to implement string escaping functions sooner or later in every project, because of stuff like that.
Our current solution is to use of temporary table because other solutions (including dynamically adding parameter in query) makes them unique queries and crashes performance.
I believe at one point IN (values) was just sugar for = ANY(ARRAY(values)), don't know if that's still the case.
Still requires that your DB adapter supports array parameters though.
Psycopg2 has a generic adaptation of iterables to a form suitable for IN, but it's hand-rolled: https://github.com/psycopg/psycopg2/blob/732ea90a4ff85f6d0cc...
Real convenient though.
sql = '... col_name IN ('
foreach(var arg in args)
if (!first)
sql.append(', ')
sql.append('?')
params.add(arg)
sql.append(')')Please, please don't say this. In the SQL standard, backslash is NOT the escape character for a string literal.
PostgreSQL, starting in v8.2, began transitioning from C-style escapes (using backslash) to SQL standard escapes (where a single quote is escaped with another single quote). Standard behavior is the default, but can be controlled with the variable standard_conforming_strings.
But you shouldn't have to know that anyway. Use out-of-band parameters that are passed in the wire protocol separately from the string. Web frameworks should already ensure this, and if they don't, they are likely broken.
If you are writing a web framework and you need to use escaping for some reason: first, make sure you can't use parameters on the wire instead; then read the product-specific literal parsing rules very carefully, considering things like multibyte characters.
The same goes for JavaScript, escape sequences, SQL string concatenation and so on. It always seems like a great idea but it is just about impossible to get it airtight.
Better do your signaling, commands and meta-data through another channel.
I still do it, and i haven't used an ORM yet that is actually useful, but it still feels wrong.
Does your software conform to Unicode 3.0 and earlier, 3.1 through 5.1, or 5.2 and later? And do your server and client software agree? If you don't know the answer (and you depend on string sanitization), you may be at risk.