Skip to content

Commit 211203a

Browse files
committed
Detect invalid Base64 encoding in signature
1 parent dccf163 commit 211203a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Diff for: src/JWT.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ public static function decode($jwt, $key, $allowed_algs = array())
8585
if (null === $payload = static::jsonDecode(static::urlsafeB64Decode($bodyb64))) {
8686
throw new UnexpectedValueException('Invalid claims encoding');
8787
}
88-
$sig = static::urlsafeB64Decode($cryptob64);
89-
88+
if (false === ($sig = static::urlsafeB64Decode($cryptob64))) {
89+
throw new UnexpectedValueException('Invalid signature encoding');
90+
}
91+
9092
if (empty($header->alg)) {
9193
throw new UnexpectedValueException('Empty algorithm');
9294
}

Diff for: tests/JWTTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,11 @@ public function testInvalidSegmentCount()
261261
$this->setExpectedException('UnexpectedValueException');
262262
JWT::decode('brokenheader.brokenbody', 'my_key', array('HS256'));
263263
}
264+
265+
public function testInvalidSignatureEncoding()
266+
{
267+
$msg = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx";
268+
$this->setExpectedException('UnexpectedValueException');
269+
JWT::decode($msg, 'secret', array('HS256'));
270+
}
264271
}

0 commit comments

Comments
 (0)