Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| The site's pages run only their own scripts: a nonce policy, nosniff, a referrer policy and no framing | 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; | |
| 3 | ||
| 4 | import { makeNonce, pagePolicy, withSiteHeaders } from "./page-headers.ts"; | |
| 5 | ||
| 6 | test("a page runs only its own scripts and is never framed", () => { | |
| 7 | const nonce = makeNonce(); | |
| 8 | assert.match(nonce, /^[A-Za-z0-9+/]{22}==$/); | |
| 9 | assert.notEqual(nonce, makeNonce()); | |
| 10 | const policy = pagePolicy(nonce); | |
| 11 | assert.ok(policy.includes(`script-src 'self' 'nonce-${nonce}'`)); | |
| 12 | assert.doesNotMatch(policy, /script-src[^;]*'unsafe-inline'/); | |
| 13 | assert.match(policy, /frame-ancestors 'none'/); | |
| 14 | assert.match(policy, /object-src 'none'/); | |
| 15 | assert.match(policy, /base-uri 'self'/); | |
| 16 | // Pictures in a README come from anywhere on HTTPS. | |
| 17 | assert.match(policy, /img-src 'self' https: data: blob:/); | |
| 18 | }); | |
| 19 | ||
| 20 | test("every answer gains nosniff and a referrer policy; pages are not framed", () => { | |
| 21 | const page = withSiteHeaders(new Response("<p>hi</p>", { headers: { "content-type": "text/html" } })); | |
| 22 | assert.equal(page.headers.get("x-content-type-options"), "nosniff"); | |
| 23 | assert.equal(page.headers.get("referrer-policy"), "strict-origin-when-cross-origin"); | |
| 24 | assert.equal(page.headers.get("x-frame-options"), "DENY"); | |
| 25 | ||
| 26 | const data = withSiteHeaders(new Response("{}", { headers: { "content-type": "application/json" } })); | |
| 27 | assert.equal(data.headers.get("x-content-type-options"), "nosniff"); | |
| 28 | assert.equal(data.headers.get("x-frame-options"), null); | |
| 29 | ||
| 30 | // A redirect's headers are fixed; the answer is copied, status and all. | |
| 31 | const moved = withSiteHeaders(Response.redirect("https://docs.g1t.sh/", 301)); | |
| 32 | assert.equal(moved.status, 301); | |
| 33 | assert.equal(moved.headers.get("location"), "https://docs.g1t.sh/"); | |
| 34 | assert.equal(moved.headers.get("referrer-policy"), "strict-origin-when-cross-origin"); | |
| 35 | ||
| 36 | const own = withSiteHeaders(new Response("", { headers: { "referrer-policy": "no-referrer" } })); | |
| 37 | assert.equal(own.headers.get("referrer-policy"), "no-referrer"); | |
| 38 | }); |