Comment On Security by Posterity

When Rory was interviewing for his position he liked what he heard. A robust data access layer, and a company policy that dictated that if you get caught writing a non-parameterized query, you're tarred, feathered, and recommended for execution. Their rigidity and adherence to the practices, as well as an interviewer asking all of the right questions regarding secure coding and good design ultimately lead to his acceptence of the position. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Security by Posterity

2008-12-18 11:05 • by Edss (unregistered)
Practical joke that never got reversed?

Re: Security by Posterity

2008-12-18 11:06 • by Ozz (unregistered)
Cue 50 references to Bobby Tables...

Re: Security by Posterity

2008-12-18 11:12 • by Rachael (unregistered)
And then...?

I want to know if he looked in the source control logs, or asked someone, to find out *why* it was changed. The answer would be interesting.

Re: Security by Posterity

2008-12-18 11:16 • by D C Ross (unregistered)
235436 in reply to 235435
The most likely reason was either "New programmer didn't understand old code and wanted to do it his own way" or "Programmer figured he was going to be fired soon and wanted to go out with a bang".

Re: Security by Posterity

2008-12-18 11:18 • by TopCod3r
I don't know. It looks like they replaced 12 lines of code with 4 lines, so in that sense, this refactoring produced more efficient code. I don't know who uses ODBC anymore though, it is all OLEDB nowadays.

Is that code in Javascript though? If so, that is a horrible practice because someone could just modify your SQL. It is better to just build a parameter string and then do the replacement on the server side.

Here is what I'm talking about:

Dim strSQL As String = "select * from [~TABLENAME~] where ~COLUMN~ = ~VALUE~"
strSQL = strSQL.Replace("~TABLENAME~", Request.QueryString("t"))
strSQL = strSQL.Replace("~COLUMN~", Request.QueryString("c"))
strSQL = strSQL.Replace("~VALUE~", Request.QueryString("v"))
'Now you can execute strSQL

This is a simple example, but you get the picture. Most of our queries have many more parameters, so you have c1/v1, c2/v2, c3/v3, etc.

The bottom line is you always want to keep your data layer on the SERVER and never on the CLIENT, I cannot stress this enough. Well most of the time not on the client, there are some exceptions that I can think of, but those are advanced scenarios.

Re: Security by Posterity

2008-12-18 11:22 • by Code Dependent
insertComment = insertComment.Replace("@Text", "'Somebody has to do it. Might as well be me.'");

Re: Security by Posterity

2008-12-18 11:22 • by ChiefCrazyTalk (unregistered)
Parameters can be finicky. My guess is that the code written the "right way" did not work, and so to meet their deadline they commented it out and they did the quick and dirty replace with strings method.

Re: Security by Posterity

2008-12-18 11:25 • by ShatteredArm (unregistered)
235444 in reply to 235437
TopCod3r:
I don't know. It looks like they replaced 12 lines of code with 4 lines, so in that sense, this refactoring produced more efficient code. I don't know who uses ODBC anymore though, it is all OLEDB nowadays.

Is that code in Javascript though? If so, that is a horrible practice because someone could just modify your SQL. It is better to just build a parameter string and then do the replacement on the server side.

Here is what I'm talking about:

Dim strSQL As String = "select * from [~TABLENAME~] where ~COLUMN~ = ~VALUE~"
strSQL = strSQL.Replace("~TABLENAME~", Request.QueryString("t"))
strSQL = strSQL.Replace("~COLUMN~", Request.QueryString("c"))
strSQL = strSQL.Replace("~VALUE~", Request.QueryString("v"))
'Now you can execute strSQL

This is a simple example, but you get the picture. Most of our queries have many more parameters, so you have c1/v1, c2/v2, c3/v3, etc.

The bottom line is you always want to keep your data layer on the SERVER and never on the CLIENT, I cannot stress this enough. Well most of the time not on the client, there are some exceptions that I can think of, but those are advanced scenarios.



Your SQL is wrong. To allow it to be even more dynamic, it should be more like so:
Dim strSQL As String = "sp_execsql 'select * from [~TABLENAME~] ...

Re: Security by Posterity

2008-12-18 11:26 • by Code Dependent
235446 in reply to 235437
TopCod3r:
...there are some exceptions that I can think of, but those are advanced scenarios.
I advise you to switch to regressed scenarios. That way you can do proper regression testing.

Re: Security by Posterity

