Skip to content

Commit

Permalink
handle request php://input
Browse files Browse the repository at this point in the history
  • Loading branch information
datlechin committed Jan 14, 2025
1 parent 5bbe8bc commit 523db39
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
3 changes: 2 additions & 1 deletion system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,9 @@ private function getPostedToken(RequestInterface $request): ?string
}

parse_str($body, $parsed);
$tokenValue = $parsed[$this->config->tokenName] ?? null;

return $parsed[$this->config->tokenName] ?? null;
return is_string($tokenValue) ? $tokenValue : null;
}

return null;
Expand Down
46 changes: 24 additions & 22 deletions tests/system/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public function testGetters(): void
$this->assertIsBool($security->shouldRedirect());
}

public function testGetPostedTokenReturnsTokenWhenValid(): void
public function testGetPostedTokenReturnsTokenFromPost(): void
{
$_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a';
$request = $this->createIncomingRequest();
Expand All @@ -325,25 +325,16 @@ public function testGetPostedTokenReturnsTokenWhenValid(): void
$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method($request));
}

public function testGetPostedTokenReturnsNullWhenEmpty(): void
public function testGetPostedTokenReturnsTokenFromHeader(): void
{
$_POST = [];
$request = $this->createIncomingRequest();
$request = $this->createIncomingRequest()->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005a');
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');

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

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

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

public function testGetPostedTokenReturnsTokenFromJsonInput(): void
public function testGetPostedTokenReturnsTokenFromJsonBody(): void
{
$_POST = [];
$jsonBody = json_encode(['csrf_test_name' => '8b9218a55906f9dcc1dc263dce7f005a']);
Expand All @@ -353,7 +344,7 @@ public function testGetPostedTokenReturnsTokenFromJsonInput(): void
$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method($request));
}

public function testGetPostedTokenReturnsTokenFromFormEncodedInput(): void
public function testGetPostedTokenReturnsTokenFromFormBody(): void
{
$_POST = [];
$formBody = 'csrf_test_name=8b9218a55906f9dcc1dc263dce7f005a';
Expand All @@ -363,13 +354,24 @@ public function testGetPostedTokenReturnsTokenFromFormEncodedInput(): void
$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method($request));
}

public function testGetPostedTokenReturnsNullFromMaliciousJsonInput(): void
public function testGetPostedTokenReturnsNullForInvalidInputs(): 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));
$method = $this->getPrivateMethodInvoker($this->createMockSecurity(), 'getPostedToken');
$testCases = [
'empty_post' => $this->createIncomingRequest(),
'malicious_post' => $this->createIncomingRequest()->setGlobal('post', ['csrf_test_name' => ['malicious' => 'data']]),
'empty_header' => $this->createIncomingRequest()->setHeader('X-CSRF-TOKEN', ''),
'malicious_json' => $this->createIncomingRequest()->setBody(json_encode(['csrf_test_name' => ['malicious' => 'data']])),
'invalid_json' => $this->createIncomingRequest()->setBody('{invalid json}'),
'missing_token_in_body' => $this->createIncomingRequest()->setBody('other=value&another=test'),
'malicious_form' => $this->createIncomingRequest()->setBody('csrf_test_name[]=malicious'),
];

foreach ($testCases as $case => $request) {
$this->assertNull(
$method($request),
"Failed asserting that {$case} returns null"
);
}
}
}

0 comments on commit 523db39

Please sign in to comment.