Skip to content

Commit 8ba7444

Browse files
committed
fix: add check if valid JSON
1 parent 99ef79b commit 8ba7444

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎system/HTTP/Exceptions/HTTPException.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ public static function forInvalidNegotiationType(string $type)
7272
return new static(lang('HTTP.invalidNegotiationType', [$type]));
7373
}
7474

75+
/**
76+
* Thrown in IncomingRequest when the json_decode() produces
77+
* an error code other than JSON_ERROR_NONE.
78+
*
79+
* @param string $error The error message
80+
*
81+
* @return static
82+
*/
83+
public static function forInvalidJSON(?string $error = null)
84+
{
85+
return new static(lang('HTTP.invalidJSON', [$error]));
86+
}
87+
7588
/**
7689
* For Message
7790
*

‎system/HTTP/IncomingRequest.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,18 @@ public function getVar($index = null, $filter = null, $flags = null)
573573
* @see http://php.net/manual/en/function.json-decode.php
574574
*
575575
* @return array|bool|float|int|stdClass|null
576+
*
577+
* @throws HTTPException When the body is invalid as JSON.
576578
*/
577579
public function getJSON(bool $assoc = false, int $depth = 512, int $options = 0)
578580
{
579-
return json_decode($this->body ?? '', $assoc, $depth, $options);
581+
$result = json_decode($this->body ?? '', $assoc, $depth, $options);
582+
583+
if (json_last_error() !== JSON_ERROR_NONE) {
584+
throw HTTPException::forInvalidJSON(json_last_error_msg());
585+
}
586+
587+
return $result;
580588
}
581589

582590
/**

‎system/Language/en/HTTP.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
// IncomingRequest
2121
'invalidNegotiationType' => '"{0}" is not a valid negotiation type. Must be one of: media, charset, encoding, language.',
22+
'invalidJSON' => 'Failed to parse JSON string. Error: {0}',
2223

2324
// Message
2425
'invalidHTTPProtocol' => 'Invalid HTTP Protocol Version: {0}',

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,19 @@ public function testGetJSONReturnsNullFromNullBody(): void
528528
$this->assertNull($request->getJSON());
529529
}
530530

531+
public function testGetJSONWithInvalidJSONString(): void
532+
{
533+
$this->expectException(HTTPException::class);
534+
$this->expectExceptionMessage('Failed to parse JSON string. Error: Syntax error');
535+
536+
$config = new App();
537+
$config->baseURL = 'http://example.com/';
538+
$json = 'Invalid JSON string';
539+
$request = $this->createRequest($config, $json);
540+
541+
$request->getJSON();
542+
}
543+
531544
public function testCanGrabGetRawInput(): void
532545
{
533546
$rawstring = 'username=admin001&role=administrator&usepass=0';

0 commit comments

Comments
 (0)