Skip to content

fix: look up request country code without storing it in the session - #39111

Draft
blarghmatey wants to merge 1 commit into
openedx:masterfrom
mitodl:tmacey/geoinfo-no-session
Draft

blarghmatey wants to merge 1 commit into
openedx:masterfrom
mitodl:tmacey/geoinfo-no-session

Conversation

@blarghmatey

Copy link
Copy Markdown
Contributor

Description

CountryMiddleware stored country_code and ip_address in the session whenever the client IP was globally routable and differed from the stored ip_address. For a request without a session cookie that condition is always true, so every such response created a new session and carried a Set-Cookie. That includes responses that have no other use for a session, such as course assets served by contentserver, 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 as request_country_code; the video block uses it to choose VIDEO_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_ip directly and are unchanged.

Implications for operators:

  • openedx.core.djangoapps.geoinfo.middleware.CountryMiddleware no longer exists. Deployments that list it in a custom MIDDLEWARE setting need to drop it.
  • The GeoIP lookup now runs once per request that renders blocks (or localizes a price), instead of once per IP change per session. Stale country_code/ip_address keys 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.test passes locally (8 tests). The new tests cover IPv4/IPv6 lookups, skipping non-global IPs, memoization, and that the session stays empty and unmodified.
  • ruff check passes on the changed files.
  • Not run locally: the courseware block render tests, which need MongoDB.
  • Manual: with no session cookie, request a public course asset from the LMS (curl -D - <lms>/asset-v1:...) and confirm the response has no session Set-Cookie.

🤖 Generated with Claude Code

https://claude.ai/code/session_019Sz2VhptEzudj4pimSv1bF

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
@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Sep 16, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @blarghmatey!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform-oncall.

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 approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To 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:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If 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 PR

Your 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:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

2 participants