Pádraic Brady has posted a 20 point list that wants to help you prevent cross-site scripting (XSS) issues in your applications.
Summarising knowledge has as much value as writing a 200 page treatise on a topic, so here is a list of 20 brief points you should bear in mind when battling Cross-Site Scripting (XSS) in PHP. Minus my usual book length brain fart . Chances are good that ignoring or acting contrary to any one of these will lead to a potential XSS vulnerability. It’s not necessarily a complete list – if you think something needs to be added, let everyone know in the comments.
His tips include things like:
- Never pass data from untrusted origins into output without either escaping or sanitising it.
- Remember that anything not explicitly defined in source code has an untrusted origin.
- Always include ENT_QUOTES, ENT_SUBSTITUTE and a valid character encoding when calling htmlspecialchars().
- Use rawurlencode() to escape strings being inserted into URLs and then HTML escape the entire URL.
- Validate all complete URLs if constructed from untrusted data.
- Remember that HTMLPurifier is the only HTML sanitiser worth using.
- Ensure that cookies which must only be transmitted over HTTPS are marked Secure.
He points out that XSS is still one of the most "popular" (and easy to exploit) attack methods out there, so keep these tips in mind when writing up your code.