diff --git a/apps/api/test/e2e/edge-upstream-down-page.e2e.test.ts b/apps/api/test/e2e/edge-upstream-down-page.e2e.test.ts index a472b4a6d..9e568c362 100644 --- a/apps/api/test/e2e/edge-upstream-down-page.e2e.test.ts +++ b/apps/api/test/e2e/edge-upstream-down-page.e2e.test.ts @@ -125,10 +125,10 @@ interface Answer { } /** One request with an explicit `Host`, which `fetch` refuses to set. */ -function ask(port: number, path: string, host: string): Promise { +function ask(port: number, path: string, host: string, cookie?: string): Promise { return new Promise((resolve, reject) => { const req = httpRequest( - { host: "127.0.0.1", port, path, method: "GET", headers: { Host: host } }, + { host: "127.0.0.1", port, path, method: "GET", maxHeaderSize: 64 * 1024, headers: { Host: host, ...(cookie ? { Cookie: cookie } : {}) } }, (res) => { const chunks: Buffer[] = []; res.on("data", (c: Buffer) => chunks.push(c)); @@ -180,7 +180,20 @@ http { # An app that answers 502 ITSELF. Its body must reach the client untouched. server { listen ${PORT_OWN_502}; location / { return 502 "BODY-FROM-APP"; } } # A healthy app, so "the page never fires on a working request" is observable. - server { listen ${PORT_HEALTHY}; location / { return 200 "BODY-FROM-HEALTHY-APP"; } } + server { + listen ${PORT_HEALTHY}; + # The stand-in app must accept the headers forwarded by the edge too. + large_client_header_buffers 4 32k; + location / { return 200 "BODY-FROM-HEALTHY-APP"; } + location = /large-headers { + content_by_lua_block { + local cookies = {} + for i = 1, 6 do cookies[i] = "session." .. i .. "=" .. string.rep("a", 3000) .. "; Path=/; HttpOnly" end + ngx.header["Set-Cookie"] = cookies + ngx.print("AUTH-RESPONSE") + } + } + } include ${SITES_DIR}/*.conf; } @@ -230,6 +243,10 @@ http { tls: false, targetUrl: `http://127.0.0.1:${PORT_HEALTHY}`, }), + unbuffered: await renderVhost({ + domain: "unbuffered.test", tls: false, + targetUrl: `http://127.0.0.1:${PORT_HEALTHY}`, proxy: { proxyBuffering: false }, + }), // No upstream at all: the page must not be in this vhost, so a missing file stays the // plain 404 it has always been. staticsite: await renderVhost({ domain: "staticsite.test", tls: false, staticRoot: WWW }), @@ -258,7 +275,8 @@ http { const container = await runtime.docker.createContainer({ Image: image, Entrypoint: ["sh", "-c"], - Cmd: [script], + // Keep each argv entry below Linux's per-string limit as Lua fixtures grow. + Cmd: ['printf "%s" "$@" | sh', "openship-edge-e2e", ...script.match(/[\s\S]{1,32768}/g)!], Tty: true, ExposedPorts: { "80/tcp": {} }, HostConfig: { @@ -271,7 +289,7 @@ http { const info = await container.inspect(); const bound = info.NetworkSettings.Ports?.["80/tcp"]?.[0]?.HostPort; - if (!bound) throw new Error("docker published no host port for 80/tcp"); + if (!bound) throw new Error("docker published no host port for 80/tcp: " + (await container.logs({ stdout: true, stderr: true })).toString()); const published = Number(bound); // Ready when it answers, dead when it exits — and a dead edge must report the reason @@ -331,6 +349,17 @@ http { expect(res.body).toBe("BODY-FROM-HEALTHY-APP"); }); + it.each(["healthy.test", "unbuffered.test"])("relays large auth headers and accepts the next cookie-bearing request: %s", async host => { + const res = await ask(port, "/large-headers", host); + expect(res.status).toBe(200); + expect(res.body).toBe("AUTH-RESPONSE"); + const cookies = res.headers["set-cookie"] as string[]; + expect(cookies).toHaveLength(6); + const cookie = cookies.map(value => value.split(";")[0]).join("; "); + expect(cookie.length).toBeGreaterThan(16 * 1024); + expect((await ask(port, "/", host, cookie)).status).toBe(200); + }); + it("answers text/html even when the URI looks like a stylesheet", async () => { // `return` with a body types the response from the request URI, so without the empty // `types { }` map a down app's `/app.css` would be `Content-Type: text/css` holding HTML diff --git a/apps/api/test/modules/projects/edge-config-read.test.ts b/apps/api/test/modules/projects/edge-config-read.test.ts index 6308cfe1d..13b961a24 100644 --- a/apps/api/test/modules/projects/edge-config-read.test.ts +++ b/apps/api/test/modules/projects/edge-config-read.test.ts @@ -44,12 +44,17 @@ const project = (over: Partial = {}) => ({ id: "p1", slug: "app", routingConfig: null, ...over }) as Project; /** A live vhost as the nginx parser would hand it back. */ +const managedHeaderBuffers = { + proxyBufferSize: "32k", proxyBuffers: "8 32k", proxyBusyBuffersSize: "64k", + largeClientHeaderBuffers: "4 32k", +}; const site = (over: Partial = {}): ImportedSite => ({ serverNames: ["app.example.com"], ssl: true, target: { kind: "proxy", url: "http://127.0.0.1:3000" }, ...over, + proxy: { ...managedHeaderBuffers, ...over.proxy }, }) as ImportedSite; /** @@ -152,7 +157,20 @@ describe("readProjectEdgeConfig — agreement", () => { const r = await readProjectEdgeConfig( project({ routingConfig: { proxy: { clientMaxBodySize: "50m" } } }), ); - expect(r.hosts[0]!.directives.map((d) => d.key)).toEqual(["clientMaxBodySize"]); + expect(r.hosts[0]!.directives.map((d) => d.key).sort()).toEqual([ + "clientMaxBodySize", ...Object.keys(managedHeaderBuffers), + ].sort()); + }); + + it("flags an older vhost until its new managed header defaults are applied", async () => { + box({ sites: { "app.example.com": site({ proxy: { + proxyBufferSize: undefined, proxyBuffers: undefined, proxyBusyBuffersSize: undefined, + largeClientHeaderBuffers: undefined, + } }) } }); + const report = await readProjectEdgeConfig(project()); + expect(report.hosts[0]!.driftCount).toBe(4); + expect(byKey(report, "proxyBufferSize")).toMatchObject({ expected: "32k", drift: true }); + expect(report.hosts[0]!.adoptable).toEqual({}); }); it("passes the box's nginx version through, since the floor depends on it", async () => { diff --git a/packages/adapters/src/infra/nginx.test.ts b/packages/adapters/src/infra/nginx.test.ts index b0522100b..8fab09a4a 100644 --- a/packages/adapters/src/infra/nginx.test.ts +++ b/packages/adapters/src/infra/nginx.test.ts @@ -1630,9 +1630,9 @@ describe("NginxProvider config generation", () => { }); describe("renderProxyOptions (curated reverse-proxy directives)", () => { - test("empty / undefined → no directives", () => { - expect(renderProxyOptions(undefined)).toBe(""); - expect(renderProxyOptions({})).toBe(""); + test("empty / undefined → bounded header defaults", () => { + expect(renderProxyOptions(undefined)).toContain("proxy_buffer_size 32k;"); + expect(renderProxyOptions({})).toBe(renderProxyOptions(undefined)); }); test("renders each valid directive", () => { @@ -1666,12 +1666,33 @@ describe("renderProxyOptions (curated reverse-proxy directives)", () => { proxyReadTimeout: "60x", proxySendTimeout: "0s", } as never); - expect(out).toBe(""); + expect(out).toBe(renderProxyOptions(undefined)); expect(out).not.toContain("add_header"); }); }); describe("registerRoute renders proxy directives at server scope", () => { + test("accepts bounded large session headers without changing response streaming", async () => { + const { nginx, conf } = setup({ certDomains: ["app.example.com"] }); + await nginx.registerRoute({ ...PROXY, proxy: { proxyBuffering: false } }); + for (const block of serverBlocks(conf("app-example-com")!)) { + expect(block).toContain("large_client_header_buffers 4 32k;"); + expect(block).toContain("proxy_buffer_size 32k;"); + expect(block).toContain("proxy_buffers 8 32k;"); + expect(block).toContain("proxy_busy_buffers_size 64k;"); + expect(block).toContain("proxy_buffering off;"); + expect(block.match(/proxy_buffer_size /g)).toHaveLength(1); + } + }); + test("preserves an explicitly configured response-buffer tuple", async () => { + const { nginx, conf } = setup({ certDomains: ["app.example.com"] }); + await nginx.registerRoute({ ...PROXY, proxy: { proxyBufferSize: "16k", largeClientHeaderBuffers: "4 16k" } }); + const output = conf("app-example-com")!; + expect(output).toContain("proxy_buffer_size 16k;"); + expect(output).toContain("large_client_header_buffers 4 16k;"); + expect(output).not.toContain("proxy_buffers "); + expect(output).not.toContain("proxy_busy_buffers_size "); + }); test("route.proxy → directive present in the generated vhost", async () => { const { nginx, conf } = setup({ certDomains: ["app.example.com"] }); await nginx.registerRoute({ ...PROXY, proxy: { clientMaxBodySize: "50m" } }); @@ -1916,13 +1937,13 @@ describe("render → parse round-trip (the renderer and the read-back agree)", ( expect(site.proxy).toEqual(expectedProxyState(FULL_PROXY, { tls: true })); }); - test("an unset directive comes back absent, not defaulted", async () => { + test("shared defaults read back without false drift", async () => { const { nginx, conf } = setup({ certDomains: ["app.example.com"] }); await nginx.registerRoute({ ...PROXY, proxy: { clientMaxBodySize: "50m" } }); const site = await scanBack(conf("app-example-com")!); - expect(site.proxy).toEqual({ clientMaxBodySize: "50m" }); - expect(site.proxyRaw).toEqual({ clientMaxBodySize: "50m" }); + expect(site.proxy).toEqual(expectedProxyState({ clientMaxBodySize: "50m" }, { tls: true })); + expect(site.proxyRaw).toEqual(site.proxy); }); test("gzip's companion defaults read back as expected, not as drift", async () => { @@ -1933,7 +1954,6 @@ describe("render → parse round-trip (the renderer and the read-back agree)", ( await nginx.registerRoute({ ...PROXY, proxy: { gzip: true } }); const site = await scanBack(conf("app-example-com")!); - expect(site.proxy).toEqual({ gzip: true, gzipMinLength: 1024 }); expect(site.proxy).toEqual(expectedProxyState({ gzip: true }, { tls: true })); }); diff --git a/packages/core/src/proxy-settings.test.ts b/packages/core/src/proxy-settings.test.ts index f332eb843..33a5aa686 100644 --- a/packages/core/src/proxy-settings.test.ts +++ b/packages/core/src/proxy-settings.test.ts @@ -243,10 +243,12 @@ describe("render → parse round-trip", () => { }); describe("resolveProxyDirectives — the one gate", () => { - it("emits nothing for empty settings", () => { - expect(resolveProxyDirectives(undefined, { scope: "all" })).toEqual([]); - expect(resolveProxyDirectives({}, { scope: "all" })).toEqual([]); - expect(resolveProxyDirectives({ clientMaxBodySize: "bad" }, { scope: "all" })).toEqual([]); + it("emits the same bounded defaults for empty and invalid settings", () => { + const defaults = resolveProxyDirectives(undefined, { scope: "all" }); + expect(defaults).toHaveLength(4); + expect(defaults).toContainEqual(expect.objectContaining({ directive: "proxy_buffer_size", text: "32k" })); + expect(resolveProxyDirectives({}, { scope: "all" })).toEqual(defaults); + expect(resolveProxyDirectives({ clientMaxBodySize: "bad" }, { scope: "all" })).toEqual(defaults); }); it("keeps TLS-only directives out of the shared scope", () => { @@ -281,7 +283,7 @@ describe("resolveProxyDirectives — the one gate", () => { it("carries gzip's companions when gzip is on, and drops them when it's off", () => { const on = resolveProxyDirectives({ gzip: true }, { scope: "shared" }); - expect(on.map((d) => d.directive)).toEqual([ + expect(on.filter(d => d.directive.startsWith("gzip")).map((d) => d.directive)).toEqual([ "gzip", "gzip_types", "gzip_vary", @@ -291,8 +293,8 @@ describe("resolveProxyDirectives — the one gate", () => { expect(on.find((d) => d.directive === "gzip_types")!.text).toContain("application/json"); const off = resolveProxyDirectives({ gzip: false }, { scope: "shared" }); - expect(off.map((d) => d.directive)).toEqual(["gzip"]); - expect(off[0]!.text).toBe("off"); + expect(off.filter(d => d.directive.startsWith("gzip")).map((d) => d.directive)).toEqual(["gzip"]); + expect(off.find(d => d.directive === "gzip")!.text).toBe("off"); }); it("lets an explicit value beat the companion default", () => { @@ -340,6 +342,7 @@ describe("expectedProxyState — what a read-back must compare against", () => { // and a read-back that compared the live vhost against the raw settings saw a // value nobody asked for and reported drift forever. expect(expectedProxyState({ gzip: true }, { tls: true })).toEqual({ + ...expectedProxyState(undefined, { tls: true }), gzip: true, gzipMinLength: 1024, }); @@ -364,14 +367,15 @@ describe("expectedProxyState — what a read-back must compare against", () => { expect(expectedProxyState(SAMPLE, { tls: true }).http2).toBe(true); }); - it("expects nothing at all when the project configured nothing", () => { - expect(expectedProxyState(null, { tls: true })).toEqual({}); - expect(expectedProxyState({ clientMaxBodySize: "20M" }, { tls: true })).toEqual({}); + it("expects effective defaults when the project configured nothing", () => { + expect(expectedProxyState(null, { tls: true })).toMatchObject({ proxyBufferSize: "32k", largeClientHeaderBuffers: "4 32k" }); + expect(expectedProxyState({ clientMaxBodySize: "20M" }, { tls: true })).toEqual(expectedProxyState(null, { tls: true })); }); it("reflects the version gate, so an old box isn't reported as drifting", () => { - expect(expectedProxyState({ http2: true }, { tls: true, nginxVersion: [1, 24, 0] })).toEqual({}); + expect(expectedProxyState({ http2: true }, { tls: true, nginxVersion: [1, 24, 0] })).toEqual(expectedProxyState(null, { tls: true })); expect(expectedProxyState({ http2: true }, { tls: true, nginxVersion: [1, 27, 1] })).toEqual({ + ...expectedProxyState(null, { tls: true }), http2: true, }); }); diff --git a/packages/core/src/proxy-settings.ts b/packages/core/src/proxy-settings.ts index 4ee6f6da8..206abbec4 100644 --- a/packages/core/src/proxy-settings.ts +++ b/packages/core/src/proxy-settings.ts @@ -383,8 +383,17 @@ export function resolveProxyDirectives( opts: { scope: "shared" | "tls" | "all"; nginxVersion?: NginxVersion | null }, ): ResolvedProxyDirective[] { const p = sanitizeProxySettings(settings); - if (!p) return []; - const values = p as Record; + // Cookies can span several response fields and return as one request field. + // Resolve these bounded defaults here so rendering and drift detection agree. + // Preserve any explicit response-buffer tuple instead of mixing sizes from + // different policies, which can violate nginx's buffer-size constraints. + const responseBuffers = p?.proxyBufferSize !== undefined || + p?.proxyBuffers !== undefined || p?.proxyBusyBuffersSize !== undefined + ? {} + : { proxyBufferSize: "32k", proxyBuffers: "8 32k", proxyBusyBuffersSize: "64k" }; + const values: Record = { + largeClientHeaderBuffers: "4 32k", ...responseBuffers, ...p, + }; // A companion is a DEFAULT: an explicitly-set directive wins, and we must not // emit the same directive twice. const explicit = new Set(