I recently noticed that I had a big hole in my application because I had done something like:
\" />
Watch this video from Scott Hanselman and Phil Haack. They cover XSS, CSRF, JSON Hijacking specifically with ASP.Net MVC.
Potentially Dangerous HTML Tags:
While not an exhaustive list, the following commonly used HTML tags could allow a malicious user to inject script code:
<applet>
<body>
<embed>
<frame>
<script>
<frameset>
<html>
<iframe>
<img>
<style>
<layer>
<link>
<ilayer>
<meta>
<object>
An attacker can use HTML attributes such as src, lowsrc, style, and href in conjunction with the preceding tags to inject cross-site scripting. For example, the src attribute of the tag can be a source of injection, as shown in the following examples.
<img src="javascript:alert('hello');">
<img src="java
script:alert('hello');">
<img src="java
script:alert('hello');">
An attacker can also use the tag to inject a script by changing the MIME type as shown in the following.
<style TYPE="text/javascript">
alert('hello');
</style>
There's a few ways:
<%: %>
syntax in ASP.NET MVC2 / .NET 4.0. (Which is just syntactic sugar for Html.Encode()
) Syntax for HTML encoding
<%: model.something %> syntax in WebForms
It is automatic in Razor i.e. @model.something will auto encode automatically no need to do anything to encode.
MVC3 HTML Helper methods return the encoded string automatically. e.g. Html.Label will return the encoded string
More about cross site scripting
http://thirum.wordpress.com/2013/10/24/how-asp-net-mvc-prevents-cross-site-scriptingxss-attack/
In ASP.Net 4.0 or later, always use <%: ... %> instead of <%= ... %> ... it does the HTML encoding for you.
Scott Gu's explanation.
Having done that, it's fairly straightforward to grep your code for <%= regularly as a security precaution.
Also, are you using the Microsoft Anti-XSS library?