Compare commits

...
2 Commits
Author SHA1 Message Date
paula 6e37c2aea7 Bump version 2026-11-12 14:58:28 -03:00
paula 7595cf396f Don't redirect to https if the request didn't come from https 2026-11-12 14:57:38 -03:00
7 changed files with 63 additions and 8 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@paulaboks/sweet", "name": "@paulaboks/sweet",
"version": "2.3.6", "version": "2.3.7",
"license": "MIT", "license": "MIT",
"exports": { "exports": {
".": "./src/mod.ts", ".": "./src/mod.ts",
+17 -3
View File
@@ -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({
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@paulaboks/sweet-init", "name": "@paulaboks/sweet-init",
"version": "2.3.6", "version": "2.3.7",
"license": "MIT", "license": "MIT",
"exports": { "exports": {
".": "./src/mod.ts", ".": "./src/mod.ts",
+1 -1
View File
@@ -5,7 +5,7 @@ import * as semver from "@std/semver";
import initConfig from "../deno.json" with { type: "json" }; import initConfig from "../deno.json" with { type: "json" };
// Keep these as is, as we replace these version in our release script // Keep these as is, as we replace these version in our release script
const FRESH_VERSION = "2.3.6"; const FRESH_VERSION = "2.3.7";
const FRESH_TAILWIND_VERSION = "1.0.0"; const FRESH_TAILWIND_VERSION = "1.0.0";
const FRESH_VITE_PLUGIN = "1.1.2"; const FRESH_VITE_PLUGIN = "1.1.2";
const PREACT_VERSION = "10.29.8"; const PREACT_VERSION = "10.29.8";
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@fresh/update", "name": "@fresh/update",
"version": "2.3.6", "version": "2.3.7",
"license": "MIT", "license": "MIT",
"exports": "./src/mod.ts", "exports": "./src/mod.ts",
"exclude": ["**/tmp/*"], "exclude": ["**/tmp/*"],
+1 -1
View File
@@ -7,7 +7,7 @@ import { walk } from "@std/fs/walk";
export const SyntaxKind = tsmorph.ts.SyntaxKind; export const SyntaxKind = tsmorph.ts.SyntaxKind;
export const FRESH_VERSION = "2.3.6"; export const FRESH_VERSION = "2.3.7";
export const PREACT_VERSION = "10.29.8"; export const PREACT_VERSION = "10.29.8";
export const PREACT_SIGNALS_VERSION = "2.11.4"; export const PREACT_SIGNALS_VERSION = "2.11.4";