It's extremely nontrivial. Even very widely studied applications can have things missed.
For new applications I'd say, ensure that:
- It uses htmlspecialchars or similar for ALL output of nontrusted data - or a templating system like Smarty with a default modifier which does this automatically
- It uses prepared queries such as with PEAR:😃B or PDO exclusively, never putting user data (directly) into SQL at all.
And these two measures will protect you from many types of attack, but not all.
Logic bugs in the authentication and authorisation code may remain- these are extremely difficult to detect if you don't know how it works and it isn't well (or correctly) comment.
Applications which output user-supplied HTML with some sanitisation are extremely difficult to validate, because it's not clear how HTML has to be modified to make it "safe" from XSS injection attacks.
Different browsers parse badly-formed HTML differently and there exist tricks which may enable some HTML to slip through the net and still be executed by a browser. I do not know how to safely do this, it is a subject of much research.
Mark