Cotton code
36 lines · 1.5 KB · TypeScriptRaw
/** * The headers every answer carries — one chokepoint, applied last. * * The site is static pages with no script, no form and nothing from another * host. Their look is one stylesheet of the site's own (/cotton.css), with a * style attribute on the app icons (the name a page change morphs between); * its fonts, icons and screenshots are the site's own files, and the one * picture the stylesheet draws inline (a tick) is a data: URL. So the policy * allows those, and nothing else. */export const CONTENT_SECURITY_POLICY = [  "default-src 'none'",  "style-src 'self' 'unsafe-inline'",  "img-src 'self' data:",  "font-src 'self'",  "form-action 'none'",  "base-uri 'none'",  "frame-ancestors 'none'",  "upgrade-insecure-requests",].join("; ");export const SECURITY_HEADERS: Readonly<Record<string, string>> = {  "Strict-Transport-Security": "max-age=31536000; includeSubDomains",  "X-Content-Type-Options": "nosniff",  "Referrer-Policy": "strict-origin-when-cross-origin",  "Permissions-Policy": "camera=(), microphone=(), geolocation=()",  "Content-Security-Policy": CONTENT_SECURITY_POLICY,};/** The response, with the site's headers stamped on (a redirect included). */export function withSiteHeaders(response: Response): Response {  const headers = new Headers(response.headers);  for (const [name, value] of Object.entries(SECURITY_HEADERS)) headers.set(name, value);  return new Response(response.body, { status: response.status, statusText: response.statusText, headers });}