Prevent Cross-Site Request Forgery (CSRF) using ASP.NET MVC’s AntiForgeryToken() helper
ASP.NET, MVC, Security September 1st, 2008Cross-site scripting (XSS) is widely regarded as the number one security issue on the web. But since XSS gets all the limelight, few developers pay much attention to another form of attack that’s equally destructive and potentially far easier to exploit. Your application can be vulnerable to cross-site request forgery (CSRF) attacks not because you the developer did something wrong (as in, failing to encode outputs leads to XSS), but simply because of how the whole Web is designed to work. Scary!
How CSRF works
So, what’s it all about? All web application platforms are potentially vulnerable to CSRF, but in this post I’ll focus on ASP.NET MVC. Imagine you have a controller class as follows:
public class UserProfileController : Controller { public ViewResult Edit() { return View(); } public ViewResult SubmitUpdate() { // Get the user's existing profile data (implementation omitted) ProfileData profile = GetLoggedInUserProfile(); // Update the user object profile.EmailAddress = Request.Form["email"]; profile.FavoriteHobby = Request.Form["hobby"]; SaveUserProfile(profile); TempData["message"] = "Your profile was updated."; return View(); } }
This is all very normal. First, the visitor goes to Edit(), which renders some form to let them change their user profile details. Secondly, they post that form to SubmitUpdate(), which saves the changes to their profile record in the database. There’s no XSS vulnerability here. Everything’s fine, right? We implement this sort of thing all the time…
Unfortunately, this innocent controller is an easy target for CSRF. Imagine that an attacker sets up the following HTML page and hosts it on some server of their own:
<body onload="document.getElementById('fm1').submit()">
<form id="fm1" action="http://yoursite/UserProfile/SubmitUpdate" method="post">
<input name="email" value="hacker@somewhere.evil" />
<input name="hobby" value="Defacing websites" />
</form>
</body>Next, they somehow persuade a victim to visit this page (basic social engineering, look it up). When this HTML page loads, it submits a valid form post to /UserProfile/SubmitUpdate on your server.
Assuming you’re using Windows authentication or some kind of cookie-based authentication system such as Forms Authentication, the automated form post will be processed within the victim’s established authentication context, and will successfully update the victim’s email address to something under the attacker’s control. All the attacker has to do now is use your “forgotten password” facility, and they’re taken control of the victim’s account.
Of course, instead of changing an victim’s email address, they can perform any action that the victim can perform with a single POST request. For example, they might be able to grant administrative permissions to another account, or post something defamatory to a CMS.
Ways to stop CSRF
There are two main ways to block CSRF:
- Check that incoming requests have a Referer header referencing your domain. This will stop requests unwittingly submitted from a third-party domain. However, some people disable their browser’s Referer header for privacy reasons, and attackers can sometimes spoof that header if the victim has certain versions of Adobe Flash installed. This is a weak solution.
- Put a user-specific token as a hidden field in legitimate forms, and check that the right value was submitted. If, for example, this token is the user’s password, then a third-party can’t forge a valid form post, because they don’t know each user’s password. However, don’t expose the user’s password this way: Instead, it’s better to use some random value (such as a GUID) which you’ve stored in the visitor’s Session collection or into a Cookie.
Using the AntiForgeryToken helpers
With Preview 5, Microsoft has added a set of helpers to the “futures” assembly, Microsoft.Web.Mvc.dll, that give you a means to detect and block CSRF using the “user-specific tokens” technique.
To use these helpers to protect a particular form, put an Html.AntiForgeryToken() into the form, e.g.,
<% using(Html.Form("UserProfile", "SubmitUpdate")) { %> <%= Html.AntiForgeryToken() %> <!-- rest of form goes here --> <% } %>
This will output something like the following:
<form action="/UserProfile/SubmitUpdate" method="post">
<input name="__MVC_AntiForgeryToken" type="hidden" value="saTFWpkKN0BYazFtN6c4YbZAmsEwG0srqlUqqloi/fVgeV2ciIFVmelvzwRZpArs" />
<!-- rest of form goes here -->
</form>At the same time, Html.AntiForgeryToken() will give the visitor a cookie called __MVC_AntiForgeryToken, with the same value as the random hidden value shown above.
Next, to validate an incoming form post, add the [ValidateAntiForgeryToken] filter to your target action method. For example,
[ValidateAntiForgeryToken] public ViewResult SubmitUpdate() { // ... etc }
This is an authorization filter that checks that:
- The incoming request has a cookie called __MVC_AntiForgeryToken
- The incoming request has a Request.Form entry called __MVC_AntiForgeryToken
- These cookie and Request.Form values match
Assuming all is well, the request goes through as normal. But if not, boom!, there’s an authorization failure with message “A required anti-forgery token was not supplied or was invalid”.
This prevents CSRF because even if a potential victim has an __MVC_AntiForgeryToken cookie, an attacker can’t find out its value, so they can’t forge a valid form post with the same value in Request.Form. But legitimate users aren’t inconvenienced at all; the mechanism is totally silent.
Using salt
Salt? What? In case you want to protect multiple forms in your application independently of each other, you can use a “salt” value when you call Html.AntiForgeryToken(), e.g.,
<%= Html.AntiForgeryToken("someArbitraryString") %>
… and also in [ValidateAntiForgeryToken], e.g.,
[ValidateAntiForgeryToken(Salt="someArbitraryString")] public ViewResult SubmitUpdate() { // ... etc }
Salt is just an arbitrary string. A different salt value means a different anti-forgery token will be generated. This means that even if an attacker manages to get hold of a valid token somehow, they can’t reuse it in other parts of the application where a different salt value is required. (If anyone can suggest other use cases for salt, please let me know.)
Limitations of the Anti-Forgery helpers
ASP.NET MVC’s anti-CSRF helpers work very nicely, but you should be aware of a few limitations:
- All legitimate visitors must accept cookies (otherwise, [ValidateAntiForgeryToken] will deny their form posts). Arguably this isn’t a limitation, because unless visitors allow cookies, you probably don’t have anything to protect anyway.
- It only works with POST requests, not GET requests. Arguably this isn’t a limitation, because under the normal HTTP conventions, you shouldn’t be using GET requests for anything other than read-only operations.
- It’s easily bypassed if you have any XSS holes on your domain. An XSS hole would allow an attacker to read a victim’s anti-forgery token value, then use it to forge valid posts. So, don’t have XSS holes!
- It relies on the potential victim’s browser implementing cross-domain boundaries solidly. Browsers are supposed to stop foreign domains from reading your app’s response text and cookies, and are supposed to stop foreign domains from writing cookies to your domain. If an attacker manages to find a way around this, they can bypass [ValidateAntiForgeryToken]. Of course that’s not supposed to be possible. For the most part, modern browsers block this line of attack.
In conclusion, ASP.NET MVC’s anti-CSRF helpers are easy to use, and work very nicely thank you!


September 2nd, 2008 at 6:11 pm
Great analysis, especially your section on Limitations. We are keeping this in the Futures because we haven’t run extensive security analysis on our approach. So we provide no guarantees that it will stop all CSRF attacks. But at the very least, we think it will help mitigate the risk and we hope to vet this with the community before rolling it into core.
September 2nd, 2008 at 6:48 pm
I’m going to assume that __MVC_AntiForgeryToken cookie has HttpOnly set. Otherwise, what’s stopping that javascript from reading __MVC_AntiForgeryToken and filling the form before calling submit()?
September 2nd, 2008 at 6:50 pm
Duh, that’s why the XSS disclaimer.
It’s probably already an HttpOnly anyways.
September 2nd, 2008 at 8:34 pm
@Haacked: Thanks - the technique you’ve applied is very similar to that I’ve seen tried and tested in past projects, so I can’t imagine there being any fundamental problems with it. Looks solid.
@Chris: Actually, when I tried it, I was surprised to see that it *doesn’t* set the HttpOnly flag. But thinking about it more, this doesn’t actually affect the fact that XSS can bypass it. If there is an XSS hole in your site, an attacker can read one of your forms across domains, extract out the token, and use it when submitting that form back across domains. The cookie (and whether it is HttpOnly or not) is irrelevant to the attacker.
September 3rd, 2008 at 7:04 am
[…] Prevent Cross-Site Request Forgery (CSRF) using ASP.NET MVC’s AntiForgeryToken() helper - Steve Sanderson shows how you can protect your ASP.NET MVC applications from Cross Site Scripting (XSS) exploits using the AntiForgeryToken helpers included in ASP.NET MVC PR5 […]
September 9th, 2008 at 1:47 pm
Why not save a copy of that token in the session? I’d guess that would be a safer way (to protect against malfunctioning browsers) because the client wouldn’t have that cookie, wouldn’t it?
Also, is it possible to validate the token by code? That way you could provide a per-user salt value, e.g. a username or password hash, which is not possible to do in attributes.
September 9th, 2008 at 2:51 pm
@Configurator - I guess they chose to use cookie storage rather than session storage for scalability (in web farm scenarios, you sometimes can’t use session storage). Also, session state is volatile (can be erased at any moment) whereas cookies can stick around indefinitely.
> Also, is it possible to validate the token by code?
Kind of, though it is awkward. You can instantiate a ValidateAntiForgeryTokenAttribute, passing a salt parameter to its constructor, then call its OnAuthorization() method. That gives you programmatic control over salt, rather than having to embed it in an attribute. It’s a bit awkward to do this because the API wasn’t designed for it - you’re expected to validate the token only using an attribute.
But out of interest, why do you want to do this? The token contains a user-specific random value anyway, so the token is always user-specific. What extra benefit do you expect to get from having a user-specific salt value?
September 9th, 2008 at 3:47 pm
I’m not sure I “get it” yet. If you’re using Forms based auth (dropping a cooking on their machine), and have a filter that requires authentication of the user before they can modify their account, does this still leave you open to CSRF? (This still assumes you are preventing XSS attacks)
Isn’t the anti-forgery token just yet-another way of authenticating the user, just like their Forms Auth token? How does the combination improve the situation?
September 9th, 2008 at 4:17 pm
> If you’re using Forms based auth (dropping a cooking on their machine), and have a filter
> that requires authentication of the user before they can modify their account,
> does this still leave you open to CSRF?
Depending on what other things your application lets that user do, yes, you can still be vulnerable to CSRF. It’s not about authenticating the user - it’s about authenticating that requests made by that user were really intentional and not automated by a third party. Wikipedia has a decent explanation of CSRF if you need to know more.
September 25th, 2008 at 5:25 pm
[…] Steve Sanderson’s blog » Blog Archive » Prevent Cross-Site Request Forgery (CSRF) using ASP.NET … […]
October 7th, 2008 at 5:56 pm
I don’t know how the token actually is calculated, just a Guid as in the post mentioned doesn’t seem very strong. Whta’s about calculating the token over different parts of information, making it more unique. Maybe take a combination of these:
- User specific value as now, maybe a Guid
- User’s IP address
- User’S Browser Agent string (I guess nobody will change it between a GET and corresponding POST)