diff --git a/src/JWT.php b/src/JWT.php index 67514b29..57d92ba8 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -111,6 +111,10 @@ public static function decode( if (null === ($payload = static::jsonDecode($payloadRaw))) { throw new UnexpectedValueException('Invalid claims encoding'); } + if (is_array($payload)) { + // prevent PHP Fatal Error in edge-cases when payload is empty array + $payload = (object) $payload; + } if (!$payload instanceof stdClass) { throw new UnexpectedValueException('Payload must be a JSON object'); } @@ -355,7 +359,7 @@ public static function jsonDecode(string $input) public static function jsonEncode(array $input): string { if (PHP_VERSION_ID >= 50400) { - $json = \json_encode($input, \JSON_UNESCAPED_SLASHES|\JSON_FORCE_OBJECT); + $json = \json_encode($input, \JSON_UNESCAPED_SLASHES); } else { // PHP 5.3 only $json = \json_encode($input); diff --git a/tests/JWTTest.php b/tests/JWTTest.php index e1984b34..191e3d2c 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -330,6 +330,15 @@ public function testDecodesEmptyArrayAsObject() $this->assertEquals((object) $payload, $decoded); } + public function testDecodesArraysInJWTAsArray() + { + $key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk'; + $payload = ['foo' => [1,2,3]]; + $jwt = JWT::encode($payload, $key, 'HS256'); + $decoded = JWT::decode($jwt, new Key($key, 'HS256')); + $this->assertEquals($payload['foo'], $decoded->foo); + } + /** * @runInSeparateProcess * @dataProvider provideEncodeDecode