Is it necessary to filter/escape unsafe variables in <title>
or other tags in <head>
to prevent XSS?
views:
524answers:
5Strictly speaking it is necessary to do htmlspecialchars()
on absolutely everything you output to a web page from PHP.
If you come to a point where this produces a wrong result (i.e. HTML code is showing up in the browser because of double encoding) you have found a design flaw in your application.
XSS would not be a problem if people would stick to this simple rule.
Yes. You should always use the htmlspecialchars
function on values that may contain HTML special characters.
As others have said, yes. For example, someone might enter this for a title:
</title><script>doEvilStuff()</script>
Which would probably do evil stuff.
EDIT: You might want to do this instead:
htmlspecialchars(strip_tags($title))
Because most (all? some?) browsers won't interpret tags in the title correctly - that is, they'll actually show up in the title rather than doing what you'd expect.