From 0dc4d0b98b52b5c3cc7c117cbee2d7de679067ce Mon Sep 17 00:00:00 2001 From: David Szkiba Date: Tue, 6 May 2025 13:49:37 +0200 Subject: [PATCH] [Security][LoginLink] Throw InvalidLoginLinkException on invalid parameters --- .../Http/LoginLink/LoginLinkHandler.php | 7 ++++++ .../Tests/LoginLink/LoginLinkHandlerTest.php | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Symfony/Component/Security/Http/LoginLink/LoginLinkHandler.php b/src/Symfony/Component/Security/Http/LoginLink/LoginLinkHandler.php index 176d316607506..02ca251106471 100644 --- a/src/Symfony/Component/Security/Http/LoginLink/LoginLinkHandler.php +++ b/src/Symfony/Component/Security/Http/LoginLink/LoginLinkHandler.php @@ -86,9 +86,16 @@ public function consumeLoginLink(Request $request): UserInterface if (!$hash = $request->get('hash')) { throw new InvalidLoginLinkException('Missing "hash" parameter.'); } + if (!is_string($hash)) { + throw new InvalidLoginLinkException('Invalid "hash" parameter.'); + } + if (!$expires = $request->get('expires')) { throw new InvalidLoginLinkException('Missing "expires" parameter.'); } + if (preg_match('/^\d+$/', $expires) !== 1) { + throw new InvalidLoginLinkException('Invalid "expires" parameter.'); + } try { $this->signatureHasher->acceptSignatureHash($userIdentifier, $expires, $hash); diff --git a/src/Symfony/Component/Security/Http/Tests/LoginLink/LoginLinkHandlerTest.php b/src/Symfony/Component/Security/Http/Tests/LoginLink/LoginLinkHandlerTest.php index 98ff60d43992c..350ecde4290a0 100644 --- a/src/Symfony/Component/Security/Http/Tests/LoginLink/LoginLinkHandlerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/LoginLink/LoginLinkHandlerTest.php @@ -240,6 +240,30 @@ public function testConsumeLoginLinkWithMissingExpiration() $linker->consumeLoginLink($request); } + public function testConsumeLoginLinkWithInvalidExpiration() + { + $user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash'); + $this->userProvider->createUser($user); + + $this->expectException(InvalidLoginLinkException::class); + $request = Request::create('/login/verify?user=weaverryan&hash=thehash&expires=%E2%80%AA1000000000%E2%80%AC'); + + $linker = $this->createLinker(); + $linker->consumeLoginLink($request); + } + + public function testConsumeLoginLinkWithInvalidHash() + { + $user = new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash'); + $this->userProvider->createUser($user); + + $this->expectException(InvalidLoginLinkException::class); + $request = Request::create('/login/verify?user=weaverryan&hash[]=an&hash[]=array&expires=1000000000'); + + $linker = $this->createLinker(); + $linker->consumeLoginLink($request); + } + private function createSignatureHash(string $username, int $expires, array $extraFields = ['emailProperty' => 'ryan@symfonycasts.com', 'passwordProperty' => 'pwhash']): string { $hasher = new SignatureHasher($this->propertyAccessor, array_keys($extraFields), 's3cret');