Skip to content

Commit b2c2be6

Browse files
committed
Update decode() to require allowed algorithms arg when verifying
1 parent 10918f2 commit b2c2be6

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

Authentication/JWT.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class JWT
2525
/**
2626
* Decodes a JWT string into a PHP object.
2727
*
28-
* @param string $jwt The JWT
29-
* @param string|Array|null $key The secret key, or map of keys
30-
* @param bool $algs List of supported verification algorithms
28+
* @param string $jwt The JWT
29+
* @param string|Array|null $key The secret key, or map of keys
30+
* @param Array $allowed_algs List of supported verification algorithms
3131
*
3232
* @return object The JWT's payload as a PHP object
3333
*
@@ -41,7 +41,7 @@ class JWT
4141
* @uses jsonDecode
4242
* @uses urlsafeB64Decode
4343
*/
44-
public static function decode($jwt, $key = null, $algs = array())
44+
public static function decode($jwt, $key = null, $allowed_algs = array())
4545
{
4646
$tks = explode('.', $jwt);
4747
if (count($tks) != 3) {
@@ -55,13 +55,16 @@ public static function decode($jwt, $key = null, $algs = array())
5555
throw new UnexpectedValueException('Invalid claims encoding');
5656
}
5757
$sig = JWT::urlsafeB64Decode($cryptob64);
58-
if (!empty($key)) {
58+
if (isset($key)) {
5959
if (empty($header->alg)) {
6060
throw new DomainException('Empty algorithm');
6161
}
6262
if (empty(self::$supported_algs[$header->alg])) {
6363
throw new DomainException('Algorithm not supported');
6464
}
65+
if (!is_array($allowed_algs) || !in_array($header->alg, $allowed_algs)) {
66+
throw new DomainException('Algorithm not allowed');
67+
}
6568
if (is_array($key)) {
6669
if (isset($header->kid)) {
6770
$key = $key[$header->kid];

tests/JWTTest.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ class JWTTest extends PHPUnit_Framework_TestCase
55
public function testEncodeDecode()
66
{
77
$msg = JWT::encode('abc', 'my_key');
8-
$this->assertEquals(JWT::decode($msg, 'my_key'), 'abc');
8+
$this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
99
}
1010

1111
public function testDecodeFromPython()
1212
{
1313
$msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg';
1414
$this->assertEquals(
15-
JWT::decode($msg, 'my_key'),
15+
JWT::decode($msg, 'my_key', array('HS256')),
1616
'*:http://application/clicky?blah=1.23&f.oo=456 AC000 123'
1717
);
1818
}
1919

2020
public function testUrlSafeCharacters()
2121
{
2222
$encoded = JWT::encode('f?', 'a');
23-
$this->assertEquals('f?', JWT::decode($encoded, 'a'));
23+
$this->assertEquals('f?', JWT::decode($encoded, 'a', array('HS256')));
2424
}
2525

2626
public function testMalformedUtf8StringsFail()
@@ -42,7 +42,7 @@ public function testExpiredToken()
4242
"message" => "abc",
4343
"exp" => time() - 20); // time in the past
4444
$encoded = JWT::encode($payload, 'my_key');
45-
JWT::decode($encoded, 'my_key');
45+
JWT::decode($encoded, 'my_key', array('HS256'));
4646
}
4747

4848
public function testBeforeValidTokenWithNbf()
@@ -52,7 +52,7 @@ public function testBeforeValidTokenWithNbf()
5252
"message" => "abc",
5353
"nbf" => time() + 20); // time in the future
5454
$encoded = JWT::encode($payload, 'my_key');
55-
JWT::decode($encoded, 'my_key');
55+
JWT::decode($encoded, 'my_key', array('HS256'));
5656
}
5757

5858
public function testBeforeValidTokenWithIat()
@@ -62,7 +62,7 @@ public function testBeforeValidTokenWithIat()
6262
"message" => "abc",
6363
"iat" => time() + 20); // time in the future
6464
$encoded = JWT::encode($payload, 'my_key');
65-
JWT::decode($encoded, 'my_key');
65+
JWT::decode($encoded, 'my_key', array('HS256'));
6666
}
6767

6868
public function testValidToken()
@@ -71,7 +71,7 @@ public function testValidToken()
7171
"message" => "abc",
7272
"exp" => time() + 20); // time in the future
7373
$encoded = JWT::encode($payload, 'my_key');
74-
$decoded = JWT::decode($encoded, 'my_key');
74+
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
7575
$this->assertEquals($decoded->message, 'abc');
7676
}
7777

@@ -83,7 +83,7 @@ public function testValidTokenWithNbf()
8383
"exp" => time() + 20, // time in the future
8484
"nbf" => time() - 20);
8585
$encoded = JWT::encode($payload, 'my_key');
86-
$decoded = JWT::decode($encoded, 'my_key');
86+
$decoded = JWT::decode($encoded, 'my_key', array('HS256'));
8787
$this->assertEquals($decoded->message, 'abc');
8888
}
8989

@@ -94,28 +94,26 @@ public function testInvalidToken()
9494
"exp" => time() + 20); // time in the future
9595
$encoded = JWT::encode($payload, 'my_key');
9696
$this->setExpectedException('SignatureInvalidException');
97-
$decoded = JWT::decode($encoded, 'my_key2');
97+
$decoded = JWT::decode($encoded, 'my_key2', array('HS256'));
9898
}
9999

100100
public function testRSEncodeDecode()
101101
{
102102
$privKey = openssl_pkey_new(array('digest_alg' => 'sha256',
103103
'private_key_bits' => 1024,
104104
'private_key_type' => OPENSSL_KEYTYPE_RSA));
105-
//JWT::setOnlyAllowedMethod('RS256');
106105
$msg = JWT::encode('abc', $privKey, 'RS256');
107106
$pubKey = openssl_pkey_get_details($privKey);
108107
$pubKey = $pubKey['key'];
109-
$decoded = JWT::decode($msg, $pubKey, true);
108+
$decoded = JWT::decode($msg, $pubKey, array('RS256'));
110109
$this->assertEquals($decoded, 'abc');
111110
}
112111

113112
public function testKIDChooser()
114113
{
115114
$keys = array('1' => 'my_key', '2' => 'my_key2');
116-
//JWT::setOnlyAllowedMethod('HS256');
117115
$msg = JWT::encode('abc', $keys['1'], 'HS256', '1');
118-
$decoded = JWT::decode($msg, $keys, true);
116+
$decoded = JWT::decode($msg, $keys, array('HS256'));
119117
$this->assertEquals($decoded, 'abc');
120118
}
121119
}

0 commit comments

Comments
 (0)