Skip to content

Verify 401 responses are not tagged as errors (fixes #838)#849

Open
mitchh456 wants to merge 2 commits intomasterfrom
fix/838-verify-401-not-tagged-as-error
Open

Verify 401 responses are not tagged as errors (fixes #838)#849
mitchh456 wants to merge 2 commits intomasterfrom
fix/838-verify-401-not-tagged-as-error

Conversation

@mitchh456
Copy link
Copy Markdown
Contributor

Summary

  • Audited the Starlette/FastAPI middleware (src/scout_apm/async_/starlette.py) and confirmed that Scout only tags responses as errors when 500 <= status <= 599 (line 60). Scout does not inspect Authorization headers or bearer tokens.
  • Searched the entire scout_apm package for any bearer/auth token handling — none exists. The only auth-related references are parameter name filtering in web_requests.py (redacting sensitive query params like token, auth_token, etc.) and reading scope["user"] from Starlette's AuthenticationMiddleware for username tagging.
  • Added a 401 endpoint and test (test_return_unauthorized_not_tagged_as_error) that explicitly verifies a 401 Unauthorized response is tracked but not tagged as an error.

Conclusion: When a FastAPI OAuth2 dependency rejects an empty bearer token and returns 401, Scout correctly records the request without marking it as an error. No code changes to the middleware are needed — this PR adds a regression test to lock in the correct behavior.

Test plan

  • Verify the new test_return_unauthorized_not_tagged_as_error test passes
  • Confirm existing Starlette integration tests still pass

Closes #838

🤖 Generated with Claude Code

mitchh456 and others added 2 commits March 31, 2026 19:38
Adds a test endpoint returning 401 and a corresponding test that confirms
Scout's Starlette/FastAPI middleware correctly tracks 401 Unauthorized
responses without tagging them as errors. Only 5xx responses trigger the
error tag, so a FastAPI OAuth2 empty bearer token rejection (which returns
401) is properly categorized as a client error, not a server error.

Closes #838

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

FastAPI OAuth2 empty bearer token fix changes 200 to 401 — verify error categorization

1 participant