Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: harden subdomain validation #359

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lib/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function generateRandom(): string {
*
* @return boolean True if the redirection URI is valid, false otherwise.
*/
public static function validateRedirectUri($expected, $actual, $allowSubdomains) {
public static function validateRedirectUri($expected, $actual, $allowSubdomains): bool {
$validatePort = true;
if (\strpos($expected, 'http://localhost:*') === 0) {
$expected = 'http://localhost' . \substr($expected, 18);
Expand All @@ -59,13 +59,7 @@ public static function validateRedirectUri($expected, $actual, $allowSubdomains)
return false;
}

if ($allowSubdomains) {
if (\strcmp($expectedUrl->hostname, $actualUrl->hostname) !== 0
&& \strcmp($expectedUrl->hostname, \str_replace(\explode('.', $actualUrl->hostname)[0] . '.', '', $actualUrl->hostname)) !== 0
) {
return false;
}
} elseif (\strcmp($expectedUrl->hostname, $actualUrl->hostname) !== 0) {
if (!self::validateDomain($expectedUrl, $actualUrl, $allowSubdomains)) {
return false;
}

Expand Down Expand Up @@ -95,12 +89,28 @@ public static function removeWildcardPort($redirectUri): string {
}

public static function isValidUrl($redirectUri): bool {
$redirectUri = Utilities::removeWildcardPort($redirectUri);
$redirectUri = self::removeWildcardPort($redirectUri);
return (\filter_var($redirectUri, FILTER_VALIDATE_URL) !== false);
}

// See https://tools.ietf.org/pdf/rfc7636.pdf#56
public static function base64url_encode($data) {
public static function base64url_encode($data): string {
return \rtrim(\strtr(\base64_encode($data), '+/', '-_'), '=');
}

private static function validateDomain(URL $expectedUrl, URL $actualUrl, bool $allowSubdomains): bool {
if (!$allowSubdomains) {
return \strcmp($expectedUrl->hostname, $actualUrl->hostname) === 0;
}

$expectedUrlParts = array_reverse(explode('.', $expectedUrl->hostname));
$actualUrlParts = array_reverse(explode('.', $actualUrl->hostname));
foreach ($expectedUrlParts as $i => $p) {
if ($p !== $actualUrlParts[$i]) {
return false;
}
}

return true;
}
}
12 changes: 7 additions & 5 deletions tests/unit/UtilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use PHPUnit\Framework\TestCase;

class UtilitiesTest extends TestCase {
public function testGenerateRandom() {
public function testGenerateRandom(): void {
$random = Utilities::generateRandom();

$this->assertEquals(64, \strlen($random));
Expand All @@ -41,7 +41,9 @@ public function providesUrlsToValidate(): array {
[false, 'https://owncloud.org:80/tests?q=1', 'https://owncloud.org:80/test?q=1', false],
[false, 'https://owncloud.org:80/test?q=1', 'https://owncloud.org:80/test?q=0', false],
[true, 'http://localhost:*/test?q=1', 'http://localhost:12345/test?q=1', false],
[false, 'http://excepted.com', 'http://aaa\@excepted.com', false]
[false, 'http://excepted.com', 'http://aaa\@excepted.com', false],
[false, 'https://trustedclient.com', 'https://munity.trustedclient.community.', true],
[false, 'https://trustedclient.com', 'https://munity.trustedclient.community', true]
];
}

Expand All @@ -52,7 +54,7 @@ public function providesUrlsToValidate(): array {
* @param $actualRedirect
* @param $allowSubDomain
*/
public function testValidateRedirectUri($expectedResult, $expectedRedirect, $actualRedirect, $allowSubDomain) {
public function testValidateRedirectUri($expectedResult, $expectedRedirect, $actualRedirect, $allowSubDomain): void {
$this->assertEquals(
$expectedResult,
Utilities::validateRedirectUri(
Expand All @@ -79,11 +81,11 @@ public function providesUrls(): array {
* @param $expected
* @param $url
*/
public function testIsValidUrl($expected, $url) {
public function testIsValidUrl($expected, $url): void {
$this->assertEquals($expected, Utilities::isValidUrl($url));
}

public function testBase64url_encode() {
public function testBase64url_encode(): void {
$data = \random_bytes(32);
$encoded = Utilities::base64url_encode($data);
$this->assertEquals(43, \strlen($encoded));
Expand Down