Check out the other posts in this series here.
In today's post on improving the security of our ASP.NET applications, we tackle the problem of unvalidated redirects and forwards. Before we get started, here is a recap of the problem we found during our security review.
Unvalidated Redirects and Forwards
The concern here is that we might have a page that forwards a user to another page as specified by a url in a query parameter. This would allow an attacker to create a link that seemed to go to a BugTracker.NET deployment but didn't actually. For instance one might create a URL like so
The page refer would send a user on to another site. This other site could be made to look exactly like an install of BugTracker.NET such that it would trick users into revealing secret data. For instance imagine a fake password change page that required the users to enter their old password. The password would then be captured by the evil site allowing them to exploit the original site. A typical example of a place where this might be used is on a login screen where it refers the user back to the page they tried to visit without being authenticated.
Testing this with exact exploit with BugTracker.net we find that it is indeed vulnerable to such attacks. On my local build of BugTracker.NET I can enter
This takes me to the login page and once I log in I find my browser redirected to the Google home page.
Validating the redirect on the server is a fairly straight forward process. All we need to do is check if the url specified in the query parameter represents a legitimate local url to BugTracker.NET. If the url is not local, we can simply redirect the user to a default page.
We could write this logic ourselves, but I would rather use an existing method if possible. As it turns out, the UrlHelper.IsLocalUrl method does exactly what we need. The only problem is that this method is in System.Web.MVC, which we are not referencing yet.
Let's start by installing the MVC NuGet package
It might seem like overkill to add references to the MVC framework just to use this single method, but I know that we are planning to start using MVC soon so now is as good a time as any to introduce the dependency.
Now we can add the check to validate the url before redirecting the user. In BugTracker, the redirect logic is handled in the Util.redirect method.
We first create an instance of the UrlHelper class, then use the UrlHelper to determine whether or not the url is a local url. Overall, this was a very simple change that has a big positive impact on the security of our application.
Highlighting External Links
Another way to improve security is to warn the user when a link will forward them to another website. This is important because in BugTracker, a user can add a link inside a bug description. Much like the evil redirect, the destination page could be made to look exactly like BugTracker and attempt to trick the user into revealing secret data.
We like the approach that Wikipedia takes here. Any links that will take the user to a site outside of Wikipedia are highlighted with an external link icon.
Normally, we would add this code to our MasterPage file so it is included in all the pages of the website. BugTracker does not use master pages yet, so we have added it to the section that renders the BugTracker menu instead. We will be looking at adding master pages in a future post.
Next, we will add the following to our main CSS file.
Note, we are using the external link icon from Wikimedia Commons. For browsers that support it, we are using the SVG version of the icon and using a png version for older browsers like IE8.
At this point, everything appears to be working as expected but after further testing we noticed that the jQuery code to highlight external links was crashing on a number of pages. As it turns out, jQuery is NOT loaded on all pages. Again, the lack of master pages means we have no centralized place to add a reference to all our pages. We are going to need to bump the 'master pages' feature up in our priority list.
Unfortunately, we can't simply add the jQuery script reference to the write_menu method above. If we do that, we will break some of the pages that already have jQuery and jQuery UI loaded. As a work-around, we decided to introduce RequireJS to dynamically load jQuery if it has not already been loaded. You can read more about this technique in the RequireJS docs.
Optimizing the background images
One final thing before we wrap this up. We can optimize our CSS by taking the external link background images and base64 encoding them directly into the stylesheet. For small images, this is more efficient because it eliminates the need for a separate HTTP request to retrieve the image.
We used a smart tag available via the Web Essentials plug-in to do this for us.