News for hackers. Stuff that matters.
Re:Finding an authorization bypass on my own website by hn_throwaway_99 on Saturday March 05, 2022 @06:57PM (#30573310)
I disagree. I would qualify this as a clear bug in mysqljs.

First off, most other DB drivers will use real prepared statements, so what is passed down to the DB is actually the templated query and a set of values. It looks like mysqljs actually parses and interpolates the string before sending it to the DB engine.

Perhaps more importantly, though, this bug is basically the exact type of bug that caused the log4shell fiasco: trying to be too "smart" with user provided values that should just be treated as "dumb" scalars. The fundamental way of filling out template parameters with something that can refer to DB column names is a major flaw IMO.

Re:Finding an authorization bypass on my own website by progforlyfe on Saturday March 05, 2022 @07:05PM (#30573376)
Bingo. Any time software is made to try parsing & escaping SQL queries before sending to the server is going to find out the hard way why that is risky.

With prepared statements it's like saying to the DB, here's the query: SELECT * FROM users WHERE email = ? AND password = ?. And here's the data: ["whatever", "whatever"]. You can literally send whatever your heart desires for those two strings, unescaped, to the DB server and let them deal with it.

Re:Finding an authorization bypass on my own website by galaxyLogic on Saturday March 05, 2022 @07:34PM (#30573616)
Prepared Statements are great. Is there any way to force the programmer to use (only) them?
Re:Finding an authorization bypass on my own website by singron on Saturday March 05, 2022 @08:52PM (#30574202)
From the DB, no. E.g. you could always have `query("... a = ? and b = "+b, [a])`.

From within a language, you somehow need to restrict how you build up the query. E.g. a query builder or ORM usually makes it hard to use anything besides parameters, but they usually have escape hatches (e.g. a where method that takes arbitrary sql strings).

One trick you can do in Go is to create a type like `type sqlLiteral string`. The type is private, so other packages can't directly instantiate it, but a string literal will automatically coerce to it. Basically, you can't compute sqlLiteral from arbitrary expressions. It must be a single hardcoded string literal.

Re:Finding an authorization bypass on my own website by nicoburns on Saturday March 05, 2022 @08:59PM (#30574251)
No, because constants and column names are of course legitimate parts of SQL queries (even those using prepared statements), and because queries with no parameters are also legitimate.
Re:Finding an authorization bypass on my own website by namibj on Saturday March 05, 2022 @10:57PM (#30574900)
Code review and static analysis work great, by allowing obviously-safe dynamic SQL where static SQL is insufficient.
Re:Finding an authorization bypass on my own website by Merad on Saturday March 05, 2022 @07:18PM (#30573485)
Both are true. When the request input is blatantly the wrong type like this, you shouldn't even be attempting to execute the sql query. But of course JavaScript encourages developers to be lazy and not even think about such things.
Re:Finding an authorization bypass on my own website by hn_throwaway_99 on Saturday March 05, 2022 @07:51PM (#30573776)
Developers shouldn't have to think about these things. If I'm doing any sort of string interpolation, whether it be in a SQL statement like this or in something like a log or print formatter, I should be able to put whatever the f I want into the value, without checking it. That's the whole point of using interpolated statements like this in the first place: it should be safe to run (but may throw something like a type error in the DB) regardless of what is stuffed in the value.

This is solely a mysqljs bug.

Re:Finding an authorization bypass on my own website by Merad on Saturday March 05, 2022 @08:29PM (#30574036)
If you use any strongly typed language with a halfway decent framework you _don't_ have to think about this because the request will automatically be validated and rejected for invalid input before your code is ever hit.
Re:Finding an authorization bypass on my own website by nicoburns on Saturday March 05, 2022 @09:04PM (#30574295)
That’s also true of a decent JS library. The problem here being that the library wasn’t decent.

A library in a strongly typed language would still need to use parameterised queries or escape the strings just like a JS library. And it would be just as easy or difficult to have an incorrect sanitising function.

In fact, the specified string is not invalid input, it would be perfectly valid to store backticks in a string field in MySQL. They just need to be correctly escaped before being submitted to the database.

Re:Finding an authorization bypass on my own website by hn_throwaway_99 on Saturday March 05, 2022 @09:42PM (#30574517)
That's not really true. Pretty much every DB driver I know, in every language, will take prepared statement values that are any valid DB datatype: strings, ints, decimals, etc., and, importantly, JSON values because most DBs now support JSON. Many JS DB frameworks will take any JS object and convert it to a JSON string because that's obviously a trivial operation (it is "JavaScript Object Notation" after all).

Again, the only bug here is the completely incorrect serialization that mysqljs does.

Re:Finding an authorization bypass on my own website by Merad on Saturday March 05, 2022 @11:04PM (#30574934)
You're missing my point because you're focused on the database query. I'm talking about the web application framework. If this was written in Asp.Net, for example, the password field would be declared as a string and the data binding code within Asp.Net would validate that the field was in fact a string as part of instantiating a model for the strongly typed request body. If someone passed in an object for that field your code for the endpoint, including all of the database interaction, would never be called because the framework would automatically return a 400 response when data binding failed.
Re:Finding an authorization bypass on my own website by thinkharderdev on Sunday March 06, 2022 @06:31AM (#30576788)
I don't think it would actually be possible to use proper prepared statements for a "feature" like this. The whole point is that query is built dynamically from any JSON object. So you could, strictly speaking, build a prepared statement dynamically but you would have the exact same bug.
Re:Finding an authorization bypass on my own website by zamalek on Saturday March 05, 2022 @09:38PM (#30574490)
Nope. Primarily, this stance is hostile to international users. We have all the tools required to transparently move data from the front-end to the backend, and back to the UI. Furthermore, simply because my password begins with '{', contains '{', or is 4367 characters long shouldn't be a concern whatsoever.

Sanitization is an ignorantly dangerous practice, because it obscures much safer practices.

Validation? That's for business rules. No more order items than there are things in stock, and stuff like that. If you need to do that because something in your stack needs it, then it's far past the time to reconsider your stack.

Re:Finding an authorization bypass on my own website by ovex on Saturday March 05, 2022 @07:31PM (#30573594)
The problem is not only that types are not checked but that strings are simply replaced in structured input. Without having looked in detail, I would bet that the implementation also fails at queries which contain question marks inside strings, i.e. question marks that are not placeholders. Even if string escaping was the right way, which it is not, the proper way to do it would be to translate the SQL statement into an AST and then replace the leaves of the tree that are placeholders with the respective escaped strings.
Re:Finding an authorization bypass on my own website by the_gipsy on Sunday March 06, 2022 @03:22AM (#30575958)
> Perhaps more importantly, though, this bug is basically the exact type of bug that caused the log4shell fiasco: trying to be too "smart" with user provided values that should just be treated as "dumb" scalars.

Which is exactly the equivalent of using JSON.parse(). You will find more library combinations, and for many of them the options can't be considered bugs like you do for mysqljs here, and you definitely won't be able to catch it.

The solution is parsing beyond JSON.parse(). You can't have arbitrary user provided structures floating around your program.