2008-12-18 11:27 • by WhiskeyJack
Teehee. I bet there's a hilarious webcomic out there that illustrates the fun of SQL injection.

Let me see if I can find the link...

Re: Security by Posterity

2008-12-18 11:36 • by Code Dependent
235450 in reply to 235447
WhiskeyJack:
Teehee. I bet there's a hilarious webcomic out there that illustrates the fun of SQL injection.

Let me see if I can find the link...
One that turns the Tables on DB-folks?

Re: Security by Posterity

2008-12-18 11:37 • by derula
235451 in reply to 235447
WhiskeyJack:
Teehee. I bet there's a hilarious webcomic out there that illustrates the fun of SQL injection.

Let me see if I can find the link...


Too late. TopCod3r has posted. You are now to flame him, not to bother with little Bobby Tables.

Re: Security by Posterity

2008-12-18 11:37 • by ounos

An Error Occured

Not sure what it was, but it was logged. A human will eventually look at it. If the problem persists, please Contact Us. If the problem is on the contact form, then ... well ... that pretty much sucks. You can email instead: alexp-at-WorseThanFailure.com.

WTF???

Re: Security by Posterity

2008-12-18 11:38 • by MyKey_
235455 in reply to 235447
WhiskeyJack:

Teehee. I bet there's a hilarious webcomic out there that illustrates the fun of SQL injection.

Let me see if I can find the link...

Yeah, I remember that one too!
It was something about Bobby, wasn't it?

Good luck finding it, maybe some of the other commenters can help us finding the link...

Re: Security by Posterity

2008-12-18 11:52 • by Downfall (unregistered)
Hey guys, I found this hilarious comic:



Re: Security by Posterity

2008-12-18 11:54 • by ~Anon (unregistered)
It had to be done.
http://xkcd.com/327/

Re: Security by Posterity

2008-12-18 11:57 • by durendal.mk3
235461 in reply to 235459
Downfall:
Hey guys, I found this hilarious comic:

(snip XKCD)



+1 Relevant

Re: Security by Posterity

2008-12-18 12:00 • by Andy Goth
235462 in reply to 235437
TopCod3r:
It looks like they replaced 12 lines of code with 4 lines, so in that sense, this refactoring produced more efficient code.
Everything on this page is a parameterized query even though it doesn't look like it. (Bonus: the query statements are automatically prepared and cached, then are finalized when no longer reachable.) Using this particular API would have replaced the 12 lines with 1 line without sacrificing security or performance.

Re: Security by Posterity

2008-12-18 12:00 • by campkev
235464 in reply to 235460
Anon:

It had to be done.
http://xkcd.com/327/

no, it really didn't

Re: Security by Posterity

2008-12-18 12:02 • by Anonymous (unregistered)
235465 in reply to 235452
ounos:
An Error Occured

Not sure what it was, but it was logged. A human will eventually look at it. If the problem persists, please Contact Us. If the problem is on the contact form, then ... well ... that pretty much sucks. You can email instead: alexp-at-WorseThanFailure.com.
WTF???
You'll get used to it. I see this error page about once a week.

Re: Security by Posterity

2008-12-18 12:16 • by SecCodr (unregistered)
235468 in reply to 235437
I hope you are joking about using the above code to handle your SQL queries. That is still very susceptible to SQL injection.

Re: Security by Posterity

2008-12-18 12:17 • by SecCodr (unregistered)
235470 in reply to 235437
TopCod3r:
I don't know. It looks like they replaced 12 lines of code with 4 lines, so in that sense, this refactoring produced more efficient code. I don't know who uses ODBC anymore though, it is all OLEDB nowadays.

Is that code in Javascript though? If so, that is a horrible practice because someone could just modify your SQL. It is better to just build a parameter string and then do the replacement on the server side.

Here is what I'm talking about:

Dim strSQL As String = "select * from [~TABLENAME~] where ~COLUMN~ = ~VALUE~"
strSQL = strSQL.Replace("~TABLENAME~", Request.QueryString("t"))
strSQL = strSQL.Replace("~COLUMN~", Request.QueryString("c"))
strSQL = strSQL.Replace("~VALUE~", Request.QueryString("v"))
'Now you can execute strSQL

This is a simple example, but you get the picture. Most of our queries have many more parameters, so you have c1/v1, c2/v2, c3/v3, etc.

The bottom line is you always want to keep your data layer on the SERVER and never on the CLIENT, I cannot stress this enough. Well most of the time not on the client, there are some exceptions that I can think of, but those are advanced scenarios.



