Skip to content

Commit 0361d92

Browse files
authored
fix: preserve JSON request body when CSRF token is not in it (#10620)
* fix: preserve JSON request body when CSRF token is not in it * refactor: optimize CSRF token removal in request body by restructuring JSON and form-encoded checks
1 parent 8fd3fdf commit 0361d92

3 files changed

Lines changed: 51 additions & 11 deletions

File tree

‎system/Security/Security.php‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -295,23 +295,24 @@ private function removeTokenInRequest(IncomingRequest $request): void
295295
try {
296296
$json = json_decode($body, flags: JSON_THROW_ON_ERROR);
297297
} catch (JsonException) {
298-
$json = null;
299-
}
298+
// The body is not valid JSON - check for form-encoded data.
299+
parse_str($body, $result);
300300

301-
if (is_object($json)) {
302-
if (property_exists($json, $tokenName)) {
303-
unset($json->{$tokenName});
304-
$request->setBody(json_encode($json));
301+
if (! array_key_exists($tokenName, $result)) {
302+
return;
305303
}
306304

305+
unset($result[$tokenName]);
306+
$request->setBody(http_build_query($result));
307+
307308
return;
308309
}
309310

310-
// If the token is found in form-encoded data, we can safely remove it.
311-
parse_str($body, $result);
312-
313-
unset($result[$tokenName]);
314-
$request->setBody(http_build_query($result));
311+
// Valid JSON never falls through to form-encoded processing.
312+
if (is_object($json) && property_exists($json, $tokenName)) {
313+
unset($json->{$tokenName});
314+
$request->setBody(json_encode($json));
315+
}
315316
}
316317

317318
private function getPostedToken(IncomingRequest $request): ?string

‎tests/system/Security/SecurityTest.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,41 @@ public function testCsrfVerifyHeaderWithJsonBodyStripsTokenFromBody(): void
237237
$this->assertSame('{"foo":"bar"}', $request->getBody());
238238
}
239239

240+
#[DataProvider('provideCsrfVerifyPreservesNonObjectJsonBody')]
241+
public function testCsrfVerifyPreservesNonObjectJsonBody(string $body): void
242+
{
243+
service('superglobals')
244+
->setServer('REQUEST_METHOD', 'POST')
245+
->setCookie('csrf_cookie_name', self::CORRECT_CSRF_HASH);
246+
247+
$security = $this->createMockSecurity();
248+
$request = $this->createIncomingRequest();
249+
250+
$request->setHeader('X-CSRF-TOKEN', self::CORRECT_CSRF_HASH);
251+
$request->setBody($body);
252+
253+
$this->assertInstanceOf(Security::class, $security->verify($request));
254+
$this->assertSame($body, $request->getBody());
255+
}
256+
257+
/**
258+
* @return iterable<string, array{string}>
259+
*/
260+
public static function provideCsrfVerifyPreservesNonObjectJsonBody(): iterable
261+
{
262+
yield 'JSON array' => ['[{"foo":"bar"}]'];
263+
264+
yield 'JSON array of scalars' => ['[1, 2, 3]'];
265+
266+
yield 'JSON number' => ['123'];
267+
268+
yield 'JSON boolean' => ['true'];
269+
270+
yield 'JSON null' => ['null'];
271+
272+
yield 'JSON string' => ['"foo"'];
273+
}
274+
240275
public function testCsrfVerifyPutBodyThrowsExceptionOnNoMatch(): void
241276
{
242277
service('superglobals')

‎user_guide_src/source/changelogs/v4.7.6.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **Security:** Fixed a bug where a valid JSON request body that is not an object
34+
(for example a JSON array) was corrupted after CSRF verification when the
35+
token was sent via the ``X-CSRF-TOKEN`` header.
36+
3337
See the repo's
3438
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
3539
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)