magic_quotes = bad .. and im pretty sure subzero was the one commenting on one of my posts saying that it wont work in PHP6, how can u change ur stand point on PHP6 integrity coding on a per post basis ?
the answer to this is simple
- do not rely on magic quotes
- do not rely on register global
- do not rely on global fixes.
By global fixes i mean the code above that mimics magic quotes when they are disabled. Treat EVERY variable independantly. And as for nost using POST's directly in queries, forget that. Yes you can using a POST in a query is the same as using a var. Except defining a new var takes up extra ram space during script execution. As long as you sanitise your variables you will be fine . for example
mysql_query("SELECT * FROM users WHERE gender = ". $_POST['gender']);
and
$gender = $_POST['gender'];
mysql_query("SELECT * FROM users WHERE gender=$gender");
nothing is different, apart from the second uses more ram space (not enough to make a difference but it all adds up, plus it keeps files shorter and nicer to read IMO)
so how would we clean up that code , pretty simple is sanitisation. There are many ways to sanitise data. A large XSS fix i once used, checks an array for allowed POST's and GET's. in this example its not needed however if you had a larger possible list of allowed terms its a good method (so you maybe searching users for their martial status where it could around 5 different thing)
the array fix
$allowed = array("m", "f");
if(!in_array($_POST['gender'], $allowed)
die("Gender ".htmlspecialchars($_POST['gender'])." not found!");
this will only allow m and f to be passed as a gender, notice how i used the POSTed var in my error statement. Generally this is bad. If you do not need to use user submitted data for display then dont. The less you do this the less areas people have the chance to test out their new xss strings that may at some point break htmlentities or chars. The user knows what they posted, so why tell them that, in this case it would be more wise to just say "gender not found". i did it like that only as an example of how to sanitise user submitted data
Ok so the array fix isnt good for that example as we only accept two things. maybe we could
mysql_query("SELECT * FROM users WHERE gender = ".($_POST['gender'] == "m" ? "m" : "f"));
here we dont actually use the variable in our statement, so we fully control what the query does. If the gender requested is M then it uses that, anything else it uses F.. we dont need to worry about if they sumbit an invalid gender, you design the interface so normal members can only post M or F.. if they post anything else then who cares if they get a resultset they didnt ask for, there not interested in it anyway
ok so thats 2 ways to santise data in the respects that we only allow what we want. but wat if we want to use submitted data that could be anything such as a avatar or profile message. heres one way
function sqlesc($x)
return "'".mysql_real_escape_string($x)."'";
mysql_query("UPDATE users SET profile=".sqlesc($_POST['prof'])." WHERE uid=".(0+$_POST['uid'])." LIMIT 1");
note: i have added an esacpe function i always use, it auto adds our quotes to the data to save us time and escapes it, plus its shorter to type which is always good
So here we have 2 posted vars, the first we simply sqlesc() it so none of the characters can break the statement, depending on your style of programming you may want to add more checks, but that is all u really need, anything else can be done on display which is what i usually do. u also have the uid to update, notice we 0+ it , you could also use (int) .. up to you. both typecast the var to a integer, we only need integers so typecast it. if there is letters in there that are intended to break the query then the int will be 0 and usually u have no user of id 0 and no users will be found/updated.
There is also one simple security trick here that many over look. thats the limit. If you only want to update one row then tell it so, first it increases speed as it wont look for other rows to update after the first, secondly any malicious code that does magically make itself through will only work on one account, stopping it being spawned to other accounts, so a delete command maliciously inserted wont work as wll as the attacker had hoped.
Now i kno the limit isnt be all end all for security. but in a limited number of situations it will help. We can all think of attacks that this wouldnt be any good for but if u have half a brain u can think of at least one attack string it would perform some help to avoid global spawning attacks. and even if it only helps protect against 1 in 100 attacks.. is it not worth those extra few characters ???
ok so i think thats enough info, just remember 1 basic rule... SANTISE DATA. as long as you sanitise data on input and output you will usually be fine. the very basic of this means, sql escape on input and htmlspecialchars on output
if you have a small example of insecure code which u would like an example on how to secure then post it along with what it does and i'll see if i can help you understand how to secure stuff.
Edit:
the get function subzero posted isnt too bad tbh. personally i dont like using functions, i learnt my PHP from some of the best in the business and i guess my coding style derives from theres. Maybe functions arent any different, but when i think about using such things i think to myself why dont they use it.. and it makes me stray away. One way that get could be imporved while still using request gloabls is like this
funtion getVar($var, $t)
{
return ($t == 1 ? $_REQUEST[$var] : ($t == 2 ? (int)$_REQUEST[$var] : ($t == 3 && $_REQUEST[$var] == true ? true : false)));
}
note:
$t = 1 (string)
$t = 2 (int)
$t = 3 (Boolean)
so what this does is take the requested variable and typecasts it, if its a string then it just passes it back, if its an int , it typecasts it to an into, and if its a bool it typecats it setting the return value as either bool(true) or bool(false)
this isnt once again be all end all of security, u will still need to atleast sanitise the string data on return, maybe use it as sqlesc(getVar("gender"));
but using inline sanitisation imo is always better in the long run, plus u get used to sanitising data if u have to keep doing it and you will learn much quicker on better methods, rather than having to keep asking about it