views:

154

answers:

1

In a server-side Classic ASP file, let's say you receive a Request string containing malicious javascript like, "alert('HACKED');"

DIM foo : foo = Request.Form("foo"); 'Contains malicious javascript

and then later we're writing javascript to screen containing that value.

%>
<script type="text/javascript">
   // some code
   <%=foo %>
   // some more code
</script>
<%

What do we do here keep ourselves safe against this form of cross-site scripting?

+1  A: 

Always remember: "Filter your input, and escape your output"

You filter data for safe storage in a database (to prevent SQL Injection), and you escape data before presenting it to the user (to prevent XSS)

Try ASP's HTMLEncode() method.

sigint
But HTMLEncode() leaves the string "alert('HACKED');" unaffected?
twh
The user will see (as literal text on the page) `alert("HACKED");` but the actual HTML will be rendered as `alert("HACKED");`. No javascript `alert()` box will appear.
sigint
Single quotes do not get encoded by HTMLEncode(), and neither do the parenthesis or the semi colon. HTMLEncode() works well for HTML, and I use it all over, but in this case I'm outputting javascript not HTML.
twh
Well that's a tough one. It would be next to impossible to determine what particular code constitutes "malicious". Can I ask why you're having your users supply you with JavaScript in the first place? Are you doing something like this: http://www.w3schools.com/JS/tryit.asp?filename=tryjs_text
sigint