From 7595cf396f0fba0b3f72903799eaac20be07a761 Mon Sep 17 00:00:00 2001 From: paulaboks Date: Thu, 12 Nov 2026 14:57:38 -0300 Subject: [PATCH] Don't redirect to https if the request didn't come from https --- packages/fresh/src/middlewares/csp.ts | 20 ++++++++-- packages/fresh/src/middlewares/csp_test.tsx | 41 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/packages/fresh/src/middlewares/csp.ts b/packages/fresh/src/middlewares/csp.ts index 0f0b6f55..0db1679d 100644 --- a/packages/fresh/src/middlewares/csp.ts +++ b/packages/fresh/src/middlewares/csp.ts @@ -99,12 +99,25 @@ export function csp(options: CSPOptions = {}): Middleware { ? "Content-Security-Policy-Report-Only" : "Content-Security-Policy"; + // Upgrading requests only makes sense when the page itself was served over + // HTTPS. On plain HTTP (e.g. local dev) it breaks things like partial + // redirects by upgrading them to an https:// URL the server doesn't serve. + // Behind a TLS-terminating proxy, enable `trustProxy` so the protocol is + // detected correctly. + const insecureMerged = merged.filter((d) => + d !== "upgrade-insecure-requests" + ); + if (!useNonce) { // Static CSP — no per-request nonce const cspString = merged.join("; "); + const insecureCspString = insecureMerged.join("; "); return async (ctx) => { const res = await ctx.next(); - res.headers.set(headerName, cspString); + res.headers.set( + headerName, + ctx.url.protocol === "https:" ? cspString : insecureCspString, + ); if (reportTo) { res.headers.set("Reporting-Endpoints", `csp-endpoint="${reportTo}"`); } @@ -117,10 +130,11 @@ export function csp(options: CSPOptions = {}): Middleware { const res = await ctx.next(); // deno-lint-ignore no-explicit-any const nonce = (res as any)[NONCE_SYMBOL] as string | undefined; + const base = ctx.url.protocol === "https:" ? merged : insecureMerged; let directives: string[]; if (nonce) { - directives = merged.map((d) => { + directives = base.map((d) => { const spaceIdx = d.indexOf(" "); const name = spaceIdx === -1 ? d : d.slice(0, spaceIdx); if (INLINE_DIRECTIVES.has(name) && d.includes("'unsafe-inline'")) { @@ -129,7 +143,7 @@ export function csp(options: CSPOptions = {}): Middleware { return d; }); } else { - directives = merged; + directives = base; } res.headers.set(headerName, directives.join("; ")); diff --git a/packages/fresh/src/middlewares/csp_test.tsx b/packages/fresh/src/middlewares/csp_test.tsx index 3fc50f0f..ce0d262d 100644 --- a/packages/fresh/src/middlewares/csp_test.tsx +++ b/packages/fresh/src/middlewares/csp_test.tsx @@ -17,6 +17,47 @@ Deno.test("CSP - GET default", async () => { ); }); +Deno.test("CSP - omits upgrade-insecure-requests over http", async () => { + const handler = new App() + .use(csp()) + .get("/", () => new Response("ok")) + .handler(); + const res = await handler(new Request("http://localhost/")); + const header = res.headers.get("Content-Security-Policy")!; + expect(header).toContain("default-src 'self'"); + expect(header).not.toContain("upgrade-insecure-requests"); +}); + +Deno.test("CSP - useNonce omits upgrade-insecure-requests over http", async () => { + const handler = new App() + .use(csp({ useNonce: true })) + .get("/", () => new Response("ok")) + .handler(); + const resHttp = await handler(new Request("http://localhost/")); + expect(resHttp.headers.get("Content-Security-Policy")).not.toContain( + "upgrade-insecure-requests", + ); + const resHttps = await handler(new Request("https://localhost/")); + expect(resHttps.headers.get("Content-Security-Policy")).toContain( + "upgrade-insecure-requests", + ); +}); + +Deno.test("CSP - keeps upgrade-insecure-requests with trusted x-forwarded-proto https", async () => { + const handler = new App({ trustProxy: true }) + .use(csp()) + .get("/", () => new Response("ok")) + .handler(); + const res = await handler( + new Request("http://localhost/", { + headers: { "x-forwarded-proto": "https" }, + }), + ); + expect(res.headers.get("Content-Security-Policy")).toContain( + "upgrade-insecure-requests", + ); +}); + Deno.test("CSP - GET with override options", async () => { const handler = new App() .use(csp({