Skip to content

Commit f2a80ed

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

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

security/csrf.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,27 @@ 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+
#[IsCsrfTokenValid(
334+
'delete-item',
335+
tokenKey: 'token',
336+
tokenSource: IsCsrfTokenValid::SOURCE_PAYLOAD | IsCsrfTokenValid::SOURCE_QUERY
337+
)]
338+
public function delete(Post $post): Response
339+
{
340+
// ... delete the object
341+
}
342+
343+
The token will be checked in each selected source, and validation fails if none match.
344+
324345
.. versionadded:: 7.1
325346

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

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

354+
.. versionadded:: 7.4
355+
356+
The ``tokenSource`` parameter was introduced in Symfony 7.4.
357+
333358
CSRF Tokens and Compression Side-Channel Attacks
334359
------------------------------------------------
335360

0 commit comments

Comments
 (0)