Reputation: 11448
I have a php page. It recieves a value for id
via get. 2 simple questions:
1 - In my code this is used only once. In an if statement like:
if ($_GET['id']==1){
Things here....
}
That is the only use of this passed id value. Do I need to sanitize it or can I just leave it safely in the if statement without checking it? Can hackers penetrate through this?
2 - Would I need to sanitize it if I had assigned it to a variable like:
$idid=$_GET['id'];
if ($idid==1){
Things here...
}
Like before this is the only use of this variable, it will not be used in echo or mysql etc.
Upvotes: 1
Views: 140
Reputation: 198117
Do I need to sanitize it or can I just leave it safely in the if statement without checking it?
That depends on the level of quality you would like to achieve. It looks like that you access the $_GET
array on a key that you don't test for existence.
This can trigger a notice like "Notice: Undefined index: id" which contains the filename as well.
Depending on server configuration, this message might be given to everybody requesting the website in question not providing the id
URL query parameter.
Next to information offered to a potential attacker, spreading notices makes it harder for you to achieve a certain level of quality for your overall script, e.g. if you decide to turn notices and errors into feedback for maintaining and improving the script.
Can hackers penetrate through this?
This information can be used to further penetrate your site then.
From the code you've shown you don't make further use of the variable / value and as far as I can say, I don't see an additional issue next to information exposure.
It's always subject what you would like to achieve, security is nothing static. See Defensive programming, Secure input and output handling and Postel's law.
Upvotes: 0
Reputation: 101936
You only need to worry about sanitation, if the GET value is inserted in some potentially harmful place, i.e. echo
(XSS), mysql_query
(SQL Injection), eval
(PHP Execution), shell_exec
(Shell execution), ... (More extensive list at Exploitable PHP functions)
Just checking for a value doesn't need any sanitation.
Upvotes: 2
Reputation: 557
use === instead of == to determine the type of the entry. In my opinion you don't need to do hard job to protect the input. Only make sure that the input is really a number.
Upvotes: -2
Reputation: 851
No you do not need to sanitize it as it does nothing if it no other value than 1.
Upvotes: 1