Skip to content

Commit 5bbe8bc

Browse files
committed
handle request php://input
1 parent 65c7240 commit 5bbe8bc

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

system/Security/Security.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ private function getPostedToken(RequestInterface $request): ?string
321321
if ($body !== '') {
322322
$json = json_decode($body);
323323
if ($json !== null && json_last_error() === JSON_ERROR_NONE) {
324-
return $json->{$this->config->tokenName} ?? null;
324+
$tokenValue = $json->{$this->config->tokenName} ?? null;
325+
326+
return is_string($tokenValue) ? $tokenValue : null;
325327
}
326328

327329
parse_str($body, $parsed);

tests/system/Security/SecurityTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,34 @@ public function testGetPostedTokenReturnsNullWhenMaliciousData(): void
342342

343343
$this->assertNull($method($request));
344344
}
345+
346+
public function testGetPostedTokenReturnsTokenFromJsonInput(): void
347+
{
348+
$_POST = [];
349+
$jsonBody = json_encode(['csrf_test_name' => '8b9218a55906f9dcc1dc263dce7f005a']);
350+
$request = $this->createIncomingRequest()->setBody($jsonBody);
351+
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');
352+
353+
$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method($request));
354+
}
355+
356+
public function testGetPostedTokenReturnsTokenFromFormEncodedInput(): void
357+
{
358+
$_POST = [];
359+
$formBody = 'csrf_test_name=8b9218a55906f9dcc1dc263dce7f005a';
360+
$request = $this->createIncomingRequest()->setBody($formBody);
361+
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');
362+
363+
$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method($request));
364+
}
365+
366+
public function testGetPostedTokenReturnsNullFromMaliciousJsonInput(): void
367+
{
368+
$_POST = [];
369+
$maliciousJson = json_encode(['csrf_test_name' => ['malicious' => 'data']]);
370+
$request = $this->createIncomingRequest()->setBody($maliciousJson);
371+
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');
372+
373+
$this->assertNull($method($request));
374+
}
345375
}

0 commit comments

Comments
 (0)