Reputation: 1739
So I have been writing a login script which upon successful login, redirects to the previous page. I've seen a few ways to pass the referer URL:
Using $_SERVER['HTTP_REFERER'] - Sends in the whole referer URL. Possible to be manipulated. So not to be trusted.
$_GET and passing the referer as url parameter. Not secure at all.
Using Hidden-fields in the form. Not so secure either even with POST.
So, since method 2 allows most flexibility, I'm choosing that. The question is: If I pass the URL without the domain ('http://www.domain.com/' stripped), and later manually prepend the domain when redirecting, can I be sure that the resulting URL redirect stays within my domain? If not, what can I do to make sure it doesn't stray away?
(Assume that, if no page with the result url exist, it will be redirected to home page)
Eg: http://www.domain.com/login.php?ref=category/products/product-1.php
On successful login, the page is redirected to 'http://www.domain.com/'.$_GET['ref']
What I know should be done to validate the referer url parameter:
'../' can help navigate to the previous folder and must be removed if any.
All tags passed in the referer url parameter must be stripped.
All '://' must be removed.
Upvotes: 0
Views: 489
Reputation: 690
Few tips:
Upvotes: 1