Skip to content

fix(responseInterceptor): prevent trailer/content-length conflict#1249

Merged
chimurai merged 1 commit into
masterfrom
ERR_HTTP_TRAILER_INVALID
May 24, 2026
Merged

fix(responseInterceptor): prevent trailer/content-length conflict#1249
chimurai merged 1 commit into
masterfrom
ERR_HTTP_TRAILER_INVALID

Conversation

@chimurai

@chimurai chimurai commented May 24, 2026

Copy link
Copy Markdown
Owner

Causing this error to be thrown:

node:_http_outgoing:522
    throw new ERR_HTTP_TRAILER_INVALID();
    ^

Error [ERR_HTTP_TRAILER_INVALID]: Trailers are invalid with this transfer encoding

closes #830

Summary by CodeRabbit

  • Bug Fixes
    • Fixed proxied response header handling to strip incompatible framing headers while preserving content-length, content-type, set-cookie, and cache-control so responses are correctly framed.
  • Tests
    • Added end-to-end tests covering responses with chunked transfer encoding and trailer headers to verify correct header filtering and body rewriting.
  • Chores
    • Updated changelog with the fix entry.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 24, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 213327a4-bb3a-4c82-aced-614c6b451093

📥 Commits

Reviewing files that changed from the base of the PR and between da3bad3 and 93cf883.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/handlers/response-interceptor.ts
  • test/e2e/response-interceptor.spec.ts

📝 Walkthrough

Walkthrough

responseInterceptor removes trailer and transfer-encoding before setting content-length; copyHeaders now ignores trailer when copying upstream headers. An end-to-end test verifies proxied responses with chunked transfer and trailers are rewritten and that trailer/transfer-encoding headers are omitted while content-length and other headers are preserved or set.

Changes

Trailer Header Handling

Layer / File(s) Summary
Header stripping and filtering
src/handlers/response-interceptor.ts, CHANGELOG.md
Before setting content-length, the interceptor removes trailer and transfer-encoding from the outgoing ServerResponse. copyHeaders now filters out trailer alongside content-encoding and transfer-encoding. A changelog entry documents the fix.
Trailer header handling test
test/e2e/response-interceptor.spec.ts
New e2e test configures an upstream endpoint with chunked transfer encoding and a trailer header, applies responseInterceptor to rewrite the JSON body, and asserts the proxied response omits trailer and transfer-encoding while ensuring content-length, content-type, set-cookie, and cache-control are present or adjusted.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I nibbled headers by the stream,
Found trailers tangled in the seam,
I lop the bits that break the length,
So buffered bytes regain their strength,
Hooray — the proxy hops along, all clean! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: preventing a trailer/content-length conflict in the responseInterceptor, which directly aligns with the primary objective of fixing the ERR_HTTP_TRAILER_INVALID error.
Linked Issues check ✅ Passed All coding requirements from issue #830 are met: trailer/content-length conflict is resolved, disallowed headers are removed when trailers present, and end-to-end test coverage is provided.
Out of Scope Changes check ✅ Passed All changes are directly related to addressing the trailer/content-length conflict issue. The responseInterceptor modifications, copyHeaders adjustments, and test additions are all within scope of issue #830.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ERR_HTTP_TRAILER_INVALID

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented May 24, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/http-proxy-middleware@1249

commit: da3bad3

@coveralls

coveralls commented May 24, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 94.697% (+0.04%) from 94.656% — ERR_HTTP_TRAILER_INVALID into master

Causing this error to be thrown:

```shell
node:_http_outgoing:522
    throw new ERR_HTTP_TRAILER_INVALID();
    ^

Error [ERR_HTTP_TRAILER_INVALID]: Trailers are invalid with this transfer encoding
```

---------

Co-authored-by: zibo <ziboilihua@gmail.com>
@chimurai chimurai force-pushed the ERR_HTTP_TRAILER_INVALID branch from da3bad3 to 93cf883 Compare May 24, 2026 20:16
@chimurai chimurai enabled auto-merge (squash) May 24, 2026 20:17
@chimurai chimurai merged commit 6c41eae into master May 24, 2026
22 of 23 checks passed
@chimurai chimurai deleted the ERR_HTTP_TRAILER_INVALID branch May 24, 2026 20:17
@chimurai chimurai added this to the v4.1 milestone May 24, 2026
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.

2 participants