Skip to content

[HttpFoundation] Add documentation for #[IsSignatureValid] attribute #21041

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
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
98 changes: 98 additions & 0 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3031,6 +3031,104 @@ If you need to know the reason why a signed URI is invalid, you can use the
Support for :doc:`Symfony Clock </components/clock>` in ``UriSigner`` was
introduced in Symfony 7.3.

Another way to validate incoming requests is to use the ``#[IsSignatureValid]`` attribute.

In the following example, all incoming requests to this controller action will be verified for
a valid signature. If the signature is missing or invalid,
a **404 Not Found** response will be returned by default:

.. code-block:: php-attributes

// src/Controller/SomeController.php
// ...

use App\Security\Attribute\IsSignatureValid;

#[IsSignatureValid]
public function someAction(): Response
{
// ...
}


You can customize the failure behavior by changing the HTTP status code returned
when validation fails, using the ``validationFailedStatusCode`` argument::


// src/Controller/SomeController.php
// ...

use App\Security\Attribute\IsSignatureValid;

#[IsSignatureValid(validationFailedStatusCode: 401)]
public function someAction(): Response
{
// ...
}

To restrict signature validation to specific HTTP methods,
use the ``methods`` argument. This can be a string or an array of methods::

// Only validate POST requests
#[IsSignatureValid(methods: 'POST')]
public function createItem(): Response
{
// ...
}

// Validate both POST and PUT requests
#[IsSignatureValid(methods: ['POST', 'PUT'])]
public function updateItem(): Response
{
// ...
}

If you prefer to throw an exception instead of returning a response,
pass ``throw: true``. This is useful when you want to handle the failure globally
(e.g., via an exception listener)::

// src/Controller/SomeController.php
// ...

use App\Security\Attribute\IsSignatureValid;

#[IsSignatureValid(throw: true)]
public function someAction(): Response
{
// ...
}

You can also apply ``#[IsSignatureValid]`` at the controller class level.
This way, all actions within the controller will automatically
be protected by signature validation::

// src/Controller/SecureController.php
// ...

use App\Security\Attribute\IsSignatureValid;

#[IsSignatureValid]
class SecureController extends AbstractController
{
public function index(): Response
{
// ...
}

public function submit(): Response
{
// ...
}
}


This attribute provides a declarative way to enforce request signature validation directly
at the controller level, helping to keep your security logic consistent and maintainable.

.. versionadded:: 7.4

The ``#[IsSignatureValid]`` attribute was introduced in Symfony 7.4.

Troubleshooting
---------------

Expand Down
Loading