<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog of developer Mikkel Ovesen &#187; mvc</title>
	<atom:link href="http://blog.ovesens.net/tag/mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ovesens.net</link>
	<description>My thoughts, stuff I need to remember or things I just want to share with the world</description>
	<lastBuildDate>Thu, 19 Jan 2012 11:55:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ASP.NET MVC Variable length lists</title>
		<link>http://blog.ovesens.net/2009/01/asp-net-mvc-variable-length-lists/</link>
		<comments>http://blog.ovesens.net/2009/01/asp-net-mvc-variable-length-lists/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 18:37:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[view model]]></category>

		<guid isPermaLink="false">/post/2009/01/10/ASPNET-MVC-Variable-length-lists.aspx</guid>
		<description><![CDATA[Well you never know when this will come in handy&#8230; http://justsimplecode.com/archive/2009/01/07/my-take-on-variable-length-lists-in-asp.net-mvc.aspx]]></description>
			<content:encoded><![CDATA[<p>
Well you never know when this will come in handy&#8230;
</p>
<p>
<a href="http://justsimplecode.com/archive/2009/01/07/my-take-on-variable-length-lists-in-asp.net-mvc.aspx">http://justsimplecode.com/archive/2009/01/07/my-take-on-variable-length-lists-in-asp.net-mvc.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/asp-net-mvc-variable-length-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prevent Cross-Site Request Forgery (CSRF) using ASP.NET MVC AntiForgeryToken helper</title>
		<link>http://blog.ovesens.net/2008/11/prevent-cross-site-request-forgery-csrf-using-asp-net-mvc-antiforgerytoken-helper/</link>
		<comments>http://blog.ovesens.net/2008/11/prevent-cross-site-request-forgery-csrf-using-asp-net-mvc-antiforgerytoken-helper/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 16:25:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">/post/2008/11/26/Prevent-Cross-Site-Request-Forgery-(CSRF)-using-ASPNET-MVC-AntiForgeryToken-helper.aspx</guid>
		<description><![CDATA[Original post: http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; Cross-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&#8217;s equally destructive and &#8230; <a href="http://blog.ovesens.net/2008/11/prevent-cross-site-request-forgery-csrf-using-asp-net-mvc-antiforgerytoken-helper/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>
Original post:
</p>
<p>
<a href="http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/" title="http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/">http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/</a>
</p>
<p>
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;
</p>
<p>
Cross-site scripting (XSS) is <a href="http://www.owasp.org/index.php/Top_10_2007">widely</a> <a href="http://tinyurl.com/3q9j7w">regarded</a> 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&rsquo;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!
</p>
<h6>How CSRF works</h6>
<p>
So, what&rsquo;s it all about? All web application platforms are potentially vulnerable to CSRF, but in this post I&rsquo;ll focus on ASP.NET MVC. Imagine you have a controller class as follows:
</p>
<div class="csharpcode">
<pre class="alt">
<span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">class</span> UserProfileController : Controller
</pre>
<pre>
<span class="lnum">   2:  </span>{
</pre>
<pre class="alt">
<span class="lnum">   3:  </span>    <span class="kwrd">public</span> ViewResult Edit() { <span class="kwrd">return</span> View(); }
</pre>
<pre>
<span class="lnum">   4:  </span>
</pre>
<pre class="alt">
<span class="lnum">   5:  </span>    <span class="kwrd">public</span> ViewResult SubmitUpdate()
</pre>
<pre>
<span class="lnum">   6:  </span>    {
</pre>
<pre class="alt">
<span class="lnum">   7:  </span>        <span class="rem">// Get the user&#39;s existing profile data (implementation omitted)</span>
</pre>
<pre>
<span class="lnum">   8:  </span>        ProfileData profile = GetLoggedInUserProfile();
</pre>
<pre class="alt">
<span class="lnum">   9:  </span>
</pre>
<pre>
<span class="lnum">  10:  </span>        <span class="rem">// Update the user object</span>
</pre>
<pre class="alt">
<span class="lnum">  11:  </span>        profile.EmailAddress = Request.Form[<span class="str">&quot;email&quot;</span>];
</pre>
<pre>
<span class="lnum">  12:  </span>        profile.FavoriteHobby = Request.Form[<span class="str">&quot;hobby&quot;</span>];
</pre>
<pre class="alt">
<span class="lnum">  13:  </span>        SaveUserProfile(profile);
</pre>
<pre>
<span class="lnum">  14:  </span>
</pre>
<pre class="alt">
<span class="lnum">  15:  </span>        TempData[<span class="str">&quot;message&quot;</span>] = <span class="str">&quot;Your profile was updated.&quot;</span>;
</pre>
<pre>
<span class="lnum">  16:  </span>        <span class="kwrd">return</span> View();
</pre>
<pre class="alt">
<span class="lnum">  17:  </span>    }
</pre>
<pre>
<span class="lnum">  18:  </span>}
</pre>
</div>
<p>
This is all very normal. First, the visitor goes to <strong>Edit()</strong>, which renders some form to let them change their user profile details. Secondly, they post that form to <strong>SubmitUpdate()</strong>, which saves the changes to their profile record in the database. There&rsquo;s no XSS vulnerability here. Everything&rsquo;s fine, right? We implement this sort of thing all the time&hellip;
</p>
<p>
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:
</p>
<div class="csharpcode">
<pre class="alt">
<span class="lnum">   1:  </span><span class="kwrd">&lt;</span><span class="html">body</span> <span class="attr">onload</span><span class="kwrd">=&quot;document.getElementById(&#39;fm1&#39;).submit()&quot;</span><span class="kwrd">&gt;</span>
</pre>
<pre>
<span class="lnum">   2:  </span>    <span class="kwrd">&lt;</span><span class="html">form</span> <span class="attr">id</span><span class="kwrd">=&quot;fm1&quot;</span> <span class="attr">action</span><span class="kwrd">=&quot;http://yoursite/UserProfile/SubmitUpdate&quot;</span> <span class="attr">method</span><span class="kwrd">=&quot;post&quot;</span><span class="kwrd">&gt;</span>
</pre>
<pre class="alt">
<span class="lnum">   3:  </span>        <span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">name</span><span class="kwrd">=&quot;email&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;hacker@somewhere.evil&quot;</span> <span class="kwrd">/&gt;</span>
</pre>
<pre>
<span class="lnum">   4:  </span>        <span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">name</span><span class="kwrd">=&quot;hobby&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;Defacing websites&quot;</span> <span class="kwrd">/&gt;</span>
</pre>
<pre class="alt">
<span class="lnum">   5:  </span>    <span class="kwrd">&lt;/</span><span class="html">form</span><span class="kwrd">&gt;</span>
</pre>
<pre>
<span class="lnum">   6:  </span><span class="kwrd">&lt;/</span><span class="html">body</span><span class="kwrd">&gt;</span>
</pre>
</div>
<p>
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 <strong>/UserProfile/SubmitUpdate</strong> on <em>your server</em>.
</p>
<p>
Assuming you&rsquo;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&rsquo;s established authentication context, and will successfully update the victim&rsquo;s email address to something under the attacker&rsquo;s control. All the attacker has to do now is use your &ldquo;forgotten password&rdquo; facility, and they&rsquo;re taken control of the victim&rsquo;s account.
</p>
<p>
Of course, instead of changing an victim&rsquo;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.
</p>
<h6>Ways to stop CSRF</h6>
<p>
There are two main ways to block CSRF:
</p>
<ul>
<li>Check that incoming requests have a <strong>Referer</strong> header referencing your domain. This will stop requests unwittingly submitted from a third-party domain. However, some people disable their browser&rsquo;s <strong>Referer</strong> 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. </li>
<li>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&rsquo;s password, then a third-party can&rsquo;t forge a valid form post, because they don&rsquo;t know each user&rsquo;s password. However, don&rsquo;t expose the user&rsquo;s password this way: Instead, it&rsquo;s better to use some random value (such as a GUID) which you&rsquo;ve stored in the visitor&rsquo;s Session collection or into a Cookie. </li>
</ul>
<h6>Using the AntiForgeryToken helpers</h6>
<p>
With Preview 5, Microsoft has added a set of helpers to the &ldquo;futures&rdquo; assembly, Microsoft.Web.Mvc.dll, that give you a means to detect and block CSRF using the &ldquo;user-specific tokens&rdquo; technique.
</p>
<p>
To use these helpers to protect a particular form, put an Html.AntiForgeryToken() into the form, e.g.,
</p>
<div class="csharpcode">
<pre class="alt">
<span class="lnum">   1:  </span><span class="asp">&lt;%</span> <span class="kwrd">using</span>(Html.Form(<span class="str">&quot;UserProfile&quot;</span>, <span class="str">&quot;SubmitUpdate&quot;</span>)) { <span class="asp">%&gt;</span>
</pre>
<pre>
<span class="lnum">   2:  </span>    <span class="asp">&lt;%</span>= Html.AntiForgeryToken() <span class="asp">%&gt;</span>
</pre>
<pre class="alt">
<span class="lnum">   3:  </span>    <span class="rem">&lt;!-- rest of form goes here --&gt;</span>
</pre>
<pre>
<span class="lnum">   4:  </span><span class="asp">&lt;%</span> } <span class="asp">%&gt;</span>
</pre>
</div>
<p>
This will output something like the following:
</p>
<div class="csharpcode">
<pre class="alt">
<span class="lnum">   1:  </span><span class="kwrd">&lt;</span><span class="html">form</span> <span class="attr">action</span><span class="kwrd">=&quot;/UserProfile/SubmitUpdate&quot;</span> <span class="attr">method</span><span class="kwrd">=&quot;post&quot;</span><span class="kwrd">&gt;</span>
</pre>
<pre>
<span class="lnum">   2:  </span>    <span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">name</span><span class="kwrd">=&quot;__MVC_AntiForgeryToken&quot;</span> <span class="attr">type</span><span class="kwrd">=&quot;hidden&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;saTFWpkKN0BYazFtN6c4YbZAmsEwG0srqlUqqloi/fVgeV2ciIFVmelvzwRZpArs&quot;</span> <span class="kwrd">/&gt;</span>
</pre>
<pre class="alt">
<span class="lnum">   3:  </span>    <span class="rem">&lt;!-- rest of form goes here --&gt;</span>
</pre>
<pre>
<span class="lnum">   4:  </span><span class="kwrd">&lt;/</span><span class="html">form</span><span class="kwrd">&gt;</span>
</pre>
</div>
<p>
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.
</p>
<p>
Next, to validate an incoming form post, add the [ValidateAntiForgeryToken] filter to your target action method. For example,
</p>
<div class="csharpcode">
<pre class="alt">
<span class="lnum">   1:  </span>[ValidateAntiForgeryToken]
</pre>
<pre>
<span class="lnum">   2:  </span><span class="kwrd">public</span> ViewResult SubmitUpdate()
</pre>
<pre class="alt">
<span class="lnum">   3:  </span>{
</pre>
<pre>
<span class="lnum">   4:  </span>    <span class="rem">// ... etc</span>
</pre>
<pre class="alt">
<span class="lnum">   5:  </span>}
</pre>
</div>
<p>
This is an authorization filter that checks that:
</p>
<ul>
<li>The incoming request has a cookie called <strong>__MVC_AntiForgeryToken</strong></li>
<li>The incoming request has a Request.Form entry called <strong>__MVC_AntiForgeryToken</strong></li>
<li>These cookie and Request.Form values match </li>
</ul>
<p>
Assuming all is well, the request goes through as normal. But if not, boom!, there&rsquo;s an authorization failure with message &ldquo;A required anti-forgery token was not supplied or was invalid&rdquo;.
</p>
<p>
This prevents CSRF because even if a potential victim has an <strong>__MVC_AntiForgeryToken</strong> cookie, an attacker can&rsquo;t find out its value, so they can&rsquo;t forge a valid form post with the same value in Request.Form. But legitimate users aren&rsquo;t inconvenienced at all; the mechanism is totally silent.
</p>
<h6>Using salt</h6>
<p>
Salt? What? In case you want to protect multiple forms in your application independently of each other, you can use a &ldquo;salt&rdquo; value when you call Html.AntiForgeryToken(), e.g.,
</p>
<div class="csharpcode">
<pre class="alt">
<span class="lnum">   1:  </span><span class="asp">&lt;%</span>= Html.AntiForgeryToken(<span class="str">&quot;someArbitraryString&quot;</span>) <span class="asp">%&gt;</span>
</pre>
</div>
<p>
&hellip; and also in [ValidateAntiForgeryToken], e.g.,
</p>
<div class="csharpcode">
<pre class="alt">
<span class="lnum">   1:  </span>[ValidateAntiForgeryToken(Salt=<span class="str">&quot;someArbitraryString&quot;</span>)]
</pre>
<pre>
<span class="lnum">   2:  </span><span class="kwrd">public</span> ViewResult SubmitUpdate()
</pre>
<pre class="alt">
<span class="lnum">   3:  </span>{
</pre>
<pre>
<span class="lnum">   4:  </span>    <span class="rem">// ... etc</span>
</pre>
<pre class="alt">
<span class="lnum">   5:  </span>}
</pre>
</div>
<p>
&nbsp;
</p>
<p>
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&rsquo;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.)
</p>
<h6>Limitations of the Anti-Forgery helpers</h6>
<p>
ASP.NET MVC&rsquo;s anti-CSRF helpers work very nicely, but you should be aware of a few limitations:
</p>
<ul>
<li>All legitimate visitors must accept cookies (otherwise, [ValidateAntiForgeryToken] will deny their form posts). Arguably this isn&rsquo;t a limitation, because unless visitors allow cookies, you probably don&rsquo;t have anything to protect anyway. </li>
<li>It only works with POST requests, not GET requests. Arguably this isn&rsquo;t a limitation, because under the normal HTTP conventions, you shouldn&rsquo;t be using GET requests for anything other than read-only operations. </li>
<li>It&rsquo;s easily bypassed if you have any XSS holes on your domain. An XSS hole would allow an attacker to read a victim&rsquo;s anti-forgery token value, then use it to forge valid posts. So, don&rsquo;t have XSS holes! </li>
<li>It relies on the potential victim&rsquo;s browser implementing cross-domain boundaries solidly. Browsers are supposed to stop foreign domains from reading your app&rsquo;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&rsquo;s not supposed to be possible. For the most part, modern browsers block this line of attack. </li>
</ul>
<p>
In conclusion, ASP.NET MVC&rsquo;s anti-CSRF helpers are easy to use, and work very nicely thank you!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2008/11/prevent-cross-site-request-forgery-csrf-using-asp-net-mvc-antiforgerytoken-helper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

