News for hackers. Stuff that matters.
Re:Don’t try to sanitize input – escape output by laumars on Thursday February 27, 2020 @02:36AM (#22431431)
Are you sure that’s what is happening at Reddit? You shouldn’t need to sanitise your inputs for SQL. Paramatised SQL has been a thing in some languages for two decades now. This really is a long solved problem by now.

Output is a different matter though but that’s because of rendering content safely down to HTML, JavaScript or JSON (to name a few examples). SQL shouldn’t come into the equation by this point.

Re:Don’t try to sanitize input – escape output by tie_ on Thursday February 27, 2020 @03:00AM (#22431514)
This. I'm tired of people implying or right out stating that SQL injection is an input validation problem. Why couldn't you have foo' OR 1=1; as a title of your post? It is all good characters as far as text entry is concerned.

SQL injection is really a problem of how you pass parameters to your SQL layer. Parametrized queries are the (easy and widely available) solution. If you are concatenating input to your SQL queries, you're doing it wrong.

Re:Don’t try to sanitize input – escape output by squiggleblaz on Thursday February 27, 2020 @03:24AM (#22431614)
How many people named O'Brien are told they can't sign up, or passwords get rejected because they contain special characters?

It's crazy.

Even if you're using 1990s technology without parameterised queries, it's not like it's impossible to say `insert into users (name, motto) values ('O\'Brien', 'foo\' OR 1=1;')`.

Re:Don’t try to sanitize input – escape output by fwsgonzo on Thursday February 27, 2020 @08:03AM (#22432831)
Yep. I've stopped counting the amount of websites that force me to use weak passwords. It's crazy that this is still a thing in 2020.

I wish the controls on browsers came with a green V that implements best practices (8+ symbols, no filter) so that people who made websites understand that this is what they should conform to. Not their own misconceptions about password security.

Re:Don’t try to sanitize input – escape output by lodovic on Friday February 28, 2020 @06:45AM (#22442123)
I wish websites would stop enforcing a "password policy". An insecure password should be a choice. If you are so sure you cannot secure your site, leave authentication to a third party provider. All this leads to is zillions of user accounts that are used only once.
Re:Don’t try to sanitize input – escape output by hinkley on Friday February 28, 2020 @11:23PM (#22448802)
I’m sorry, your password must be 16 characters or less and contain no white space or punctuation.
Re:Don’t try to sanitize input – escape output by chaz6 on Friday February 28, 2020 @11:29AM (#22443917)
I have heard stories about people in Ireland struggling to get certain services because their name contains a Fada, but some of the identity paperwork they have is missing the Fada due to lack of support by computer systems.
Re:Don’t try to sanitize input – escape output by simias on Thursday February 27, 2020 @09:13AM (#22433380)
I blame PHP. Many webdevs active today started with it, and the standard library's solution to injections was escaping everything half a dozen times just in case. Because PHP being PHP nobody saw any red flags when they implemented a function named "mysql_real_escape_string". Apparently they've deprecated these functions since then, but the damage is done.
Re:Don’t try to sanitize input – escape output by ivanhoe on Thursday February 27, 2020 @12:04PM (#22435144)
But that's not a thing for 15 years or more? PDO was added around 2005, and even before that anyone in their right mind used mysqli extension for prepared statements. Since 2012 you can't even use the mysql extension without getting a depreciation warning.

And yes, in 90s php's security sucked, but that was nothing php specific, it was just the sentiment of that time. Everyone did it, in all languages. I remember using tons of $dbh->do() in Perl's DBI back then, intentionally avoiding to prepare statements for a quick and dirty stuff (and most of the scripts back then were quick and dirty stuff). It's in a big part because we were used to building desktop apps and thinking in terms of security that applied for them like being careful about your pointers and input strings lengths and stack overflows and stuff. Web was still pretty new thing.

Re:Don’t try to sanitize input – escape output by teh_klev on Thursday February 27, 2020 @01:11PM (#22435938)
> But that's not a thing for 15 years or more? PDO was added around 2005

Ex-shared hosting bod here, who had the joy of managing our PHP environments :(

Sadly in the real world, even after the great big (and pointless) act of deprecating and removing the mysql_* library, naive developers (and experienced ones that should've know better) just moved onto mysqli_* or PDO and still used string concatenation with raw inputs, instead of learning how to parameterise their queries.

Used to drive me flippin' nuts.

Re:Don’t try to sanitize input – escape output by ivanhoe on Friday February 28, 2020 @07:55AM (#22442427)
> naive developers (and experienced ones that should've know better) just moved onto mysqli_* or PDO and still used string concatenation with raw inputs, instead of learning how to parameterise their queries.

True, I stand corrected, I've just checked and Wordpress still does it just like that: https://github.com/WordPress/WordPress/blob/master/wp-includ...

Re:Don’t try to sanitize input – escape output by hinkley on Friday February 28, 2020 @11:37PM (#22448852)
The one and only time I argued with someone about PHP, the forum the guy ran got hacked the very next morning and was running a botnet of some sort. I was smug but quiet, and I never really thought about it but the timing makes me wonder if he thought I was involved.

Anyway, there’s a patch release the next day, and somewhere I find the diff. Now I can’t read PHP but I know what string concatenation looks like, especially if someone does a diff on it. I’ll be damned if the diff didn’t fix one SQL string concatenation that was less than five lines from code with the same structure. Scsry.

Re:Don’t try to sanitize input – escape output by nakkijono on Thursday February 27, 2020 @12:47PM (#22435651)
PHP and vulnerable example code is an additional thing. Most people just copypaste from tutorials. For example, the first search result for "PHP mysql example" gives you the wrong example first https://www.w3schools.com/php/php_mysql_select.asp
Re:Don’t try to sanitize input – escape output by auiya on Thursday February 27, 2020 @08:27AM (#22433018)
> Are you sure that’s what is happening at Reddit?

"I was the first (paid) employee of reddit" https://www.jedberg.net/

I mean...

Re:Don’t try to sanitize input – escape output by laumars on Friday February 28, 2020 @06:51AM (#22442139)
That doesn't mean the information supplied was accurate. Which, as it happened, it wasn't and this whole thing was really just a massive misunderstanding.
Re:Don’t try to sanitize input – escape output by jedberg on Thursday February 27, 2020 @03:39AM (#22431672)
When I said "sanitized input" I was simplifying parameterized SQL in the case of reddit. But since I said "for your datastore" I was being less specific since different datastores require different methods.
Re:Don’t try to sanitize input – escape output by laumars on Thursday February 27, 2020 @04:10AM (#22431802)
Parametrised SQL isn’t sanitising your input. It’s injecting those values in at byte code so your values are separate to the query language. Calling that process “sanitisation” is, at best, highly misleading.

Also the methods don’t really change across different SQL databases, at least not conceptually. Sure the RDBMS drivers might change but these days that stuff is usually abstracted away into a single framework for SQL. The real significant change would be switching to a NoSQL database but if you’re doing that then it’s not SQL you need to be “sanitising” anyway.

Re:Don’t try to sanitize input – escape output by jedberg on Thursday February 27, 2020 @04:19AM (#22431839)
> Calling that process “sanitisation” is, at best, highly misleading.

That's a fair criticism. But that is what I've called it for a long time.

> The real significant change would be switching to a NoSQL database but if you’re doing that then it’s not SQL you need to be “sanitising” anyway.

Right, that's why I started off by saying "Sanitize your inputs so that it can be safely stored in your data store", so that it could apply to any data store.

It's just a terminology argument at this point. But my main point was that you need to still think carefully about how you're going to store your data and do it safely, and then also make things safe on the way out, like the article suggests.

Re:Don’t try to sanitize input – escape output by shawnz on Thursday February 27, 2020 @09:06AM (#22433305)
> That's a fair criticism. But that is what I've called it for a long time.

I think many people would agree that "sanitization" is loosely defined and I think that's exactly what led to the misunderstandings that the OP's article is trying to address.

> that's why I started off by saying "Sanitize your inputs so that it can be safely stored in your data store"

That could be interpreted like "escape quotes at the start of the request if you know that you're using a database where quotes have a special meaning" a la PHP magic quotes, which I'm guessing is not what you meant but it is what the OP is criticizing. The key is that the sanitization (or whatever you want to call it) shouldn't happen until you're ready to insert into the DB, otherwise that data will be coupled with database logic through the whole flow of your app

Re:Don’t try to sanitize input – escape output by wccrawford on Thursday February 27, 2020 @08:13AM (#22432913)
You don't "sanitize your inputs" for your datastore. You escape the outputs as you send it to your datastore. (Either through oldschool methods, or parameterization.)

Sanitizing your input means changing them permanently. You don't actually want to do that. You want to store exactly what the original value was, but you want to do it safely. When you retrieve it again, it should be the same as it was originally.

If you "sanitize" the input, you won't necessarily have the original value ever again.

Re:Don’t try to sanitize input – escape output by ashearer on Thursday February 27, 2020 @10:41AM (#22434199)
Yes. It's the word "sanitize" itself that misleads people. It creates the mindset that input from users is dirty and must be made clean, and "clean" is "safe" to use in any context.

(I've seen the line of thought taken one step further: taking the realization that it's impractical to make strings universally safe for any context—even if you HTML entity-encode it twice, what if a recipient decodes it three times?—and concluding that security is hard and we can only approach it asymptotically, so shrugs XSS-like bugs are normal and unavoidable given finite time & budget.)

If the mindset is more like converting units, it becomes clearer. You can't concatenate HTML with a general Unicode string without converting the string to HTML first, any more than you can add inches and centimeters directly. "Cleaning" the centimeters would make no sense.

Re:Don’t try to sanitize input – escape output by nulbyte on Thursday February 27, 2020 @07:26AM (#22432622)
> But my main point was that you need to still think carefully about how you're going to store your data...

I think this is true, but doesn't quite reach the point GP is making: speaking correctly is also important. Calling parameterization input sanitization communicates the wrong message. And abstracting the wrong solution to apply it to a different problem isn't all that helpful. You could just as easily encode or hash input to fit the underlying data format without losing data (except in the case of truncation), but that isn't input sanitization, either.

Input sanitization is strictly checking front-end input against a ruleset and rejecting anything that does not comply. This is fundamentally different than dealing with anything thrown at you and handling it gracefully.

Re:Don’t try to sanitize input – escape output by Izkata on Thursday February 27, 2020 @08:30AM (#22433031)
> Input sanitization is strictly checking front-end input against a ruleset and rejecting anything that does not comply.

That's validation. Sanitization involves altering the data to make it safe (comes from the word "sanitary").

Re:Don’t try to sanitize input – escape output by organsnyder on Thursday February 27, 2020 @10:23AM (#22434020)
Rejecting input is a valid sanitization strategy, though.
Re:Don’t try to sanitize input – escape output by SahAssar on Thursday February 27, 2020 @02:27PM (#22436832)
That's not how the word is usually used within development in my experience though.

I think most devs think of sanitization as "make X safe", not "see if X is safe, if not reject" since that is usually called validation.

Using hand sanitizer does not remove your hands if they have harmful bacteria. The hands are still there, just cleaned from (some) of the harmful parts.

Re:Don’t try to sanitize input – escape output by Sean1708 on Thursday February 27, 2020 @08:36AM (#22433077)
The article refers to that as escaping output rather than sanitising input:

> And of course use your SQL engine’s parameterized query features so it properly escapes variables when building SQL:

Re:Don’t try to sanitize input – escape output by mannykannot on Thursday February 27, 2020 @08:26AM (#22433008)
We should certainly use parameterization always, and this will allow us to save 'Bobby Tables' type input into our databases, but we should acknowledge that there is then the potential risk that some internal, probably non- public-facing program or script, either now or in the future, will contain a bug that leads to its execution.

The spread of natural language processing into systems and analysis tools might increase the scope for this sort of thing.

Re:Don’t try to sanitize input – escape output by laumars on Thursday February 27, 2020 @09:20AM (#22433451)
I agree you can never say never but you also can't sanitise against a risk that hasn't been defined yet simply because you wouldn't know what needs to be sanitised.

For example what if your NLP is bootstrapped from a shell script and your database content has been stripped of SQL but still contains stuff that might be interpreted as $(sub-shells)? Before long you run into a situation where literally no characters are considered safe (eg even alpha characters in the English alphabet are used as tokens in some programming languages and "what if someone builds a script in one of those languages?").

The only sane way to address the unknown is to treat raw strings as "dirty" and follow best practices when handling them (plus all the usual processes to properly test your code before it's used in production). In which case you're back to no longer needing input sanitisation.

Re:Don’t try to sanitize input – escape output by mannykannot on Thursday February 27, 2020 @09:53AM (#22433733)
My point is not that input sanitization is a solution, but that parameterized database input is not the end of the issue, from a broader, whole-systems security point of view.

Many data types have some concept of well-formedness, and in those cases, there are pragmatic reasons for only accepting well-formed input that go even beyond the security aspect.

Re:Don’t try to sanitize input – escape output by laumars on Thursday February 27, 2020 @10:26AM (#22434059)
I completely agree and I also said this in my original comment you replied to.

This is why I got confused when you said "but we should acknowledge..." (ie thinking you were raising a point other than what myself and others had already acknowledged).

Re:Don’t try to sanitize input – escape output by mannykannot on Thursday February 27, 2020 @10:32AM (#22434109)
It is not clear to me that the post I originally replied to did acknowledge this point.
Re:Don’t try to sanitize input – escape output by laumars on Thursday February 27, 2020 @10:38AM (#22434172)
> You shouldn’t need to sanitise your inputs

> Output is a different matter though

"Output" doesn't have to be front end rendered nor even internet facing. It could also be input for another internal process.

Re:Don’t try to sanitize input – escape output by mannykannot on Thursday February 27, 2020 @11:14AM (#22434489)
Indeed, so there was a point to be made.
Re:Don’t try to sanitize input – escape output by laumars on Friday February 28, 2020 @07:40AM (#22442357)
I honestly don't understand what the point you're trying to make is then.

If you're saying people should be aware that handling data safely requires more steps than just parametrised SQL then yes I touched on that, as have others, and it's not something anyone is unaware of. Hence why there has been so many high quality posts discussing the different methods of validation, sanitisation and escaping. So it's a rather strange position to assume when you say "we need to acknowledge" given that's what everyone (including me) has been doing. But it never hurts to be categoric about important points like that so your original post is still relevant.

If you're making some other point then I've already had two stabs at deciphering it and failed both times. So it's really not clear what that point is.

However if your intention was just trolling me then fine, I bit and you won.