Solve XSS by signing SCRIPT tags
blog.jgc.org
blog.jgc.org
What about dynamically-generated scripts that include user input? People do this all the time in the real world. Even if you sign it, you're still screwed if you get quoting wrong.
The crypto is also superfluous; you'd get the same protection by having the server set a long random nonce in a header, and then require every <script> element to bear the same nonce. No crypto required.
How do you securely tell the browser what the nonce is to check?
I'm not endorsing "script tokens" either --- I think it's a bad idea to change all browsers for a half-measure --- but I think you can execute your core idea far more simply than with crypto.
Do you mean filtering what is sent to the browser - validating libraries so user input cannot generate URIs, IMGs, etc?
<iframe src="data:text/html;base64,PHNjcmlwdD5hbGVydCgnRk9PQkFSJyk8L3NjcmlwdD4="></iframe>
Why wouldn't this work: Have a meta-tag (or something in the <head>) that says: do not allow in-line script tags on this HTML page. Only run scripts from external .js files. No onclick/onmouse code allowed in HTML. For additional security (I don't know if this is possible or not), external .js files cannot do document.write("<script>...") or $("#foo").innerHTML("<script>...") i.e. if external .js file makes an Ajax call and document.write() the response, the response should be handled as non-executable. Moreover, only external .js files can attach events and that too, using attachEvent etc. and not via the on* attributes.
2. Browsers will ignore any 'on' attributes.
3. HTML may link to JS with one or more LINK tags.
4. JS may assign 'on' attribute event handlers to individual HTML tags.
5. JS will have access to only the cookies of domain hosting the JS file.
There's still a risk of bad LINK tags creeping in, but the code will only be able to see the cookies of the domain hosting the dodgy JS.
what it seems to boil down to is : sanitize user input.
In what circumstances is this not sufficient?
But yes. Sanitizing user input (assuming a perfect sanitizer) can assure perfect XSS protection, but at a rather severe loss of features (no images / URLs / formatting in your comments). A separate, limited DSL is often the solution for this (BBCode, Markdown, etc), but those sometimes make mistakes too, especially when they're younger.
[1]: http://www.reddit.com/r/programming/comments/cluc5/html_inje...
Instead, parse the HTML just as you'd have to with BBCode or Markdown. Store it in an internal representation that's only capable of the minimum needed formatting features. (There's no actual HTML left at this stage. It's equivalent to any other DSL.) Then render HTML out of that parse tree, so that every bit of user input is HTML entity-encoded, and everything else (tags and attributes) comes from constants in the program.
This can be even more secure than regexp-based DSL translators that build up a result from input in multiple passes, since they tend to lack such a well-defined separation between input text and output HTML.
Do you know of good libraries for this type of work off the top of your head? bonus points for native java