Skip to content

Commit 91ba332

Browse files
datlechinpaulbalandan
authored andcommitted
fix: ensure csrf token is string
1 parent 5f8aa24 commit 91ba332

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

system/Security/Security.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private function getPostedToken(RequestInterface $request): ?string
307307
// Does the token exist in POST, HEADER or optionally php:://input - json data or PUT, DELETE, PATCH - raw data.
308308

309309
if ($tokenValue = $request->getPost($this->config->tokenName)) {
310-
return $tokenValue;
310+
return is_string($tokenValue) ? $tokenValue : null;
311311
}
312312

313313
if ($request->hasHeader($this->config->headerName)

tests/system/Security/SecurityTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Config\Security as SecurityConfig;
2626
use PHPUnit\Framework\Attributes\BackupGlobals;
2727
use PHPUnit\Framework\Attributes\Group;
28+
use ReflectionClass;
29+
use ReflectionMethod;
2830

2931
/**
3032
* @internal
@@ -49,6 +51,16 @@ private function createMockSecurity(?SecurityConfig $config = null): MockSecurit
4951
return new MockSecurity($config);
5052
}
5153

54+
private function getPostedTokenMethod(): ReflectionMethod
55+
{
56+
$reflection = new ReflectionClass(Security::class);
57+
$method = $reflection->getMethod('getPostedToken');
58+
59+
$method->setAccessible(true);
60+
61+
return $method;
62+
}
63+
5264
public function testBasicConfigIsSaved(): void
5365
{
5466
$security = $this->createMockSecurity();
@@ -315,4 +327,37 @@ public function testGetters(): void
315327
$this->assertIsString($security->getCookieName());
316328
$this->assertIsBool($security->shouldRedirect());
317329
}
330+
331+
public function testGetPostedTokenReturnsTokenWhenValid(): void
332+
{
333+
$method = $this->getPostedTokenMethod();
334+
$security = $this->createMockSecurity();
335+
336+
$_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a';
337+
$request = $this->createIncomingRequest();
338+
339+
$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $method->invoke($security, $request));
340+
}
341+
342+
public function testGetPostedTokenReturnsNullWhenEmpty(): void
343+
{
344+
$method = $this->getPostedTokenMethod();
345+
$security = $this->createMockSecurity();
346+
347+
$_POST = [];
348+
$request = $this->createIncomingRequest();
349+
350+
$this->assertNull($method->invoke($security, $request));
351+
}
352+
353+
public function testGetPostedTokenReturnsNullWhenMaliciousData(): void
354+
{
355+
$method = $this->getPostedTokenMethod();
356+
$security = $this->createMockSecurity();
357+
358+
$_POST['csrf_test_name'] = ['malicious' => 'data'];
359+
$request = $this->createIncomingRequest();
360+
361+
$this->assertNull($method->invoke($security, $request));
362+
}
318363
}

0 commit comments

Comments
 (0)