-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] #[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
Open
santysisi
wants to merge
1
commit into
symfony:7.4
Choose a base branch
from
santysisi:feature/verify-signature-attribute
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+507
−2
Open
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
38 changes: 38 additions & 0 deletions
38
src/Symfony/Component/Security/Http/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,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Attribute; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Attribute to ensure the request URI contains a valid signature before allowing controller execution. | ||
* | ||
* When applied, this attribute verifies that the request is signed and the signature is still valid (e.g., not expired). | ||
* Behavior can be customized to either return a specific HTTP status code or throw an exception to be handled globally. | ||
* | ||
* @author Santiago San Martin <sanmartindev@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)] | ||
final class IsSignatureValid | ||
{ | ||
/** | ||
* @param int|null $validationFailedStatusCode The HTTP status code to return if the signature is invalid. | ||
* @param bool|null $throw If true, an exception is thrown on signature failure instead of returning a response. | ||
* @param array|string $methods HTTP methods that require signature validation. | ||
*/ | ||
public function __construct( | ||
public ?int $validationFailedStatusCode = Response::HTTP_NOT_FOUND, | ||
public ?bool $throw = null, | ||
public array|string $methods = [], | ||
) { | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.4 | ||
--- | ||
|
||
* Add `#[IsSignatureValid]` attribute to validate URI signatures | ||
|
||
7.3 | ||
--- | ||
|
||
|
61 changes: 61 additions & 0 deletions
61
src/Symfony/Component/Security/Http/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,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\UriSigner; | ||
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\Security\Http\Attribute\IsSignatureValid; | ||
|
||
/** | ||
* Handles the IsSignatureValid attribute. | ||
* | ||
* @author Santiago San Martin <sanmartindev@gmail.com> | ||
*/ | ||
class IsSignatureValidAttributeListener implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private readonly UriSigner $uriSigner, | ||
) { | ||
} | ||
|
||
public function onKernelControllerArguments(ControllerArgumentsEvent $event): void | ||
{ | ||
/** @var IsSignatureValid[] $attributes */ | ||
if (!\is_array($attributes = $event->getAttributes()[IsSignatureValid::class] ?? null)) { | ||
return; | ||
} | ||
|
||
$request = $event->getRequest(); | ||
foreach ($attributes as $attribute) { | ||
$methods = \array_map('strtoupper', (array) $attribute->methods); | ||
if ($methods && !\in_array($request->getMethod(), $methods, true)) { | ||
continue; | ||
} | ||
|
||
if ($attribute->throw) { | ||
$this->uriSigner->verify($request); | ||
continue; | ||
} | ||
if (!$this->uriSigner->checkRequest($request)) { | ||
throw new HttpException($attribute->validationFailedStatusCode, 'The URI signature is invalid.'); | ||
} | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 30]]; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.