Skip to content

fix(oauth): Preserve session payload across cycle_key() in authorize flow#111738

Merged
michelletran-sentry merged 2 commits intomasterfrom
seer/fix/oauth-session-keyerror
Mar 30, 2026
Merged

fix(oauth): Preserve session payload across cycle_key() in authorize flow#111738
michelletran-sentry merged 2 commits intomasterfrom
seer/fix/oauth-session-keyerror

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry bot commented Mar 27, 2026

This PR addresses a KeyError: 'oa2' that occurred in the /oauth/authorize/ endpoint when an unauthenticated user attempted to log in during an OAuth authorization flow.

Root Cause:
The _logged_out_post method in oauth_authorize.py calls request.session.cycle_key() to prevent session fixation attacks. However, cycle_key() regenerates the session ID and, in doing so, was inadvertently clearing the OAuth payload stored under request.session["oa2"]. Consequently, when the code attempted to access request.session["oa2"]["uid"] immediately after cycle_key(), it resulted in a KeyError.

Solution:
The fix involves preserving the oa2 payload across the cycle_key() operation. Before request.session.cycle_key() is called, the existing request.session["oa2"] data is retrieved and stored in a temporary variable. After cycle_key() has regenerated the session, this saved payload is restored to the new session, and the uid is updated with the authenticated user's ID. Finally, request.session.modified is set to True to ensure the changes are persisted.

This approach maintains the security benefits of cycle_key() while ensuring the necessary OAuth context is not lost during the login process.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Mar 27, 2026
@markstory markstory marked this pull request as ready for review March 27, 2026 19:03
@markstory markstory requested a review from a team as a code owner March 27, 2026 19:03
@markstory markstory added Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests and removed Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests labels Mar 27, 2026
@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Mar 30, 2026
@michelletran-sentry michelletran-sentry added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Mar 30, 2026
@michelletran-sentry michelletran-sentry enabled auto-merge (squash) March 30, 2026 16:42
@michelletran-sentry michelletran-sentry merged commit dfe9a86 into master Mar 30, 2026
72 of 86 checks passed
@michelletran-sentry michelletran-sentry deleted the seer/fix/oauth-session-keyerror branch March 30, 2026 16:54
@github-actions github-actions bot locked and limited conversation to collaborators Apr 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants