Likewise, tutorials and training need to do start doing things the right way from the outset.
Thirty-one-year-old Laurie Love is currently staring down the possibility of 99 years in prison.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32147509#p32147509:g9hqizrl said:Maldoror[/url]":g9hqizrl]Thirty-one-year-old Laurie Love is currently staring down the possibility of 99 years in prison.
Am I the only one who thinks this is a disproportionately long custodial sentence for this crime? Surely a few years in the slammer would set him straight.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32147827#p32147827:2zo9kne0 said:quietnine[/url]":2zo9kne0]Proper escaping folks. That's all we ask.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32147575#p32147575:zhixxq4h said:ChrisSD[/url]":zhixxq4h]It is baffling to me that there are web devs writing for some major sites who are writing code in such a flawed way. I don't want to call them incompetent exactly but at this point there is little excuse for such elementary problems.
Personally I've found the slogan "parametrize, not sanitize" helps inexperienced devs to remember good practice. The only issue is they sometimes take it a bit too literally but I can't figure out a pithy catchphrase that explains sanity checks have their place so long as it's built on top of prepared statements and not as a replacement to it.
Pull * (or a subset) and only return the requested field. More sent over the wire but it's safe and optimization-friendly.[url=http://meincmagazine.com/civis/viewtopic.php?p=32148031#p32148031:3g8zxmao said:Dayvid[/url]":3g8zxmao]as an arbitrary example SELECT A, ?field FROM TABLE is doomed - there's no way I can specify that as a particular field from that database table in a prepared statement.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32147575#p32147575:2pdtmwof said:ChrisSD[/url]":2pdtmwof]It is baffling to me that there are web devs writing for some major sites who are writing code in such a flawed way. I don't want to call them incompetent exactly but at this point there is little excuse for such elementary problems.
Personally I've found the slogan "parametrize, not sanitize" helps inexperienced devs to remember good practice. The only issue is they sometimes take it a bit too literally but I can't figure out a pithy catchphrase that explains sanity checks have their place so long as it's built on top of prepared statements and not as a replacement to it.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32147575#p32147575:1rxhp40h said:ChrisSD[/url]":1rxhp40h]It is baffling to me that there are web devs writing for some major sites who are writing code in such a flawed way. I don't want to call them incompetent exactly but at this point there is little excuse for such elementary problems.
Personally I've found the slogan "parametrize, not sanitize" helps inexperienced devs to remember good practice. The only issue is they sometimes take it a bit too literally but I can't figure out a pithy catchphrase that explains sanity checks have their place so long as it's built on top of prepared statements and not as a replacement to it.
I think there are a couple of reasons for this. First most devs/dbas are fluent either in programming languages or SQL but rarely both. Second many common languages do not have good native facilities available to parameterize the input. The frameworks that have excellent facilities require one more item to study. So, when the PHB says build a website that does whatever by x date you are often stuck with using the tools and skills you already have.
I my personal situation I am programmer with a decent knowledge of SQL and very limited knowledge of the frameworks and parameterizing APIs. So if a PHB says build a website quickly I would naturally fall back on what I know. Though I am aware of SQL injection and know it is reasonably straightforward to block with the right tools I am not very skilled at those tools (and obviously I know it). If I have the time I would learn one the tools but often the PHB does not want to delay a project because they have made promises that must be kept or they lose face with upper mismanagement.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32148109#p32148109:2viv61xp said:rockforbrains[/url]":2viv61xp][url=http://meincmagazine.com/civis/viewtopic.php?p=32147575#p32147575:2viv61xp said:ChrisSD[/url]":2viv61xp]It is baffling to me that there are web devs writing for some major sites who are writing code in such a flawed way. I don't want to call them incompetent exactly but at this point there is little excuse for such elementary problems.
Personally I've found the slogan "parametrize, not sanitize" helps inexperienced devs to remember good practice. The only issue is they sometimes take it a bit too literally but I can't figure out a pithy catchphrase that explains sanity checks have their place so long as it's built on top of prepared statements and not as a replacement to it.
I think there are a couple of reasons for this. First most devs/dbas are fluent either in programming languages or SQL but rarely both. Second many common languages do not have good native facilities available to parameterize the input. The frameworks that have excellent facilities require one more item to study. So, when the PHB says build a website that does whatever by x date you are often stuck with using the tools and skills you already have.
I my personal situation I am programmer with a decent knowledge of SQL and very limited knowledge of the frameworks and parameterizing APIs. So if a PHB says build a website quickly I would naturally fall back on what I know. Though I am aware of SQL injection and know it is reasonably straightforward to block with the right tools I am not very skilled at those tools (and obviously I know it). If I have the time I would learn one the tools but often the PHB does not want to delay a project because they have made promises that must be kept or they lose face with upper mismanagement.
I can understand that an amateur (in terms of web development) may not know what they don't know. This doesn't excuse professional web devs nor the companies that not only won't hire professionals but also won't give their current employees the time to get some basic knowledge of the problem domain.[url=http://meincmagazine.com/civis/viewtopic.php?p=32148109#p32148109:1zh2hw6a said:rockforbrains[/url]":1zh2hw6a][url=http://meincmagazine.com/civis/viewtopic.php?p=32147575#p32147575:1zh2hw6a said:ChrisSD[/url]":1zh2hw6a]It is baffling to me that there are web devs writing for some major sites who are writing code in such a flawed way. I don't want to call them incompetent exactly but at this point there is little excuse for such elementary problems.
Personally I've found the slogan "parametrize, not sanitize" helps inexperienced devs to remember good practice. The only issue is they sometimes take it a bit too literally but I can't figure out a pithy catchphrase that explains sanity checks have their place so long as it's built on top of prepared statements and not as a replacement to it.
I think there are a couple of reasons for this. First most devs/dbas are fluent either in programming languages or SQL but rarely both. Second many common languages do not have good native facilities available to parameterize the input. The frameworks that have excellent facilities require one more item to study. So, when the PHB says build a website that does whatever by x date you are often stuck with using the tools and skills you already have.
I my personal situation I am programmer with a decent knowledge of SQL and very limited knowledge of the frameworks and parameterizing APIs. So if a PHB says build a website quickly I would naturally fall back on what I know. Though I am aware of SQL injection and know it is reasonably straightforward to block with the right tools I am not very skilled at those tools (and obviously I know it). If I have the time I would learn one the tools but often the PHB does not want to delay a project because they have made promises that must be kept or they lose face with upper mismanagement.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32147665#p32147665:34e0evw0 said:April King[/url]":34e0evw0]Seeing md5($password) in the article's primary image was enough to give me serious heartburn. I certainly hope that screenshot is of code that is 20 years old.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32148031#p32148031:4qulgo2b said:Dayvid[/url]":4qulgo2b]It's partially because prepared statements aren't a complete solution and don't allow for some use cases - eg anything involving structural statements. You can use a prepared statement to make SELECT A,B FROM TABLE WHERE C = ?param safe, but as an arbitrary example SELECT A, ?field FROM TABLE is doomed - there's no way I can specify that as a particular field from that database table in a prepared statement. There's workarounds (eg using a giant case/if else/whatever statement to build the query from comparing ?field to preapproved values if they're static) but they're ugly enough that people can avoid them.
You can argue don't do that and for a lot of cases that's reasonable, but it's common enough that this still happens. As soon as you're in that situation you're back with the trying to sanitize inputs and it doesn't really matter how careful you are, there's probably sql injection bugs.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32148379#p32148379:je20vx62 said:ramases[/url]":je20vx62][url=http://meincmagazine.com/civis/viewtopic.php?p=32148031#p32148031:je20vx62 said:Dayvid[/url]":je20vx62]It's partially because prepared statements aren't a complete solution and don't allow for some use cases - eg anything involving structural statements. You can use a prepared statement to make SELECT A,B FROM TABLE WHERE C = ?param safe, but as an arbitrary example SELECT A, ?field FROM TABLE is doomed - there's no way I can specify that as a particular field from that database table in a prepared statement. There's workarounds (eg using a giant case/if else/whatever statement to build the query from comparing ?field to preapproved values if they're static) but they're ugly enough that people can avoid them.
You can argue don't do that and for a lot of cases that's reasonable, but it's common enough that this still happens. As soon as you're in that situation you're back with the trying to sanitize inputs and it doesn't really matter how careful you are, there's probably sql injection bugs.
First, IME true need for that type of dynamic query generation is much more rare than commonly thought off. Rare enough that I would refuse any such change during review, unless compelling need can be demonstrated.
Besides, many of the widely reported cases of successful SQL injection did not fall under it. There was absolutely no excuse for not using prepared statements there.
Second, you can safely generate such queries using prepared statements and without if/else monsters: Every single API worth using provides for a way to retrieve the column names for a table.
All you need to do is to check each requested field name against this whitelist, and if a match is found, use the column name from the whitelist instead from the user-supplied input; if no match (or more than one match) is found, you log a security incident.
This way, even if someone managed to trick your comparison function into accepting malicious input, the "damage" is limited that they can perhaps choose a different field, a capability they'd implicitly have in this scenario anyway.
Once you've sanitized your column names this way, you use that to generate the prepared statement, and then proceed as usual.
That's something you stick into some utility part of your code-base if you truly need this capability, and I honestly have troubles thinking of a scenario where there is both a legitimate need for this type of query generation but where column-name-comparison against the table structure would be too much effort.
Ain't that the truth.The Article":2ucnat2b said:But with developers apparently unwilling to do things the right way, it's time for the creators of these frameworks to do the right thing and force all database access to go through prepared statements. This won't make it impossible for wayward developers to use string manipulation and continue to create highly exploitable flaws,
[url=http://meincmagazine.com/civis/viewtopic.php?p=32147575#p32147575:3dkln2ot said:ChrisSD[/url]":3dkln2ot]It is baffling to me that there are web devs writing for some major sites who are writing code in such a flawed way. I don't want to call them incompetent exactly . . .
[url=http://meincmagazine.com/civis/viewtopic.php?p=32148661#p32148661:lcqyodev said:photochemsyn[/url]":lcqyodev][url=http://meincmagazine.com/civis/viewtopic.php?p=32147575#p32147575:lcqyodev said:ChrisSD[/url]":lcqyodev]It is baffling to me that there are web devs writing for some major sites who are writing code in such a flawed way. I don't want to call them incompetent exactly . . .
It might be related to the lack of consequences for sloppy work in the field. I.e. Let's say in contrast you design a chemical production system with equally bad flaws. The consequences of sloppy work are explosions, toxic leaks, people dying, massive damages and lawsuits. Whereas in web development work, leaky flawed systems look good and seem to work fine, and since the startup owner doesn't want to pay for comprehensive testing of the system, just wants to start generating revenue as soon as possible. . .
Thirty-one-year-old Laurie Love is currently staring down the possibility of 99 years in prison.
Am I the only one who thinks this is a disproportionately long custodial sentence for this crime? Surely a few years in the slammer would set him straight.
ALL SQL is a string, submitted to the database. Doesn't matter the source - prepared statement, stored procedure, etc.[url=http://meincmagazine.com/civis/viewtopic.php?p=32148031#p32148031:8nehhpdr said:Dayvid[/url]":8nehhpdr][url=http://meincmagazine.com/civis/viewtopic.php?p=32147575#p32147575:8nehhpdr said:ChrisSD[/url]":8nehhpdr]It is baffling to me that there are web devs writing for some major sites who are writing code in such a flawed way. I don't want to call them incompetent exactly but at this point there is little excuse for such elementary problems.
Personally I've found the slogan "parametrize, not sanitize" helps inexperienced devs to remember good practice. The only issue is they sometimes take it a bit too literally but I can't figure out a pithy catchphrase that explains sanity checks have their place so long as it's built on top of prepared statements and not as a replacement to it.
It's partially because prepared statements aren't a complete solution and don't allow for some use cases - eg anything involving structural statements. You can use a prepared statement to make SELECT A,B FROM TABLE WHERE C = ?param safe, but as an arbitrary example SELECT A, ?field FROM TABLE is doomed - there's no way I can specify that as a particular field from that database table in a prepared statement. There's workarounds (eg using a giant case/if else/whatever statement to build the query from comparing ?field to preapproved values if they're static) but they're ugly enough that people can avoid them.
There are safeguards for such industries where software issues will put lives at risk, which is typically reflected in the cost as satisifying those requirements costs time and money.[url=http://meincmagazine.com/civis/viewtopic.php?p=32148661#p32148661:14uehz1j said:photochemsyn[/url]":14uehz1j][url=http://meincmagazine.com/civis/viewtopic.php?p=32147575#p32147575:14uehz1j said:ChrisSD[/url]":14uehz1j]It is baffling to me that there are web devs writing for some major sites who are writing code in such a flawed way. I don't want to call them incompetent exactly . . .
It might be related to the lack of consequences for sloppy work in the field. I.e. Let's say in contrast you design a chemical production system with equally bad flaws. The consequences of sloppy work are explosions, toxic leaks, people dying, massive damages and lawsuits. Whereas in web development work, leaky flawed systems look good and seem to work fine, and since the startup owner doesn't want to pay for comprehensive testing of the system, just wants to start generating revenue as soon as possible. . .
You've never had to deal with a poorly performing query before. Native SQL is the only means - ORM has never been the silver bullet, and prepared statements still require someone to have actual SQL knowledge.[url=http://meincmagazine.com/civis/viewtopic.php?p=32148379#p32148379:1p2focg3 said:ramases[/url]":1p2focg3]
Besides, many of the widely reported cases of successful SQL injection did not fall under it. There was absolutely no excuse for not using prepared statements there.
Second, you can safely generate such queries using prepared statements and without if/else monsters: Every single API worth using provides for a way to dynamically retrieve the column names for a table; there is no need for hardcoded lists.
All you need to do is to check each requested field name against this whitelist, and if a match is found, use the column name from the whitelist instead from the user-supplied input; if no match (or more than one match) is found, you log a security incident.
Once you've sanitized your column names this way, you use that to generate the prepared statement, and bind parameters.
That's something you stick into some utility part of your code-base if you truly need this capability, and I honestly have troubles thinking of a scenario where there is both a legitimate need for this type of query generation but where column-name-comparison against the table structure would be too much effort.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32149207#p32149207:2uvrwmjo said:millad[/url]":2uvrwmjo]For any PHP programmers out there reading this and are thinking "Okay, but what do I do about it?" Please use and implement PDO (PHP Data Objects). Read through the manual on php.net and search for "(The only proper) PDO tutorial - PHP Delusions", and of course "PHP The Right Way"
Obviously, you can use a framework to help you if you like, but it is not required at all and you could still shoot yourself in the foot with a framework. Understand how to use PDO. All of your database efforts going forward will be much easier and safer for everyone.
So you [and your upvoters] do not know what dynamic SQL is, or when to use it...[url=http://meincmagazine.com/civis/viewtopic.php?p=32147477#p32147477:1kky9atc said:psd[/url]":1kky9atc]Likewise, tutorials and training need to do start doing things the right way from the outset.
A massive effort should be launched to downvote or outright remove answers that suggest string concatenation of SQL statements from places like StackOverflow and other forums.
[url=http://meincmagazine.com/civis/viewtopic.php?p=32149267#p32149267:kmp9vvoc said:psiclone[/url]":kmp9vvoc][url=http://meincmagazine.com/civis/viewtopic.php?p=32149207#p32149207:kmp9vvoc said:millad[/url]":kmp9vvoc]For any PHP programmers out there reading this and are thinking "Okay, but what do I do about it?" Please use and implement PDO (PHP Data Objects). Read through the manual on php.net and search for "(The only proper) PDO tutorial - PHP Delusions", and of course "PHP The Right Way"
Obviously, you can use a framework to help you if you like, but it is not required at all and you could still shoot yourself in the foot with a framework. Understand how to use PDO. All of your database efforts going forward will be much easier and safer for everyone.
The trick is when you have over 100 fields to deal with. That gets really fun...NOT!