|
1 | | -import hmac |
2 | 1 | import inspect |
3 | 2 | import re |
4 | 3 | import warnings |
|
7 | 6 | from django.conf import settings |
8 | 7 | from django.core.exceptions import ImproperlyConfigured, PermissionDenied |
9 | 8 | from django.middleware.csrf import rotate_token |
| 9 | +from django.utils.crypto import constant_time_compare |
10 | 10 | from django.utils.deprecation import RemovedInDjango61Warning |
11 | 11 | from django.utils.module_loading import import_string |
12 | 12 | from django.views.decorators.debug import sensitive_variables |
@@ -175,7 +175,7 @@ def login(request, user, backend=None): |
175 | 175 | if SESSION_KEY in request.session: |
176 | 176 | if _get_user_session_key(request) != user.pk or ( |
177 | 177 | session_auth_hash |
178 | | - and not hmac.compare_digest( |
| 178 | + and not constant_time_compare( |
179 | 179 | request.session.get(HASH_SESSION_KEY, ""), session_auth_hash |
180 | 180 | ) |
181 | 181 | ): |
@@ -217,7 +217,7 @@ async def alogin(request, user, backend=None): |
217 | 217 | if await request.session.ahas_key(SESSION_KEY): |
218 | 218 | if await _aget_user_session_key(request) != user.pk or ( |
219 | 219 | session_auth_hash |
220 | | - and not hmac.compare_digest( |
| 220 | + and not constant_time_compare( |
221 | 221 | await request.session.aget(HASH_SESSION_KEY, ""), |
222 | 222 | session_auth_hash, |
223 | 223 | ) |
@@ -323,15 +323,15 @@ def get_user(request): |
323 | 323 | session_hash_verified = False |
324 | 324 | else: |
325 | 325 | session_auth_hash = user.get_session_auth_hash() |
326 | | - session_hash_verified = hmac.compare_digest( |
| 326 | + session_hash_verified = constant_time_compare( |
327 | 327 | session_hash, session_auth_hash |
328 | 328 | ) |
329 | 329 | if not session_hash_verified: |
330 | 330 | # If the current secret does not verify the session, try |
331 | 331 | # with the fallback secrets and stop when a matching one is |
332 | 332 | # found. |
333 | 333 | if session_hash and any( |
334 | | - hmac.compare_digest(session_hash, fallback_auth_hash) |
| 334 | + constant_time_compare(session_hash, fallback_auth_hash) |
335 | 335 | for fallback_auth_hash in user.get_session_auth_fallback_hash() |
336 | 336 | ): |
337 | 337 | request.session.cycle_key() |
@@ -364,15 +364,15 @@ async def aget_user(request): |
364 | 364 | session_hash_verified = False |
365 | 365 | else: |
366 | 366 | session_auth_hash = user.get_session_auth_hash() |
367 | | - session_hash_verified = hmac.compare_digest( |
| 367 | + session_hash_verified = constant_time_compare( |
368 | 368 | session_hash, session_auth_hash |
369 | 369 | ) |
370 | 370 | if not session_hash_verified: |
371 | 371 | # If the current secret does not verify the session, try |
372 | 372 | # with the fallback secrets and stop when a matching one is |
373 | 373 | # found. |
374 | 374 | if session_hash and any( |
375 | | - hmac.compare_digest(session_hash, fallback_auth_hash) |
| 375 | + constant_time_compare(session_hash, fallback_auth_hash) |
376 | 376 | for fallback_auth_hash in user.get_session_auth_fallback_hash() |
377 | 377 | ): |
378 | 378 | await request.session.acycle_key() |
|
0 commit comments