Skip to content

Commit 247e482

Browse files
committed
[Security] Add tokenSource parameter for CSRF token validation sources
1 parent 88b722c commit 247e482

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

security/csrf.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,29 @@ array, the attribute is ignored for that request, and no CSRF validation occurs:
321321
// ... delete the object
322322
}
323323

324+
You can also choose where the CSRF token is read from using the ``tokenSource`` parameter
325+
This is a bitfield allowing you to combine these sources:
326+
327+
* ``IsCsrfTokenValid::SOURCE_PAYLOAD`` (default): request payload (POST body / json)
328+
* ``IsCsrfTokenValid::SOURCE_QUERY``: query string
329+
* ``IsCsrfTokenValid::SOURCE_HEADER``: request headers
330+
331+
Example:
332+
333+
.. code-block:: php
334+
335+
#[IsCsrfTokenValid(
336+
'delete-item',
337+
tokenKey: 'token',
338+
tokenSource: IsCsrfTokenValid::SOURCE_PAYLOAD | IsCsrfTokenValid::SOURCE_QUERY
339+
)]
340+
public function delete(Post $post): Response
341+
{
342+
// ... delete the object
343+
}
344+
345+
The token will be checked in each selected source, in order, and validation fails if none match.
346+
324347
.. versionadded:: 7.1
325348

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

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

356+
.. versionadded:: 7.4
357+
358+
The ``tokenSource`` parameter was introduced in Symfony 7.4.
359+
333360
CSRF Tokens and Compression Side-Channel Attacks
334361
------------------------------------------------
335362

0 commit comments

Comments
 (0)