Skip to content

fix: answer 401 instead of a login redirect to non-HTML Studio requests - #39296

Open
blarghmatey wants to merge 2 commits into
openedx:masterfrom
mitodl:tmacey/cms-401-for-non-html-login-redirect
Open

blarghmatey wants to merge 2 commits into
openedx:masterfrom
mitodl:tmacey/cms-401-for-non-html-login-redirect

Conversation

@blarghmatey

@blarghmatey blarghmatey commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Most Studio views are protected by login_required, which redirects an anonymous request to LOGIN_URL. A script follows that redirect, and with LMS SSO each request then starts an OAuth login of its own. When the Studio session expires under an open Authoring MFE page, the page's parallel requests start that many logins against one session. social-core keeps the OAuth state in a single session key, so the logins overwrite each other and all but a few fail in validate_state with AuthStateForbidden or AuthStateMissing, which Studio serves as a 500. When the MFE is on another origin, the browser instead blocks the followed redirect at the LMS /oauth2/authorize and the request fails with no response. Either way the MFE never sees a response it can act on.

This adds LoginRedirectToUnauthorizedMiddleware to Studio. It replaces a redirect to LOGIN_URL with a 401 when the request is anonymous and its Accept header does not include text/html. That is the test SafeSessionMiddleware already uses when it rejects a session, with one difference: a request with no Accept header keeps the redirect here, where SafeSessionMiddleware answers it with 401. Page navigations keep the redirect. The middleware sets the studio_login_redirect_replaced custom attribute when it replaces a redirect.

Roles affected: Course Author. A non-HTML client that relied on following the redirect to the login page gets a 401 instead.

Supporting information

Testing instructions

cms/djangoapps/contentstore/tests/test_middleware.py covers a 401 for anonymous non-HTML GET and POST requests, the redirect kept for HTML and for a missing Accept header, authenticated requests and other responses passing through, and each form of LOGIN_URL. It passes locally (15 tests). Two existing tests asserted the redirect for an anonymous request that does not accept HTML and now expect 401: TestLibraryAccess.test_creation (an AJAX POST) and TestCourseReIndex.test_reindex_course (Accept: application/json).

Against a running Studio, with no session cookie:

  1. curl -i -H 'Accept: application/json' <studio>/xblock/outline/<usage key> returns 401.
  2. curl -i -H 'Accept: text/html' <studio>/xblock/outline/<usage key> returns 302 to /login/.
  3. With fix: restore the Studio session without leaving the page when Studio requests fail frontend-app-authoring#3290, open a course outline in the Authoring MFE and delete the Studio session cookie (leave the LMS session and JWT cookies). On reload the MFE starts one Studio login instead of one per request.

I have not run step 3 end to end.

Deadline

None.

https://claude.ai/code/session_01JxJWMHVXsuXaChbeEpKT8t

Studio views protected by login_required redirect an unauthenticated
request to LOGIN_URL. A script follows that redirect, and with LMS SSO
each request then starts an OAuth login of its own. When a Studio session
expires under an open authoring MFE page, the page's parallel requests
start that many logins against one session. social-core keeps the OAuth
state in a single session key, so the logins overwrite each other and all
but a few fail in validate_state with AuthStateForbidden or
AuthStateMissing, which Studio serves as a 500.

This adds a Studio middleware that replaces a redirect to LOGIN_URL with a
401 when the request is anonymous and its Accept header does not include
text/html. That is the test SafeSessionMiddleware uses when it rejects a
session, except that a request with no Accept header keeps the redirect
here.

Claude-Session: https://claude.ai/code/session_01Rr6xsQQbrmFiSgQWMHJSA2
@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Oct 22, 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.

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.

…ests

TestLibraryAccess.test_creation (ajax_post) and
TestCourseReIndex.test_reindex_course (Accept: application/json) asserted
the login redirect for an anonymous request. Neither request accepts
text/html, so LoginRedirectToUnauthorizedMiddleware now answers it with 401.

Claude-Session: https://claude.ai/code/session_01JxJWMHVXsuXaChbeEpKT8t

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

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