Skip to content

Commit

Permalink
Ensure numeric type of iat and nbf parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaruesweg committed Sep 8, 2022
1 parent 018dfc4 commit 71b2329
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/JWT.php
Expand Up @@ -142,19 +142,31 @@ public static function decode(

// Check the nbf if it is defined. This is the time that the
// token can actually be used. If it's not yet that time, abort.
if (isset($payload->nbf) && $payload->nbf > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . \date(DateTime::ISO8601, $payload->nbf)
);
if (isset($payload->nbf)) {
if (!is_int($payload->nbf)) {
throw new UnexpectedValueException('The property nbf must be of type integer.');
}

if ($payload->nbf > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . \date(DateTime::ISO8601, $payload->nbf)
);
}
}

// Check that this token has been created before 'now'. This prevents
// using tokens that have been created for later use (and haven't
// correctly used the nbf claim).
if (isset($payload->iat) && $payload->iat > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . \date(DateTime::ISO8601, $payload->iat)
);
if (isset($payload->iat)) {
if (!is_int($payload->iat)) {
throw new UnexpectedValueException('The property iat must be of type integer.');
}

if ($payload->iat > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
'Cannot handle token prior to ' . \date(DateTime::ISO8601, $payload->iat)
);
}
}

// Check if this token has expired.
Expand Down Expand Up @@ -194,6 +206,12 @@ public static function encode(
if (isset($head) && \is_array($head)) {
$header = \array_merge($head, $header);
}
if (isset($payload['nbf']) && !is_int($payload['nbf'])) {
throw new UnexpectedValueException('The property nbf must be an integer containing a unix timestamp.');
}
if (isset($payload['iat']) && !is_int($payload['iat'])) {
throw new UnexpectedValueException('The property nbf must be an integer containing a unix timestamp.');
}
$segments = [];
$segments[] = static::urlsafeB64Encode((string) static::jsonEncode($header));
$segments[] = static::urlsafeB64Encode((string) static::jsonEncode($payload));
Expand Down

0 comments on commit 71b2329

Please sign in to comment.