I hope you are joking about using the above code to handle your SQL queries. That is still very susceptible to SQL injection.

Re: Security by Posterity

2008-12-18 12:18 • by TopCod3r
235472 in reply to 235462
Andy Goth:
TopCod3r:
It looks like they replaced 12 lines of code with 4 lines, so in that sense, this refactoring produced more efficient code.
Everything on this page is a parameterized query even though it doesn't look like it. (Bonus: the query statements are automatically prepared and cached, then are finalized when no longer reachable.) Using this particular API would have replaced the 12 lines with 1 line without sacrificing security or performance.


Thanks for the link. That reminds me of the data access library that I wrote at my last job. It basically wrote the SQL for you, and made it so you almost didn't even need to know how to write code in order to write a program. I would have probably been able to sell to other developers and make some money, but I had to sign an intellectual property agreement when I was hired. That link you gave me might be the motivation I need to try to write a newer more powerful version of what I did before. I just won't be able to use any of the same code. Except my version will work with VB.NET, not Tcl, so it will be usable by many more people.

Re: Security by Posterity

2008-12-18 12:22 • by mjk340
The only secure application is one that doesn't use the internet, or a computer. I would just mail a product catalog to all potential clients and ask them to pay with a money order.

Re: Security by Posterity

2008-12-18 12:28 • by File Not Found (unregistered)
235477 in reply to 235470
SecCodr:
TopCod3r:
blah, blah


I hope you are joking about using the above code to handle your SQL queries. That is still very susceptible to SQL injection.


And you are very susceptible to TopCod3rs comments.

Re: Security by Posterity

2008-12-18 12:29 • by Maurits
235478 in reply to 235470
SecCodr:
TopCod3r:
...


I hope you are joking...


Shark Tank has JIM THE BOSS. TDWTF has TopCod3r.

Re: Security by Posterity

2008-12-18 12:37 • by St Mary's Hospital for the Grizzly Bears (unregistered)
235480 in reply to 235474
mjk340:
The only secure application is one that doesn't use the internet, or a computer. I would just mail a product catalog to all potential clients and ask them to pay with a money order.


Mail fraud?

Re: Security by Posterity

2008-12-18 13:00 • by Dominic (unregistered)
Simple fix (assumes that the DB is SQL Server)...

insertQuery = Queries.InsertForumSignUp;

insertQuery = insertQuery.Replace("@Name", "'" + signUpEntity.Name.Replace("'", "''") + "'");
insertQuery = insertQuery.Replace("@Email", "'" + signUpEntity.Email.Replace("'", "''") + "'");
insertQuery = insertQuery.Replace("@Type", "'" + signUpEntity.Type.Replace("'", "''") + "'");
insertQuery = insertQuery.Replace("@ForumsOption", signUpEntity.ForumsOption.ToString());


Checking that .Name .Email and .Type don't throw NullReference exceptions left as an exercise to the reader.

Re: Security by Posterity

2008-12-18 13:07 • by ParkinT
So at some point they had done it the right way, but later changed it to the wrong way, but preseved the correct method for posterity, I guess.

For backward compatibility, I am sure.

Re: Security by Posterity

2008-12-18 13:26 • by WhiskeyJack
235489 in reply to 235486
ParkinT:
So at some point they had done it the right way, but later changed it to the wrong way, but preseved the correct method for posterity, I guess.

For backward compatibility, I am sure.


Hey, redundancy is good, right?

Maybe they should uncomment the second query. Then, to make sure everything is working, execute BOTH sections of code. And compare the results. If the output from both is the same, then everything is working great!

Re: Security by Posterity

2008-12-18 13:34 • by K (unregistered)
235490 in reply to 235437
TopCod3r:
I don't know. It looks like they replaced 12 lines of code with 4 lines, so in that sense, this refactoring produced more efficient code. I don't know who uses ODBC anymore though, it is all OLEDB nowadays.

Is that code in Javascript though? If so, that is a horrible practice because someone could just modify your SQL. It is better to just build a parameter string and then do the replacement on the server side.

Here is what I'm talking about:

Dim strSQL As String = "select * from [~TABLENAME~] where ~COLUMN~ = ~VALUE~"
strSQL = strSQL.Replace("~TABLENAME~", Request.QueryString("t"))
strSQL = strSQL.Replace("~COLUMN~", Request.QueryString("c"))
strSQL = strSQL.Replace("~VALUE~", Request.QueryString("v"))
'Now you can execute strSQL

