Skip to content

fix: handle invalid HTTP/2 headers gracefully (#4356)#5077

Open
mcollina wants to merge 2 commits intomainfrom
fix/http2-invalid-headers
Open

fix: handle invalid HTTP/2 headers gracefully (#4356)#5077
mcollina wants to merge 2 commits intomainfrom
fix/http2-invalid-headers

Conversation

@mcollina
Copy link
Copy Markdown
Member

@mcollina mcollina commented Apr 20, 2026

When session.request() throws synchronously due to invalid HTTP/2 headers (e.g., duplicate headers like "content-type" and "Content-Type"), the error was not being caught, causing an uncaught exception.

The fix wraps all session.request() calls with try-catch blocks. When an error is caught, only the request is errored (via util.errorRequest) and the function returns false. The session is not destroyed because the error is caused by the client sending invalid headers, not the server.

This allows the client to continue using the same session for subsequent requests, and the error is properly caught by user code.

Closes #4356

@mcollina mcollina requested a review from metcoder95 April 20, 2026 16:29
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 81.81818% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.08%. Comparing base (a1d6766) to head (c6bb580).
⚠️ Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
lib/dispatcher/client-h2.js 81.81% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5077      +/-   ##
==========================================
- Coverage   93.10%   93.08%   -0.03%     
==========================================
  Files         110      110              
  Lines       35770    35853      +83     
==========================================
+ Hits        33303    33372      +69     
- Misses       2467     2481      +14     

☔ 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.

Comment thread lib/dispatcher/client-h2.js Outdated
Comment on lines +534 to +542
// An error here means the server sent invalid HTTP/2 headers
// (e.g., HTTP/1 headers like "http2-settings"). This is a server error.
// We destroy the socket/session and error the request so the client
// can retry with a new connection.
util.errorRequest(client, request, err)
const socket = session[kSocket]
session.destroy(err)
util.destroy(socket, err)
return false
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don’t know how h2 works, but did this cause other "valid", ongoing requests that use the same socket/session to be interrupted?

When session.request() throws synchronously due to invalid HTTP/2 headers
(e.g., duplicate headers like 'content-type' and 'Content-Type'), the error
was not being caught, causing an uncaught exception.

The fix wraps all session.request() calls with try-catch blocks. When an
error is caught, only the request is errored (via util.errorRequest) and
the function returns false. The session is not destroyed because the error
is caused by the client sending invalid headers, not the server.

This allows the client to continue using the same session for subsequent
requests, and the error is properly caught by user code.

Closes #4356
@mcollina mcollina force-pushed the fix/http2-invalid-headers branch from dcf9d03 to 500eb60 Compare April 21, 2026 09:34
Copy link
Copy Markdown
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

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

Tests seems, failing; rest lgtm

@mcollina
Copy link
Copy Markdown
Member Author

@metcoder95 PTAL

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.

Invalid HTTP2 headers should be recoverable

4 participants