Skip to content

Commit

Permalink
fix: IncomingRequest::getJsonVar() may cause TypeError
Browse files Browse the repository at this point in the history
See codeigniter4#5391

[TypeError]
dot_array_search(): Argument #2 ($array) must be of type array, null given, called in .../system/HTTP/IncomingRequest.php on line 540
at SYSTEMPATH/Helpers/array_helper.php:21
  • Loading branch information
kenjis committed Nov 25, 2021
1 parent 0a55082 commit 567f0c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,11 @@ public function getJsonVar(string $index, bool $assoc = false, ?int $filter = nu
{
helper('array');

$data = dot_array_search($index, $this->getJSON(true));
$json = $this->getJSON(true);
if (! is_array($json)) {
return null;
}
$data = dot_array_search($index, $json);

if ($data === null) {
return null;
Expand Down
23 changes: 23 additions & 0 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,29 @@ public function testGetVarWorksWithJsonAndGetParams()
$this->assertSame('buzz', $all['fizz']);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5391
*/
public function testGetJsonVarReturnsNullFromNullBody()
{
$config = new App();
$config->baseURL = 'http://example.com/';
$json = null;
$request = new IncomingRequest($config, new URI(), $json, new UserAgent());

$this->assertNull($request->getJsonVar('myKey'));
}

public function testgetJSONReturnsNullFromNullBody()
{
$config = new App();
$config->baseURL = 'http://example.com/';
$json = null;
$request = new IncomingRequest($config, new URI(), $json, new UserAgent());

$this->assertNull($request->getJSON());
}

public function testCanGrabGetRawInput()
{
$rawstring = 'username=admin001&role=administrator&usepass=0';
Expand Down

0 comments on commit 567f0c6

Please sign in to comment.