This is a simple example, but you get the picture. Most of our queries have many more parameters, so you have c1/v1, c2/v2, c3/v3, etc.

The bottom line is you always want to keep your data layer on the SERVER and never on the CLIENT, I cannot stress this enough. Well most of the time not on the client, there are some exceptions that I can think of, but those are advanced scenarios.



That's friggin' brilliant... Let's see, how about something like http://.../query?t=users&c=1&v=1 which yields:

select * from users where ~1 = 1

Excellent example of secure query programming! It's on the server though, so it must be safe???

Re: Security by Posterity

2008-12-18 13:35 • by diaphanein (unregistered)
235492 in reply to 235478
Maurits:
SecCodr:
TopCod3r:
...


I hope you are joking...


Shark Tank has JIM THE BOSS. TDWTF has TopCod3r.


Every forum should have a warning sign: PLEASE DON'T FEED THE TROLLS.

Re: Security by Posterity

2008-12-18 13:43 • by Kender (unregistered)
235493 in reply to 235485
Dominic:
Simple fix (assumes that the DB is SQL Server)...

insertQuery = insertQuery.Replace("@Name", "'" + signUpEntity.Name.Replace("'", "''") + "'");


Yeah right. So:
select * from a where b = @name;
with signUpEntity.name = \' or 1 <> \
becomes
select * from a where b = '\'' or 1 <> \';
or whatever.
Doubling single quotes is *not* a solution :(

Re: Security by Posterity

2008-12-18 13:50 • by anonymous (unregistered)
235495 in reply to 235444
hmm. wouldn't this be easier ?
Dim strSQL as String = "sp_execsql '~SQL~'"
strSQL = strSQL.Replace("~SQL~",Request.QueryString("SQL"))
execute!

Re: Security by Posterity

2008-12-18 13:53 • by TopCod3r
235496 in reply to 235495
anonymous:
hmm. wouldn't this be easier ?
Dim strSQL as String = "sp_execsql '~SQL~'"
strSQL = strSQL.Replace("~SQL~",Request.QueryString("SQL"))
execute!


I think you are missing the point, but that's ok, I don't blame you. The reason you don't want to do that is so the client doesn't have to know how to build SQL code, and also so a hacker can't just stick whatever SQL he wants... like deleting your entire orders table.

Re: Security by Posterity

2008-12-18 13:56 • by anonymous (unregistered)
235498 in reply to 235496
TopCod3r:
anonymous:
hmm. wouldn't this be easier ?
Dim strSQL as String = "sp_execsql '~SQL~'"
strSQL = strSQL.Replace("~SQL~",Request.QueryString("SQL"))
execute!


I think you are missing the point, but that's ok, I don't blame you. The reason you don't want to do that is so the client doesn't have to know how to build SQL code, and also so a hacker can't just stick whatever SQL he wants... like deleting your entire orders table.


Ah. Now you see the problem with your code. replace the V in the query string with v=fake_value; delete from orders;

Re: Security by Posterity

2008-12-18 14:13 • by PSWorx
235500 in reply to 235437
TopCod3r:
I don't know. It looks like they replaced 12 lines of code with 4 lines, so in that sense, this refactoring produced more efficient code. I don't know who uses ODBC anymore though, it is all OLEDB nowadays.

Is that code in Javascript though? If so, that is a horrible practice because someone could just modify your SQL. It is better to just build a parameter string and then do the replacement on the server side.

Here is what I'm talking about:

Dim strSQL As String = "select * from [~TABLENAME~] where ~COLUMN~ = ~VALUE~"
strSQL = strSQL.Replace("~TABLENAME~", Request.QueryString("t"))
strSQL = strSQL.Replace("~COLUMN~", Request.QueryString("c"))
strSQL = strSQL.Replace("~VALUE~", Request.QueryString("v"))
'Now you can execute strSQL

This is a simple example, but you get the picture. Most of our queries have many more parameters, so you have c1/v1, c2/v2, c3/v3, etc.

The bottom line is you always want to keep your data layer on the SERVER and never on the CLIENT, I cannot stress this enough. Well most of the time not on the client, there are some exceptions that I can think of, but those are advanced scenarios.



Why not replace it with strSQL = "DROP TABLE ORDERS"?
That's four lines to one line, think of the efficiency gain!

Re: Security by Posterity

2008-12-18 14:18 • by Max (unregistered)
Note to all programmers:

What a company focuses on in its interview are three things:

1) Can you do the job?
2) Will you fit with the team?
3) Will you fix our stupid problems?

