clove/pkviewer
web/src/hooks.server.ts
/* web/src/hooks.server.ts */
import type { Handle } from "@sveltejs/kit";
import { dev } from "$app/environment";
/**
* Security headers.
*
* They live here rather than in the API because the browser never talks to the
* API: every header a visitor receives comes from this tier.
*
* Two deliberate weakenings:
* 1. `script-src` allows 'unsafe-inline' for SvelteKit's inline hydration
* script. Survivable because pkviewer accepts no user HTML or JavaScript.
* 2. `img-src` allows any https host: avatars live wherever each system put them.
*
* `frame-ancestors 'none'` matters most: /manage must never be framable.
*/
const isProduction = !dev;
// Development needs eval and the hot-reload WebSocket; production keeps the strict policy
const scriptSrc = isProduction
? "script-src 'self' 'unsafe-inline'"
: "script-src 'self' 'unsafe-inline' 'unsafe-eval'";
const connectSrc = isProduction
? "connect-src 'self'"
: "connect-src 'self' ws: wss:";
const CSP = [
"default-src 'self'",
"base-uri 'self'",
"form-action 'self'",
"frame-ancestors 'none'",
"object-src 'none'",
scriptSrc,
// Theme tokens are a scoped <style>; custom CSS may @import from fonts.googleapis.com
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
// Must agree with CSS_URL_HOSTS, or a font the compiler allows fails silently
"font-src 'self' https://fonts.gstatic.com https://m.doughmination.gay",
"img-src 'self' https: data:",
connectSrc,
"manifest-src 'self'",
].join("; ");
const BASE_HEADERS = [
{ key: "Content-Security-Policy", value: CSP },
{ key: "X-Content-Type-Options", value: "nosniff" },
{ key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
// Redundant with frame-ancestors for modern browsers, kept for older ones
{ key: "X-Frame-Options", value: "DENY" },
{
key: "Permissions-Policy",
value: "camera=(), microphone=(), geolocation=(), payment=(), usb=(), interest-cohort=()",
},
{ key: "Cross-Origin-Opener-Policy", value: "same-origin" },
];
const headers = [...BASE_HEADERS];
if (isProduction) {
headers.push({
key: "Strict-Transport-Security",
value: "max-age=31536000; includeSubDomains",
});
// Production only: on an http dev origin this would upgrade local subresources
headers[0] = {
key: "Content-Security-Policy",
value: `${CSP}; upgrade-insecure-requests`,
};
}
// Public pages are indexable; the control planes never are, HTML or not
const NO_INDEX = /^\/(manage|admin)(\/|$)/;
export const handle: Handle = async ({ event, resolve }) => {
const response = await resolve(event);
for (const { key, value } of headers) {
response.headers.set(key, value);
}
if (NO_INDEX.test(event.url.pathname)) {
response.headers.set("X-Robots-Tag", "noindex, nofollow");
}
return response;
};