Validate referer before redirecting

This commit is contained in:
Giuseppe Criscione 2019-03-16 14:01:02 +01:00
parent 9b405395d9
commit 05bb5b1c41
2 changed files with 14 additions and 1 deletions

View File

@ -84,7 +84,7 @@ trait AdminTrait
*/
protected function redirectToReferer($code = 302, $default = '/')
{
if (!is_null(HTTPRequest::referer()) && HTTPRequest::referer() !== Uri::current()) {
if (HTTPRequest::validateReferer($this->uri('/')) && HTTPRequest::referer() !== Uri::current()) {
Header::redirect(HTTPRequest::referer(), $code);
} else {
Header::redirect($this->uri($default), $code);

View File

@ -108,6 +108,19 @@ class HTTPRequest
return static::hasHeader('Referer') ? static::$headers['Referer'] : null;
}
/**
* Check if the request referer has the same origin
*
* @param string $path Optional URI path
*
* @return bool
*/
public static function validateReferer($path = null)
{
$base = Uri::normalize(Uri::base() . '/' . ltrim($path, '/'));
return substr(static::referer(), 0, strlen($base)) === $base;
}
/**
* Get request origin
*