How to avoid open-redirect vulnerability and safely redirect on successful login (HINT: ASP.NET MVC 2 default code is vulnerable)

前端 未结 4 2180
[愿得一人]
[愿得一人] 2021-02-04 03:06

Normally, when a site requires that you are logged in before you can access a certain page, you are taken to the login screen and after successfully authenticating yourself, you

相关标签:
4条回答
  • 2021-02-04 03:14

    Yes this is a vulnerability. Before redirecting you need to inspect the returnUrl string parameter by passing it to a Uri object and make sure that the target domain is the same as the requesting domain. You should also take into account the case when returnUrl is a relative address like /admin. No problem in this case as the redirect will be to the same application.

    0 讨论(0)
  • 2021-02-04 03:31

    Jon Galloway wrote up an article with a solution for MVC 2 (and 1).

    Here's the snippet that should help with your issue:

    SECURED (original article updated 2014)

    private bool IsLocalUrl(string url)
    {
      return System.Web.WebPages.RequestExtensions.IsUrlLocalToHost(
          RequestContext.HttpContext.Request, url);
    }
    
    0 讨论(0)
  • 2021-02-04 03:34

    As long as you use one of the variants of Redirect that uses controller and action parameters or a route name, you should be alright, provided you have adequate security controls on your controller methods.

    The concept being, whatever you use for your redirect must go through the routing engine and be validated by matching a route.

    But I suspect that the real vulnerability is Cross-Site Scripting. Unless your malicious user can inject some Javascript into the page, they have no way of manipulating the return Url, or any of its parameters (since you otherwise control all of the server and browser code).

    0 讨论(0)
  • 2021-02-04 03:38

    You could always keep a record of the previous page with TempData when the user is not authenticated and use that to redirect to the previous page instead of a url parameter.

    0 讨论(0)
提交回复
热议问题