Skip to content

Commit 606b8ea

Browse files
authored
Merge pull request #120 from marekm4/fix-domain-check
[Security] Add exact check for domain and port
2 parents 31b81cb + b7ff4ea commit 606b8ea

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/OAuth2.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,8 @@ protected function validateRedirectUri($inputUri, $storedUris)
14331433

14341434
foreach ($storedUris as $storedUri) {
14351435
if (strcasecmp(substr($inputUri, 0, strlen($storedUri)), $storedUri) === 0) {
1436-
return true;
1436+
return parse_url($inputUri, PHP_URL_HOST) === parse_url($storedUri, PHP_URL_HOST) &&
1437+
parse_url($inputUri, PHP_URL_PORT) === parse_url($storedUri, PHP_URL_PORT);
14371438
}
14381439
}
14391440

tests/OAuth2Test.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,50 @@ public function testFinishClientAuthorizationThrowsErrorIfNoMatchingUri()
892892
}
893893
}
894894

895+
public function testFinishClientAuthorizationThrowsErrorIfNoMatchingDomain()
896+
{
897+
$stub = new OAuth2GrantCodeStub;
898+
$stub->addClient(new OAuth2Client('blah', 'foo', array('http://a.example.com')));
899+
$oauth2 = new OAuth2($stub);
900+
901+
$data = new \stdClass;
902+
903+
try {
904+
$oauth2->finishClientAuthorization(true, $data, new Request(array(
905+
'client_id' => 'blah',
906+
'response_type' => 'code',
907+
'state' => '42',
908+
'redirect_uri' => 'http://a.example.com.test.com/',
909+
)));
910+
$this->fail('The expected exception OAuth2ServerException was not thrown');
911+
} catch (OAuth2ServerException $e) {
912+
$this->assertSame('redirect_uri_mismatch', $e->getMessage());
913+
$this->assertSame('The redirect URI provided does not match registered URI(s).', $e->getDescription());
914+
}
915+
}
916+
917+
public function testFinishClientAuthorizationThrowsErrorIfNoMatchingPort()
918+
{
919+
$stub = new OAuth2GrantCodeStub;
920+
$stub->addClient(new OAuth2Client('blah', 'foo', array('http://a.example.com:80')));
921+
$oauth2 = new OAuth2($stub);
922+
923+
$data = new \stdClass;
924+
925+
try {
926+
$oauth2->finishClientAuthorization(true, $data, new Request(array(
927+
'client_id' => 'blah',
928+
'response_type' => 'code',
929+
'state' => '42',
930+
'redirect_uri' => 'http://a.example.com:8080/',
931+
)));
932+
$this->fail('The expected exception OAuth2ServerException was not thrown');
933+
} catch (OAuth2ServerException $e) {
934+
$this->assertSame('redirect_uri_mismatch', $e->getMessage());
935+
$this->assertSame('The redirect URI provided does not match registered URI(s).', $e->getDescription());
936+
}
937+
}
938+
895939
public function testFinishClientAuthorizationThrowsErrorIfRedirectUriAttemptsPathTraversal()
896940
{
897941
$stub = new OAuth2GrantCodeStub;

0 commit comments

Comments
 (0)