8000 [FrameworkBundle][HttpFoundation] Add Clock support for `UriSigner` by kbond · Pull Request #60222 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle][HttpFoundation] Add Clock support for UriSigner #60222

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 8000 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

Merged
merged 1 commit into from
Apr 17, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
->set('uri_signer', UriSigner::class)
->args([
new Parameter('kernel.secret'),
'_hash',
'_expiration',
service('clock')->nullOnInvalid(),
])
->lazy()
->alias(UriSigner::class, 'uri_signer')
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Add `EventStreamResponse` and `ServerEvent` classes to streamline server event streaming
* Add support for `valkey:` / `valkeys:` schemes for sessions
* `Request::getPreferredLanguage()` now favors a more preferred language above exactly matching a locale
* Allow `UriSigner` to use a `ClockInterface`

7.2
---
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/UriSignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\HttpFoundation\Exception\LogicException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\UriSigner;
Expand Down Expand Up @@ -199,6 +200,29 @@ public function testCheckWithUriExpiration()
$this->assertFalse($signer->check($relativeUriFromNow3));
}

public function testCheckWithUriExpirationWithClock()
{
$clock = new MockClock();
$signer = new UriSigner('foobar', clock: $clock);

$this->assertFalse($signer->check($signer->sign('http://example.com/foo', new \DateTimeImmutable('2000-01-01 00:00:00'))));
$this->assertFalse($signer->check($signer->sign('http://example.com/foo?foo=bar', new \DateTimeImmutable('2000-01-01 00:00:00'))));
$this->assertFalse($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer', new \DateTimeImmutable('2000-01-01 00:00:00'))));

$this->assertFalse($signer->check($signer->sign('http://example.com/foo', 1577836800))); // 2000-01-01
$this->assertFalse($signer->check($signer->sign('http://example.com/foo?foo=bar', 1577836800))); // 2000-01-01
$this->assertFalse($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer', 1577836800))); // 2000-01-01

$relativeUriFromNow1 = $signer->sign('http://example.com/foo', new \DateInterval('PT3S'));
$relativeUriFromNow2 = $signer->sign('http://example.com/foo?foo=bar', new \DateInterval('PT3S'));
$relativeUriFromNow3 = $signer->sign('http://example.com/foo?foo=bar&0=integer', new \DateInterval('PT3S'));
$clock->sleep(10);

$this->assertFalse($signer->check($relativeUriFromNow1));
$this->assertFalse($signer->check($relativeUriFromNow2));
$this->assertFalse($signer->check($relativeUriFromNow3));
}

public function testNonUrlSafeBase64()
{
$signer = new UriSigner('foobar');
Expand Down
11 changes: 9 additions & 2 deletions src/Symfony/Component/HttpFoundation/UriSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpFoundation;

use Psr\Clock\ClockInterface;
use Symfony\Component\HttpFoundation\Exception\LogicException;

/**
Expand All @@ -26,6 +27,7 @@
#[\SensitiveParameter] private string $secret,
private string $hashParameter = '_hash',
private string $expirationParameter = '_expiration',
private ?ClockInterface $clock = null,
) {
if (!$secret) {
throw new \InvalidArgumentException('A non-empty secret is required.');
Expand Down Expand Up @@ -109,7 +111,7 @@
}

if ($expiration = $params[$this->expirationParameter] ?? false) {
return time() < $expiration;
return $this->now()->getTimestamp() < $expiration;
}

return true;
Expand Down Expand Up @@ -153,9 +155,14 @@
}

if ($expiration instanceof \DateInterval) {
return \DateTimeImmutable::createFromFormat('U', time())->add($expiration)->format('U');
return $this->now()->add($expiration)->format('U');
}

return (string) $expiration;
}

private function now(): \DateTimeImmutable
{
return $this->clock?->now() ?? \DateTimeImmutable::createFromFormat('U', time());

Check failure on line 166 in src/Symfony/Component/HttpFoundation/UriSigner.php

View workflow job for this annotation

GitHub Actions / Psalm

FalsableReturnStatement

src/Symfony/Component/HttpFoundation/UriSigner.php:166:16: FalsableReturnStatement: The declared return type 'DateTimeImmutable' for Symfony\Component\HttpFoundation\UriSigner::now does not allow false, but the function returns 'DateTimeImmutable|false' (see https://psalm.dev/137)

Check failure on line 166 in src/Symfony/Component/HttpFoundation/UriSigner.php

View workflow job for this annotation

GitHub Actions / Psalm

FalsableReturnStatement

src/Symfony/Component/HttpFoundation/UriSigner.php:166:16: FalsableReturnStatement: The declared return type 'DateTimeImmutable' for Symfony\Component\HttpFoundation\UriSigner::now does not allow false, but the function returns 'DateTimeImmutable|false' (see https://psalm.dev/137)
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"doctrine/dbal": "^3.6|^4",
"predis/predis": "^1.1|^2.0",
"symfony/cache": "^6.4.12|^7.1.5",
"symfony/clock": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/mime": "^6.4|^7.0",
Expand Down
Loading
0