Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure csrf token is string #9365

Merged
merged 11 commits into from
Jan 18, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
handle request php://input
datlechin authored and paulbalandan committed Jan 18, 2025

Verified

This commit was signed with the committer’s verified signature.
jenniferarnesen Jen Jones Arnesen
commit c3afea61b9515057a84075d0c077d3508dc037ba
4 changes: 3 additions & 1 deletion system/Security/Security.php
Original file line number Diff line number Diff line change
@@ -321,7 +321,9 @@ private function getPostedToken(RequestInterface $request): ?string
if ($body !== '') {
$json = json_decode($body);
if ($json !== null && json_last_error() === JSON_ERROR_NONE) {
return $json->{$this->config->tokenName} ?? null;
$tokenValue = $json->{$this->config->tokenName} ?? null;

return is_string($tokenValue) ? $tokenValue : null;
}

parse_str($body, $parsed);
30 changes: 30 additions & 0 deletions tests/system/Security/SecurityTest.php
Original file line number Diff line number Diff line change
@@ -342,4 +342,34 @@ public function testGetPostedTokenReturnsNullWhenMaliciousData(): void

$this->assertNull($method($request));
}

public function testGetPostedTokenReturnsTokenFromJsonInput(): void
{
$_POST = [];
$jsonBody = json_encode(['csrf_test_name' => '8b9218a55906f9dcc1dc263dce7f005a']);
$request = $this->createIncomingRequest()->setBody($jsonBody);
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');

$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method($request));
}

public function testGetPostedTokenReturnsTokenFromFormEncodedInput(): void
{
$_POST = [];
$formBody = 'csrf_test_name=8b9218a55906f9dcc1dc263dce7f005a';
$request = $this->createIncomingRequest()->setBody($formBody);
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');

$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method($request));
}

public function testGetPostedTokenReturnsNullFromMaliciousJsonInput(): void
{
$_POST = [];
$maliciousJson = json_encode(['csrf_test_name' => ['malicious' => 'data']]);
$request = $this->createIncomingRequest()->setBody($maliciousJson);
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');

$this->assertNull($method($request));
}
}