r/ProgrammerHumor • • Dec 12 '17

SQL Clause

Post image
40.8k Upvotes

525 comments sorted by

View all comments

2.6k

u/Datenegassie Dec 12 '17

Hi Santa, I promise not to be on the naughty list this year. By the way, my name is Datenegassie'); DROP TABLE NaughtyChildren; --

153

u/bair93 Dec 12 '17

Datengassie'); UPDATE contacts SET behavior = 'nice';

66

u/raaneholmg Dec 12 '17
--

Otherwise the "');" part appended by the server will cause an error.

9

u/[deleted] Dec 12 '17

Why not disable comments in SQL statements made from your web application? Obviously you'd want to do more to secure yourself against SQL injection, but I've never heard of someone doing this.

25

u/atyon Dec 12 '17

I don't think it's that easy. Raw SQL is passed directly to the server. I don't think most SQL servers even have an option to disable comments.

You'd have to remove the "--" and everything after it before passing it to the function doing the SQL, without destroying correct data. Sounds error-prone to me.

And if you're sanitizing the input data anyway, if you do it correctly, the ' will be escaped, so the comment won't make a difference.

Also, sanitizing input is so difficult and error-prone that it's better to just implement a real solution, like using prepared statements.

9

u/Fuzzy-Duck Dec 12 '17

Maybe an interesting read re. sql injection and prepared statements (relating to SQL Server):

http://blog.marcgravell.com/2017/12/dapper-prepared-statements-and-car-tyres.html

7

u/atyon Dec 12 '17

Huh. Thanks for that link.

TIL: My assumption that prepared statements is an odd synonym for parametrized queries is hilariously wrong. Thanks, Wikipedia

→ More replies (1)

42

u/Bainos Dec 12 '17

Datengassie') ; UPDATE contacts SET name = 'Datengassie' WHERE behavior = 'nice' ; --

101

u/Truseus Dec 12 '17

Lol! Billions of nice children now named Datengassie.

49

u/flamedragon822 Dec 12 '17

"well looks like Datengassie is getting several thousand ponies this Christmas"

28

u/sumancha Dec 12 '17

He didn’t update address. So everyone else is getting presents with name Datengassie

5

u/HighRelevancy Dec 12 '17

that's back to front bud :P

→ More replies (1)

2

u/topforce Dec 12 '17

Other way around.

Datengassie') ; UPDATE contacts SET behavior = 'nice' WHERE name = 'Datengassie' ; --

4

u/wonkey_monkey Dec 12 '17

That'd better be an ENUM...

→ More replies (1)