Skip to content

fix: prevent panic in extractSubject for malformed bearer Authorization header - #2053

Merged
nacx merged 1 commit into
envoyproxy:mainfrom
ashnaaseth2325-oss:fix/extractsubject-bounds-check
Apr 16, 2026
Merged

fix: prevent panic in extractSubject for malformed bearer Authorization header#2053
nacx merged 1 commit into
envoyproxy:mainfrom
ashnaaseth2325-oss:fix/extractsubject-bounds-check

Conversation

@ashnaaseth2325-oss

Copy link
Copy Markdown
Contributor

Description

Summary

This PR fixes a potential index out of bounds panic in extractSubject when the Authorization header contains a bearer scheme without a token. It adds a defensive length check and a corresponding test case in handlers_test.go.


Fix

// BEFORE
parts := strings.SplitN(authzHeader, " ", 2)
if !strings.EqualFold(parts[0], "bearer") {
    return ""
}
_, _, _ = jwt.NewParser().ParseUnverified(parts[1], &claims)


// AFTER
parts := strings.SplitN(authzHeader, " ", 2)
if !strings.EqualFold(parts[0], "bearer") {
    return ""
}
if len(parts) < 2 {
    return ""
}
_, _, _ = jwt.NewParser().ParseUnverified(parts[1], &claims)

Verification

All unit tests pass locally with go test ./internal/mcpproxy/..., including the newly added case for Authorization: bearer. The change was also validated by confirming that malformed headers are handled gracefully without causing a panic.

Add a len(parts) < 2 check before accessing parts[1] in extractSubject
to prevent a panic when the Authorization header contains only the bearer
scheme with no token. Add a test case covering this path.

Signed-off-by: ashnaaseth2325-oss <ashnaaseth2325@gmail.com>
@ashnaaseth2325-oss
ashnaaseth2325-oss requested a review from a team as a code owner April 15, 2026 23:51
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 15, 2026
@ashnaaseth2325-oss ashnaaseth2325-oss changed the title Fix: Panic in extractSubject for malformed bearer Authorization header fix: prevent panic in extractSubject for malformed bearer Authorization header Apr 15, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.38%. Comparing base (7cf2c44) to head (d43c7d1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2053   +/-   ##
=======================================
  Coverage   84.38%   84.38%           
=======================================
  Files         130      130           
  Lines       18114    18116    +2     
=======================================
+ Hits        15285    15287    +2     
  Misses       1883     1883           
  Partials      946      946           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ashnaaseth2325-oss

Copy link
Copy Markdown
Contributor Author

Hello @nacx
This PR adds a small guard to handle Authorization: bearer safely and prevent a panic in extractSubject. It also includes a test for this case. All tests pass locally, happy to make any changes if needed!

@nacx
nacx merged commit 9ed89f7 into envoyproxy:main Apr 16, 2026
35 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants