-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
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.
+503
−1
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
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
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
<?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\UriSigner; | ||
|
||
/** | ||
* 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 <sanmartindev@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)] | ||
final class IsSignatureValid | ||
{ | ||
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 = 'uri_signer', | ||
) { | ||
if (!method_exists(UriSigner::class, 'verify')) { | ||
throw new \LogicException('The `IsSignatureValid` attribute requires symfony/http-foundation >= 7.3.'); | ||
} | ||
|
||
$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
45 changes: 45 additions & 0 deletions
45
src/Symfony/Component/Security/Http/DependencyInjection/UriSignerLocatorPass.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 <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\DependencyInjection; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpFoundation\UriSigner; | ||
use Symfony\Component\Security\Http\EventListener\IsSignatureValidAttributeListener; | ||
|
||
/** | ||
* Registers all UriSigner services in a service locator and injects it into the IsSignatureValidAttributeListener for dynamic signer resolution. | ||
* | ||
* @author Santiago San Martin <sanmartindev@gmail.com> | ||
*/ | ||
class UriSignerLocatorPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$locateableServices = []; | ||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if (UriSigner::class === $definition->getClass()) { | ||
$locateableServices[$id] = new Reference($id); | ||
} | ||
} | ||
|
||
8000 | $container | |
->register('controller.is_signature_valid_attribute_listener', IsSignatureValidAttributeListener::class) | ||
->addTag('kernel.event_subscriber') | ||
->setBindings([ | ||
ContainerInterface::class => ServiceLocatorTagPass::register($container, $locateableServices), | ||
]); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
<?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 Psr\Container\ContainerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\UriSigner; | ||
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; | ||
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 ContainerInterface $container, | ||
) { | ||
} | ||
|
||
public function onKernelControllerArguments(ControllerArgumentsEvent $event): void | ||
{ | ||
if (!\is_array($attributes = $event->getAttributes(IsSignatureValid::class) ?? null)) { | ||
return; | ||
} | ||
|
||
$request = $event->getRequest(); | ||
foreach ($attributes as $attribute) { | ||
$methods = array_map('strtoupper', $attribute->methods); | ||
if ($methods && !\in_array($request->getMethod(), $methods, true)) { | ||
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]]; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Symfony/Component/Security/Http/Tests/DependencyInjection/UriSignerLocatorPassTest.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,46 @@ | ||
<?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\Tests\DependencyInjection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\HttpFoundation\UriSigner; | ||
use Symfony\Component\Security\Http\DependencyInjection\UriSignerLocatorPass; | ||
|
||
class UriSignerLocatorPassTest extends TestCase | ||
{ | ||
public function testSetUriSignerLocator() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('controller.is_signature_valid_attribute_listener'); | ||
|
||
$container->register('UriSignerOne')->setClass(UriSigner::class); | ||
$container->register('UriSignerTwo')->setClass(UriSigner::class); | ||
$container->register('UriSignerThree')->setClass(UriSigner::class); | ||
$container->register('One')->setClass('Foo'); | ||
$container->register('Two')->setClass('Bar'); | ||
$container->register('Three')->setClass('Baz'); | ||
|
||
$pass = new UriSignerLocatorPass(); | ||
$pass->process($container); | ||
|
||
$id = (string) $container->getDefinition('controller.is_signature_valid_attribute_listener')->getBindings()[ContainerInterface::class]->getValues()[0]; | ||
$arguments = $container->getDefinition($id)->getArguments()[0]; | ||
|
||
$this->assertCount(3, $arguments); | ||
$this->assertArrayHasKey('UriSignerOne', $arguments); | ||
$this->assertArrayHasKey('UriSignerTwo', $arguments); | ||
$this->assertArrayHasKey('UriSignerThree', $arguments); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.