Skip to content

Commit 530907b

Browse files
committed
[HttpFoundation] Add documentation for #[IsSignatureValid] attribute with usage examples and options
1 parent 3e4ebb5 commit 530907b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

routing.rst

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,6 +3151,88 @@ If you need to know the reason why a signed URI is invalid, you can use the
31513151
Support for :doc:`Symfony Clock </components/clock>` in ``UriSigner`` was
31523152
introduced in Symfony 7.3.
31533153

3154+
Another way to validate incoming requests is to use the ``#[IsSignatureValid]`` attribute.
3155+
3156+
In the following example, all incoming requests to this controller action will be verified for
3157+
a valid signature. If the signature is missing or invalid,
3158+
a ``SignedUriException`` will be thrown::
3159+
3160+
.. code-block:: php-attributes
3161+
3162+
// src/Controller/SomeController.php
3163+
// ...
3164+
3165+
use App\Security\Attribute\IsSignatureValid;
3166+
3167+
#[IsSignatureValid]
3168+
public function someAction(): Response
3169+
{
3170+
// ...
3171+
}
3172+
3173+
To restrict signature validation to specific HTTP methods,
3174+
use the ``methods`` argument. This can be a string or an array of methods::
3175+
3176+
// Only validate POST requests
3177+
#[IsSignatureValid(methods: 'POST')]
3178+
public function createItem(): Response
3179+
{
3180+
// ...
3181+
}
3182+
3183+
// Validate both POST and PUT requests
3184+
#[IsSignatureValid(methods: ['POST', 'PUT'])]
3185+
public function updateItem(): Response
3186+
{
3187+
// ...
3188+
}
3189+
3190+
You can also use a custom signer by specifying the signer argument,
3191+
which must refer to a service that extends ``UriSigner`` or is an instance of it::
3192+
3193+
// src/Controller/SomeController.php
3194+
// ...
3195+
3196+
use App\Security\Attribute\IsSignatureValid;
3197+
3198+
// Use a custom URI signer service
3199+
#[IsSignatureValid(signer: 'my_custom_signer_service')]
3200+
public function signedAction(): Response
3201+
{
3202+
// ...
3203+
}
3204+
3205+
You can also apply ``#[IsSignatureValid]`` at the controller class level.
3206+
This way, all actions within the controller will automatically
3207+
be protected by signature validation::
3208+
3209+
// src/Controller/SecureController.php
3210+
// ...
3211+
3212+
use App\Security\Attribute\IsSignatureValid;
3213+
3214+
#[IsSignatureValid]
3215+
class SecureController extends AbstractController
3216+
{
3217+
public function index(): Response
3218+
{
3219+
// ...
3220+
}
3221+
3222+
public function submit(): Response
3223+
{
3224+
// ...
3225+
}
3226+
}
3227+
3228+
3229+
This attribute provides a declarative way to enforce request signature validation directly
3230+
at the controller level, helping to keep your security logic consistent and maintainable.
3231+
3232+
.. versionadded:: 7.4
3233+
3234+
The ``#[IsSignatureValid]`` attribute was introduced in Symfony 7.4.
3235+
31543236
Troubleshooting
31553237
---------------
31563238

0 commit comments

Comments
 (0)