Assume some PHP code which echoes an input sanitized by first applying addslashes() and then htmlspecialchars() to an HTML document. I have heard that this is an unsafe approach, but cannot figure out why.
Any suggestions as to what sort of formatting could be applied to a dangerous input, such as JavaScript in script tags, to bypass the security measures imposed by the two functions would be appreciated.
addslashesis irrelevant to XSS (and there is almost always something better in places where it is actually useful).htmlspecialcharsis not an unsafe approach. It is just insufficient by itself.htmlspecialcharswill protect you if you put the content as the body of a "safe" element.It will protect you if you put the content as the value of a "safe" attribute if you also properly quote the value.
It won't protect you if you put it as the value of an "unsafe" attribute or element (where the content may be treated as JavaScript) such as
<script>,onmoseover,hreforstyle.For example:
will give you:
which means the same as:
which will steal your cookies when you click the button.