The first SonarQube Cloud scan reported 57 issues. 45 are fixed in code, the 11 on the saved itch.io test pages leave the scan via .sonarcloud.properties, and css:S8776 (Tailwind's @utility nesting) is a false positive to mark on SonarQube Cloud. Worker: trailing slashes and the gate cookie's padding are trimmed by trimEnd() instead of /\/+$/ (quadratic on the public 404 path); the feed and collection parsers lose their backtracking regexes; compareCodeUnits keeps the code-unit sorts (the rule's localeCompare is locale-dependent); Number.*, DEFAULT_SIZES, crypto.randomUUID; i18n-edit.mjs reads stdin. Pipeline: the NSFW notices are one selector list, either class (the rule's ".a.b" would need both), each tested; plain if-guards; update_info.main returns None; no plain-http adapter; [[ ]] in cf_build_status.sh. Supply chain: requirements*.txt are hash-locked pip-tools compiles of requirements*.in (on Python 3.14, which Dependabot reads from the header), installed with --require-hashes --only-binary :all: in block scalars; npm ci --ignore-scripts in CI and the release builds (verified by manual release runs on all platforms); npm run cf:check. Release 4.4.2: also ships the toolchain upgrade (#149, #154) and Dependabot #141-#148, #150, #152, #153. CLAUDE.md gotchas 40 and 41. [skip-discuss] Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
70 lines
2.6 KiB
TypeScript
70 lines
2.6 KiB
TypeScript
/**
|
|
* Cloudflare Access verification for /admin and /api/admin/* (maintainer).
|
|
*
|
|
* Access sits in front of those paths, but a Worker with static assets does
|
|
* not receive `ctx.access`, and requests that bypass the zone (workers.dev,
|
|
* previews — both disabled in wrangler.jsonc) would skip Access entirely.
|
|
* So every protected request re-verifies the `Cf-Access-Jwt-Assertion` JWT
|
|
* here: signature against the team's JWKS, issuer, and the application's AUD.
|
|
*/
|
|
import { createRemoteJWKSet, jwtVerify, type JWTPayload } from 'jose'
|
|
import { trimEnd } from './text'
|
|
|
|
export interface AccessEnv {
|
|
ACCESS_TEAM_DOMAIN: string // https://<team>.cloudflareaccess.com
|
|
ACCESS_AUD_ADMIN: string
|
|
ADMIN_EMAILS: string // comma-separated; a secret, not in the repo
|
|
SITE_ORIGIN: string
|
|
}
|
|
|
|
export interface Identity {
|
|
kind: 'maintainer'
|
|
/** The maintainer's email. */
|
|
id: string
|
|
}
|
|
|
|
export class AccessError extends Error {}
|
|
|
|
const jwksByTeam = new Map<string, ReturnType<typeof createRemoteJWKSet>>()
|
|
|
|
function jwks(teamDomain: string) {
|
|
let set = jwksByTeam.get(teamDomain)
|
|
if (!set) {
|
|
set = createRemoteJWKSet(new URL(`${teamDomain}/cdn-cgi/access/certs`))
|
|
jwksByTeam.set(teamDomain, set)
|
|
}
|
|
return set
|
|
}
|
|
|
|
async function verify(request: Request, env: AccessEnv, audience: string): Promise<JWTPayload> {
|
|
const token = request.headers.get('cf-access-jwt-assertion')
|
|
if (!token) throw new AccessError('missing Access token')
|
|
// The issuer is the bare origin; tolerate a trailing slash pasted from the dashboard.
|
|
const team = trimEnd(env.ACCESS_TEAM_DOMAIN.trim(), '/')
|
|
try {
|
|
const { payload } = await jwtVerify(token, jwks(team), { issuer: team, audience })
|
|
return payload
|
|
} catch {
|
|
throw new AccessError('invalid Access token')
|
|
}
|
|
}
|
|
|
|
const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS'])
|
|
|
|
/** The maintainer, signed in through the "Admin" Access application. */
|
|
export async function requireMaintainer(request: Request, env: AccessEnv): Promise<Identity> {
|
|
const payload = await verify(request, env, env.ACCESS_AUD_ADMIN)
|
|
const email = String(payload.email ?? '').toLowerCase()
|
|
const allowed = env.ADMIN_EMAILS.toLowerCase()
|
|
.split(',')
|
|
.map((s) => s.trim())
|
|
.filter(Boolean)
|
|
if (!email || !allowed.includes(email)) throw new AccessError('not a maintainer')
|
|
// Cross-site request forgery guard for writes (the Access cookie is sent
|
|
// with top-level cross-site requests too).
|
|
if (!SAFE_METHODS.has(request.method) && request.headers.get('origin') !== env.SITE_ORIGIN) {
|
|
throw new AccessError('bad origin')
|
|
}
|
|
return { kind: 'maintainer', id: email }
|
|
}
|