Preventing Malicious Redirects

If your site permits open redirects, you may be unknowingly helping attackers take advantage of your user base.

Risks

Prevalence Common
Rating prevelance on a298cccc3e525887223509d0e6fe9a464d7d7f60574014de1fe402608154d354 Rating prevelance on a298cccc3e525887223509d0e6fe9a464d7d7f60574014de1fe402608154d354 Rating prevelance on a298cccc3e525887223509d0e6fe9a464d7d7f60574014de1fe402608154d354
Exploitability Easy
Rating exploitability on 6b817c6c589f0911378579408b6cbfc6d82345849ae2da559b8d11602b9a987b Rating exploitability on 6b817c6c589f0911378579408b6cbfc6d82345849ae2da559b8d11602b9a987b Rating exploitability on 6b817c6c589f0911378579408b6cbfc6d82345849ae2da559b8d11602b9a987b
Impact Worrying
Rating impact on 48bdb4077813afe9762f27e229e64207ec59c3891a54a3adf931c2c91a6d99bd Rating impact on 48bdb4077813afe9762f27e229e64207ec59c3891a54a3adf931c2c91a6d99bd Rating impact on 48bdb4077813afe9762f27e229e64207ec59c3891a54a3adf931c2c91a6d99bd

Redirects are a useful function to have when building a website. If a user attempts to access a resource before they are logged in, it is conventional to redirect them to the login page, put the original URL in a query parameter, and after they have logged in, automatically redirect them to their original destination. This type of functionality shows you are putting thought into the user experience, and is to be encouraged. However, you need to be sure anywhere you do redirects, they are done safely – otherwise you are putting your users in harm’s way by enabling phishing attacks.

Modern web-mail services are very good at spotting spam and other types of malicious messages. One detection method they use is to parse the out-bound links in HTML emails. These links are compared to a black-list of banned domains; if the domain is deemed to be malicious, the email is redirected to the junk folder.

This is why spammers and phishers find open redirects so enticing. If they can “bounce” a user off your website (an apparently valid domain), their messages are less likely to be marked as malicious. If the user clicks on the link, they will see your website in the link, but they will end up at whatever site the attacker wants to direct them to. A confused user might download malware or worse, because of the trust they put in your site!

Protection

Disallow Offsite Redirects

You can prevent redirects to other domains by checking the URL being passed to the redirect function. Make sure all redirect URLs are relative paths – i.e. they start with a single / character. (Note that URLs starting with // will be interpreted by the browser as a protocol agnostic, absolute URL – so they should be rejected too.)

If you do need to perform external redirects, consider whitelisting the individual sites that you permit redirects to.

Check the Referrer When Doing Redirects

Redirects to URLs passed in query parameters should only be triggered by pages on your site. Any other sites triggering a redirect should be treated with extreme suspicion. As a second layer of defense, check that the Referer in the HTTP request matches your domain whenever you perform a redirect.

Code Samples

The code samples below demonstrate how to check that a URL is a relative path.

Validate for relative paths with:


import re

def is_relative(url):
  return re.match(r"^\/[^\/\\]", url)

Validate for relative paths with:


def is_relative(url)
  url =~ /^\/[^\/\\]/
end

A redirect like the following is open to abuse:


protected void doGet(HttpServletRequest request, HttpServletResponse response) {
  String url = request.getParameter("url");
  if (url != null) {
    response.sendRedirect(url);
  }
}

Make sure you validate the url parameter wherever you invoke the sendRedirect method:


protected void doGet(HttpServletRequest request, HttpServletResponse response) {
  String url = request.getParameter("url");
  if (url != null && isRelative(url)) {
    response.sendRedirect(url);
  }
}

// Allow anything starting with "/", except paths starting
// "//" and "/\".
private boolean isRelative(url) {
  return url.matches("/[^/\\]?.*");
}

Validate URLs with the following snippet:


private bool IsRelativePath(string url)
{
   if (string.IsNullOrEmpty(url))
   {
      return false;
   }
   else
   {
      if (url.Length == 1) return true;

      // Allow anything starting with "/", except paths starting
      // "//" and "/\". Allow the "~/" syntax too.
      return (url[0] == '/' && (url[1] != '/' && url[1] != '\\')) ||
             (url[0] == '~' && url[1] == '/');
   }
}

.NET allows the syntax ~/ for URLs relative to the domain of your site - you can choose whether to support this in redirects.

Validate relative paths with:


function isRelative(url) {
  return url && url.match(/^\/[^\/\\]/);
}

Other Considerations

Check Client-Side Code Too!

Redirects can happen in client-side JavaScript, too! Validate any code that sets window.location, to ensure the URL is not taken from untrusted input.

Interstitial Pages

Some sites insert interstitial pages when the user is leaving the site – “you are now leaving tinyrobotninjas.com, you will be automatically redirected in 5 seconds”. This is a good defense against doppelganger domains – websites that have a very similar domain name, in order to trick the user into trusting them. If you implement an interstitial page that passes the URL in the query parameter, be sure to check the Referer header, or else they could be open to abuse.

Aggregator Sites

Aggregator sites often make use of redirects to do click-counting. URLs are chosen by the community, then when a user clicks on the link, the click-through count is incremented, and the user is redirected to their destination. If your site implements this type of functionality, redirects to external sites are part of doing business. Just be sure to check the Referer each time you do a redirect!

Is your website using open redirects?

Netsparker n 834848961a0bf6ec5556448ff47f421d0b1204a572877a59717064b1088e8c43 Check today. Scan your website for Open Redirects and other vulnerabilities with the Netsparker Web Application Security Scanner.