So if they focus on good design to the exclusion of all else, you can be very sure they are answering #3, not #1. Their code will suck.

Re: Security by Posterity

2008-12-18 15:06 • by Evo
In Soviet Russia, strings escape you!

Re: Security by Posterity

2008-12-18 15:12 • by ClutchDude (unregistered)
sb.append("SELECT ");
sb.append(" COALESCE(BAG_APRS_AMT, 0.0), ");//1
sb.append("BAG_APRS_DT, ");//2
sb.append("BAG_APRS_DSC, ");//3
<snip>

My WTF is progress. When I asked about this code, the programmer said,"We've had a lot of folks work on this with their own conventions..."

Re: Security by Posterity

2008-12-18 15:25 • by heltoupee
235516 in reply to 235433
Cue 50 references to Bobby Tables..


That's funny, we have no record of little Bobby Tables. Or any other students. Oh. My. GOD.

Re: Security by Posterity

2008-12-18 15:28 • by Mod Vinson (unregistered)
235517 in reply to 235498
anonymous:
TopCod3r:
anonymous:
hmm. wouldn't this be easier ?
Dim strSQL as String = "sp_execsql '~SQL~'"
strSQL = strSQL.Replace("~SQL~",Request.QueryString("SQL"))
execute!


I think you are missing the point, but that's ok, I don't blame you. The reason you don't want to do that is so the client doesn't have to know how to build SQL code, and also so a hacker can't just stick whatever SQL he wants... like deleting your entire orders table.


Ah. Now you see the problem with your code. replace the V in the query string with v=fake_value; delete from orders;
Do you see what you have just done?

Re: Security by Posterity

2008-12-18 15:57 • by Walleye (unregistered)
235522 in reply to 235492
diaphanein:
Maurits:
SecCodr:
TopCod3r:
...


I hope you are joking...


Shark Tank has JIM THE BOSS. TDWTF has TopCod3r.


Every forum should have a warning sign: PLEASE DON'T FEED THE TROLLS.


At least JIM is witty in his trollery. Maybe TopCod3r should be FRIERED!

Re: Security by Posterity

2008-12-18 16:39 • by brunascle
235531 in reply to 235493
Kender:

Yeah right. So:
select * from a where b = @name;
with signUpEntity.name = \' or 1 <> \
becomes
select * from a where b = '\'' or 1 <> \';
or whatever.


which works just fine, the "or 1 <> \" is still inside the string. a backslash doesnt escape a single quote.

Re: Security by Posterity

2008-12-18 17:48 • by Bernie (unregistered)
235539 in reply to 235437
TopCod3r:
It looks like they replaced 12 lines of code with 4 lines, so in that sense, this refactoring produced more efficient code.

Exactly! All good coders know that fewer lines always produce more efficient code. In fact, the first thing I do when starting on an existing project is strip all LFs & CRs from the code. I could just replace all of the code with a command to restart the computer, but I don't like showing off.

Re: Security by Posterity

2008-12-18 18:26 • by Ross (unregistered)
From now on Bobby Table is 327 and 179 messes with my brain.
CAPTCHA: mara - a misspelling of a dwarf mine?

Re: Security by Posterity

2008-12-18 18:30 • by M (unregistered)
235542 in reply to 235437
Please submit my request:

http://thedailywtf.com/query?t=dual; delete from comments&c=userid&v='TopCod3r' and user_type %3d 'dipsh*t'

Re: Security by Posterity

2008-12-18 18:51 • by lolwtf
235543 in reply to 235451
derula:
WhiskeyJack:
Teehee. I bet there's a hilarious webcomic out there that illustrates the fun of SQL injection.

Let me see if I can find the link...


Too late. TopCod3r has posted. You are now to flame him, not to bother with little Bobby Tables.
Can't we do both? TopCod3r, you're completely wrong, watch what happens when Bobby Tables visits your site.

Re: Security by Posterity

2008-12-18 19:56 • by Random (unregistered)
articleText = articleText.Replace("preseved", "preserved");

Re: Security by Posterity

2008-12-18 20:34 • by James (unregistered)
You whiny bastards. TopCod3r is the best thing about this site... Consider it free internet survival training.
« PrevPage 1 | Page 2Next »

Add Comment