ShaolinF Posted May 6, 2010 Share Posted May 6, 2010 Hi Guys, I do the following to GET an id: $id = $_GET['id']; There needs to be some filtering/sanitization. I have noticed that many people just cast it as an int and leave it at that. Is that really the best way to approach this issue ? It doesnt look right to me. Im hoping someone can shed some light on this from a secure coding POV. Quote Link to comment https://forums.phpfreaks.com/topic/200913-casting-variables-as-the-sole-security-measure/ Share on other sites More sharing options...
xeross Posted May 6, 2010 Share Posted May 6, 2010 I generally don't use typecasts, if something needs to be a number I tend to use is_numeric, and for more complex checks you could use regular expressions or readily available php functions like is_int, is_string, is_bool, etc. ~Xeross Quote Link to comment https://forums.phpfreaks.com/topic/200913-casting-variables-as-the-sole-security-measure/#findComment-1054199 Share on other sites More sharing options...
.josh Posted May 6, 2010 Share Posted May 6, 2010 there is nothing wrong with type casting if that's all it takes to validate data format. For instance, with pagination. You are already gonna have condition to make sure number is within range 1-X, so that's covered. As for the rest of it, forcing the value to int nicely covers everything else: making sure it's a whole number. Right tool for the right job, sort of thing. Quote Link to comment https://forums.phpfreaks.com/topic/200913-casting-variables-as-the-sole-security-measure/#findComment-1054281 Share on other sites More sharing options...
Mchl Posted May 6, 2010 Share Posted May 6, 2010 checking with is_numeric() on the other hand can actually let some types of SQL injections through. Strings like "0x01ABCDEF" do return true on is_numeric(), and in some multibyte encodings they can be used to break query. Quote Link to comment https://forums.phpfreaks.com/topic/200913-casting-variables-as-the-sole-security-measure/#findComment-1054311 Share on other sites More sharing options...
xeross Posted May 7, 2010 Share Posted May 7, 2010 checking with is_numeric() on the other hand can actually let some types of SQL injections through. Strings like "0x01ABCDEF" do return true on is_numeric(), and in some multibyte encodings they can be used to break query. Ah ok, I didn't know that, time to edit my code Quote Link to comment https://forums.phpfreaks.com/topic/200913-casting-variables-as-the-sole-security-measure/#findComment-1054729 Share on other sites More sharing options...
Mchl Posted May 7, 2010 Share Posted May 7, 2010 With UTF-8 you're probably safe, but I'd advise to do the changes nevertheless. Quote Link to comment https://forums.phpfreaks.com/topic/200913-casting-variables-as-the-sole-security-measure/#findComment-1054732 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.