diff --git a/CHANGELOG.md b/CHANGELOG.md index fd2bd3f..05e5e6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,27 @@ +## 2026-11-15 17:27 + +| Check | Status | Details | +|-------|--------|---------| +| Lint | ✓ PASS | 0 errors | +| Build | ✓ PASS | 27 pages built, 5 pages indexed (Pagefind) | +| Unit Tests | ✓ PASS | 75 tests passed | +| E2E Tests | ✓ PASS | 96 tests passed | +| Lighthouse | ✓ PASS | 99.8% performance (avg) | + +### Lighthouse Performance (local build) + +| Page | Scores (3 runs) | +|------|-----------------| +| / (index) | 100%, 100%, 100% | +| /about | 99%, 99%, 99% | +| /posts | 100%, 100%, 100% | +| /archives (soft 404, not linked) | 100%, 100%, 100% | +| /404 | 100%, 100%, 100% | + +**Diagnostics:** None for the blog. Live site not measured (Turnstile gate). CMS checks: format clean, 175 unit tests passed. + +Changes: The CMS proxy now says why the GitHub App token could not be created (GitHub's status and message, or an unusable private key) instead of a bare 502; no key or token is ever included. + ## 2026-11-15 14:22 | Check | Status | Details | diff --git a/cms/functions/gh/[[path]].ts b/cms/functions/gh/[[path]].ts index 2e4e0ff..4621ad3 100644 --- a/cms/functions/gh/[[path]].ts +++ b/cms/functions/gh/[[path]].ts @@ -170,9 +170,15 @@ export async function appJwt( return `${unsigned}.${toB64url(sig)}`; } +/** A token-mint failure whose message is safe to show the owner. */ +export class TokenError extends Error {} + let installation: { token: string; expiresAt: number } | undefined; -async function installationToken(env: Required, now: number) { +async function installationToken( + env: Required, + now: number +): Promise { if (installation && installation.expiresAt - now > 300) return installation.token; const res = await fetch( @@ -195,8 +201,19 @@ async function installationToken(env: Required, now: number) { } ); if (!res.ok) { - console.error("cms proxy: installation token request failed", res.status); - return undefined; + // GitHub's error message names the cause (unknown installation, permissions not granted) and holds no secret. + const detail = await res + .json() + .then(b => String((b as { message?: unknown }).message ?? "")) + .catch(() => ""); + console.error( + "cms proxy: installation token request failed", + res.status, + detail + ); + throw new TokenError( + `GitHub answered ${res.status}${detail ? `: ${detail.slice(0, 200)}` : ""}` + ); } const body = (await res.json()) as { token: string; expires_at: string }; installation = { @@ -401,11 +418,20 @@ export async function onRequest({ return json(405, "Method Not Allowed"); } - const token = await installationToken(env, now).catch(() => { - console.error("cms proxy: installation token could not be created"); - return undefined; - }); - if (!token) return json(502, "GitHub token unavailable"); + let token: string; + try { + token = await installationToken(env, now); + } catch (error) { + const reason = + error instanceof TokenError + ? error.message + : "the private key could not be used (expected PKCS#8, BEGIN PRIVATE KEY)"; + console.error( + "cms proxy: installation token could not be created:", + reason + ); + return json(502, `GitHub token unavailable: ${reason}`); + } // Installation tokens get 403 on /user, and GitHub omits `permissions` for them; Sveltia needs both. const upstreamRaw = target.path === "/user" ? `/users/${OWNER}` : target.raw; diff --git a/cms/test/unit/token-diagnostics.test.ts b/cms/test/unit/token-diagnostics.test.ts new file mode 100644 index 0000000..74ded07 --- /dev/null +++ b/cms/test/unit/token-diagnostics.test.ts @@ -0,0 +1,93 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { + ORIGIN, + OWNER_EMAIL, + TEAM, + pkcs8Pem, + publicJwk, + rsaKeyPair, + sign, + validClaims, +} from "./proxy-helpers"; + +type Proxy = typeof import("../../functions/gh/[[path]]"); +let proxy: Proxy; +let jwt: string; +let jwk: object; +let env: Record; + +beforeEach(async () => { + vi.resetModules(); + proxy = await import("../../functions/gh/[[path]]"); + const access = await rsaKeyPair(); + const app = await rsaKeyPair(); + jwk = await publicJwk(access.publicKey, "kid-1"); + jwt = await sign( + access.privateKey, + { alg: "RS256", kid: "kid-1" }, + validClaims() + ); + env = { + GITHUB_APP_ID: "123456", + GITHUB_APP_INSTALLATION_ID: "7890", + GITHUB_APP_PRIVATE_KEY: await pkcs8Pem(app.privateKey), + OWNER_EMAIL, + }; +}); +afterEach(() => { + vi.unstubAllGlobals(); + vi.restoreAllMocks(); +}); + +function stubGitHub(tokenResponse: Response) { + vi.stubGlobal( + "fetch", + vi.fn(async (input: string | URL) => { + const url = new URL(input); + if (url.href === `${TEAM}/cdn-cgi/access/certs`) + return new Response(JSON.stringify({ keys: [jwk] })); + return tokenResponse; + }) + ); +} + +const call = () => + proxy.onRequest({ + request: new Request(`${ORIGIN}/gh/api/v3/user`, { + headers: { + "Sec-Fetch-Site": "same-origin", + "Cf-Access-Jwt-Assertion": jwt, + }, + }), + env, + }); + +it("shows GitHub's status and message when the token mint is refused", async () => { + vi.spyOn(console, "error").mockImplementation(() => {}); + stubGitHub( + new Response( + JSON.stringify({ + message: + "The permissions requested are not granted to this installation.", + }), + { status: 422 } + ) + ); + const res = await call(); + expect(res.status).toBe(502); + expect(await res.text()).toContain( + "GitHub answered 422: The permissions requested are not granted to this installation." + ); +}); + +it("names the private key when it cannot be imported, without echoing it", async () => { + vi.spyOn(console, "error").mockImplementation(() => {}); + env.GITHUB_APP_PRIVATE_KEY = + "-----BEGIN PRIVATE KEY-----\nAAAA\n-----END PRIVATE KEY-----"; + stubGitHub(new Response("{}", { status: 201 })); + const res = await call(); + const text = await res.text(); + expect(res.status).toBe(502); + expect(text).toContain("private key could not be used"); + expect(text).not.toContain("AAAA"); +});