Skip to content

fix: return 401 instead of 500 on malformed GitHub OAuth tokens (#951)#952

Merged
maxachis merged 1 commit intodevfrom
fix/951-jwt-decode-error
Apr 20, 2026
Merged

fix: return 401 instead of 500 on malformed GitHub OAuth tokens (#951)#952
maxachis merged 1 commit intodevfrom
fix/951-jwt-decode-error

Conversation

@maxachis
Copy link
Copy Markdown
Contributor

Summary

  • Malformed gh_access_token values sent to /oauth/login-with-github and /oauth/link-to-github were raising jwt.DecodeError (and a leaked ValueError: not enough values to unpack) unhandled, producing 500s and the SolarWinds stack-trace alerts noted in "Not Enough Segments" error on login-with-github and link-to-github #951.
  • SimpleJWT.decode now normalizes header-shape malformedness (missing/non-int kid) to DecodeError so callers only handle one exception type.
  • get_github_user_info now catches DecodeError and surfaces it as 401 Unauthorized, matching the existing treatment of ExpiredSignatureError.

Test plan

  • New unit tests (tests/middleware/test_simple_jwt.py) cover: no-dot, one-dot, empty, and 3-segment-but-invalid tokens.
  • New integration tests (tests/integration/oauth/test_github_token_malformed.py) hit both /oauth/login-with-github and /oauth/link-to-github with the same malformed inputs and assert 401.
  • Existing OAuth token-expired test, auth, and middleware suites still pass.
  • ruff check and basedpyright --level error clean.

Closes #951.

🤖 Generated with Claude Code

Malformed gh_access_token values sent to /oauth/login-with-github and
/oauth/link-to-github were raising jwt.DecodeError out of SimpleJWT.decode
unhandled, producing 500 responses (and stack-trace alerts in SolarWinds).

- Normalize header-shape malformedness in SimpleJWT.decode to DecodeError
  so callers only need to handle one exception type.
- Catch DecodeError in get_github_user_info and surface it as 401.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@josh-chamberlain
Copy link
Copy Markdown
Contributor

I appreciate you persisting and addressing this!

@maxachis maxachis merged commit ef49436 into dev Apr 20, 2026
4 of 5 checks passed
@maxachis maxachis deleted the fix/951-jwt-decode-error branch April 20, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants