Don't redirect to https if the request didn't come from https
This commit is contained in:
@@ -99,12 +99,25 @@ export function csp<State>(options: CSPOptions = {}): Middleware<State> {
|
|||||||
? "Content-Security-Policy-Report-Only"
|
? "Content-Security-Policy-Report-Only"
|
||||||
: "Content-Security-Policy";
|
: "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) {
|
if (!useNonce) {
|
||||||
// Static CSP — no per-request nonce
|
// Static CSP — no per-request nonce
|
||||||
const cspString = merged.join("; ");
|
const cspString = merged.join("; ");
|
||||||
|
const insecureCspString = insecureMerged.join("; ");
|
||||||
return async (ctx) => {
|
return async (ctx) => {
|
||||||
const res = await ctx.next();
|
const res = await ctx.next();
|
||||||
res.headers.set(headerName, cspString);
|
res.headers.set(
|
||||||
|
headerName,
|
||||||
|
ctx.url.protocol === "https:" ? cspString : insecureCspString,
|
||||||
|
);
|
||||||
if (reportTo) {
|
if (reportTo) {
|
||||||
res.headers.set("Reporting-Endpoints", `csp-endpoint="${reportTo}"`);
|
res.headers.set("Reporting-Endpoints", `csp-endpoint="${reportTo}"`);
|
||||||
}
|
}
|
||||||
@@ -117,10 +130,11 @@ export function csp<State>(options: CSPOptions = {}): Middleware<State> {
|
|||||||
const res = await ctx.next();
|
const res = await ctx.next();
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
const nonce = (res as any)[NONCE_SYMBOL] as string | undefined;
|
const nonce = (res as any)[NONCE_SYMBOL] as string | undefined;
|
||||||
|
const base = ctx.url.protocol === "https:" ? merged : insecureMerged;
|
||||||
|
|
||||||
let directives: string[];
|
let directives: string[];
|
||||||
if (nonce) {
|
if (nonce) {
|
||||||
directives = merged.map((d) => {
|
directives = base.map((d) => {
|
||||||
const spaceIdx = d.indexOf(" ");
|
const spaceIdx = d.indexOf(" ");
|
||||||
const name = spaceIdx === -1 ? d : d.slice(0, spaceIdx);
|
const name = spaceIdx === -1 ? d : d.slice(0, spaceIdx);
|
||||||
if (INLINE_DIRECTIVES.has(name) && d.includes("'unsafe-inline'")) {
|
if (INLINE_DIRECTIVES.has(name) && d.includes("'unsafe-inline'")) {
|
||||||
@@ -129,7 +143,7 @@ export function csp<State>(options: CSPOptions = {}): Middleware<State> {
|
|||||||
return d;
|
return d;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
directives = merged;
|
directives = base;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.headers.set(headerName, directives.join("; "));
|
res.headers.set(headerName, directives.join("; "));
|
||||||
|
|||||||
@@ -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 () => {
|
Deno.test("CSP - GET with override options", async () => {
|
||||||
const handler = new App()
|
const handler = new App()
|
||||||
.use(csp({
|
.use(csp({
|
||||||
|
|||||||
Reference in New Issue
Block a user