php - Is this much escaping necessary?
I was looking at the and it had this line of code:
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
It says underneath:
htmlspecialchars() makes sure any characters that are special in html are properly encoded so people can't inject HTML tags or Javascript into your page.
Is this really necessary?
Can someone actually put in malious code into that one line? What is there to worry about? Can something be injected into that line to run some php code? Are they just getting people accustomed to watching for this even though there is no threat in this case?
Answer
Solution:
If someone's name happened to be:
Then a worm could be unleashed on your site. In essence the worm is just a script that takes the users session cookie, logs in, and then does malicious stuff, replicating itself as more people view it.
Answer
Solution:
Yes, it's necessary. See Wikipedia for information on cross-site scripting vulnerabilities.
PHP can't be run using XSS, but an attacker could steal a session cookie. Potentially disastrous if that session cookie is for an administrator on a CMS, as an example.
Answer
Solution:
It's not only for security. For example if you receive
Fred&Jen
as$_POST['name']
it would make a XML document invalid.Answer
Solution:
The threat here is XSS. It's Javascript code that an attacker would try to execute using an XSS attack.
To see for yourself, remove the htmlspecialchars() then post this as
name
:You'll see that your PHP code will print that out and the browser will execute the Javascript. The most common XSS attacks are on web applications where a user has a logged in session. A successfull XSS attack could read the victim's session ID cookie using
document.cookie
and then send it to the attackers website where the attacker could proceed with a session hijack.This attack is known as a Reflective XSS. A more severe type of XSS is a Persistent XSS which is where you store the input to a database for example, then you print it out on your homepage for all users to see, or any other page. A persistent XSS is much more harmful because any visitor of the page where the XSS persists can be attacked without actually having to re-launch the attack for each victim.
Answer
Solution:
If you are only going to print what that same users posts, they cannot do much harm to you or other users. However, it is useful nevertheless as user might use ">" and "<" and they would ruin the representation of the content.
However, if you are going to show the content which other users submit, this becomes much more dangerous.
User can inject anything from links to other sites to javascript.
And all users who come to the site which displays this bit of user submitted content will be redirected to evilsite.
Answer
Solution:
Yeah, someone could for example inject some Javascript code, which when displayed would for example redirect them to another site (which is fairly harmless).
example:
Would redirect the user to google if it were to be posted without being escaped
Answer
Solution:
PHP code cannot be injected into that field as it is not evaluated by the PHP interpreter, however it does introduce the potential for cross site scripting.
You can test this by using something like this:
Then in the "name" field, submit this code and see what happens:
Answer
Solution:
Can someone actually put in malious code into that one line?
Yes.