Skip to content

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

Open
blarghmatey wants to merge 3 commits into
openedx:masterfrom
mitodl:tmacey/geoinfo-no-session
Open

blarghmatey wants to merge 3 commits into
openedx:masterfrom
mitodl:tmacey/geoinfo-no-session

Conversation

@blarghmatey

@blarghmatey blarghmatey commented Sep 16, 2026 •

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 turns the middleware into a no-op 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:

  • CountryMiddleware is now a deprecated no-op that removes itself at startup and logs a warning; drop it from custom MIDDLEWARE settings before it is removed.
  • 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

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Sep 16, 2026
@openedx-webhooks

openedx-webhooks commented Sep 16, 2026 •

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.

Details
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.

@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Sep 16, 2026
@mphilbrick211 mphilbrick211 moved this from Needs Triage to Waiting on Author in Contributions Sep 17, 2026
@blarghmatey
blarghmatey marked this pull request as ready for review September 17, 2026 15:18
@mphilbrick211 mphilbrick211 moved this from Waiting on Author to Ready for Review in Contributions Nov 4, 2026
@mphilbrick211 mphilbrick211 added the needs reviewer assigned PR needs to be (re-)assigned a new reviewer label Nov 4, 2026
@AhtishamShahid
AhtishamShahid self-requested a review November 6, 2026 10:20

@AhtishamShahid AhtishamShahid left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Deleting CountryMiddleware outright means any deployment whose settings still list it fails at startup with ModuleNotFoundError: No module named 'openedx.core.djangoapps.geoinfo.middleware'. I reproduced this locally with the PR applied.

Could we keep a no-op shim for one release and remove it later through a DEPR ticket? Raising MiddlewareNotUsed makes Django drop it at startup, so it costs nothing per request:

import warnings

from django.core.exceptions import MiddlewareNotUsed


class CountryMiddleware:
    """
    Deprecated no-op. The country code is now looked up per request by
    geoinfo.api.country_code_for_request; remove this from MIDDLEWARE.
    """

    def __init__(self, get_response):
        warnings.warn(
            "CountryMiddleware is deprecated and does nothing; remove it from MIDDLEWARE.",
            DeprecationWarning,
            stacklevel=2,
        )
        raise MiddlewareNotUsed()

@blarghmatey

Copy link
Copy Markdown
Contributor Author

@AhtishamShahid Added the shim as you suggested in 011b00f (DeprecationWarning, then MiddlewareNotUsed so Django drops it at startup) and a log.warning in d691c91 since DeprecationWarning is hidden by default; geoinfo/tests 9 passed, including a test that loads MIDDLEWARE with it still listed. Removal is tracked in openedx/public-engineering#575.

@AhtishamShahid AhtishamShahid left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

checks are failing; otherwise, looks good .

blarghmatey and others added 3 commits November 13, 2026 13:40
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_019Sz2VhptEzudj4pimSv1bF
Deployments whose MIDDLEWARE setting still lists
openedx.core.djangoapps.geoinfo.middleware.CountryMiddleware fail at
startup with ModuleNotFoundError once the module is deleted. The shim
emits a DeprecationWarning and raises MiddlewareNotUsed, so Django drops
it from the chain at load time and it costs nothing per request.

Claude-Session: https://claude.ai/code/session_01FGZebDghNhg4LQEvB7NWni
Python's default warning filters hide DeprecationWarning outside
__main__, so operators still listing CountryMiddleware in MIDDLEWARE
would not see the deprecation. A log.warning at load time puts it in the
service logs.

Claude-Session: https://claude.ai/code/session_01FGZebDghNhg4LQEvB7NWni
@blarghmatey
blarghmatey force-pushed the tmacey/geoinfo-no-session branch from d691c91 to f151ae5 Compare November 13, 2026 18:44

This branch has not been deployed

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

Labels

needs reviewer assigned PR needs to be (re-)assigned a new reviewer open-source-contribution PR author is not from Axim or 2U

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

4 participants