From 1b1e922fe234a93792ce52ddfe38eeb5cf042987 Mon Sep 17 00:00:00 2001 From: poli0981 <127664709+poli0981@users.noreply.github.com> Date: Sat, 24 Oct 2026 11:48:47 +0700 Subject: [PATCH] Docs for v2.0, plus widget and CSP fixes (#100) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs: README, CHANGELOG and the docs suite brought up to v2.0 - README.md (new): what the site is, stack, how the Worker fits together, getting started on Node 26, commands, deploying with Workers Builds, the admin, security and licensing. - CHANGELOG.md (new): v2.0.0 in Keep a Changelog form, plus a summary of the untagged 1.x months. - SECURITY.md: admin.poli0981.dev, the bot gate and the widgets worker are in scope; tagged releases; reports go to a private repository; what the admin stores. - THIRD-PARTY-NOTICES.md: every infrastructure service, with what each one is used for. CONTENT-LICENSE.md: the content submodule and /media images. - poli0981.dev-docs v2.0: decision log D16–D19; architecture (worker entry, stack, repo layout, dev setup, ADR 09–11); design system (Glacier Teal tokens, theme modes, a11y rules); security (headers, single-key CIDR denylist, body limits, secrets table, supply chain on a public repo, admin hardening and Access recommendations, structured logs); Cloudflare setup (correct Workers Builds commands and the deploy incident, ADMIN_EMAILS, Access settings, observability); CI/CD as it runs today; bug reports, widgets, error pages, routes, roadmap with status and next steps, maintenance (TypeScript 7 bumps, rollback, R2/KV backup). Co-Authored-By: Claude Opus 5.5 * docs(changelog): link the documentation PR Co-Authored-By: Claude Opus 5.5 * fix(widgets): Steam icons via Steam's Cloudflare CDN; optional GitHub token Found by watching the first cron run of the new worker in production: no image had been mirrored and two sources were stale. - media.steampowered.com answers requests from Workers with 403, so every Steam icon mirror failed. cdn.cloudflare.steamstatic.com serves the same files to Workers (probed from Cloudflare's network). - The GitHub events API allows 60 unauthenticated requests an hour per IP and Workers share egress IPs, so the feed was 403 for hours at a time. The worker now sends GITHUB_TOKEN when that optional secret exists (a fine-grained token with no permissions is enough for public data). - YouTube's feeds/videos.xml currently answers 404 for both the channel and the uploads-playlist URL, from Cloudflare and from elsewhere; the worker already keeps the last good feed, so nothing changes there. Co-Authored-By: Claude Opus 5.5 * fix(csp): callout styles were blocked; the CSP check now covers styles Lighthouse flagged a CSP violation on the one MDX post. Astro hashes the styles of the components a page renders, but not of a component that only appears inside MDX content: Callout's inline ", true], + ["", true], + ["X", false], + ]) { + STYLE.lastIndex = 0; + if (STYLE.test(html) !== shouldMatch) failures.push(`style block: wrong result for ${html}`); + } + for (const [html, shouldMatch] of [ + ['

', true], + ["", true], + ['', false], + ['

', false], + ]) { + STYLE_ATTR.lastIndex = 0; + if (STYLE_ATTR.test(html) !== shouldMatch) + failures.push(`style attribute: wrong result for ${html}`); + } if (failures.length > 0) { console.error("CSP guard SELFTEST FAILED — a matcher is wrong:\n"); for (const failure of failures) console.error(` ${failure}`); @@ -107,6 +130,7 @@ const offenders = []; const withoutCsp = []; let pages = 0; let scripts = 0; +let styles = 0; for (const file of htmlFiles(ROOT)) { const html = readFileSync(file, "utf8"); @@ -128,6 +152,17 @@ for (const file of htmlFiles(ROOT)) { offenders.push({ file, hash, preview: body.trim().slice(0, 80) }); } } + + for (const [, , body] of html.matchAll(STYLE)) { + styles += 1; + const hash = `sha256-${createHash("sha256").update(body, "utf8").digest("base64")}`; + if (!declared.has(hash)) { + offenders.push({ file, hash, preview: ` diff --git a/src/styles/prose.css b/src/styles/prose.css index 6910168..9cccf93 100644 --- a/src/styles/prose.css +++ b/src/styles/prose.css @@ -144,3 +144,52 @@ .prose .token.url { color: var(--muted); } + +/* Callout (src/components/Callout.astro) — used from MDX content, so its rules live here. */ +.callout { + display: flex; + gap: 0.75rem; + padding: 0.9rem 1rem; + border-radius: var(--radius-card); + border: 1px solid var(--line); + background: var(--surface); + font-family: var(--ff-body); + font-size: var(--text-body); +} +.callout-icon { + flex: none; + inline-size: 1.5rem; + block-size: 1.5rem; + display: grid; + place-items: center; + border-radius: 999px; + font-family: var(--ff-mono); + font-weight: 700; + font-size: 0.85rem; + color: var(--bg); +} +.callout-title { + font-weight: 600; + margin-block-end: 0.25rem; +} +.callout-body > * + * { + margin-block-start: 0.5em; +} +.callout.info { + border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); +} +.callout.info .callout-icon { + background: var(--accent); +} +.callout.tip { + border-color: color-mix(in srgb, var(--ok) 40%, var(--line)); +} +.callout.tip .callout-icon { + background: var(--ok); +} +.callout.warn { + border-color: color-mix(in srgb, var(--warn) 45%, var(--line)); +} +.callout.warn .callout-icon { + background: var(--warn); +} diff --git a/tests/widgets-worker.test.ts b/tests/widgets-worker.test.ts index e236bb7..7c14c81 100644 --- a/tests/widgets-worker.test.ts +++ b/tests/widgets-worker.test.ts @@ -72,7 +72,10 @@ function upstream( { type: "PushEvent" }, ]) ); - if (url.startsWith("https://i.ytimg.com/") || url.startsWith("https://media.steampowered.com/")) + if ( + url.startsWith("https://i.ytimg.com/") || + url.startsWith("https://cdn.cloudflare.steamstatic.com/") + ) return overrides.img?.(call) ?? image(); if (url.startsWith("https://discord.example/")) return overrides.discord?.(call) ?? new Response(null, { status: 204 }); @@ -172,9 +175,13 @@ describe("widget sources", () => { assert.ok(calls.some((c) => c.url.includes("key=key%26x"))); // Push, release, release-without-a-tag — WatchEvent skipped, capped at three. assert.deepEqual( - (await fetchGitHub()).map((e) => e.detail), + (await fetchGitHub({})).map((e) => e.detail), ["2 commits", "v1", "release"], ); + const gh = calls.find((c) => c.url.startsWith("https://api.github.com/")); + assert.equal(gh?.headers.get("authorization"), null); + await fetchGitHub({ GITHUB_TOKEN: "ro-token" }); + assert.equal(calls.at(-1)?.headers.get("authorization"), "Bearer ro-token"); assert.ok(kv.store.has(`widgets:img:steam:570:${HASH}`)); }); @@ -197,7 +204,7 @@ describe("widget sources", () => { }); await assert.rejects(fetchYouTube(env(kv)), /youtube 500/); await assert.rejects(fetchSteam(env(kv)), /steam 403/); - await assert.rejects(fetchGitHub(), /github 503/); + await assert.rejects(fetchGitHub({}), /github 503/); }); }); diff --git a/workers/widgets/src/feeds.ts b/workers/widgets/src/feeds.ts index 802480b..4efd8ec 100644 --- a/workers/widgets/src/feeds.ts +++ b/workers/widgets/src/feeds.ts @@ -25,6 +25,12 @@ export interface WidgetsEnv { STEAM_API_KEY: string; STEAM_ID64: string; DISCORD_WEBHOOK_CI?: string; + /** + * Optional, any fine-grained token with no permissions (public data only). Unauthenticated + * the events API allows 60 requests an hour per IP, and Workers share their egress IPs, + * so the GitHub feed goes stale for hours at a time without one. + */ + GITHUB_TOKEN?: string; } const DAY_MS = 24 * 60 * 60 * 1000; @@ -36,9 +42,13 @@ const IMAGE_TTL_SECONDS = 14 * 24 * 60 * 60; const MAX_IMAGE_BYTES = 2 * 1024 * 1024; const IMAGE_TYPES = new Set(["image/jpeg", "image/png", "image/webp"]); -const get = (url: string, accept?: string) => +const get = (url: string, accept?: string, token?: string) => fetch(url, { - headers: { "user-agent": UA, ...(accept ? { accept } : {}) }, + headers: { + "user-agent": UA, + ...(accept ? { accept } : {}), + ...(token ? { authorization: `Bearer ${token}` } : {}), + }, signal: AbortSignal.timeout(TIMEOUT_MS), }); @@ -155,17 +165,20 @@ export async function fetchSteam(env: WidgetsEnv): Promise { return Promise.all( games.map(async ({ iconHash, ...game }) => { if (!iconHash) return game; - const source = `https://media.steampowered.com/steamcommunity/public/images/apps/${game.appid}/${iconHash}.jpg`; + // media.steampowered.com answers Workers with 403; Steam's own Cloudflare CDN serves + // the same file. + const source = `https://cdn.cloudflare.steamstatic.com/steamcommunity/public/images/apps/${game.appid}/${iconHash}.jpg`; const ok = await mirrorImage(env.KV, `steam:${game.appid}:${iconHash}`, source); return { ...game, icon: ok ? `/ext-img/steam/${game.appid}/${iconHash}` : "" }; }), ); } -export async function fetchGitHub(): Promise { +export async function fetchGitHub(env: Pick): Promise { const res = await get( "https://api.github.com/users/poli0981/events/public?per_page=10", "application/vnd.github+json", + env.GITHUB_TOKEN, ); if (!res.ok) throw new Error(`github ${res.status}`); return normalizeGitHub((await res.json()) as GhEvent[]); diff --git a/workers/widgets/src/index.ts b/workers/widgets/src/index.ts index d6a0885..fc3ed44 100644 --- a/workers/widgets/src/index.ts +++ b/workers/widgets/src/index.ts @@ -43,7 +43,7 @@ export default { const sources: [Source, string, () => Promise][] = [ ["yt", "widgets:yt", () => fetchYouTube(env)], ["steam", "widgets:steam", () => fetchSteam(env)], - ["gh", "widgets:gh", fetchGitHub], + ["gh", "widgets:gh", () => fetchGitHub(env)], ]; const results = await Promise.all( sources.map(([src, key, fetcher]) => refresh(env.KV, key, src, status, now, fetcher)),