Skip to content

Handle malformed Authorization headers in HybridAuthBackend without 500s#3489

Merged
zurdi15 merged 2 commits into
masterfrom
copilot/fix-auth-header-handling
Jun 6, 2026
Merged

Handle malformed Authorization headers in HybridAuthBackend without 500s#3489
zurdi15 merged 2 commits into
masterfrom
copilot/fix-auth-header-handling

Conversation

Copilot AI commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Malformed Authorization headers were triggering an unguarded unpack in HybridAuthBackend.authenticate, causing ValueError and 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

    • Guarded header tokenization before unpacking.
    • If Authorization does not split into exactly two parts, authentication returns None (unauthenticated path) instead of raising.
  • Backend auth coverage

    • Added parametrized auth-backend tests for malformed header shapes:
      • single token ("Foo")
      • empty bearer token shape ("Bearer ")
      • extra segments ("a b c")
  • Public endpoint regression coverage

    • Added heartbeat test cases asserting malformed Authorization values do not break middleware handling for /api/heartbeat.
if "Authorization" in conn.headers:
    auth_header_parts = conn.headers["Authorization"].split()
    if len(auth_header_parts) != 2:
        return None

    scheme, token = auth_header_parts

Co-authored-by: zurdi15 <34356590+zurdi15@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ValueError on malformed Authorization header in HybridAuthBackend Handle malformed Authorization headers in HybridAuthBackend without 500s Jun 6, 2026
Copilot AI requested a review from zurdi15 June 6, 2026 22:23
@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Test Results (postgresql)

    1 files      1 suites   4m 35s ⏱️
1 561 tests 1 561 ✅ 0 💤 0 ❌
1 563 runs  1 563 ✅ 0 💤 0 ❌

Results for commit a2775ca.

@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Test Results (mariadb)

    1 files      1 suites   4m 55s ⏱️
1 561 tests 1 561 ✅ 0 💤 0 ❌
1 563 runs  1 563 ✅ 0 💤 0 ❌

Results for commit a2775ca.

@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
17002 12247 72% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
backend/handler/auth/hybrid_auth.py 92% 🟢
TOTAL 92% 🟢

updated for commit: a2775ca by action🐍

@zurdi15
zurdi15 marked this pull request as ready for review June 6, 2026 22:36
Copilot AI review requested due to automatic review settings June 6, 2026 22:36
@zurdi15
zurdi15 merged commit bb82cf4 into master Jun 6, 2026
11 of 13 checks passed
@zurdi15
zurdi15 deleted the copilot/fix-auth-header-handling branch June 6, 2026 22:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.authenticate to avoid unpacking malformed Authorization header values.
  • Added parametrized tests for malformed Authorization header shapes against HybridAuthBackend.
  • Added a heartbeat endpoint regression test to ensure malformed Authorization headers 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.

Comment on lines 27 to +32
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
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.

HybridAuthBackend.authenticate raises ValueError → HTTP 500 on a malformed Authorization header (should be 401)

3 participants