Skip to content

Commit 49cdf25

Browse files
committed
Uuse safe_str_cmp when comparing jwt csrf token to submitted csrf token
1 parent 69e4643 commit 49cdf25

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

flask_jwt_extended/utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import datetime
22
import json
3-
import os
43
import uuid
54
from functools import wraps
65

7-
import binascii
86
import jwt
97
import six
108
from flask import request, current_app
9+
from werkzeug.security import safe_str_cmp
1110
try:
1211
from flask import _app_ctx_stack as ctx_stack
1312
except ImportError: # pragma: no cover
@@ -42,7 +41,6 @@ def get_jwt_claims():
4241
return getattr(ctx_stack.top, 'jwt_user_claims', {})
4342

4443

45-
# TODO set csrf token in jwt when creating tokens (if enabled)
4644
def _create_csrf_token():
4745
return str(uuid.uuid4())
4846

@@ -193,11 +191,9 @@ def _decode_jwt_from_cookies(type):
193191
algorithm = get_algorithm()
194192
token = _decode_jwt(token, secret, algorithm)
195193

196-
# TODO use a safe string comparison here, to prevent timing attacks on the
197-
# csrf token
198194
if get_cookie_csrf_protect():
199195
csrf = request.headers.get(csrf_header_key, None)
200-
if not csrf or csrf != token['csrf']:
196+
if not csrf or not safe_str_cmp(csrf, token['csrf']):
201197
raise NoAuthorizationError("Missing or invalid csrf double submit header")
202198

203199
return token

0 commit comments

Comments
 (0)