Open Redirection Vulnerability Explained and How to Prevent It

Introduction 

 
An Open Redirection is when a web application or server uses a user-submitted link to redirect the user to a given website or page.
 
How can I identify is my application is vulnerable or not?
  1. If your application redirects to URL which is directly given by user that’s specified via the request such as query string or form data.
  2. The redirection is performed without checking if the URL is a local URL.
 
Below is the code:
  1. [HttpPost]  
  2.         [AllowAnonymous]  
  3.         public async Task<IActionResult> Login(LoginViewModel model,string returnUrl)  
  4.         {  
  5.             if (ModelState.IsValid)  
  6.             {  
  7.                 var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, truefalse);  
  8.                 if (result.Succeeded)  
  9.                 {  
  10.                     if(!string.IsNullOrEmpty(returnUrl))  
  11.                     return Redirect(returnUrl);  
  12.                     else  
  13.                     {  
  14.                         return RedirectToAction("List""Home");  
  15.                     }  
  16.                     //return RedirectToAction("List", "Home");  
  17.                 }  
  18.   
  19.                 ModelState.AddModelError("""Invalid Login");             
  20.             }  
  21.             return View();  
  22.         }  
In this code we pass this URL directly to the Redirect. We never check if the URL is local or not, meaning that our application is vulnerable to open redirect attacks.
 
https://ourwebsite.com/account/login?returnURL=http://hackerwebsite.com/account/login
 
See in the above URL where the first part is our website and in return, the URL is given by a hacker which could be malicious or steal data. If you see the first part it looks like your website and generally, we wouldn't look at the second part which the hacker could use to easily redirect us to their site.
 

To Prevent Open Redirect Attacks

 
LocalRedirect In Asp.Net Core
 
Rather than using Redirect, use LocalRedirect so when the user tries to add another domain URL it will prevent it and give an error.
 
See in the above image that we used Local redirect in our code. When we login, I pass the return URL as =https://google.com which is not local and our complete URL as below,
 
https://localhost:44387/Account/Login?ReturnUrl=https://google.com

So it will throw an error like below:

Exception Message
 
The supplied URL is not local. A URL with an absolute path is considered local if it does not have a host/authority part. URLs using virtual paths (‘~/’) are also local.
 
As we handle error globally so that’s why such page and message occurs.
 
Url.IsLocalUrl In Asp.Net Core
 
If you want to use Redirects only then you can check the URL first and then perform a redirection. The code for checking the URL is shown below
  1. Url.IsLocalUrl(returnUrl)  
And our application code becomes
  1. [HttpPost]  
  2.         [AllowAnonymous]  
  3.         public async Task<IActionResult> Login(LoginViewModel model,string returnUrl)  
  4.         {  
  5.             if (ModelState.IsValid)  
  6.             {  
  7.                 var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, truefalse);  
  8.                 if (result.Succeeded)  
  9.                 {  
  10.                     if(!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))  
  11.                     return Redirect(returnUrl);  
  12.                     else  
  13.                     {  
  14.                         return RedirectToAction("List""Home");  
  15.                     }  
  16.                     //return RedirectToAction("List", "Home");  
  17.                 }  
  18.   
  19.                 ModelState.AddModelError("""Invalid Login");             
  20.             }  
  21.             return View();  
  22.         }