Skip to content

Commit d67523f

Browse files
authored
Detect invalid Base64 encoding in signature (#162)
1 parent b2a5316 commit d67523f

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Diff for: src/JWT.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ public static function decode($jwt, $key, $allowed_algs = array())
8787
if (null === $payload = static::jsonDecode(static::urlsafeB64Decode($bodyb64))) {
8888
throw new UnexpectedValueException('Invalid claims encoding');
8989
}
90-
$sig = static::urlsafeB64Decode($cryptob64);
91-
90+
if (false === ($sig = static::urlsafeB64Decode($cryptob64))) {
91+
throw new UnexpectedValueException('Invalid signature encoding');
92+
}
9293
if (empty($header->alg)) {
9394
throw new UnexpectedValueException('Empty algorithm');
9495
}

Diff for: tests/JWTTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ public function testInvalidSegmentCount()
267267
JWT::decode('brokenheader.brokenbody', 'my_key', array('HS256'));
268268
}
269269

270+
public function testInvalidSignatureEncoding()
271+
{
272+
$msg = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx";
273+
$this->setExpectedException('UnexpectedValueException');
274+
JWT::decode($msg, 'secret', array('HS256'));
275+
}
276+
270277
public function testVerifyError()
271278
{
272279
$this->setExpectedException('DomainException');

0 commit comments

Comments
 (0)