feat: add support for CSP (#72)
This commit adds support for running with CSP. CSP can be enabled on a per page basis. If this is done, fresh will set a default CSP that is very restrictive. Users can modify the CSP with the `useCSP` hook. Fresh will add its own script resources to the CSP automatically.
This commit is contained in:
+54
-8
@@ -19,6 +19,11 @@ import {
|
||||
RendererModule,
|
||||
} from "./types.ts";
|
||||
import { render as internalRender } from "./render.tsx";
|
||||
import {
|
||||
ContentSecurityPolicy,
|
||||
ContentSecurityPolicyDirectives,
|
||||
SELF,
|
||||
} from "../runtime/csp.ts";
|
||||
|
||||
export class ServerContext {
|
||||
#dev: boolean;
|
||||
@@ -78,6 +83,7 @@ export class ServerContext {
|
||||
component,
|
||||
handler,
|
||||
runtimeJS: Boolean(config?.runtimeJS ?? false),
|
||||
csp: Boolean(config?.csp ?? false),
|
||||
};
|
||||
pages.push(page);
|
||||
} else if (
|
||||
@@ -149,7 +155,6 @@ export class ServerContext {
|
||||
params: Record<string, string | string[]>,
|
||||
) => {
|
||||
if (page.component === undefined) return undefined;
|
||||
// deno-lint-ignore require-await
|
||||
const render = async () => {
|
||||
const preloads = page.runtimeJS
|
||||
? this.#bundler.getPreloads(bundlePath).map(bundleAssetUrl)
|
||||
@@ -162,11 +167,45 @@ export class ServerContext {
|
||||
url: new URL(req.url),
|
||||
params,
|
||||
});
|
||||
const headers: Record<string, string> = {
|
||||
"content-type": "text/html; charset=utf-8",
|
||||
};
|
||||
let firstChunk: string;
|
||||
const iterator = body[Symbol.asyncIterator]();
|
||||
try {
|
||||
const { value, done } = await iterator.next();
|
||||
if (done) throw new Error("Body is empty.");
|
||||
firstChunk = value[0];
|
||||
const csp: ContentSecurityPolicy | undefined = value[1];
|
||||
if (csp) {
|
||||
if (this.#dev) {
|
||||
csp.directives.connectSrc = [
|
||||
...(csp.directives.connectSrc ?? []),
|
||||
SELF,
|
||||
];
|
||||
}
|
||||
const directive = serializeCSPDirectives(csp.directives);
|
||||
if (csp.reportOnly) {
|
||||
headers["content-security-policy-report-only"] = directive;
|
||||
} else {
|
||||
headers["content-security-policy"] = directive;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Error rendering page", err);
|
||||
return new Response("500 Internal Server Error", {
|
||||
status: 500,
|
||||
headers: {
|
||||
"content-type": "text/plain",
|
||||
},
|
||||
});
|
||||
}
|
||||
const bodyStream = new ReadableStream<Uint8Array>({
|
||||
async start(controller) {
|
||||
controller.enqueue(new TextEncoder().encode(firstChunk));
|
||||
try {
|
||||
for await (const chunk of body) {
|
||||
controller.enqueue(new TextEncoder().encode(chunk));
|
||||
controller.enqueue(new TextEncoder().encode(chunk as string));
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Rendering failed:\n", err);
|
||||
@@ -177,12 +216,7 @@ export class ServerContext {
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
return new Response(bodyStream, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"content-type": "text/html; charset=utf-8",
|
||||
},
|
||||
});
|
||||
return new Response(bodyStream, { status: 200, headers });
|
||||
};
|
||||
return render;
|
||||
};
|
||||
@@ -363,3 +397,15 @@ function sanitizePathToRegex(path: string): string {
|
||||
.replaceAll("\)", "\\)")
|
||||
.replaceAll("\:", "\\:");
|
||||
}
|
||||
|
||||
function serializeCSPDirectives(csp: ContentSecurityPolicyDirectives): string {
|
||||
return Object.entries(csp)
|
||||
.filter(([_key, value]) => value !== undefined)
|
||||
.map(([k, v]: [string, string | string[]]) => {
|
||||
// Turn camel case into snake case.
|
||||
const key = k.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
|
||||
const value = Array.isArray(v) ? v.join(" ") : v;
|
||||
return `${key} ${value}`;
|
||||
})
|
||||
.join("; ");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user