-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[HttpFoundation] Add #[IsSignatureValid] attribute
#60395
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
Merged
fabpot
merged 1 commit into
symfony:7.4
from
santysisi:feature/verify-signature-attribute
Sep 14, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,9 +11,13 @@ | |
|
|
||
| namespace Symfony\Component\HttpFoundation\Exception; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpKernel\Attribute\WithHttpStatus; | ||
|
|
||
| /** | ||
| * @author Kevin Bond <[email protected]> | ||
| */ | ||
| #[WithHttpStatus(Response::HTTP_FORBIDDEN)] | ||
| final class ExpiredSignedUriException extends SignedUriException | ||
| { | ||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,9 +11,13 @@ | |
|
|
||
| namespace Symfony\Component\HttpFoundation\Exception; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpKernel\Attribute\WithHttpStatus; | ||
|
|
||
| /** | ||
| * @author Kevin Bond <[email protected]> | ||
| */ | ||
| #[WithHttpStatus(Response::HTTP_NOT_FOUND)] | ||
| abstract class SignedUriException extends \RuntimeException implements ExceptionInterface | ||
| { | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
src/Symfony/Component/HttpKernel/Attribute/IsSignatureValid.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\HttpKernel\Attribute; | ||
|
|
||
| /** | ||
| * Validates the request signature for specific HTTP methods. | ||
| * | ||
| * This class determines whether a request's signature should be validated | ||
| * based on the configured HTTP methods. If the request method matches one | ||
| * of the specified methods (or if no methods are specified), the signature | ||
| * is checked. | ||
| * | ||
| * If the signature is invalid, a {@see \Symfony\Component\HttpFoundation\Exception\SignedUriException} | ||
| * is thrown during validation. | ||
| * | ||
| * @author Santiago San Martin <[email protected]> | ||
| */ | ||
| #[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)] | ||
| final class IsSignatureValid | ||
| { | ||
| /** @var string[] */ | ||
| public readonly array $methods; | ||
| public readonly ?string $signer; | ||
|
|
||
| /** | ||
| * @param string[]|string $methods HTTP methods that require signature validation. An empty array means that no method filtering is done | ||
| * @param string $signer The ID of the UriSigner service to use for signature validation. Defaults to 'uri_signer' | ||
| */ | ||
| public function __construct( | ||
| array|string $methods = [], | ||
| ?string $signer = null, | ||
| ) { | ||
| $this->methods = (array) $methods; | ||
| $this->signer = $signer; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Symfony/Component/HttpKernel/EventListener/IsSignatureValidAttributeListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\HttpKernel\EventListener; | ||
|
|
||
| use Psr\Container\ContainerInterface; | ||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
| use Symfony\Component\HttpFoundation\UriSigner; | ||
| use Symfony\Component\HttpKernel\Attribute\IsSignatureValid; | ||
| use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; | ||
| use Symfony\Component\HttpKernel\KernelEvents; | ||
|
|
||
| /** | ||
| * Handles the IsSignatureValid attribute. | ||
| * | ||
| * @author Santiago San Martin <[email protected]> | ||
| */ | ||
| class IsSignatureValidAttributeListener implements EventSubscriberInterface | ||
| { | ||
| public function __construct( | ||
| private readonly UriSigner $uriSigner, | ||
| private readonly ContainerInterface $container, | ||
| ) { | ||
| } | ||
|
|
||
| public function onKernelControllerArguments(ControllerArgumentsEvent $event): void | ||
| { | ||
| if (!$attributes = $event->getAttributes(IsSignatureValid::class)) { | ||
| return; | ||
| } | ||
|
|
||
| $request = $event->getRequest(); | ||
| foreach ($attributes as $attribute) { | ||
| $methods = array_map('strtoupper', $attribute->methods); | ||
| if ($methods && !\in_array($request->getMethod(), $methods, true)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (null === $attribute->signer) { | ||
| $this->uriSigner->verify($request); | ||
| continue; | ||
| } | ||
|
|
||
| $signer = $this->container->get($attribute->signer); | ||
| if (!$signer instanceof UriSigner) { | ||
| throw new \LogicException(\sprintf('The service "%s" is not an instance of "%s".', $attribute->signer, UriSigner::class)); | ||
| } | ||
|
|
||
| $signer->verify($request); | ||
| } | ||
| } | ||
|
|
||
| public static function getSubscribedEvents(): array | ||
| { | ||
| return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 30]]; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the phpdoc
@var string[]to properly document the public API