Handle malformed Authorization headers in HybridAuthBackend without 500s#3489
Conversation
Co-authored-by: zurdi15 <34356590+zurdi15@users.noreply.github.com>
Test Results (postgresql) 1 files 1 suites 4m 35s ⏱️ Results for commit a2775ca. |
Test Results (mariadb) 1 files 1 suites 4m 55s ⏱️ Results for commit a2775ca. |
☂️ Python Coverage
Overall Coverage
New FilesNo new covered files... Modified Files
|
There was a problem hiding this comment.
Pull request overview
This PR hardens backend authentication handling so malformed Authorization headers no longer crash request processing (avoiding HTTP 500s), and adds regression tests to ensure public endpoints like /api/heartbeat remain unaffected.
Changes:
- Added defensive parsing in
HybridAuthBackend.authenticateto avoid unpacking malformedAuthorizationheader values. - Added parametrized tests for malformed
Authorizationheader shapes againstHybridAuthBackend. - Added a heartbeat endpoint regression test to ensure malformed
Authorizationheaders still return HTTP 200.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| backend/handler/auth/hybrid_auth.py | Adds a guard around Authorization header tokenization to prevent ValueError-triggered 500s. |
| backend/tests/handler/auth/test_auth.py | Adds parametrized tests asserting malformed Authorization headers do not authenticate and do not crash. |
| backend/tests/endpoints/test_heartbeat.py | Adds a regression test ensuring /api/heartbeat still succeeds with malformed Authorization headers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if "Authorization" in conn.headers: | ||
| scheme, token = conn.headers["Authorization"].split() | ||
| auth_header_parts = conn.headers["Authorization"].split() | ||
| if len(auth_header_parts) != 2: | ||
| return None | ||
|
|
||
| scheme, token = auth_header_parts |
Malformed
Authorizationheaders were triggering an unguarded unpack inHybridAuthBackend.authenticate, causingValueErrorand HTTP 500s in authentication middleware (including on public routes like/api/heartbeat). This change makes malformed headers fail authentication cleanly instead of crashing request handling.Auth parsing hardening
Authorizationdoes not split into exactly two parts, authentication returnsNone(unauthenticated path) instead of raising.Backend auth coverage
"Foo")"Bearer ")"a b c")Public endpoint regression coverage
Authorizationvalues do not break middleware handling for/api/heartbeat.