Write up the following post
http://www.troyhunt.com/2012/01/aspnet-session-hijacking-with-google.html
Write up the following post
http://www.troyhunt.com/2012/01/aspnet-session-hijacking-with-google.html
Razor view engine treats the contents of MvcHtmlString objects as if they were encoded, even when that is not the case. We can use Html.Raw helper method to include our HTML into the web page without it being encoded.
In ASP.Net MVC Framework, request validation is enabled by default. You can disable it by decorate your controller/action method by ValidateInput attribute and passing false to it. If you decorate the controller with it, that means you enabled/disabled request validation for all action methods within this controller (of course not recommended). If you decorate the action method by that attribute, that means you enabled/disabled request validation for all posted data/model properties. Also you can disable request validation for a specific property by decorate it with AllowHtml attribute.
We can protect our site by keeping a record of each client IP address when a session starts, we can deny any requests that originate from a different IP. But you should avoid this technique when you deal with the public internet.
We can mark a cookie with the HttpOnly flag, and the browser will hide its existence from javascript but will continue to send it via all HTTP requests. By default ASP.Net marks ASP.Net_SessionId and /AUTH as HttpOnly. Of course you can apply HttpOnly to your session cookies when you create them.
What is Cross Site Request Forgery (CSRF)?
“A CSRF attack forces a logged-on victim’s browser to send a pre-authenticated request to a vulnerable web application, which then forces the victim’s browser to perform a hostile action to the benefit of the attacker. CSRF can be as powerful as the web application that it attacks.”
Cross-site scripting (XSS) is widely regarded as one of the most used security issue on the web. 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.
All web application platforms are potentially vulnerable to CSRF, but in this post we’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); ViewData["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. 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.
There are two main ways to block CSRF:
The core ASP.NET MVC package includes a set of helpers 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="__RequestVerificationToken" type="hidden" value="saTFWpkKN0BYazFtN6c4YbZAmsEwG0srqlUqqloi/fVgeV2ciIFVmelvzwRZpArs" /> <!-- rest of form goes here --> </form>
At the same time, Html.AntiForgeryToken() will give the visitor a cookie called __RequestVerificationToken, 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:
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 __RequestVerificationToken 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
The simple solution for adding AJAX Cross-Site request forgery is to add the Request Verification Token to the data
function CallAjax() { var data = { "ajaxparameter ": 'hello moon' }; data.__RequestVerificationToken = $("input[name=__RequestVerificationToken]").val(); $.ajax({ type: "POST", url: '@Url.Action("AjaxCall")', dataType: "json", data: JSON.stringify(data), success: function () { alert('Ajax call succesful'); } }); return false; }
this can also be done via the $ajaxSetup
$.ajaxSetup({ global: true, beforeSend: function (xhr, settings) { if (settings.data != "") { settings.data += '&'; } settings.data += '__RequestVerificationToken=' + $("input[name=__RequestVerificationToken]").val(); } });
this then leaves the AJAX call as you would normally find it
function CallAjax() { var data = { "ajaxparameter ": 'hello moon' }; $.ajax({ type: "POST", url: '@Url.Action("AjaxCall")', dataType: "json", data: JSON.stringify(data), success: function () { alert('Ajax call succesful'); } }); return false; }
The form token can be a problem for AJAX requests, because an AJAX request might send JSON data, not HTML form data. One solution is to send the tokens in a custom HTTP header. The following code uses Razor syntax to generate the tokens, and then adds the tokens to an AJAX request. The tokens are generated at the server by calling AntiForgery.GetTokens.
<script> @functions{ public string TokenHeaderValue() { string cookieToken, formToken; AntiForgery.GetTokens(null, out cookieToken, out formToken); return cookieToken + ":" + formToken; } } $.ajax("api/values", { type: "post", contentType: "application/json", data: { }, // JSON data goes here dataType: "json", headers: { 'RequestVerificationToken': '@TokenHeaderValue()' } }); </script>
When you process the request, extract the tokens from the request header. Then call the AntiForgery.Validate method to validate the tokens. The Validate method throws an exception if the tokens are not valid.
void ValidateRequestHeader(HttpRequestMessage request) { string cookieToken = ""; string formToken = ""; IEnumerable<string> tokenHeaders; if (request.Headers.TryGetValues("RequestVerificationToken", out tokenHeaders)) { string[] tokens = tokenHeaders.First().Split(':'); if (tokens.Length == 2) { cookieToken = tokens[0].Trim(); formToken = tokens[1].Trim(); } } AntiForgery.Validate(cookieToken, formToken); }
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
Will different web servers in a load balanced configuration create the same token in the HTML forms?
If all machines across the farm share the same <machineKey>, everything will work. There are lots of resources on how to set this. There’s also a tutorial on MSDN.
Note that the name <machineKey> is a bit misleading, since this is actually set per-application in ~/Web.config. So set the <machineKey> explicitly in your app’s Web.config, then deploy across your farm.
ASP.NET MVC’s anti-CSRF helpers work very nicely, but you should be aware of a few limitations:
Links
The Open Web Application Security Project
The orginal article was produced by Steven Sanderson:
Prevent Cross-Site Request Forgery (CSRF) using ASP.NET MVC’s AntiForgeryToken() helper
Source for this article
Following the TDD approach when you are working with MVC it is quite easy to produce Unit Tests that are based on the MVC Controllers, with more and more business logic appearing in the User Interface by using libraries such as jQuery it is becoming more important to perform Unit Testing on JavaScript.
What options are available today, more importantly what are the pros and cons of each solution?
Pros
can be invoked from an ant build file
launches browser to run the tests
Eclipse plug-in
Cons
launches browser to run the tests
Does not support js file to write the unit test code: it has to be embedded inside an html file
it has not been updated for a few years
Notes:
There is a JsUnit (2).
An ‘ant’ is an open source build tool; “Ant” because it is a little thing that can build big things.
Pros
ant driven
supports js file
very simple to use
Cons
Simulation of JavaScript engine: not advanced enough to support all coding types
Pros
Can be invoked from ant build file
Simulates real browser behaviour
Cons
Simulation of JavaScript engine from a limited number of browser versions
No activity for 2 years: it does not support Firefox versions 2.x nor 3.x
Pros
Runs on actual browser
Cons
JavaScript only framework: cannot be called from ant build file
Pros
Runs on actual browser
Cons
Does not seem to support all code types
JavaScript only framework: cannot be called from ant build file,
Pros
Runs on actual browser
Cons
JavaScript only framework: cannot be called from ant build file
Pros
Can be intergrated with most testing frameworks, such as MSTest, NUnit, xUnit etc
ant build supported
Simple to install, only requires a single dll
Browserless
Can be installed using NuGet
Small overhead of 56k
Cons
Not very active on CodePlex, with only 51 downloads
Reference directly to the js files, so if the js files are moved all the tests need to be updated
I know there are many many more and this is just a cross sample of JavaScript testing libraries, but I think I may have found the crown jewels for TDD.
It looks like JSTest is, not only the only choice we have, it is the simplest and best approach to JavaScript Unit Testing, but why has it only had 51 downloads? Have I found a hidden gem or a monster waiting to bite me?
JSTest does provide an easy way to apply the Test-Driven Development (TDD) process
For an example to use it check out Unit Testing JavaScript with MSTest and JSTest.Net by Shawn Sweeney, I have created a sample MVC application showing it working.
JSTestMVCExample.zip (2.67 mb)
Conclusion, I do think we now have the tools to be fully “TDD” compliant.