Skip to content

Commit 841dac7

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 43d70ae commit 841dac7

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/JWT.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -460,19 +460,22 @@ private static function getKey(
460460
$keyOrKeyArray,
461461
?string $kid
462462
): Key {
463+
464+
$kid = (string) $kid;
465+
463466
if ($keyOrKeyArray instanceof Key) {
464467
return $keyOrKeyArray;
465468
}
466469

467-
if (empty($kid) && $kid !== '0') {
468-
throw new UnexpectedValueException('"kid" empty, unable to lookup correct key');
469-
}
470-
471470
if ($keyOrKeyArray instanceof CachedKeySet) {
472471
// Skip "isset" check, as this will automatically refresh if not set
473472
return $keyOrKeyArray[$kid];
474473
}
475474

475+
if (!is_array($keyOrKeyArray) && !$keyOrKeyArray instanceof ArrayAccess) {
476+
throw new UnexpectedValueException('Expecting a Key or an associative array of keys');
477+
}
478+
476479
if (!isset($keyOrKeyArray[$kid])) {
477480
throw new UnexpectedValueException('"kid" invalid, unable to lookup correct key');
478481
}

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 testInvalidKeyOrKeyArray()
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)