Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle non-array JSON in validation #8288

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions system/HTTP/Exceptions/HTTPException.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,15 @@ public static function forInvalidSameSiteSetting(string $samesite)
{
return new static(lang('Security.invalidSameSiteSetting', [$samesite]));
}

/**
* Thrown when the JSON format is not supported.
* This is specifically for cases where data validation is expected to work with key-value structures.
*
* @return HTTPException
*/
public static function forUnsupportedJSONFormat()
{
return new static(lang('HTTP.unsupportedJSONFormat'));
}
}
1 change: 1 addition & 0 deletions system/Language/en/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// IncomingRequest
'invalidNegotiationType' => '"{0}" is not a valid negotiation type. Must be one of: media, charset, encoding, language.',
'invalidJSON' => 'Failed to parse JSON string. Error: {0}',
'unsupportedJSONFormat' => 'The provided JSON format is not supported.',

// Message
'invalidHTTPProtocol' => 'Invalid HTTP Protocol Version: {0}',
Expand Down
5 changes: 5 additions & 0 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Validation;

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

if (! is_array($this->data)) {
throw HTTPException::forUnsupportedJSONFormat();
}

return $this;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,25 @@ public function testJsonInputInvalid(): void
->run();
}

public function testJsonInputNotKeyValue(): void
{
$this->expectException(HTTPException::class);
$this->expectExceptionMessage('The provided JSON format is not supported.');

$config = new App();
$json = '4';
$request = new IncomingRequest($config, new SiteURI($config), $json, new UserAgent());
$request->setHeader('Content-Type', 'application/json');

$rules = [
'role' => 'if_exist|max_length[5]',
];
$this->validation
->withRequest($request->withMethod('POST'))
->setRules($rules)
->run();
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6466
*/
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Message Changes
***************

- Added ``HTTP.invalidJSON`` error message.
- Added ``HTTP.unsupportedJSONFormat`` error message.

*******
Changes
Expand Down
Loading