diff --git a/README.md b/README.md index 9778c37..9252420 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,8 @@ Without an override, login requests the current URI. The provider accepts root-r The `authwaveHost` constructor argument selects the provider and defaults to `login.authwave.com`. For a self-hosted provider, supply its scheme, host and optional port, such as `https://login.example.com`. +Callback cleanup redirects use a local path without a scheme or host. The client removes the authentication response parameter while preserving the other query values and fragment. + Logging out ----------- diff --git a/src/Authenticator.php b/src/Authenticator.php index 482bd81..4b18165 100644 --- a/src/Authenticator.php +++ b/src/Authenticator.php @@ -259,10 +259,11 @@ private function completeAuth():void { } private function tidyResponseData(?string $returnQuery = null):void { - $redirectUri = new Uri((string)$this->currentUriPath); - if(!is_null($returnQuery)) { - $redirectUri = $redirectUri->withQuery($returnQuery); - } + $path = "/" . ltrim($this->currentUriPath->getPath(), "/"); + $redirectUri = (new Uri()) + ->withPath($path) + ->withQuery($returnQuery ?? $this->currentUriPath->getQuery()) + ->withFragment($this->currentUriPath->getFragment()); $this->redirectHandler->redirect( $redirectUri->withoutQueryValue(self::RESPONSE_QUERY_PARAMETER) diff --git a/test/phpunit/CallbackCleanupTest.php b/test/phpunit/CallbackCleanupTest.php new file mode 100644 index 0000000..0df0a86 --- /dev/null +++ b/test/phpunit/CallbackCleanupTest.php @@ -0,0 +1,93 @@ + */ + public static function cleanupUris():array { + return [ + "network path" => ["//evil.example/path", "/path"], + "network root" => ["//evil.example", "/"], + "absolute URI" => ["https://evil.example/path", "/path"], + "credentials" => ["https://user:pass@evil.example/path", "/path"], + "multiple leading slashes" => ["https://app.example//evil.example/path", "/evil.example/path"], + "backslash" => ["/\\evil.example/path", "/%5Cevil.example/path"], + "encoded slashes" => ["/%2F%2Fevil.example/path", "/%2F%2Fevil.example/path"], + "encoded path" => ["/members/a%2Fb", "/members/a%2Fb"], + "ordinary path" => ["/members/orders", "/members/orders"], + ]; + } + + #[DataProvider("cleanupUris")] + public function testUnsolicitedResponseRedirectsOnlyToLocalPath(string $input, string $expected):void { + $_SESSION = []; + $redirect = new class extends RedirectHandler { + public ?UriInterface $uri = null; + public function redirect(UriInterface $uri, int $code = 303):void { + $this->uri = $uri; + } + }; + $auth = new Authenticator( + "deployment_" . str_repeat("s", 32), + $input . "?keep=%7e&flag&AUTHWAVE_RESPONSE_DATA=anything#details", + redirectHandler: $redirect, + ); + + self::assertFalse($auth->isLoggedIn()); + self::assertNotNull($redirect->uri); + self::assertSame($expected . "?keep=%7e&flag#details", (string)$redirect->uri); + self::assertSame("", $redirect->uri->getScheme()); + self::assertSame("", $redirect->uri->getAuthority()); + self::assertStringStartsWith("/", (string)$redirect->uri); + self::assertFalse(str_starts_with((string)$redirect->uri, "//")); + } + + public function testAuthenticatedCleanupPreservesSavedQueryAndEncodedPath():void { + $secret = str_repeat("s", 32); + $token = new Token($secret); + $_SESSION = [SessionData::class => new SessionData($token, returnQuery: "filter=original&flag&encoded=%7e")]; + // A map-only session has no native browser session ID. + $session = new class extends GlobalSessionContainer { + public function regenerate():void {} + }; + $cipher = sodium_crypto_secretbox( + '{"id":"user-id","email":"person@example.com"}', + $token->getSecretIv()->getBytes(), $secret, + ); + $redirect = self::createMock(RedirectHandler::class); + $redirect->expects(self::once())->method("redirect")->with(self::callback( + fn(UriInterface $uri):bool => (string)$uri === "/members/a%2Fb?filter=original&flag&encoded=%7e#details" + )); + + $auth = new Authenticator( + "deployment_" . $secret, + new Uri("https://user:pass@evil.example//members/a%2Fb?AUTHWAVE_RESPONSE_DATA=" + . urlencode(base64_encode($cipher)) . "#details"), + session: $session, + redirectHandler: $redirect, + ); + self::assertSame("user-id", $auth->getAuthUser()->id); + } + + public function testDuplicateResponseParametersAreRemoved():void { + $_SESSION = []; + $redirect = self::createMock(RedirectHandler::class); + $redirect->expects(self::once())->method("redirect")->with(self::callback( + fn(UriInterface $uri):bool => (string)$uri === "/path?flag&next=https://evil.example" + )); + new Authenticator( + "deployment_" . str_repeat("s", 32), + "//evil.example/path?AUTHWAVE_RESPONSE_DATA=one&flag&AUTHWAVE_RESPONSE_DATA=two&next=https://evil.example", + redirectHandler: $redirect, + ); + } +}