Preventing Open Openirect Usage in C # Web Forms Application

The code looks like this, this is the obvious code:

readonly Regex alphaNumericRegex = new Regex("^[a-zA-Z0-9_]*$");
private const string USERNAME = "bitzleon";
protected void Page_PreInit(object sender, EventArgs e)
{
    if (HttpContext.Current.Request.QueryString.Get("userid") == null)
    {
        UriBuilder uriBuilder = new UriBuilder(Request.Url);
        NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query);
        query["userid"] = USERNAME;
        uriBuilder.Query = query.ToString();
        if (alphaNumericRegex.IsMatch(query["userid"]) && IsLocalUrl(uriBuilder.ToString()))
        {
            Response.Redirect(uriBuilder.ToString());
        }
    }
}

      

The line is Response.Redirect(uriBuilder.ToString());

throwing an error in my Veracode checks, but I run two checks to ensure that the redirect is valid and internal.

First, the request must be alpha numeric - the regex will take care of that, and second, the address we are redirecting to is only local. The method I am using to validate the URL looks like this:

public bool IsLocalUrl(string url)
{
    if (string.IsNullOrEmpty(url))
    {
        return false;
    }
    Uri absoluteUri;
    if (Uri.TryCreate(url, UriKind.Absolute, out absoluteUri))
    {
        return String.Equals(Request.Url.Host, absoluteUri.Host,
                    StringComparison.OrdinalIgnoreCase);
    }
    bool isLocal = !url.StartsWith("http:", StringComparison.OrdinalIgnoreCase)
                    && !url.StartsWith("https:", StringComparison.OrdinalIgnoreCase)
                    && Uri.IsWellFormedUriString(url, UriKind.Relative);
    return isLocal;
}

      

But Veracode still does not believe that these fixes are sufficient to ensure that the HARDCODED redirection is verified enough.

This is the complete post on veracode.

This is the page they lead me to when showing examples of how this attack works.

I would like to assume that this is a false positive, but it still affects my result.

+3
redirect c # c # -4.0


source to share


No one has answered this question yet

Check out similar questions:

540
How do I save application settings in a Windows Forms application?
359
How do I get the IP address of the server that my C # application is running on?
251
The web application project [...] is configured to use IIS. Web server [...] not found.
241
How do I stop C # apps from closing automatically?
233
How to open the default browser in C #
210
Is SecureString ever practical in a C # application?
68
C # functionalities
22
Prevent redirection after form submission
2
Troubleshooting Infinite Redirection in a Web Application
0
Redirect to an external page in a web forms application



All Articles
Loading...
X
Show
Funny
Dev
Pics