Repository navigation
feat(oauth): Add PKCE support for OAuth2 authorization code flow - #104418
Conversation
|
This PR has a migration; here is the generated SQL for for --
-- Add field code_challenge to apigrant
--
ALTER TABLE "sentry_apigrant" ADD COLUMN "code_challenge" varchar(128) NULL;
--
-- Add field code_challenge_method to apigrant
--
ALTER TABLE "sentry_apigrant" ADD COLUMN "code_challenge_method" varchar(10) NULL; |
❌ 2 Tests Failed:
View the top 2 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
Moves PKCE validation and OAuth security checks from the endpoint layer to the model layer. The oauth_token endpoint now calls ApiToken.from_grant() instead of duplicating token creation logic with .objects.create(). Enhances from_grant() to handle redirect_uri binding and PKCE verification inside the lock, fixing TOCTOU race conditions where an application could be deactivated or grant could be reused between validation and token creation. All validations now properly invalidate grants on failure per RFC 6749 §10.5.
|
i think we're good to go |
michelletran-sentry
left a comment
There was a problem hiding this comment.
Generally LGTM. There's just the one question about removing the status filter for getting ApiApplication.
| @@ -286,7 +325,18 @@ def _logged_out_post( | |||
| response = super().post(request, application=application, **kwargs) | |||
| # once they login, bind their user ID | |||
| if request.user.is_authenticated: | |||
| request.session["oa2"]["uid"] = request.user.id | |||
| # Regenerate session to prevent session fixation attacks | |||
There was a problem hiding this comment.
I think this prevents attackers (who somehow has the user's session id before they're logged in) from reusing that session after the user has logged in (so now, the attacker can call /authorize again and it will see the user as already authenticated). I'm not sure whether it's simple to get/inject a session id onto a Sentry cookie, so this might not be that exploitable. But from what I've read (on ChatGPT and other sources), cycling the session id after authenticating is a best practice on this endpoint.
| ) | ||
| logger.warning( | ||
| "Token request for inactive application", | ||
| extra={"client_id": client_id, "application_id": application.id}, |
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 11 months ago
In general, to fix clear-text logging of sensitive information, either stop logging the sensitive fields, or replace them with non-sensitive derivatives (e.g., hashes, redacted forms, or booleans like “exists/does_not_exist”) that still allow debugging without exposing secrets. For OAuth flows, you should never log client secrets, access tokens, or refresh tokens; client IDs may be logged only if you are certain they are not considered sensitive in your environment, but when in doubt, redact or transform them.
For this specific issue, we only need to change the log call around lines 170–173. We should avoid logging the raw client_id while still allowing operators to correlate events to an application when needed. A safe approach without changing existing behavior elsewhere is:
- Replace
extra={"client_id": client_id, "application_id": application.id}with:- either an indicator like
client_id_provided: bool(client_id)and keepapplication_id, or - a hashed version of
client_id(e.g., usinghashlib.sha256(client_id.encode()).hexdigest()), which avoids logging the clear-text value.
- either an indicator like
Because we want minimal impact and no new imports if possible, using a boolean flag is simplest and does not require new dependencies. This keeps the log useful (“was there a client id?”) but ensures that no value derived from user-supplied credentials is logged. Concretely:
- In
src/sentry/web/frontend/oauth_token.py, locate thelogger.warning("Token request for inactive application", extra={...})call. - Replace the
extradict to removeclient_id, and optionally addclient_id_provided(derived frombool(client_id)), leavingapplication_idunchanged.
No new methods or imports are required for this fix.
| @@ -169,7 +169,10 @@ | ||
| ) | ||
| logger.warning( | ||
| "Token request for inactive application", | ||
| extra={"client_id": client_id, "application_id": application.id}, | ||
| extra={ | ||
| "client_id_provided": bool(client_id), | ||
| "application_id": application.id, | ||
| }, | ||
| ) | ||
| # For authorization_code, invalidate the grant per RFC 6749 §10.5 | ||
| if grant_type == GrantTypes.AUTHORIZATION: |
|
YOLO |
Implements RFC 7636 Proof Key for Code Exchange (PKCE) to protect OAuth2 authorization code grants from interception attacks. Adds code_challenge and code_challenge_method support to the authorize endpoint, and validates code_verifier in the token endpoint. Supports both S256 and plain methods.
Refs #99002
Replaces #99452