2 ms·
That's an example of hazardously bad programming practices. You're one mistake away from complete disaster. You should be sure that it takes more than one mista
by astrodust 13y ago
That's an example of hazardously bad programming practices. You're one mistake away from complete disaster. You should be sure that it takes more than one mistake to expose you to that sort of risk.
Casting to int is not a general purpose escaping system, and further, if you miss even one of these your entire application can be trashed.
Using mysql_query at all is a sign there's something severely wrong with your application.
- steve-howard 13y agoIt's a sign that the developer has had a fairly informal introduction to SQL. Personally I see no reason not to use the PDO extension in any situation where you would use mysql.
- ratsbane 13y agoThere is an opposing viewpoint, i.e. if you actually need an int, casting to int is one of the most reasonable ways of getting it.
- krapp 13y agoYes, and given that (IIRC) $_GET always returns strings anyway, casting to int before adding to a database makes sense. Even so, you should still attempt to validate the string with ctype_digit() and make sure it accurately represents the integer you expect. If you just cast directly to an int, you can't really predict the results. And also, doing the cast inside the sql statement makes it difficult to see. It should be done, if at all, outside the query where it's obvious to anyone looking at the code that this is something that should be paid attention to.
- astrodust 13y agoIn this case, you don't "need an int", you need a value that's safe to put in a database query. If you're inserting a value in a database, you always, always, always use the proper escaping mechanism. No exceptions. That's why using a library with a reliable, well-defined, easy to use escaping system is absolutely imperative.
- ratsbane 13y agoIn any case the real problem with this line of code is not the int cast, it's that it's using a GET request to delete a record.
- oneeyedpigeon 13y agoNot necessarily - there can still be data in the _GET array, even if the request method is POST.
- ratsbane 13y agoYes, fair point, but I do think it's a red flag. On looking a bit further I see that yes, in fact, he is using a GET link to delete records: https://github.com/Paton/Saaave/blob/master/_views/browse.php https://github.com/Paton/Saaave/blob/master/_views/browse.ph... This is an actual, serious problem which I would note in a code review, as opposed to the int thing which cannot, as far as I can can tell, ever lead to an exploit or malfunction which would have been avoided by using a named escape function in this code. If anyone can think of a specific example to prove this wrong, please say so.
- smsm42 13y agoDepends on the purpose of the code. If it's one-page script then it's fine. If it's the context of the bigger app, mysql_query is probably a problem.