Files
poli0981andClaude Opus 5.5 5ced6b4a86 feat(web): Turnstile gate, in-site legal pages and clean URLs on ytgenerator.stream
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>
2026-09-30 19:26:40 +07:00

100 lines
3.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
CURRENT_TERMS_VERSION,
LEGAL_DOCS,
LEGAL_DOC_IDS,
TERMS_COOKIE,
isLegalDocId,
legalDocPath,
needsConsent,
termsVersionFromCookie,
} from "@config/legal";
/**
* Guards for the pure legal config driving the consent gate, the in-app Legal
* Center, the static /legal pages and the Cloudflare Worker. The gate is a
* boot-time blocker, so the version logic must be exactly right: anything
* below the current version (or a non-number) re-shows the gate; the current
* version (or higher) lets the app through.
*/
describe("needsConsent", () => {
it("requires consent at version 0 (never accepted)", () => {
expect(needsConsent(0)).toBe(true);
});
it("requires consent below the current version (terms changed)", () => {
expect(needsConsent(CURRENT_TERMS_VERSION - 1)).toBe(true);
});
it("passes once the current version is accepted", () => {
expect(needsConsent(CURRENT_TERMS_VERSION)).toBe(false);
});
it("passes for a future stored version (never downgrades)", () => {
expect(needsConsent(CURRENT_TERMS_VERSION + 5)).toBe(false);
});
it("treats a non-number as needing consent", () => {
expect(needsConsent(undefined as unknown as number)).toBe(true);
expect(needsConsent(NaN)).toBe(true);
});
it("re-asks users who accepted v1 — the v1.0.0 Privacy Policy changed materially", () => {
expect(CURRENT_TERMS_VERSION).toBeGreaterThanOrEqual(2);
expect(needsConsent(1)).toBe(true);
});
});
describe("LEGAL_DOCS", () => {
it("lists the consent documents first, in agreement order", () => {
expect(LEGAL_DOCS.filter((d) => d.consent).map((d) => d.id)).toEqual([
"terms",
"privacy",
"disclaimer",
"license",
]);
});
it("has unique ids, label keys and sources", () => {
for (const key of ["id", "labelKey", "source"] as const) {
expect(new Set(LEGAL_DOCS.map((d) => d[key])).size).toBe(LEGAL_DOCS.length);
}
});
it("keys every label under legal.docs and points every source at a repo file", () => {
for (const doc of LEGAL_DOCS) {
expect(doc.labelKey).toMatch(/^legal\.docs\.[a-zA-Z]+$/);
expect(doc.source).toMatch(/^[A-Z_]+(\.md)?$/);
}
});
it("serves every document under /legal/<id>", () => {
for (const id of LEGAL_DOC_IDS) {
expect(legalDocPath(id)).toBe(`/legal/${id}`);
expect(isLegalDocId(id)).toBe(true);
}
expect(isLegalDocId("nope")).toBe(false);
expect(isLegalDocId("")).toBe(false);
});
});
describe("termsVersionFromCookie", () => {
it("reads the version the gate page recorded", () => {
expect(termsVersionFromCookie(`a=1; ${TERMS_COOKIE}=2; b=3`)).toBe(2);
expect(termsVersionFromCookie(`${TERMS_COOKIE}=12`)).toBe(12);
});
it("returns null when absent, empty or malformed", () => {
expect(termsVersionFromCookie(undefined)).toBeNull();
expect(termsVersionFromCookie("")).toBeNull();
expect(termsVersionFromCookie("other=2")).toBeNull();
expect(termsVersionFromCookie(`${TERMS_COOKIE}=abc`)).toBeNull();
expect(termsVersionFromCookie(`${TERMS_COOKIE}=-1`)).toBeNull();
expect(termsVersionFromCookie(`${TERMS_COOKIE}=99999`)).toBeNull();
});
it("does not match a cookie whose name merely ends with the same text", () => {
expect(termsVersionFromCookie(`x${TERMS_COOKIE}=2`)).toBeNull();
});
});