Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Fight Off Forgery Attacks

Modern browsers implement restrictions to fend off cross-site scripting (XSS) attacks. But there's another type of attack that is even easier to launch -- cross-site request forgery (CSRF). A CSRF attack consists of two distinct steps:

CSRF attackers set up a Web page with clickable elements that can trigger malicious operations. The fake page typically contains hidden script code and markup that collects data from a local computer and posts it to the attacker's server. As the victim triggers the malicious code, the HTML page performs a form post to the attacker's server. Because the post occurs from the victim's computer, authentication cookies on the machine are uploaded. As a result, the attacker is served cookies that may identify another user to some sort of target application. If successful, a CSRF attack lets intruders upload their own data through the victim's account and possibly gain full control over the victim's credentials.

How can you protect yourself? ASP.NET MVC offers one solution. In ASP.NET MVC, you publish server functionalities through a public method of a controller class. If the method is critical, you can add attributes to prevent CSRF attacks. To make this possible, ASP.NET MVC provides a helper method to generate some ad hoc HTML markup and a ValidateAntiForgeryToken attribute.

public ActionResult Update(Customer customer)

The HttpPost attribute requires a POST request to execute the method. This alone cuts off all possible requests made through a plain GET. Additionally, ValidateAntiForgeryToken instructs the invoker of the ASP.NET MVC method to look for some special content in the request body before executing the code.

ValidateAntiForgeryToken contains code that kicks in during the authorization phase of an action method request, ensuring the posted request contains a cookie and a form field with a common fixed name. If any of these items are missing, an exception is thrown; otherwise, the attribute ensures that the content of the cookie and the input field match.

How do you insert content into a Web page? By using this ASP.NET MVC HTML helper:

<%= Html.AntiForgeryToken() %>

The Html.AntiForgeryToken method creates a cookie on your machine and adds a hidden field like this to the form:

<input name="__RequestVerificationToken" type="hidden"
       value="j3Cj++/JUcS+kUMy/9Obj/oM6ZW7vZozNo7+S" />

If the target of the form includes the ValidateAntiForgeryToken attribute, the content of the cookie and input field are matched before the action method is authorized. In this way, intruders can't create valid cookies because they don't know what content to put in them. And even if the victim's machine already contains an antiforgery cookie (because of a previous legitimate operation against the site), the content of the cookie can't be read via script to arrange a form input field on-the-fly. An antiforgery cookie, in fact, is HttpOnly and can't be accessed via script.

Not the definitive barrier, but a little thing that makes it harder for the bad guys.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.