Skip to content

Commit 0ec48e4

Browse files
authored
Merge pull request #240 from kelvinmo/chore/jwt-check-missing-alg
Add check for missing alg header
2 parents d40e389 + 609f606 commit 0ec48e4

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

src/SimpleJWT/JWT.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public static function decode(string $token, KeySet $keys, string $expected_alg,
134134
}
135135

136136
// Check signatures
137+
if (!isset($headers['alg'])) throw new InvalidTokenException('alg parameter missing', InvalidTokenException::TOKEN_PARSE_ERROR);
137138
if ($headers['alg'] != $expected_alg) throw new InvalidTokenException('Unexpected algorithm', InvalidTokenException::SIGNATURE_VERIFICATION_ERROR);
138139
/** @var SignatureAlgorithm $signer */
139140
$signer = AlgorithmFactory::create($expected_alg);

tests/JWTTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ function testCrit() {
335335
$this->assertTrue($jwt->getClaim('iss'));
336336
}
337337

338+
function testMissingAlg() {
339+
$this->expectException('SimpleJWT\InvalidTokenException');
340+
$this->expectExceptionCode(InvalidTokenException::TOKEN_PARSE_ERROR);
341+
342+
$set = $this->getPublicKeySet();
343+
$token = 'eyJ0eXAiOiAiSldUIn0.eyJpc3MiOiJqb2UiLCJleHAiOjEzMDA4MTkzODB9.lLajjIbnhRpthOPhbJTaxoQ8JHYSAwSUl1Vxc4eQcIU';
344+
$jwt = JWT::decode($token, $set, 'HS256');
345+
$this->assertTrue($jwt->getClaim('iss'));
346+
}
347+
338348
function testInvalidToken() {
339349
$this->expectException('SimpleJWT\InvalidTokenException');
340350

0 commit comments

Comments
 (0)