A Worker (worker/) now sits in front of the static build, configured from the same wrangler.jsonc. Cloudflare builds and deploys it; nothing in GitHub Actions does. Gate - First visit shows a branded check page (8 languages, Accept-Language) with Cloudflare Turnstile. The token is verified server-side against TURNSTILE_SECRET: success, hostname, action "enter", age <= 300 s. Only Cloudflare's own test-key results skip the hostname/action check, so `npm run cf:dev` works with the published test keys. - Pass -> `__Host-ytg_gate` (HttpOnly, Secure, SameSite=Lax, 24 h): an HMAC over expiry + a user-agent hash, keyed from the Turnstile secret, so no second secret exists and a cookie copied into a scraper is useless. - The page reloads in place instead of redirecting, so there is no `next` parameter to open-redirect with; /__gate/status lets the page tell a cookie-blocking browser what is wrong instead of looping; after one failure it waits for a click rather than resubmitting. - Missing secret = deployment mistake: the site stays up without the gate and the Worker logs it, instead of locking every visitor out. - The agree box on the gate records `ytg_terms`, which the app reads on boot so web visitors meet one blocking screen, not two. Headers and static files - worker/headers.ts sets CSP (nonce + strict-dynamic on the gate, no script at all on legal pages), nosniff, Referrer-Policy, Permissions-Policy, COOP, CORP, X-Frame-Options on everything the Worker returns; _headers covers the files served directly; a test keeps the two baselines identical. - `not_found_handling: "404-page"` (a static 404.html) instead of the SPA fallback, which would have handed index.html past the gate for any excluded path. The Worker serves the shell itself by fetching "/" - not "/index.html", which html_handling would 307 back to the home page. - security.txt (RFC 9116) is generated per build so Expires stays ~6 months ahead; the Web Analytics beacon is injected into the web build only. - theme-init.js applies the saved theme before first paint (no inline script). Legal in-site - build-plugins/legal-docs.ts renders TERMS, PRIVACY, DISCLAIMER (+ vi), LICENSE, THIRD_PARTY_NOTICES, SECURITY, NOTICE and CODE_OF_CONDUCT at build time into static /legal pages (public, before the gate) and a lazy `virtual:legal-docs` module for the in-app Legal Center, which works offline on desktop and Android and never leaves the webview. - PRIVACY, TERMS and SECURITY rewritten for Cloudflare hosting, Turnstile, Web Analytics and the two cookies; contacts moved to the poli0981.dev aliases (privacy@, legal@, dmca@, copyright@, security@, code@). - CURRENT_TERMS_VERSION 1 -> 2: the Privacy Policy changed materially. App - Web uses BrowserRouter (real paths; analytics can count them), Tauri keeps HashRouter; old /#/x bookmarks are rewritten to /x before the router reads them. Consent is a layout route; /legal sits outside it. - <html lang> follows the UI language; a stale chunk after a deploy triggers one reload instead of an error page. .gitattributes pins LF everywhere so Windows checkouts stop failing Prettier. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.7 KiB
TypeScript
69 lines
2.7 KiB
TypeScript
import { readFileSync } from "fs";
|
|
import { resolve } from "path";
|
|
import { describe, it, expect } from "vitest";
|
|
import { APP_CSP, BASELINE_HEADERS, LEGAL_CSP, gateCsp } from "../../worker/headers";
|
|
|
|
/** Parse the `/*` block of public/_headers into name → value. */
|
|
function staticHeadersForAllPaths(): Record<string, string> {
|
|
const text = readFileSync(resolve(import.meta.dirname, "../../public/_headers"), "utf8");
|
|
const out: Record<string, string> = {};
|
|
let inBlock = false;
|
|
for (const line of text.split(/\r?\n/)) {
|
|
if (line.startsWith("#") || line.trim() === "") continue;
|
|
if (!line.startsWith(" ")) {
|
|
inBlock = line.trim() === "/*";
|
|
continue;
|
|
}
|
|
if (!inBlock) continue;
|
|
const colon = line.indexOf(":");
|
|
out[line.slice(0, colon).trim()] = line.slice(colon + 1).trim();
|
|
}
|
|
return out;
|
|
}
|
|
|
|
describe("security headers", () => {
|
|
const staticHeaders = staticHeadersForAllPaths();
|
|
|
|
it("gives Worker responses the same baseline as static files", () => {
|
|
for (const [name, value] of Object.entries(BASELINE_HEADERS)) {
|
|
if (name === "Cross-Origin-Resource-Policy") continue; // set per path in _headers
|
|
expect(staticHeaders[name], name).toBe(value);
|
|
}
|
|
});
|
|
|
|
it("serves the app shell under the same CSP either way", () => {
|
|
expect(staticHeaders["Content-Security-Policy"]).toBe(APP_CSP);
|
|
});
|
|
|
|
it("never sets HSTS — the zone edge owns it", () => {
|
|
expect(Object.keys(staticHeaders).map((h) => h.toLowerCase())).not.toContain(
|
|
"strict-transport-security",
|
|
);
|
|
expect(Object.keys(BASELINE_HEADERS).map((h) => h.toLowerCase())).not.toContain(
|
|
"strict-transport-security",
|
|
);
|
|
});
|
|
|
|
it("lets only the analytics beacon in beside our own scripts", () => {
|
|
expect(APP_CSP).toContain("script-src 'self' https://static.cloudflareinsights.com;");
|
|
expect(APP_CSP).toContain("connect-src 'self' https://cloudflareinsights.com;");
|
|
expect(APP_CSP).not.toContain("'unsafe-eval'");
|
|
expect(APP_CSP).toMatch(/script-src [^;]*$|script-src [^;]*;/);
|
|
expect(APP_CSP.match(/script-src ([^;]*)/)?.[1]).not.toContain("'unsafe-inline'");
|
|
});
|
|
|
|
it("allows Turnstile only on the gate page, with a per-request nonce", () => {
|
|
const csp = gateCsp("abc123");
|
|
expect(csp).toContain(
|
|
"script-src 'nonce-abc123' 'strict-dynamic' https://challenges.cloudflare.com",
|
|
);
|
|
expect(csp).toContain("frame-src https://challenges.cloudflare.com");
|
|
expect(APP_CSP).not.toContain("challenges.cloudflare.com");
|
|
});
|
|
|
|
it("allows no script at all on the prerendered legal pages", () => {
|
|
expect(LEGAL_CSP).toContain("default-src 'none'");
|
|
expect(LEGAL_CSP).not.toContain("script-src");
|
|
});
|
|
});
|