I've got an idea as to how to defeat XSS so simple that it may work and was wondering if anybody had already done something similar.
The goal here is not efficiency but security. In other words I don't care about trading bandwith for security. My idea would be to serve every single character inside simple HTML tags.
Basically if the following, for whatever reason, would be a working XSS exploit: 0WNED!byXSS (which for whatever reason nobody would have detected), then it would be served as:
<i>0</i><i>W</i><i>N</i>E<i>D</i>!<i>by</i>X<i>S</i>S<i>
Basically a loop would make sure to only do:
- write
<i> - write a single character
- write
</i>
Could something like that technically work (performances and bandwith issues aside) and has something like this ever been done?
EDIT
Here's a (simplified) example of what I'm thinking about.
Instead of serving this:
<html>
<head>
<title>Sample</title>
</head>
<body>
<table>
<tr>
<td>Here goes user input</td>
<td>potentially containing an XSS exploit</td>
</tr>
</table>
</table>
</body>
</html>
I'd serve this:
<html>
<head>
<title>Sample</title>
</head>
<body>
<table>
<tr>
<td><i>H</i><i>e</i><i>r</i><i>e</i><i> </i><i>g</i><i>o</i><i>e</i><i>s</i><i> </i><i>u</i><i>s</i><i>e</i><i>r</i><i> </i><i>i</i><i>n</i><i>p</i><i>u</i><i>t</i></td>
<td><i>p</i><i>o</i><i>t</i><i>e</i><i>n</i><i>t</i><i>i</i><i>a</i><i>l</i><i>l</i><i>y</i><i> </i><i>c</i><i>o</i><i>n</i><i>t</i><i>a</i><i>i</i><i>n</i><i>i</i><i>n</i><i>g</i><i> </i><i>a</i><i>n</i><i> </i><i>X</i><i>S</i><i>S</i><i> </i><i>e</i><i>x</i><i>p</i><i>l</i><i>o</i>it</td>
</tr>
</table>
</table>
</body>
</html>
The <i> tag was just arbitrarily chosen, any (preferrably one-letter) tag would do.
Various functionalities of the OS / browser seem to keep working correctly:
- I can cut'n'paste correctly to the clipboard
- I can still search for text (e.g. I can look out for "goes" and despite the fact that every single letter of "goes" is wrapped by tags the browser still finds it fine)
I take it that search engine would still correctly parse/index the content (not too sure about that that said).
To me it looks like quite a simple and effective way to prevent lots of exploits based on evil user inputs.

and wrap them accordingly. If you really followed your example code to the letter (no pun intended), then would display as instead of a single space character. Same would go for any Base64 (or any other way) encoded URL strings. – TildalWave Mar 10 at 18:09<and&characters. Also it doesn't work in attribute values. I see no win here. – bobince Mar 10 at 21:16