fix: look up request country code without storing it in the session - #39111
blarghmatey wants to merge 1 commit into
Conversation
CountryMiddleware wrote country_code and ip_address into the session on every request whose client IP differed from the stored one. For a visitor without a session cookie that is every request, so each response created a new session and set a session cookie, including responses such as course assets that otherwise have no use for a session. Replace the middleware with geoinfo.api.country_code_for_request, which the two readers (block rendering and localized price text) call directly. The lookup is memoized on the request and never touches the session. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019Sz2VhptEzudj4pimSv1bF
|
Thanks for the pull request, @blarghmatey! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. 🔘 Update the status of your PRYour PR is currently marked as a draft. After completing the steps above, update its status by clicking "Ready for Review", or removing "WIP" from the title, as appropriate. Where can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
Description
CountryMiddlewarestoredcountry_codeandip_addressin the session whenever the client IP was globally routable and differed from the storedip_address. For a request without a session cookie that condition is always true, so every such response created a new session and carried aSet-Cookie. That includes responses that have no other use for a session, such as course assets served bycontentserver, and it adds a session-store write per request from cookie-less clients.On an LMS behind a CDN we observed this directly: repeated cookie-less requests for a public course asset each returned a different new session cookie.
This PR removes the middleware and adds
geoinfo.api.country_code_for_request(request), which the two consumers of the session value now call:lms/djangoapps/courseware/block_render.py(user_location, passed to the user service asrequest_country_code; the video block uses it to chooseVIDEO_CDN_URL)openedx/core/djangoapps/catalog/utils.py(get_localized_price_text)The lookup is memoized on the request object and never reads or writes the session. Embargo and registration already call
country_code_from_ipdirectly and are unchanged.Implications for operators:
openedx.core.djangoapps.geoinfo.middleware.CountryMiddlewareno longer exists. Deployments that list it in a customMIDDLEWAREsetting need to drop it.country_code/ip_addresskeys in existing sessions are ignored.Testing instructions
pytest openedx/core/djangoapps/geoinfo/tests/test_api.py openedx/core/djangoapps/catalog/tests/test_utils.py::TestGetLocalizedPriceText --ds=lms.envs.testpasses locally (8 tests). The new tests cover IPv4/IPv6 lookups, skipping non-global IPs, memoization, and that the session stays empty and unmodified.ruff checkpasses on the changed files.curl -D - <lms>/asset-v1:...) and confirm the response has no sessionSet-Cookie.🤖 Generated with Claude Code
https://claude.ai/code/session_019Sz2VhptEzudj4pimSv1bF