Skip to content
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
10 changes: 10 additions & 0 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ public static function decode(
if (!$payload instanceof stdClass) {
throw new UnexpectedValueException('Payload must be a JSON object');
}
if (isset($payload->iat) && !\is_numeric($payload->iat)) {
throw new UnexpectedValueException('Payload iat must be a number');
}
if (isset($payload->nbf) && !\is_numeric($payload->nbf)) {
throw new UnexpectedValueException('Payload nbf must be a number');
}
if (isset($payload->exp) && !\is_numeric($payload->exp)) {
throw new UnexpectedValueException('Payload exp must be a number');
}

$sig = static::urlsafeB64Decode($cryptob64);
if (empty($header->alg)) {
throw new UnexpectedValueException('Empty algorithm');
Expand Down
27 changes: 27 additions & 0 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,31 @@ public function testAdditionalHeaderOverrides()
$this->assertEquals('my_key_id', $headers->kid, 'key param not overridden');
$this->assertEquals('HS256', $headers->alg, 'alg param not overridden');
}

public function testDecodeExpectsIntegerIat()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Payload iat must be a number');

$payload = JWT::encode(['iat' => 'not-an-int'], 'secret', 'HS256');
JWT::decode($payload, new Key('secret', 'HS256'));
}

public function testDecodeExpectsIntegerNbf()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Payload nbf must be a number');

$payload = JWT::encode(['nbf' => 'not-an-int'], 'secret', 'HS256');
JWT::decode($payload, new Key('secret', 'HS256'));
}

public function testDecodeExpectsIntegerExp()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Payload exp must be a number');

$payload = JWT::encode(['exp' => 'not-an-int'], 'secret', 'HS256');
JWT::decode($payload, new Key('secret', 'HS256'));
}
}