Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): drop the client Expect header before forwarding uploads

Bingxi Zhao (Frank)2f546a555a53
diff --git a/web/lib/streaming-upload-proxy.ts b/web/lib/streaming-upload-proxy.ts
index ceecd7e57..364ac82de 100644
--- a/web/lib/streaming-upload-proxy.ts
+++ b/web/lib/streaming-upload-proxy.ts
@@ -2,8 +2,18 @@ import { resolveBackendApiBase } from "./backend-runtime-config";
// HTTP/1.1 hop-by-hop headers describe one transport connection and must not
// be replayed on the independent frontend -> backend connection.
+//
+// ``expect`` belongs here too: Node's server already answered the client's
+// ``100-continue`` before this handler ever ran, so the negotiation is over.
+// Replaying it is not merely redundant — undici rejects any request carrying
+// the header ("expect header not supported"), which fails the forward *mid
+// upload*. The browser, still writing the body, never reads that 500 and
+// reports a bare "Failed to fetch". Only clients that add the header for
+// large bodies are affected (curl past 1KB, and TUN/HTTP proxies in front of
+// the browser), which is why the breakage looks size-dependent.
const HOP_BY_HOP_HEADERS = new Set([
"connection",
+ "expect",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
diff --git a/web/tests/streaming-upload-proxy.test.ts b/web/tests/streaming-upload-proxy.test.ts
index ebbcd7efc..48c251317 100644
--- a/web/tests/streaming-upload-proxy.test.ts
+++ b/web/tests/streaming-upload-proxy.test.ts
@@ -58,3 +58,31 @@ test("upload proxy streams the original body and preserves the backend response"
assert.equal(response.headers.has("connection"), false);
assert.equal(await response.text(), '{"task_id":"kb_init_1"}');
});
+
+test("upload proxy drops the client's Expect header before forwarding", async () => {
+ // A proxy sitting in front of the browser (or curl past 1KB) adds
+ // `Expect: 100-continue` once the body is large enough. Node answered it
+ // already; replaying it makes undici throw and the forward dies mid-upload.
+ const request = new Request(
+ "http://frontend.test/api/knowledge-bases/kb/upload",
+ {
+ method: "POST",
+ headers: {
+ "content-type": "multipart/form-data; boundary=example",
+ expect: "100-continue",
+ },
+ body: new Blob(["multipart bytes"]).stream(),
+ duplex: "half",
+ } as StreamingRequestInit,
+ );
+
+ const response = await forwardBackendUpload(request, {
+ apiBaseUrl: "http://127.0.0.1:8123",
+ fetchImpl: async (_input, init) => {
+ assert.equal(new Headers(init?.headers).has("expect"), false);
+ return new Response('{"task_id":"kb_upload_1"}', { status: 200 });
+ },
+ });
+
+ assert.equal(response.status, 200);
+});