Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8319767

Browse files
authored
fix(proxy): strip transfer-encoding header from proxied response (#1248)
1 parent bb10838 commit 8319767

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/utils/proxy.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ export async function proxy(
8888
const cookies: string[] = [];
8989

9090
for (const [key, value] of response.headers.entries()) {
91-
if (key === "content-encoding") {
92-
continue;
93-
}
94-
if (key === "content-length") {
91+
if (
92+
key === "content-encoding" ||
93+
key === "content-length" ||
94+
key === "transfer-encoding"
95+
) {
9596
continue;
9697
}
9798
if (key === "set-cookie") {

test/proxy.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,54 @@ describeMatrix("proxy", (t, { it, expect, describe }) => {
168168
expect(resText).toEqual(message);
169169
});
170170

171+
it("preserves custom headers from proxied response", async () => {
172+
t.app.all("/debug", (event) => {
173+
event.res.headers.set("x-custom-header", "preserved");
174+
event.res.headers.set("content-type", "text/plain");
175+
return "hello";
176+
});
177+
178+
t.app.all("/", (event) => {
179+
return proxyRequest(event, "/debug");
180+
});
181+
182+
const res = await t.fetch("/", {
183+
method: "GET",
184+
});
185+
186+
// Custom headers should be preserved
187+
expect(res.headers.get("x-custom-header")).toEqual("preserved");
188+
// Content should be proxied correctly
189+
expect(await res.text()).toEqual("hello");
190+
});
191+
192+
it.runIf(t.target === "web")(
193+
"strips transfer-encoding header from proxied response",
194+
async () => {
195+
t.app.all("/debug", (event) => {
196+
// Simulate an upstream that sends transfer-encoding header
197+
event.res.headers.set("transfer-encoding", "chunked");
198+
event.res.headers.set("x-custom-header", "preserved");
199+
return "hello";
200+
});
201+
202+
t.app.all("/", (event) => {
203+
return proxyRequest(event, "/debug");
204+
});
205+
206+
const res = await t.fetch("/", {
207+
method: "GET",
208+
});
209+
210+
// transfer-encoding should be stripped by proxy()
211+
// (only testable in web mode; node's HTTP server re-adds it for streaming)
212+
expect(res.headers.has("transfer-encoding")).toBe(false);
213+
// Other headers should be preserved
214+
expect(res.headers.get("x-custom-header")).toEqual("preserved");
215+
expect(await res.text()).toEqual("hello");
216+
},
217+
);
218+
171219
it(
172220
"can handle failed proxy requests gracefully",
173221
async () => {

0 commit comments

Comments
 (0)