Commit dfe9a86
fix(oauth): Preserve session payload across cycle_key() in authorize flow (#111738)
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.
<!--
Sentry employees and contractors can delete or ignore the following.
-->
### 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.
Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com>
Co-authored-by: michelletran-sentry <167130096+michelletran-sentry@users.noreply.github.com>1 parent 486e7c6 commit dfe9a86
1 file changed
+8
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
339 | 339 | | |
340 | 340 | | |
341 | 341 | | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
342 | 345 | | |
343 | 346 | | |
344 | 347 | | |
345 | | - | |
346 | | - | |
347 | | - | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
348 | 353 | | |
349 | 354 | | |
350 | 355 | | |
| |||
0 commit comments