Skip to content

Commit a09f76f

Browse files
committed
Allow JWT::decode to accept an empty string as a valid kid
There are instances when using CachedKeySet where a key is returned with an empty string as the kid. This is a valid use case and should be allowed. For example Teleport Proxy uses this pattern to allow for a default key. The getKey method can be simplified, as well as refactored to follow the same pattern as the CachedKeySet class which casts null kids to an empty string. This change also adds a test to ensure that an empty string kid is a valid kid.
1 parent 76808fa commit a09f76f

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

Diff for: src/JWT.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,15 @@ private static function getKey(
465465
$keyOrKeyArray,
466466
?string $kid
467467
): Key {
468+
469+
$kid = (string) $kid;
470+
468471
if ($keyOrKeyArray instanceof Key) {
469472
return $keyOrKeyArray;
470473
}
471474

472-
if (empty($kid) && $kid !== '0') {
473-
throw new UnexpectedValueException('"kid" empty, unable to lookup correct key');
474-
}
475-
476-
if ($keyOrKeyArray instanceof CachedKeySet) {
477-
// Skip "isset" check, as this will automatically refresh if not set
478-
return $keyOrKeyArray[$kid];
475+
if (!is_array($keyOrKeyArray) && !$keyOrKeyArray instanceof ArrayAccess) {
476+
throw new UnexpectedValueException('Expecting a Key or an associative array of keys');
479477
}
480478

481479
if (!isset($keyOrKeyArray[$kid])) {

Diff for: tests/JWTTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,19 @@ public function testKIDChooser()
327327
$this->assertEquals($decoded, $expected);
328328
}
329329

330+
public function testArrayAccessKIDChooserWhenJWTHasNoKey()
331+
{
332+
$key = new Key('my_key0', 'HS256');
333+
$keys = new ArrayObject([
334+
'' => $key,
335+
]);
336+
$msg = JWT::encode(['message' => 'abc'], $key->getKeyMaterial(), 'HS256');
337+
$decoded = JWT::decode($msg, $keys);
338+
$expected = new stdClass();
339+
$expected->message = 'abc';
340+
$this->assertEquals($decoded, $expected);
341+
}
342+
330343
public function testArrayAccessKIDChooser()
331344
{
332345
$keys = new ArrayObject([
@@ -383,6 +396,26 @@ public function testInvalidSignatureEncoding()
383396
JWT::decode($msg, new Key('secret', 'HS256'));
384397
}
385398

399+
public function testInvalideKeyOrKeyArray()
400+
{
401+
$key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk';
402+
$payload = ['foo' => [1, 2, 3]];
403+
$jwt = JWT::encode($payload, $key, 'HS256');
404+
$this->expectException(UnexpectedValueException::class);
405+
$this->expectExceptionMessage('Expecting a Key or an associative array of keys');
406+
JWT::decode($jwt, 'SomeKeyNotAnArray');
407+
}
408+
409+
public function testKeyNotInKeyOrKeyArray()
410+
{
411+
$key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk';
412+
$payload = ['foo' => [1, 2, 3]];
413+
$jwt = JWT::encode($payload, $key, 'HS256');
414+
$this->expectException(UnexpectedValueException::class);
415+
$this->expectExceptionMessage('"kid" invalid, unable to lookup correct key');
416+
JWT::decode($jwt, ['notrealkey' => 'SomeKeyNotAnArray']);
417+
}
418+
386419
public function testHSEncodeDecode()
387420
{
388421
$msg = JWT::encode(['message' => 'abc'], 'my_key', 'HS256');

0 commit comments

Comments
 (0)