Skip to content

Commit

Permalink
Merge pull request openedx#19865 from edx/ormsbee/log_b64_cookie
Browse files Browse the repository at this point in the history
Improve SafeCookieData Error Logging
  • Loading branch information
David Ormsbee authored Feb 25, 2019
2 parents 0983091 + 40e1e1e commit 0857a1c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions openedx/core/djangoapps/safe_sessions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
the entire cookie and use it to impersonate the victim.
"""

from base64 import b64encode
from contextlib import contextmanager
from hashlib import sha256
from logging import ERROR, getLogger
Expand Down Expand Up @@ -261,7 +261,6 @@ def process_request(self, request):
final verification before sending the response (in
process_response).
"""

cookie_data_string = request.COOKIES.get(settings.SESSION_COOKIE_NAME)
if cookie_data_string:

Expand Down Expand Up @@ -459,10 +458,6 @@ def _delete_cookie(request, response):
Delete the cookie by setting the expiration to a date in the past,
while maintaining the domain, secure, and httponly settings.
"""
log.warning(
u"SafeCookieData is deleting session cookie for user %d",
request.user.id
)
response.set_cookie(
settings.SESSION_COOKIE_NAME,
max_age=0,
Expand All @@ -472,6 +467,21 @@ def _delete_cookie(request, response):
httponly=settings.SESSION_COOKIE_HTTPONLY or None,
)

# Log the cookie, but cap the length and base64 encode to make sure nothing
# malicious gets directly dumped into the log.
cookie_header = request.META.get('HTTP_COOKIE', '')[:4096]
log.warning(
u"Malformed Cookie Header? First 4K, in Base64: %s",
b64encode(cookie_header)
)

# Note, there is no request.user attribute at this point.
if hasattr(request, 'session') and hasattr(request.session, 'session_key'):
log.warning(
u"SafeCookieData deleted session cookie for session %s",
request.session.session_key
)


def _is_from_logout(request):
"""
Expand Down

0 comments on commit 0857a1c

Please sign in to comment.