Skip to content

Commit c9c16e9

Browse files
committed
bug #57488 [Security] check token in payload instead just request (eltharin)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Security] check token in payload instead just request | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Replace request by getPayload for allow token to ba passed in Json for an API call Commits ------- a94fe82 [Security] check token in payload instead just request
2 parents ab8f354 + a94fe82 commit c9c16e9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
4646
foreach ($attributes as $attribute) {
4747
$id = $this->getTokenId($attribute->id, $request, $arguments);
4848

49-
if (!$this->csrfTokenManager->isTokenValid(new CsrfToken($id, $request->request->getString($attribute->tokenKey)))) {
49+
if (!$this->csrfTokenManager->isTokenValid(new CsrfToken($id, $request->getPayload()->getString($attribute->tokenKey)))) {
5050
throw new InvalidCsrfTokenException('Invalid CSRF token.');
5151
}
5252
}

src/Symfony/Component/Security/Http/Tests/EventListener/IsCsrfTokenValidAttributeListenerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,28 @@ public function testIsCsrfTokenValidCalledCorrectly()
8888
$listener->onKernelControllerArguments($event);
8989
}
9090

91+
public function testIsCsrfTokenValidCalledCorrectlyInPayload()
92+
{
93+
$request = new Request(server: ['headers' => ['content-type' => 'application/json']], content: json_encode(['_token' => 'bar']));
94+
95+
$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
96+
$csrfTokenManager->expects($this->once())
97+
->method('isTokenValid')
98+
->with(new CsrfToken('foo', 'bar'))
99+
->willReturn(true);
100+
101+
$event = new ControllerArgumentsEvent(
102+
$this->createMock(HttpKernelInterface::class),
103+
[new IsCsrfTokenValidAttributeMethodsController(), 'withDefaultTokenKey'],
104+
[],
105+
$request,
106+
null
107+
);
108+
109+
$listener = new IsCsrfTokenValidAttributeListener($csrfTokenManager);
110+
$listener->onKernelControllerArguments($event);
111+
}
112+
91113
public function testIsCsrfTokenValidCalledCorrectlyWithCustomExpressionId()
92114
{
93115
$request = new Request(query: ['id' => '123'], request: ['_token' => 'bar']);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy