Skip to content
Merged
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
25 changes: 25 additions & 0 deletions security/csrf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,27 @@ array, the attribute is ignored for that request, and no CSRF validation occurs:
// ... delete the object
}

You can also choose where the CSRF token is read from using the ``tokenSource`` parameter
This is a bitfield allowing you to combine these sources:

* ``IsCsrfTokenValid::SOURCE_PAYLOAD`` (default): request payload (POST body / json)
* ``IsCsrfTokenValid::SOURCE_QUERY``: query string
* ``IsCsrfTokenValid::SOURCE_HEADER``: request headers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* ``IsCsrfTokenValid::SOURCE_HEADER``: request headers
* ``IsCsrfTokenValid::SOURCE_HEADER``: request header

As it checks only one no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Fixed while merging. Thanks!


Example::

#[IsCsrfTokenValid(
'delete-item',
tokenKey: 'token',
tokenSource: IsCsrfTokenValid::SOURCE_PAYLOAD | IsCsrfTokenValid::SOURCE_QUERY
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion, but perhaps overkill

Document one simple source and one with combination?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it "as is" because I think it's simple enough to understand it. Thanks.

)]
public function delete(Post $post): Response
{
// ... delete the object
}

The token will be checked in each selected source, and validation fails if none match.

.. versionadded:: 7.1

The :class:`Symfony\\Component\\Security\\Http\\Attribute\\IsCsrfTokenValid`
Expand All @@ -330,6 +351,10 @@ array, the attribute is ignored for that request, and no CSRF validation occurs:

The ``methods`` parameter was introduced in Symfony 7.3.

.. versionadded:: 7.4

The ``tokenSource`` parameter was introduced in Symfony 7.4.

CSRF Tokens and Compression Side-Channel Attacks
------------------------------------------------

Expand Down