Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix construction of cookie using user supplied input #12808 #13029

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions kolibri/core/auth/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,17 +1046,23 @@ def get_session_response(self, request):

if isinstance(user, AnonymousUser):
response = Response(session)
if not request.COOKIES.get("visitor_id"):
try:
existing_visitor_id = request.COOKIES.get("visitor_id")
if existing_visitor_id:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small tweak - no need to add this if/else here - if you pass None to the UUID constructor it will also throw a TypeError so we can handle all cases where the uuid is not a valid v4 UUID in the except block.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I got that I'll change it.

UUID(existing_visitor_id, version=4)
response.set_cookie(
"visitor_id", existing_visitor_id, expires=visitor_cookie_expiry
)
else:
visitor_id = str(uuid4().hex)
response.set_cookie(
"visitor_id", visitor_id, expires=visitor_cookie_expiry
)
except (ValueError, TypeError):
visitor_id = str(uuid4().hex)
response.set_cookie(
"visitor_id", visitor_id, expires=visitor_cookie_expiry
)
else:
response.set_cookie(
"visitor_id",
request.COOKIES.get("visitor_id"),
expires=visitor_cookie_expiry,
)
return response
# Set last activity on session to the current time to prevent session timeout
# Only do this for logged in users, as anonymous users cannot get logged out!
Expand Down
Loading