diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index b23cd8e8..c44be8c9 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -237,7 +237,9 @@ A scheduled failure opens or updates an issue labelled `security-header-drift`, The check is kept out of the publish smoke tests on purpose, because a failure there rolls back a good publish for what is a coder.com problem. Run it locally with `pnpm --filter @coder/docs-router headers:check` after building the packages, with `DOCS_SITE_URL` and `DOCS_BASE_PATH` set; it refuses a site URL whose path is not the base path. -With `DOCS_ANALYTICS` on (coder.com's consent manager and tag manager, `apps/docs/README.md` "Analytics"), `script-src` also names Osano, Google Tag Manager, and the vendors its container injects, as `docsContentSecurityPolicy({ analytics: true })` in `@coder/docs-config` builds it; every other directive is unchanged. +With `DOCS_ANALYTICS` on (coder.com's consent manager and tag manager, `apps/docs/README.md` "Analytics"), `script-src` also names Osano, Google Tag Manager, and the vendors its container injects, as `docsContentSecurityPolicy({ analytics: true })` in `@coder/docs-config` builds it, plus `blob:` (`ANALYTICS_SCRIPT_SCHEMES`) for Osano's workers and ZoomInfo's session script; every other directive is unchanged. +The policy names no `worker-src`, so a worker falls back to `script-src`, and `blob:` there covers both. +It adds nothing an injected script could not already do while `'unsafe-inline'` stays, and has to be revisited with the hardening path below. Each vendor is named by origin except jsDelivr, a public CDN for any npm package, which is named only by the one package directory its tag loads (`https://cdn.jsdelivr.net/npm/hockeystack@latest/`). The router applies that policy to archive responses when its own `DOCS_ANALYTICS` is on; `apps/docs/vercel.json`, which cannot vary by environment, keeps the policy above. Widening `script-src` this way is a security decision and needs sign-off before the flag is turned on anywhere. diff --git a/apps/docs/README.md b/apps/docs/README.md index be4b5ed1..96a20117 100644 --- a/apps/docs/README.md +++ b/apps/docs/README.md @@ -647,7 +647,7 @@ Nothing reloads on a reader's first choice, for a reader who arrives having deni The scripts, origins, and bootstrap live in `@coder/docs-config` (`analytics.ts`), shared with the router. A reader who never chooses, or who denies, sends one request to `cmp.osano.com` for the script and, on a choice, one to `consent.api.osano.com` to record it. -With the flag on, `script-src` needs Osano, GTM, and every vendor GTM-NLLNT3K's tags inject, listed in `ANALYTICS_SCRIPT_ORIGINS`. +With the flag on, `script-src` needs Osano, GTM, and every vendor GTM-NLLNT3K's tags inject, listed in `ANALYTICS_SCRIPT_ORIGINS`, and `blob:` (`ANALYTICS_SCRIPT_SCHEMES`): Osano starts workers from `blob:` URLs and ZoomInfo's tag writes a `blob:` script. That list is a measurement of one container at one point in time, so it drifts as Marketing changes the tags in GTM-NLLNT3K; its docstring describes how to re-derive it. Every entry is a bare origin except jsDelivr's, which is `https://cdn.jsdelivr.net/npm/hockeystack@latest/` rather than the whole CDN, since the bare origin would allow any npm package or GitHub file. Measured on coder.com on 2026-09-29, HockeyStack's tag is the only one that loads from jsDelivr, fetching `hockeystack.min.js` and `hockeystack-6sense.min.js` from that directory without a redirect. @@ -658,12 +658,13 @@ Turning the tags on for those previews means shipping `docsContentSecurityPolicy This has no effect on the public site, which the router serves and gates separately, above. coder.com's `next.config.js` restates the `/docs` policy (step 4 of "Serving through coder.com" below, and the [cutover runbook](https://app.notion.com/p/3f8d579be59281a691f1e6146c2fc1a9), internal, on Notion) and must carry the same origins. Only `script-src` needs them: the policy has no `default-src`, so Osano's and the tags' requests, images, frames, and styles are not restricted. +Workers are the exception that `blob:` covers, since with no `worker-src` they fall back to `script-src`. The offline export never loads any of this: `turbo.json` does not pass `DOCS_ANALYTICS` to its build, its source names none of the analytics exports, and `pnpm --filter @coder/offlinedocs analytics:check`, run in CI after the build, fails if any page in `out/` carries Osano, GTM, the bootstrap, or the footer control. Turning the tags on for the public site takes four switches, in this order, because a page that loads the tags under a policy without their origins gets its scripts blocked: -1. coder.com's `next.config.js` page policy for the docs path gains `ANALYTICS_SCRIPT_ORIGINS` in `script-src`. +1. coder.com's `next.config.js` page policy for the docs path gains `ANALYTICS_SCRIPT_ORIGINS` and `ANALYTICS_SCRIPT_SCHEMES` in `script-src`. 2. The site's router project (`coder-docs` for `docs`) has `DOCS_ANALYTICS` set to `1` for Production, and the router is redeployed. 3. The site's `analytics` flag in the site table (`apps/docs/scripts/publish/sites.mjs`) is `true`, as it already is for `docs`; the publisher builds with it, and the weekly production header check reads the active site's flag to know which policy to expect. 4. Every line, `main`, and every tag is republished, since a version only embeds the tags when it is built. diff --git a/packages/docs-config/src/analytics.test.ts b/packages/docs-config/src/analytics.test.ts index db1ac09f..317f0954 100644 --- a/packages/docs-config/src/analytics.test.ts +++ b/packages/docs-config/src/analytics.test.ts @@ -3,6 +3,7 @@ import test from "node:test"; import vm from "node:vm"; import { ANALYTICS_SCRIPT_ORIGINS, + ANALYTICS_SCRIPT_SCHEMES, DOCS_CONTENT_SECURITY_POLICY, GTM_SCRIPT_URL, OSANO_SCRIPT_URL, @@ -46,6 +47,7 @@ test("the analytics policy widens script-src only", () => { "'self'", "'unsafe-inline'", ...ANALYTICS_SCRIPT_ORIGINS, + "blob:", ]); assert.deepEqual(directives["object-src"], ["'none'"]); assert.deepEqual(directives["base-uri"], ["'self'"]); @@ -54,6 +56,13 @@ test("the analytics policy widens script-src only", () => { assert.equal(Object.keys(directives).length, 5); }); +test("blob: is allowed only while 'unsafe-inline' is", () => { + const scriptSrc = (docsContentSecurityPolicy({ analytics: true }).split(";")[0] ?? "").split( + /\s+/, + ); + if (scriptSrc.includes("blob:")) assert.ok(scriptSrc.includes("'unsafe-inline'")); +}); + const JSDELIVR_HOCKEYSTACK = "https://cdn.jsdelivr.net/npm/hockeystack@latest/"; test("the analytics origins cover the two scripts the docs load and are plain https origins", () => { @@ -68,6 +77,19 @@ test("the analytics origins cover the two scripts the docs load and are plain ht assert.equal(new Set(ANALYTICS_SCRIPT_ORIGINS).size, ANALYTICS_SCRIPT_ORIGINS.length); }); +test("blob: scripts are allowed only with the tags on, and no other scheme is", () => { + assert.deepEqual(ANALYTICS_SCRIPT_SCHEMES, ["blob:"]); + const scriptSrc = (policy: string) => (policy.split(";")[0] ?? "").split(/\s+/).slice(1); + assert.ok(scriptSrc(docsContentSecurityPolicy({ analytics: true })).includes("blob:")); + assert.ok(!scriptSrc(docsContentSecurityPolicy({ analytics: false })).includes("blob:")); + for (const source of scriptSrc(docsContentSecurityPolicy({ analytics: true }))) { + assert.ok( + source.startsWith("'") || source.startsWith("https://") || source === "blob:", + `${source} is neither a keyword, an https source nor blob:`, + ); + } +}); + test("jsDelivr is allowed only for the HockeyStack package directory its tag loads", () => { const scriptSrc = (docsContentSecurityPolicy({ analytics: true }).split(";")[0] ?? "").split( /\s+/, diff --git a/packages/docs-config/src/analytics.ts b/packages/docs-config/src/analytics.ts index 71a47a95..9e52fa46 100644 --- a/packages/docs-config/src/analytics.ts +++ b/packages/docs-config/src/analytics.ts @@ -60,8 +60,22 @@ export const ANALYTICS_SCRIPT_ORIGINS: readonly string[] = [ "https://j.6sc.co", "https://cdn.cr-relay.com", "https://static.hotjar.com", + // Hotjar's surveys (survey-v2.*.js), measured on coder.com/docs on 2026-11-30. + "https://script.hj.contentsquare.net", ]; +/** + * The tags also run script from `blob:` addresses, measured on coder.com/docs + * on 2026-11-30: Osano starts its classification workers from `blob:` URLs + * (the policy names no `worker-src`, so workers fall back to `script-src`), + * and ZoomInfo's `zi-tag.js` writes its session code to a `blob:` script. + * Only a script already running on the page can make a `blob:` address, and + * `'unsafe-inline'` already lets any inline script run, so this adds nothing + * an injected script could not already do. Revisit it if `'unsafe-inline'` + * gives way to hashes (ARCHITECTURE.md, "Hardening path"). + */ +export const ANALYTICS_SCRIPT_SCHEMES: readonly string[] = ["blob:"]; + const BASE_SCRIPT_SRC = ["'self'", "'unsafe-inline'"]; const OTHER_DIRECTIVES = [ "object-src 'none'", @@ -72,7 +86,9 @@ const OTHER_DIRECTIVES = [ /** The public docs policy for a deployment with the tags on or off. */ export function docsContentSecurityPolicy({ analytics }: { analytics: boolean }): string { - const scriptSrc = analytics ? [...BASE_SCRIPT_SRC, ...ANALYTICS_SCRIPT_ORIGINS] : BASE_SCRIPT_SRC; + const scriptSrc = analytics + ? [...BASE_SCRIPT_SRC, ...ANALYTICS_SCRIPT_ORIGINS, ...ANALYTICS_SCRIPT_SCHEMES] + : BASE_SCRIPT_SRC; return [`script-src ${scriptSrc.join(" ")}`, ...OTHER_DIRECTIVES].join("; "); } diff --git a/packages/docs-config/src/index.ts b/packages/docs-config/src/index.ts index e2bcb454..163ffdaa 100644 --- a/packages/docs-config/src/index.ts +++ b/packages/docs-config/src/index.ts @@ -96,6 +96,7 @@ export type { export { isCodeTitle, stripCodeTitle } from "./code-title.js"; export { ANALYTICS_SCRIPT_ORIGINS, + ANALYTICS_SCRIPT_SCHEMES, analyticsEnabled, consentBootstrapScript, DOCS_CONTENT_SECURITY_POLICY,