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

JSON Cast exception test cases #1911

Merged
merged 9 commits into from
Apr 7, 2019
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
6 changes: 6 additions & 0 deletions system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public function __get(string $key)
* @param null $value
*
* @return $this
* @throws \Exception
*/
public function __set(string $key, $value = null)
{
Expand Down Expand Up @@ -349,6 +350,11 @@ public function __set(string $key, $value = null)
if (($castTo === 'json' || $castTo === 'json-array') && function_exists('json_encode'))
{
$value = json_encode($value);

if (json_last_error() !== JSON_ERROR_NONE)
{
throw CastException::forInvalidJsonFormatException(json_last_error());
}
}
}

Expand Down
90 changes: 89 additions & 1 deletion tests/system/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodeIgniter;

use CodeIgniter\Exceptions\CastException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Test\ReflectionHelper;
use Tests\Support\SomeEntity;
Expand Down Expand Up @@ -430,6 +431,93 @@ public function testCastAsJSONArray()
$this->assertEquals($data, $entity->eleventh);
}

public function testCastAsJSONErrorDepth()
{
$entity = $this->getCastEntity();

// Create array with depth 513 to get depth error
$array = [];
$value = "test value";
$keys = rtrim(str_repeat('test.', 513), '.');
$keys = explode(".", $keys);
$current = &$array;
foreach ($keys as $key)
{
$current = &$current[$key];
}
$current = $value;

$this->expectException(CastException::class);
$this->expectExceptionMessage('Maximum stack depth exceeded');

$entity->tenth = $array;
$this->getPrivateProperty($entity, 'tenth');
}

public function testCastAsJSONErrorUTF8()
{
$entity = $this->getCastEntity();

$this->expectException(CastException::class);
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');

$entity->tenth = "\xB1\x31";
$this->getPrivateProperty($entity, 'tenth');
}

public function testCastAsJSONSyntaxError()
{
$entity = new Entity();

$method = $this->getPrivateMethodInvoker($entity,'castAsJson');

$this->expectException(CastException::class);
$this->expectExceptionMessage('Syntax error, malformed JSON');

$method("{ this is bad string", true);
}

public function testCastAsJSONAnotherErrorDepth()
{
$entity = new Entity();

$method = $this->getPrivateMethodInvoker($entity,'castAsJson');

$this->expectException(CastException::class);
$this->expectExceptionMessage('Maximum stack depth exceeded');

$string = '{'.str_repeat('"test":{', 513).'"test":"value"'.str_repeat('}', 513).'}';

$method($string, true);
}

public function testCastAsJSONControlCharCheck()
{
$entity = new Entity();

$method = $this->getPrivateMethodInvoker($entity,'castAsJson');

$this->expectException(CastException::class);
$this->expectExceptionMessage('Unexpected control character found');

$string = "{\n\t\"property1\": \"The quick brown fox\njumps over the lazy dog\",\n\t\"property2\":\"value2\"\n}";

$method($string, true);
}

public function testCastAsJSONStateMismatch()
{
$entity = new Entity();

$method = $this->getPrivateMethodInvoker($entity,'castAsJson');

$this->expectException(CastException::class);
$this->expectExceptionMessage('Underflow or the modes mismatch');

$string = '[{"name":"jack","product_id":"1234"]';

$method($string, true);
}
//--------------------------------------------------------------------

public function testAsArray()
Expand Down Expand Up @@ -648,7 +736,7 @@ protected function getCastNullableEntity()
'casts' => [
'string_null' => '?string',
'string_empty' => 'string',
'integner_null' => '?integer',
'integer_null' => '?integer',
'integer_0' => 'integer',
],
'dates' => [],
Expand Down