fix(worker): accept the GitHub App key as downloaded (PKCS#1)

Approving from the queue failed with 500 "internal_error"; the Worker log
said: TypeError: "pkcs8" must be PKCS#8 formatted string. GitHub downloads
App keys as PKCS#1, which WebCrypto cannot import.

- github.ts wraps a PKCS#1 PEM into PKCS#8 itself (and turns two-character
  "
" escapes into newlines), so the downloaded key works unchanged.
- GitHub failures throw GitHubError and the admin API answers 502
  github_error with the reason, instead of a bare internal error.
- Admin CSP allows the Cloudflare beacon the zone injects into every HTML
  response; dropped browsing-topics from Permissions-Policy (Chrome no
  longer knows the feature and logs an error on every page).
- Docs: the key needs no openssl conversion any more.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
poli0981andClaude Opus 5 committed 2026-09-20 15:38:05 +07:00
1 parent ddcaa25df5
commit 066f368b53
7 files changed
+108 -15

No files matched your search

+1 -1
View File
@@ -46,7 +46,7 @@ Local admin without Access: put `DEV_ADMIN_EMAIL=you@example.com` in `webapp/.de
- `admin.ts`: API (queue list / approve / reject / add / edit the 3 fields / delete with reason + R2 cleanup / restore) and the `/admin` shell with its own CSP. The maintainer's email goes only to the D1 audit log, never into a commit (the repo is public).
- `repo.ts` + `github.ts` + `catalog.ts`: writes as a **GitHub App** (installation token, cached per isolate) with GraphQL `createCommitOnBranch` + `expectedHeadOid` (verified commits; `commitWithRetry` rebuilds from the new head on a race). Reads use the raw media type (no base64/UTF-8 mojibake). `catalog.ts` ports `data_store.py` byte-for-byte (golden fixtures).
- `queue.ts`: URL classification (catalog → blocklist → pending → previously deleted) + the D1 store (`worker/migrations/`, additive only — never edit an applied migration). `data.ts`: catalog facts read from the site's own `/data` via `env.ASSETS`, cached per isolate (loaded values only — see gotchas).
- Configuration: non-secret settings are `vars` in `wrangler.jsonc` (empty until setup); secrets (`GH_APP_PRIVATE_KEY` PKCS#8, `ADMIN_EMAILS`, `TURNSTILE_SECRET`) via `wrangler secret put`, typed in `worker/env.ts` as optional. A feature whose settings are missing answers **503**; the public site needs none of them. `env.staging` = Worker `free-games-itchio-list-staging` on `staging.freeitchgames.win` (no cron, own R2/D1/rate-limit namespaces).
- Configuration: non-secret settings are `vars` in `wrangler.jsonc` (empty until setup); secrets (`GH_APP_PRIVATE_KEY` — the PEM GitHub downloads (PKCS#1) or PKCS#8; `github.ts` converts, `ADMIN_EMAILS`, `TURNSTILE_SECRET`) via `wrangler secret put`, typed in `worker/env.ts` as optional. A feature whose settings are missing answers **503**; the public site needs none of them. `env.staging` = Worker `free-games-itchio-list-staging` on `staging.freeitchgames.win` (no cron, own R2/D1/rate-limit namespaces).
## Web app structure (`webapp/src/`)
+2 -2
View File
@@ -60,8 +60,8 @@ Do these once (and again with the `-staging` names for the staging Worker).
Worker's plain-text variables with the file's values. Secrets are not affected.
9. **Secrets** (`cd webapp`; add `--env staging` for staging):
```sh
openssl pkcs8 -topk8 -nocrypt -in app.private-key.pem -out app.pk8.pem # GitHub App key → PKCS#8
npx wrangler secret put GH_APP_PRIVATE_KEY < app.pk8.pem
# The Worker converts GitHub's PKCS#1 key itself, so the downloaded .pem works as is:
npx wrangler secret put GH_APP_PRIVATE_KEY < app.private-key.pem
npx wrangler secret put ADMIN_EMAILS # comma-separated
npx wrangler secret put TURNSTILE_SECRET
```
+1 -1
View File
@@ -9,7 +9,7 @@
Referrer-Policy: strict-origin-when-cross-origin
X-Frame-Options: DENY
Cross-Origin-Opener-Policy: same-origin
Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=(), usb=(), browsing-topics=()
Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=(), usb=()
Content-Security-Policy: default-src 'self'; script-src 'self' https://static.cloudflareinsights.com https://challenges.cloudflare.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self' https://cloudflareinsights.com; frame-src https://challenges.cloudflare.com; manifest-src 'self'; worker-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'
# Hashed build output never changes under the same name.
+8 -2
View File
@@ -21,6 +21,7 @@ import { canonicalize } from './canonical'
import { validateEdit, type FieldEdit } from './catalog'
import { catalogUrls, deletedGames, type DataSource } from './data'
import { ACCESS_CONFIG, GITHUB_CONFIG, missingConfig, type WorkerEnv } from './env'
import { GitHubError } from './github'
import type { GitHubEnv } from './github'
import { HttpError, SECURITY_HEADERS, errorResponse, isObject, json, readJson } from './http'
import { r2Key } from './img'
@@ -35,10 +36,12 @@ const THUMB_WIDTHS = [160, 640]
// itch.io (candidates are not in the catalog yet, so /img won't serve them).
export const ADMIN_CSP = [
"default-src 'self'",
"script-src 'self'",
// The zone's Web Analytics setting injects the beacon into every HTML
// response, this page included; without these it only logs a CSP error.
"script-src 'self' https://static.cloudflareinsights.com",
"style-src 'self' 'unsafe-inline'",
"img-src 'self' data: https://img.itch.zone",
"connect-src 'self'",
"connect-src 'self' https://cloudflareinsights.com",
"font-src 'self'",
"object-src 'none'",
"base-uri 'self'",
@@ -307,6 +310,9 @@ export async function handleAdminApi(
now: new Date(),
})
} catch (e) {
// GitHub refused the write (bad App key, missing permission, API down):
// say so in the admin instead of a bare "internal error".
if (e instanceof GitHubError) return errorResponse(new HttpError(502, 'github_error', e.message))
return errorResponse(e)
}
}
+1 -1
View File
@@ -6,7 +6,7 @@
* runtime: a route whose configuration is incomplete answers 503.
*/
interface Secrets {
GH_APP_PRIVATE_KEY?: string // PKCS#8 PEM of the GitHub App
GH_APP_PRIVATE_KEY?: string // GitHub App PEM (PKCS#1 as downloaded, or PKCS#8)
ADMIN_EMAILS?: string // comma-separated maintainer emails
TURNSTILE_SECRET?: string
/** Local `wrangler dev` only (admin without Access); ignored off localhost. */
+32 -1
View File
@@ -1,4 +1,5 @@
import { exportPKCS8, generateKeyPair, jwtVerify } from 'jose'
import { createPrivateKey, generateKeyPairSync } from 'node:crypto'
import { SignJWT, exportPKCS8, generateKeyPair, importPKCS8, importSPKI, jwtVerify } from 'jose'
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import {
ConflictError,
@@ -6,6 +7,7 @@ import {
commitWithRetry,
readFile,
resetTokenCache,
toPkcs8,
utf8ToBase64,
type GitHubEnv,
} from './github'
@@ -127,3 +129,32 @@ describe('commit', () => {
expect(await commitWithRetry(env, async () => null)).toBeNull()
})
})
describe('toPkcs8', () => {
// GitHub downloads App keys as PKCS#1; WebCrypto only imports PKCS#8.
it('wraps PKCS#1, unescapes one-line keys and leaves PKCS#8 alone', () => {
const { privateKey } = generateKeyPairSync('rsa', {
modulusLength: 2048,
privateKeyEncoding: { type: 'pkcs1', format: 'pem' },
publicKeyEncoding: { type: 'spki', format: 'pem' },
})
const pkcs8 = createPrivateKey(privateKey).export({ type: 'pkcs8', format: 'pem' }).toString().trim()
const bare = (pem: string) => pem.replace(/\s+/g, '')
expect(bare(toPkcs8(privateKey))).toBe(bare(pkcs8))
const oneLine = privateKey.split('\n').join(String.raw`\n`)
expect(bare(toPkcs8(oneLine))).toBe(bare(pkcs8))
expect(toPkcs8(pkcs8)).toBe(pkcs8)
})
it('produces a key jose can sign an App JWT with', async () => {
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
modulusLength: 2048,
privateKeyEncoding: { type: 'pkcs1', format: 'pem' },
publicKeyEncoding: { type: 'spki', format: 'pem' },
})
const key = await importPKCS8(toPkcs8(privateKey), 'RS256')
const jwt = await new SignJWT({}).setProtectedHeader({ alg: 'RS256' }).setIssuer('app-id').sign(key)
const verified = await jwtVerify(jwt, await importSPKI(publicKey, 'RS256'))
expect(verified.payload.iss).toBe('app-id')
})
})
+63 -7
View File
@@ -16,7 +16,7 @@ export interface GitHubEnv {
GITHUB_REPO: string // "owner/name"
GH_APP_ID: string
GH_APP_INSTALLATION_ID: string
GH_APP_PRIVATE_KEY: string // PKCS#8 PEM
GH_APP_PRIVATE_KEY: string // PEM: PKCS#1 (as GitHub downloads it) or PKCS#8
}
const API = 'https://api.github.com'
@@ -30,14 +30,70 @@ const BASE_HEADERS = {
export class ConflictError extends Error {}
/** Anything GitHub (or its credentials) is unhappy about — the admin shows the message. */
export class GitHubError extends Error {}
let cached: { token: string; expiresAt: number } | undefined
export function resetTokenCache(): void {
cached = undefined
}
function bytesToBase64(bytes: Uint8Array): string {
let binary = ''
for (let i = 0; i < bytes.length; i += 0x8000) binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000))
return btoa(binary)
}
function der(tag: number, body: Uint8Array): Uint8Array {
const length: number[] = []
if (body.length < 0x80) length.push(body.length)
else {
const size: number[] = []
for (let v = body.length; v > 0; v >>= 8) size.unshift(v & 0xff)
length.push(0x80 | size.length, ...size)
}
const out = new Uint8Array(1 + length.length + body.length)
out[0] = tag
out.set(length, 1)
out.set(body, 1 + length.length)
return out
}
// SEQUENCE { OID 1.2.840.113549.1.1.1 (rsaEncryption), NULL }
const RSA_ALGORITHM = Uint8Array.of(0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00)
const ESCAPED_NEWLINE = /\\n/g
const PEM_LABEL = /-----[^-]+-----/g
/**
* WebCrypto imports PKCS#8 only, but GitHub hands out the App key as PKCS#1
* ("BEGIN RSA PRIVATE KEY"), so wrap that one instead of failing. Two-character
* "\n" escapes (a key pasted into a one-line field) become real newlines.
*/
export function toPkcs8(pem: string): string {
const text = pem.replace(ESCAPED_NEWLINE, '\n').trim()
if (!text.includes('BEGIN RSA PRIVATE KEY')) return text
const binary = atob(text.replace(PEM_LABEL, '').replace(/\s+/g, ''))
const pkcs1 = new Uint8Array(binary.length)
for (let i = 0; i < binary.length; i++) pkcs1[i] = binary.charCodeAt(i)
const version = Uint8Array.of(0x02, 0x01, 0x00)
const key = der(0x04, pkcs1)
const inner = new Uint8Array(version.length + RSA_ALGORITHM.length + key.length)
inner.set(version)
inner.set(RSA_ALGORITHM, version.length)
inner.set(key, version.length + RSA_ALGORITHM.length)
const base64 = bytesToBase64(der(0x30, inner)).replace(/(.{64})/g, '$1\n')
return `-----BEGIN PRIVATE KEY-----\n${base64}\n-----END PRIVATE KEY-----`
}
async function appJwt(env: GitHubEnv): Promise<string> {
const key = await importPKCS8(env.GH_APP_PRIVATE_KEY, 'RS256')
let key: CryptoKey
try {
key = await importPKCS8(toPkcs8(env.GH_APP_PRIVATE_KEY), 'RS256')
} catch (e) {
throw new GitHubError(`GH_APP_PRIVATE_KEY is not a usable RSA private key: ${(e as Error).message}`)
}
const now = Math.floor(Date.now() / 1000)
return new SignJWT({})
.setProtectedHeader({ alg: 'RS256' })
@@ -53,7 +109,7 @@ async function installationToken(env: GitHubEnv): Promise<string> {
method: 'POST',
headers: { ...BASE_HEADERS, Authorization: `Bearer ${await appJwt(env)}` },
})
if (!res.ok) throw new Error(`GitHub App token: HTTP ${res.status}`)
if (!res.ok) throw new GitHubError(`GitHub App token: HTTP ${res.status} (check the App id, installation id and key)`)
const body = (await res.json()) as { token: string; expires_at: string }
cached = { token: body.token, expiresAt: Date.parse(body.expires_at) }
return body.token
@@ -69,7 +125,7 @@ async function gh(env: GitHubEnv, path: string, init: RequestInit = {}): Promise
async function headOid(env: GitHubEnv): Promise<string> {
const res = await gh(env, `/repos/${env.GITHUB_REPO}/git/ref/heads/${BRANCH}`)
if (!res.ok) throw new Error(`read ref: HTTP ${res.status}`)
if (!res.ok) throw new GitHubError(`read ref: HTTP ${res.status}`)
return ((await res.json()) as { object: { sha: string } }).object.sha
}
@@ -79,14 +135,14 @@ export async function readFile(env: GitHubEnv, path: string, ref: string): Promi
headers: { Accept: 'application/vnd.github.raw+json' },
})
if (res.status === 404) return null
if (!res.ok) throw new Error(`read ${path}: HTTP ${res.status}`)
if (!res.ok) throw new GitHubError(`read ${path}: HTTP ${res.status}`)
return res.text()
}
/** Names of the files directly in a directory at a commit. */
export async function listDir(env: GitHubEnv, path: string, ref: string): Promise<string[]> {
const res = await gh(env, `/repos/${env.GITHUB_REPO}/contents/${path}?ref=${ref}`)
if (!res.ok) throw new Error(`list ${path}: HTTP ${res.status}`)
if (!res.ok) throw new GitHubError(`list ${path}: HTTP ${res.status}`)
const items = (await res.json()) as { name: string; type: string }[]
return items.filter((i) => i.type === 'file').map((i) => i.name)
}
@@ -136,7 +192,7 @@ export async function commit(env: GitHubEnv, input: CommitInput): Promise<string
}
const oid = body.data?.createCommitOnBranch?.commit.oid
if (!res.ok || errors.length > 0 || !oid) {
throw new Error(`commit: HTTP ${res.status} ${errors.map((e) => e.message).join('; ')}`)
throw new GitHubError(`commit: HTTP ${res.status} ${errors.map((e) => e.message).join('; ')}`)
}
return oid
}