Skip to content

Commit 93d248c

Browse files
authored
fix: limit the number of segments a JWT can be exploded into (#639)
1 parent 70c5221 commit 93d248c

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎src/JWT.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static function decode(
107107
if (empty($keyOrKeyArray)) {
108108
throw new InvalidArgumentException('Key may not be empty');
109109
}
110-
$tks = \explode('.', $jwt);
110+
$tks = \explode('.', $jwt, 4);
111111
if (\count($tks) !== 3) {
112112
throw new UnexpectedValueException('Wrong number of segments');
113113
}

‎tests/JWTTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,21 @@ public function testInvalidToken()
323323
JWT::decode($encoded, $decodeKey);
324324
}
325325

326+
public function testInvalidTokenSegments()
327+
{
328+
$dummyToken = 'dGhlIHZhbHVlIGRvZXNuJ3QgbWF0dGVy.T25seSB0aGUgbnVtYmVyIG9mIHNlZ21lbnRz.VGhpcyBzaG91bGQgYmUgYSBzaWduYXR1cmU.YnV0IHRoZXJlIGlzIG1vcmU';
329+
$this->expectException(UnexpectedValueException::class);
330+
JWT::decode($dummyToken, $this->hmacKey);
331+
}
332+
333+
public function testInvalidTokenManySegments()
334+
{
335+
$dummyToken = 'dGhlIHZhbHVlIGRvZXNuJ3QgbWF0dGVy.T25seSB0aGUgbnVtYmVyIG9mIHNlZ21lbnRz.VGhpcyBzaG91bGQgYmUgYSBzaWduYXR1cmU';
336+
$dummyToken .= str_repeat('.KzE', 999999);
337+
$this->expectException(UnexpectedValueException::class);
338+
JWT::decode($dummyToken, $this->hmacKey);
339+
}
340+
326341
public function testNullKeyFails()
327342
{
328343
$payload = [

0 commit comments

Comments
 (0)