Skip to content

Commit eb4bb53

Browse files
committed
Changed header's alg parameter to be case sensitive per the JWT spec.
1 parent a0c5b62 commit eb4bb53

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

jwt/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def sign(self, msg, key):
7575
return b''
7676

7777
def verify(self, msg, key, sig):
78-
return True
78+
return False
7979

8080

8181
class HMACAlgorithm(Algorithm):

jwt/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def verify_signature(payload, signing_input, header, signature, key='',
144144
raise TypeError('audience must be a string or None')
145145

146146
try:
147-
alg_obj = _algorithms[header['alg'].upper()]
147+
alg_obj = _algorithms[header['alg']]
148148
key = alg_obj.prepare_key(key)
149149

150150
if not alg_obj.verify(signing_input, key, signature):

tests/test_jwt.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
_algorithms as jwt_algorithms
1515
)
1616

17+
from jwt.exceptions import DecodeError
18+
1719
if sys.version_info >= (2, 7):
1820
import unittest
1921
else:
@@ -68,6 +70,28 @@ def test_encode_bad_type(self):
6870
for t in types:
6971
self.assertRaises(TypeError, lambda: jwt.encode(t, 'secret'))
7072

73+
def test_encode_algorithm_param_should_be_case_sensitive(self):
74+
payload = {'hello': 'world'}
75+
76+
jwt.encode(payload, 'secret', algorithm='HS256')
77+
78+
with self.assertRaises(NotImplementedError) as context:
79+
jwt.encode(payload, None, algorithm='hs256')
80+
81+
exception = context.exception
82+
self.assertEquals(str(exception), 'Algorithm not supported')
83+
84+
def test_decode_algorithm_param_should_be_case_sensitive(self):
85+
example_jwt = ('eyJhbGciOiJoczI1NiIsInR5cCI6IkpXVCJ9' # alg = hs256
86+
'.eyJoZWxsbyI6IndvcmxkIn0'
87+
'.5R_FEPE7SW2dT9GgIxPgZATjFGXfUDOSwo7TtO_Kd_g')
88+
89+
with self.assertRaises(DecodeError) as context:
90+
jwt.decode(example_jwt, 'secret')
91+
92+
exception = context.exception
93+
self.assertEquals(str(exception), 'Algorithm not supported')
94+
7195
def test_encode_datetime(self):
7296
secret = 'secret'
7397
current_datetime = datetime.utcnow()

0 commit comments

Comments
 (0)