The state of PHP security
Posted Dec 26, 2006 18:57 UTC (Tue) by iabervon
In reply to: The state of PHP security
Parent article: The state of PHP security
My point is that doing
handle.do_select(new SQLStatement("SELECT id, name FROM customers WHERE country='" + country + "'"))
should always give a runtime error "Single quote in SQL statement". The only way to do this query should be something like
handle.do_select(new SQLStatement("SELECT id, name FROM customers WHERE country=") + country).
You'll note that this is easier than the insecure way (you don't have to know how to put a string in a SQL statement, or remember to close your single quotes or anything like that, and it's shorter anyway, unless your language happens to have operator overloading for strings and nothing else), and, additionally, the system prevents the insecure way from ever being executed, regardless of whether its input is malicious in a particular case or not. The slightly inconvenient case is where you'd be able to use a hard-coded constant embedded in your SQL if that were permitted; but hard-coded constants are a pain for further development anyway; some day your DBA will make you change them, and you'll be sad if they aren't split out into a single location with a logical name. (The only exception being patterns for LIKE, where you have to suffer through making your pattern a string constant instead of having it embedded in the SQL.)
The only sure way to educate people not to write insecure code is to make insecure code not work at all. The lazy people who do things the insecure way for the prototype will find that the prototype doesn't even run, so they'll go back and do it the secure way.
to post comments)