42 lines · 1.8 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import type { Env } from "../env.ts";import { CONTENT_SECURITY_POLICY, SECURITY_HEADERS } from "../headers.ts";import worker from "../index.ts";/** * Every answer leaves the Worker through one door (`withSiteHeaders`), so a * page, the 404 page, a redirect and the Mac feed all carry the security headers. */const env = { PUBLIC_ORIGIN: "https://cottondb.com", ASSETS: { fetch: async () => new Response("<!doctype html><title>t</title>", { status: 200, headers: { "Content-Type": "text/html; charset=utf-8" }, }), },} as unknown as Env;describe("the security headers", () => { it.each(["https://cottondb.com/", "https://cottondb.com/privacy", "https://cottondb.com/beta", "https://www.cottondb.com/", "https://cottondb.com/mac/appcast.xml"])( "are on %s", async (url) => { const res = await worker.fetch(new Request(url), env); for (const [name, value] of Object.entries(SECURITY_HEADERS)) { expect(res.headers.get(name), name).toBe(value); } }, ); it("allow the site's stylesheet (and the icons' style attribute), pictures and fonts from the site itself, and nothing else: no script, no outside resource, no form", () => { expect(CONTENT_SECURITY_POLICY).toContain("default-src 'none'"); expect(CONTENT_SECURITY_POLICY).toContain("style-src 'self' 'unsafe-inline'"); expect(CONTENT_SECURITY_POLICY).not.toContain("script-src"); expect(CONTENT_SECURITY_POLICY.split("; ").filter((d) => d.includes("'self'")).sort()).toEqual(["font-src 'self'", "img-src 'self' data:", "style-src 'self' 'unsafe-inline'"]); expect(CONTENT_SECURITY_POLICY).toContain("form-action 'none'"); expect(CONTENT_SECURITY_POLICY).toContain("frame-ancestors 'none'"); });});