Skip to content

Commit

Permalink
[ss-2018-008] Validate against malformed urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman authored and robbieaverill committed May 14, 2018
1 parent e967ab0 commit 9053014
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Control/Director.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,13 +728,26 @@ public static function is_relative_url($url)
*/
public static function is_site_url($url)
{
$urlHost = parse_url($url, PHP_URL_HOST);
$parsedURL = parse_url($url);

// Validate user (disallow slashes)
if (!empty($parsedURL['user']) && strstr($parsedURL['user'], '\\')) {
return false;
}
if (!empty($parsedURL['pass']) && strstr($parsedURL['pass'], '\\')) {
return false;
}

// Validate host[:port]
$actualHost = parse_url(self::protocolAndHost(), PHP_URL_HOST);
if ($urlHost && $actualHost && $urlHost == $actualHost) {
if (!empty($parsedURL['host'])
&& $actualHost
&& $parsedURL['host'] === $actualHost
) {
return true;
} else {
return self::is_relative_url($url);
}

return self::is_relative_url($url);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/php/Control/DirectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ public function testIsSiteUrl()
$this->assertFalse(Director::is_site_url("http://test.com?url=" . Director::absoluteBaseURL()));
$this->assertFalse(Director::is_site_url("http://test.com?url=" . urlencode(Director::absoluteBaseURL())));
$this->assertFalse(Director::is_site_url("//test.com?url=" . Director::absoluteBaseURL()));
$this->assertFalse(Director::is_site_url('http://google.com\@test.com'));
$this->assertFalse(Director::is_site_url('http://google.com/@test.com'));
$this->assertFalse(Director::is_site_url('http://google.com:pass\@test.com'));
$this->assertFalse(Director::is_site_url('http://google.com:pass/@test.com'));
}

/**
Expand Down

0 comments on commit 9053014

Please # to comment.