Skip to content

Commit 3f61632

Browse files
eltharinfabpot
authored andcommitted
[Security] check token in payload instead just request
1 parent 9ad65b9 commit 3f61632

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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
}

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)