Skip to content

Commit 2f1c401

Browse files
committed
Fix possible vulnerability with decoding tokens (refs #39)
1 parent 6d66b8f commit 2f1c401

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

flask_jwt_extended/tokens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def decode_jwt(encoded_token, secret, algorithm, csrf):
8888
:return: Dictionary containing contents of the JWT
8989
"""
9090
# This call verifies the ext, iat, and nbf claims
91-
data = jwt.decode(encoded_token, secret, algorithm=algorithm)
91+
data = jwt.decode(encoded_token, secret, algorithms=[algorithm])
9292

9393
# Make sure that any custom claims we expect in the token are present
9494
if 'jti' not in data:

tests/test_protected_endpoints.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,22 @@ def bad_logout():
385385
with self.assertRaises(RuntimeWarning):
386386
client.post('/logout-bad')
387387

388+
def test_jwt_with_different_algorithm(self):
389+
self.app.config['JWT_ALGORITHM'] = 'HS256'
390+
self.app.secret_key = 'test_secret'
391+
access_token = encode_access_token(
392+
identity='bobdobbs',
393+
secret='test_secret',
394+
algorithm='HS512',
395+
expires_delta=timedelta(minutes=5),
396+
fresh=True,
397+
user_claims={},
398+
csrf=False
399+
)
400+
status, data = self._jwt_get('/protected', access_token)
401+
self.assertEqual(status, 422)
402+
self.assertIn('msg', data)
403+
388404

389405
class TestEndpointsWithCookies(unittest.TestCase):
390406

0 commit comments

Comments
 (0)