Skip to content
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* Add `assertEmailAddressNotContains()` to the `MailerAssertionsTrait`
* Add `framework.type_info.aliases` option
* Add `KernelBrowser::getSession()`
* Add autoconfiguration tag `kernel.uri_signer` to `Symfony\Component\HttpFoundation\UriSigner`

7.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class UnusedTagsPass implements CompilerPassInterface
'kernel.fragment_renderer',
'kernel.locale_aware',
'kernel.reset',
'kernel.uri_signer',
'ldap',
'mailer.transport_factory',
'messenger.bus',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@
use Symfony\Component\HttpClient\ThrottlingHttpClient;
use Symfony\Component\HttpClient\UriTemplateHttpClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\AsTargetedValueResolver;
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\EventListener\IsSignatureValidAttributeListener;
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
use Symfony\Component\HttpKernel\Profiler\ProfilerStateChecker;
use Symfony\Component\JsonStreamer\Attribute\JsonStreamable;
Expand Down Expand Up @@ -288,6 +290,9 @@ public function load(array $configs, ContainerBuilder $container): void
if (!class_exists(RunProcessMessageHandler::class)) {
$container->removeDefinition('process.messenger.process_message_handler');
}
if (!class_exists(IsSignatureValidAttributeListener::class)) {
$container->removeDefinition('controller.is_signature_valid_attribute_listener');
}

if ($this->hasConsole()) {
$loader->load('console.php');
Expand Down Expand Up @@ -762,6 +767,8 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('mime.mime_type_guesser');
$container->registerForAutoconfiguration(LoggerAwareInterface::class)
->addMethodCall('setLogger', [new Reference('logger')]);
$container->registerForAutoconfiguration(UriSigner::class)
->addTag('kernel.uri_signer');

$container->registerAttributeForAutoconfiguration(AsEventListener::class, static function (ChildDefinition $definition, AsEventListener $attribute, \ReflectionClass|\ReflectionMethod $reflector) {
$tagAttributes = get_object_vars($attribute);
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Symfony\Component\HttpKernel\EventListener\CacheAttributeListener;
use Symfony\Component\HttpKernel\EventListener\DisallowRobotsIndexingListener;
use Symfony\Component\HttpKernel\EventListener\ErrorListener;
use Symfony\Component\HttpKernel\EventListener\IsSignatureValidAttributeListener;
use Symfony\Component\HttpKernel\EventListener\LocaleListener;
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
Expand Down Expand Up @@ -148,6 +149,13 @@
->tag('kernel.event_subscriber')
->tag('kernel.reset', ['method' => '?reset'])

->set('controller.is_signature_valid_attribute_listener', IsSignatureValidAttributeListener::class)
->args([
service('uri_signer'),
tagged_locator('kernel.uri_signer'),
])
->tag('kernel.event_subscriber')

->set('controller.helper', ControllerHelper::class)
->tag('container.service_subscriber')

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 @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate using `Request::sendHeaders()` after headers have already been sent; use a `StreamedResponse` instead
* Add `#[WithHttpStatus]` to define status codes: 404 for `SignedUriException` and 403 for `ExpiredSignedUriException`
* Add support for the `QUERY` HTTP method
* Add support for structured MIME suffix

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 src/Symfony/Component/HttpKernel/Attribute/IsSignatureValid.php
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;
Copy link
Member

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

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;
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add support for the `QUERY` HTTP method
* Deprecate implementing `__sleep/wakeup()` on kernels; use `__(un)serialize()` instead
* Deprecate implementing `__sleep/wakeup()` on data collectors; use `__(un)serialize()` instead
* Add `#[IsSignatureValid]` attribute to validate URI signatures
* Make `Profile` final and `Profiler::__sleep()` internal

7.3
Expand Down
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]];
}
}
Loading
Loading