Skip to content

fix: read latest CSRF token from cookie#38272

Open
asadali145 wants to merge 3 commits intoopenedx:masterfrom
mitodl:asad/pick-csrf-from-cookie
Open

fix: read latest CSRF token from cookie#38272
asadali145 wants to merge 3 commits intoopenedx:masterfrom
mitodl:asad/pick-csrf-from-cookie

Conversation

@asadali145
Copy link
Copy Markdown
Contributor

Issue Links

https://github.com/mitodl/hq/issues/10704

Description

Fixes intermittent 403 CSRF errors encountered by learners when submitting problem answers in the Learning MFE.

The Learning MFE renders course units inside an LMS-served iframe. When a learner submits a problem, the page sends POST requests to the LMS (e.g., /event and /courses/{course_key}/xblock/{usage_key}/handler/xmodule_handler/problem_check). These requests require a valid CSRF token in the X-CSRFToken header.

Root Cause

The CSRF token was being cached at page load:

  • The LMS initialized jQuery AJAX with a static headers block
  • $.cookie('csrftoken') was read once when the iframe loaded
  • If the user re-authenticated in another tab (or CSRF rotated), the cookie updated but the header did not
  • Subsequent requests sent a stale CSRF token, resulting in HTTP 403 errors

Server logs confirmed:

Forbidden (CSRF token from the 'X-Csrftoken' HTTP header incorrect.)

Fix

Update AJAX setup to read the CSRF cookie immediately before each unsafe same-origin request (via beforeSend) instead of caching it at load time.

This ensures:

  • The X-CSRFToken header always matches the latest csrftoken cookie
  • Requests remain valid even after re-authentication or CSRF rotation

Impact

  • Learners: Fixes intermittent failures when submitting problems in Learning MFE
  • Developers / Operators: No action required

Testing Instructions

Reproduction Steps

  1. Start LMS with this branch (ensure updated static assets are served)
  2. Log in and open a course unit with a problem (Learning MFE)
  3. Keep this tab open
  4. In another tab:
    • Log out
    • Log back in (CSRF token rotates)
  5. Return to the original tab (without refreshing)
  6. Submit an answer

Expected Behavior

Before fix:

  • Submission may fail with HTTP 403
  • Logs show CSRF token mismatch

After fix:

  • Submission succeeds
  • Request uses the latest CSRF token

Additional Verification

  • Open DevTools → Network tab
  • Inspect problem_check request
  • Confirm:
    • X-CSRFToken header matches current csrftoken cookie

Regression Checks

  • Normal problem submission works without re-login flow

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Apr 2, 2026
@openedx-webhooks
Copy link
Copy Markdown

Thanks for the pull request, @asadali145!

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

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 Apr 2, 2026
@asadali145 asadali145 moved this from Needs Triage to Ready for Review in Contributions Apr 2, 2026
@pdpinch
Copy link
Copy Markdown
Contributor

pdpinch commented Apr 2, 2026

Are there tests for this? Is that even possible?

@asadali145
Copy link
Copy Markdown
Contributor Author

Are there tests for this? Is that even possible?

@pdpinch There are no existing tests, that's why I skipped that part.

@pdpinch
Copy link
Copy Markdown
Contributor

pdpinch commented Apr 2, 2026

Is this going to add overhead to every ajax request?

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: Ready for Review

Development

Successfully merging this pull request may close these issues.

3 participants