Skip to content

Commit cb4fe38

Browse files
committed
fix: Handle non-array JSON in validation
1 parent 7a38206 commit cb4fe38

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

system/HTTP/Exceptions/HTTPException.php

+11
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,15 @@ public static function forInvalidSameSiteSetting(string $samesite)
228228
{
229229
return new static(lang('Security.invalidSameSiteSetting', [$samesite]));
230230
}
231+
232+
/**
233+
* Thrown when the JSON format is not supported.
234+
* This is specifically for cases where data validation is expected to work with key-value structures.
235+
*
236+
* @return HTTPException
237+
*/
238+
public static function forUnsupportedJSONFormat()
239+
{
240+
return new static(lang('HTTP.unsupportedJSONFormat'));
241+
}
231242
}

system/Language/en/HTTP.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// IncomingRequest
2121
'invalidNegotiationType' => '"{0}" is not a valid negotiation type. Must be one of: media, charset, encoding, language.',
2222
'invalidJSON' => 'Failed to parse JSON string. Error: {0}',
23+
'unsupportedJSONFormat' => 'The provided JSON format is not supported.',
2324

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

system/Validation/Validation.php

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Validation;
1313

1414
use Closure;
15+
use CodeIgniter\HTTP\Exceptions\HTTPException;
1516
use CodeIgniter\HTTP\IncomingRequest;
1617
use CodeIgniter\HTTP\RequestInterface;
1718
use CodeIgniter\Validation\Exceptions\ValidationException;
@@ -496,6 +497,10 @@ public function withRequest(RequestInterface $request): ValidationInterface
496497
if (strpos($request->getHeaderLine('Content-Type'), 'application/json') !== false) {
497498
$this->data = $request->getJSON(true);
498499

500+
if (! is_array($this->data)) {
501+
throw HTTPException::forUnsupportedJSONFormat();
502+
}
503+
499504
return $this;
500505
}
501506

tests/system/Validation/ValidationTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,25 @@ public function testJsonInputInvalid(): void
809809
->run();
810810
}
811811

812+
public function testJsonInputNotKeyValue(): void
813+
{
814+
$this->expectException(HTTPException::class);
815+
$this->expectExceptionMessage('The provided JSON format is not supported.');
816+
817+
$config = new App();
818+
$json = '4';
819+
$request = new IncomingRequest($config, new SiteURI($config), $json, new UserAgent());
820+
$request->setHeader('Content-Type', 'application/json');
821+
822+
$rules = [
823+
'role' => 'if_exist|max_length[5]',
824+
];
825+
$this->validation
826+
->withRequest($request->withMethod('POST'))
827+
->setRules($rules)
828+
->run();
829+
}
830+
812831
/**
813832
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6466
814833
*/

0 commit comments

Comments
 (0)