Skip to content

fix: Throw exception on invalid base64 #576

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 24 additions & 5 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,22 @@ public static function decode(
throw new UnexpectedValueException('Wrong number of segments');
}
list($headb64, $bodyb64, $cryptob64) = $tks;
$headerRaw = static::urlsafeB64Decode($headb64);
try {
$headerRaw = static::urlsafeB64Decode($headb64);
} catch (InvalidArgumentException $e) {
throw new UnexpectedValueException('Unable to decode header');
}
if (null === ($header = static::jsonDecode($headerRaw))) {
throw new UnexpectedValueException('Invalid header encoding');
}
if ($headers !== null) {
$headers = $header;
}
$payloadRaw = static::urlsafeB64Decode($bodyb64);
try {
$payloadRaw = static::urlsafeB64Decode($bodyb64);
} catch (InvalidArgumentException $e) {
throw new UnexpectedValueException('Unable to decode payload');
}
if (null === ($payload = static::jsonDecode($payloadRaw))) {
throw new UnexpectedValueException('Invalid claims encoding');
}
Expand All @@ -127,7 +135,11 @@ public static function decode(
if (!$payload instanceof stdClass) {
throw new UnexpectedValueException('Payload must be a JSON object');
}
$sig = static::urlsafeB64Decode($cryptob64);
try {
$sig = static::urlsafeB64Decode($cryptob64);
} catch (InvalidArgumentException $e) {
throw new UnexpectedValueException('Unable to decode signature');
}
if (empty($header->alg)) {
throw new UnexpectedValueException('Empty algorithm');
}
Expand Down Expand Up @@ -411,11 +423,18 @@ public static function jsonEncode(array $input): string
*
* @return string A decoded string
*
* @throws InvalidArgumentException invalid base64 characters
* @throws InvalidArgumentException invalid base64URL characters
*/
public static function urlsafeB64Decode(string $input): string
{
return \base64_decode(self::convertBase64UrlToBase64($input));
if (strpbrk($input, '+/=') !== false) {
throw new InvalidArgumentException('Input is not valid Base64URL');
}
$result = \base64_decode(self::convertBase64UrlToBase64($input), true);
if ($result === false) {
throw new InvalidArgumentException('Input is not valid Base64URL');
}
return $result;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ public function testMalformedJsonThrowsException()
JWT::jsonDecode('this is not valid JSON string');
}

public function testBase64UrlDecode()
{
$decoded = JWT::urlsafeB64Decode('VGVzdCB3aXRoIGEgbWludXM-');
$expected = 'Test with a minus>';
$this->assertSame($expected, $decoded);
}

public function testMalformedBase64Url()
{
$this->expectException(InvalidArgumentException::class);
JWT::urlsafeB64Decode('VGVzdCB3aXR&oIGEgbWludXM-');
}

public function testMalformedBase64UrlButValidBase64()
{
$this->expectException(InvalidArgumentException::class);
JWT::urlsafeB64Decode('VGVzdCB3aXRoIGEgc2xhc2g/');
}

public function testExpiredToken()
{
$this->expectException(ExpiredException::class);
Expand Down