views:

141

answers:

3

I heard of sites using other site to redirect users either to their own site or to hide behind another site. In my code i redirect in a few places such as post a comment (its easier to use a return url then figure out the page using data given).

How do i check if the return URL is my own url? I think i use absolute paths so i can easily check if the first character is '/' but then i will lose relative flexibility. This also disallows me from doing http://mysite.com/blah in the redirect url. I could patch the url by adding mysite + string but i'll need to figure out if string is a relative url or already a mysite.com url.

Whats the easiest way to ensure i am only redirecting to my site?

A: 

This seems to be an odd question, and it should not be a concern if you are in full control over the redirect process. If for some reason you are allowing input from the user to be actively involved in a redirect (as in the code below)

Response.Redirect(someUserInput);

Then, yes, a user could have your code send them off to who knows where. But if all you are ever doing is

Response.Redirect("/somepage.aspx")

Then those redirects will always be on your site.

Like I said, it seems to be an odd question. The more prominent concerns in terms of user input are typically SQL Injection attacks and cross-site scripting. I've not really heard about "malicious redirects."

Anthony Pegram
If I understand the Q, he's got a page that accepts a ReturnUrl query string param. When the page is done, he does a `Response.Redirect` to that URL. Usually, _his_ code is what constructs the ReturnUrl param, but what if some other code does, instead.
John Saunders
@John, ahh. I like the solution you present.
Anthony Pegram
+2  A: 

I hadn't thought of this before, but how about using an encrypted version of the URL in the query string parameter?

Alternatively, you could keep a list of the actual URLs in some persistent store (persistent for a couple of hours, maybe), and in the query string, just include the index into the persistent store of URLs. Since You'd be the only code manipulating this persistent, server-side store, the worst a malicious user could do would be to redirect to a different valid URL.

John Saunders
Interesting but that sounds like it COULD be more work then checking if the url starts with [A-z]:// and checking if its my site, another site and if its a relative or absolute path. I am thinking there may be a way with URI to check if the domain has http and if its my downmain or not. Last time i tried i didnt see any obvious solution. Maybe i should just unforced absolute paths (but not urls) and simply check if it starts with '/'. +1 for good and interesting answer.
acidzombie24
I didn't want to do pattern matching on the URL because of things like IP addresses, load balancers, partner sites, etc. Instead, I reversed it with the idea that I know which URLs I redirect to (in theory), and just want to ensure that it's one of those URLs.
John Saunders
+1  A: 

How about, if the redirectUrl contains "://" (which includes http://, https://, ftp://, etc.) then it must also start with "http://mysite.com". If it does not contain "://" then it is relative and should not be a problem. Something like this:

if (!(redirectUrl.Contains("://") ^ redirectUrl.IndexOf("http://mysite.com") == 0))
{
    Response.Redirect(redirectUrl);
}
James Lawruk
i like it. I also dont have to worry about ports and other crazy stuff. cool, i think i'll use this. NOTE: The only problem with the solution is it doesnt work with subdomain but i happen to not be using any ;).
acidzombie24
Great! FYI: I changed it slightly, changed Contains() to IndexOf()
James Lawruk