From 8fa1d4d0b66e62b49af8e22b411b468e593d7cea Mon Sep 17 00:00:00 2001 From: Robert Raposa Date: Thu, 6 Jan 2022 16:49:58 -0500 Subject: [PATCH] fix: conditionally set safe session monitoring (#29724) Fixes a bug where a custom attribute was being set even for cases where we did not have the appropriate data, and an exception was being raised. ARCHBOM-1940 --- openedx/core/djangoapps/safe_sessions/middleware.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/openedx/core/djangoapps/safe_sessions/middleware.py b/openedx/core/djangoapps/safe_sessions/middleware.py index 5d16114eb0..6d600b5713 100644 --- a/openedx/core/djangoapps/safe_sessions/middleware.py +++ b/openedx/core/djangoapps/safe_sessions/middleware.py @@ -505,8 +505,9 @@ class SafeSessionMiddleware(SessionMiddleware, MiddlewareMixin): # add a session hash custom attribute for all requests to help monitoring # requests that come both before and after a mistmatch - session_hash = obscure_token(request.cookie_session_field) - set_custom_attribute('safe_sessions.session_id_hash.parsed_cookie', session_hash) + if hasattr(request, 'cookie_session_field'): + session_hash = obscure_token(request.cookie_session_field) + set_custom_attribute('safe_sessions.session_id_hash.parsed_cookie', session_hash) # log request header if this user id was involved in an earlier mismatch log_request_headers = cache.get(