fix(worker): scope Access AUDs per route instead of pinning a token name
`wrangler tail` settled what Cloudflare does not document: a service token's
identity arrives as its opaque Client ID —
ingest: principal not allowed { got: 'a5d1053e...access',
expected: 'f2p-discovery' }
— not its friendly name. The obvious fix was to paste that Client ID into
INGEST_SERVICE_PRINCIPAL, but that publishes half of a credential pair in a
public repo to shore up a check that was the weaker half of the design anyway.
The real defect is that ACCESS_AUD was ONE pooled list. verifyAccessJwt accepted
any AUD in it for any route, so the three credentials were interchangeable at
the JWT layer: an admin session satisfied /api/ingest/*, and the unattended
discovery token satisfied /api/admin/* — which holds the GitHub App credential
that can write to this repository. The name pin existed only to paper over that,
and a string comparison is a poor substitute for not accepting the token at all.
Now verifyAccessJwt takes the AUDs valid FOR THAT ROUTE:
ACCESS_AUD_ADMIN /admin, /api/admin/* (email policies)
ACCESS_AUD_INGEST /api/ingest/* (Service Auth, f2p-discovery)
A token minted for the wrong application now fails signature verification
rather than being caught downstream, so credential crossing is structurally
impossible instead of merely checked for. A second guard rejects the wrong
credential CLASS per route — service tokens are unattended and must never drive
admin; a human session must never drive ingest.
Neither AUD is a secret: any Access-covered path 302s to a login URL carrying
its own AUD as the `kid=` parameter, readable with an unauthenticated curl.
That is how both were obtained, and it is why they can live in the repo while
the Client ID should not.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
5 files changed
+61
-61
No files matched your search
Vendored
+3
-3
@@ -7,8 +7,8 @@ interface __BaseEnv_Env {
|
||||
SITE_ORIGIN: "https://free-steam-games.win";
|
||||
IMG_TRANSFORM: "false";
|
||||
ACCESS_TEAM_DOMAIN: "polished-hall-48fd.cloudflareaccess.com";
|
||||
INGEST_SERVICE_PRINCIPAL: "f2p-discovery";
|
||||
ACCESS_AUD: "ca93cf2c7444c20f13b0667a3063d4ac37f702a802e712721da53abb510f9194,a15d6e938cf3109651473e37c0c0fdf84a851248d8f57fdfda26808b40d3c3cb";
|
||||
ACCESS_AUD_ADMIN: "ca93cf2c7444c20f13b0667a3063d4ac37f702a802e712721da53abb510f9194,a15d6e938cf3109651473e37c0c0fdf84a851248d8f57fdfda26808b40d3c3cb";
|
||||
ACCESS_AUD_INGEST: "bd54655abb62f1bb72f8541177ef2406a066007a4c5ac60eb654f90f6ea88754";
|
||||
}
|
||||
declare namespace Cloudflare {
|
||||
interface GlobalProps {
|
||||
@@ -21,7 +21,7 @@ type StringifyValues<EnvType extends Record<string, unknown>> = {
|
||||
[Binding in keyof EnvType]: EnvType[Binding] extends string ? EnvType[Binding] : string;
|
||||
};
|
||||
declare namespace NodeJS {
|
||||
interface ProcessEnv extends StringifyValues<Pick<Cloudflare.Env, "SITE_ORIGIN" | "IMG_TRANSFORM" | "ACCESS_TEAM_DOMAIN" | "INGEST_SERVICE_PRINCIPAL" | "ACCESS_AUD">> {}
|
||||
interface ProcessEnv extends StringifyValues<Pick<Cloudflare.Env, "SITE_ORIGIN" | "IMG_TRANSFORM" | "ACCESS_TEAM_DOMAIN" | "ACCESS_AUD_ADMIN" | "ACCESS_AUD_INGEST">> {}
|
||||
}
|
||||
|
||||
// Begin runtime types
|
||||
|
||||
+20
-1
@@ -37,7 +37,15 @@ export default {
|
||||
const isAdminPage = pathname === "/admin" || pathname.startsWith("/admin/");
|
||||
|
||||
if (isAdminApi || isIngestApi || isAdminPage) {
|
||||
const who = await verifyAccessJwt(request, env);
|
||||
// The ingest surface is machine-only and the admin surface is
|
||||
// human-only, and they are separate Access applications with separate
|
||||
// AUDs. Choosing the AUD set here is what stops either credential
|
||||
// reaching the other's routes.
|
||||
const who = await verifyAccessJwt(
|
||||
request,
|
||||
env,
|
||||
isIngestApi ? env.ACCESS_AUD_INGEST : env.ACCESS_AUD_ADMIN,
|
||||
);
|
||||
if (!who) {
|
||||
// The client response is deliberately opaque, which makes this hard to
|
||||
// debug from outside — so say what happened in Workers Logs.
|
||||
@@ -60,6 +68,17 @@ export default {
|
||||
});
|
||||
}
|
||||
|
||||
// Belt and braces on top of the per-route AUD. A service token must
|
||||
// never drive the admin surface (it is unattended and cannot be
|
||||
// challenged), and a human session must never drive ingest.
|
||||
if (isIngestApi !== who.isServiceToken) {
|
||||
console.warn("access: wrong credential class for route", {
|
||||
path: pathname,
|
||||
isServiceToken: who.isServiceToken,
|
||||
});
|
||||
return jsonError(404, "not found");
|
||||
}
|
||||
|
||||
// CSRF. access.ts accepts the JWT from the CF_Authorization cookie, so a
|
||||
// cross-origin HTML form POST would carry it and Access would forward the
|
||||
// request with the assertion header attached. Same-origin browser
|
||||
|
||||
@@ -75,6 +75,16 @@ function decodeJson(part: string): Record<string, unknown> {
|
||||
export async function verifyAccessJwt(
|
||||
request: Request,
|
||||
env: Env,
|
||||
/**
|
||||
* Comma-separated AUDs acceptable FOR THIS ROUTE. Passed in per call rather
|
||||
* than read from one global list: this Worker sits behind three Access
|
||||
* applications, and a single union list means any of the three credentials
|
||||
* satisfies any route — an admin session could drive /api/ingest/*, and the
|
||||
* unattended discovery token could drive /api/admin/*, which holds a
|
||||
* repository-write credential. Scoping the AUD to the route makes that
|
||||
* crossing impossible rather than merely checked for afterwards.
|
||||
*/
|
||||
allowedAud: string,
|
||||
): Promise<AccessIdentity | null> {
|
||||
const token =
|
||||
request.headers.get("Cf-Access-Jwt-Assertion") ??
|
||||
@@ -110,12 +120,11 @@ export async function verifyAccessJwt(
|
||||
// minted for any other application in the same Access organisation would
|
||||
// be accepted here.
|
||||
//
|
||||
// ACCESS_AUD is a comma-separated LIST because each Access application has
|
||||
// its own AUD tag, and this Worker sits behind two: one for /admin and one
|
||||
// for /api/admin/*. Configuring only the first made every API call fail
|
||||
// verification while the page itself worked — and both failures surfaced
|
||||
// as an identical 404.
|
||||
const allowed = env.ACCESS_AUD.split(",").map((a) => a.trim()).filter(Boolean);
|
||||
// Each Access application has its own AUD tag, so this is a list even for
|
||||
// one route group. Configuring only /admin's tag once made every API call
|
||||
// fail verification while the page itself worked — and both failures
|
||||
// surfaced as an identical 404.
|
||||
const allowed = allowedAud.split(",").map((a) => a.trim()).filter(Boolean);
|
||||
const aud = claims.aud;
|
||||
const audList = (Array.isArray(aud) ? aud : [aud]).filter(
|
||||
(a): a is string => typeof a === "string",
|
||||
|
||||
@@ -5,6 +5,13 @@
|
||||
* Authenticated by a Cloudflare Access SERVICE TOKEN, not a human login. The
|
||||
* caller is a GitHub Actions job, so there is no browser and no session.
|
||||
*
|
||||
* The caller is pinned by AUD in worker/index.ts: only tokens minted by the
|
||||
* Access application whose sole policy is the `f2p-discovery` service token
|
||||
* reach here. That is why this file does not re-check the principal by name —
|
||||
* Access reports a service token's identity as its opaque Client ID, not its
|
||||
* friendly name, so a name comparison here would be both wrong and weaker
|
||||
* than the AUD it duplicates.
|
||||
*
|
||||
* IMPORTANT: this surface can only ever PROPOSE. It writes rows with
|
||||
* status='pending' and cannot approve, commit, or otherwise reach the
|
||||
* repository. A stolen service token buys an attacker a filled review queue,
|
||||
@@ -35,27 +42,6 @@ function json(body: unknown, status = 200): Response {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Only the discovery pipeline's own service token may use this surface.
|
||||
*
|
||||
* verifyAccessJwt accepts a token bearing ANY configured AUD, so without this
|
||||
* an admin session would satisfy the ingest routes and — far worse — the
|
||||
* ingest token would satisfy the admin routes. Pinning the principal on both
|
||||
* sides is what stops the two credentials crossing.
|
||||
*/
|
||||
function isIngestPrincipal(who: AccessIdentity, env: Env): boolean {
|
||||
if (!who.isServiceToken) return false;
|
||||
// Comma-separated. Cloudflare does not document whether `common_name` on a
|
||||
// service-token JWT carries the token's friendly NAME or its Client ID, and
|
||||
// the two are indistinguishable from this side, so the pin accepts either
|
||||
// spelling of the same one token rather than guessing wrong and 404ing a
|
||||
// correctly-configured caller.
|
||||
return env.INGEST_SERVICE_PRINCIPAL.split(",")
|
||||
.map((p) => p.trim())
|
||||
.filter(Boolean)
|
||||
.includes(who.email);
|
||||
}
|
||||
|
||||
interface CandidateIn {
|
||||
appid?: unknown;
|
||||
link?: unknown;
|
||||
@@ -142,18 +128,6 @@ export async function handleIngestApi(
|
||||
env: Env,
|
||||
who: AccessIdentity,
|
||||
): Promise<Response> {
|
||||
if (!isIngestPrincipal(who, env)) {
|
||||
// The identity Access actually handed us. Without this the rejection is a
|
||||
// bare 404 indistinguishable from "no Access application covers the path",
|
||||
// and the only way to learn the real principal is to guess at it.
|
||||
console.warn("ingest: principal not allowed", {
|
||||
got: who.email,
|
||||
isServiceToken: who.isServiceToken,
|
||||
expected: env.INGEST_SERVICE_PRINCIPAL,
|
||||
});
|
||||
return jsonError(404, "not found");
|
||||
}
|
||||
|
||||
const route = url.pathname.slice("/api/ingest/".length);
|
||||
|
||||
// Cheap reachability probe. discover_new.py calls this BEFORE spending any
|
||||
|
||||
+16
-18
@@ -82,24 +82,22 @@
|
||||
// vars so the Worker can verify tokens itself rather than trusting that a
|
||||
// request reached it through the Access-protected path.
|
||||
"ACCESS_TEAM_DOMAIN": "polished-hall-48fd.cloudflareaccess.com",
|
||||
// The Access service-token name the discovery pipeline authenticates as.
|
||||
// Pinned so the ingest token cannot use /api/admin/* and an admin session
|
||||
// cannot use /api/ingest/* — verifyAccessJwt alone would accept either.
|
||||
"INGEST_SERVICE_PRINCIPAL": "f2p-discovery",
|
||||
|
||||
// Comma-separated: one AUD per Access application. This Worker sits behind
|
||||
// three, and each has its own tag:
|
||||
// ca93cf2c… /admin (email policy)
|
||||
// a15d6e93… /api/admin/* (email policy)
|
||||
// bd54655a… /api/ingest/* (service auth, token f2p-discovery)
|
||||
// AUDs are grouped BY ROUTE, not pooled into one list. verifyAccessJwt is
|
||||
// called with only the group a route accepts, so the unattended discovery
|
||||
// token cannot reach /api/admin/* (which holds a repository-write
|
||||
// credential) and an admin session cannot reach /api/ingest/*.
|
||||
//
|
||||
// ADD A TAG HERE whenever another Access application is pointed at this
|
||||
// Worker. Forgetting fails only AFTER the application starts working, and
|
||||
// the two failures look nothing alike in `wrangler tail`:
|
||||
// hasHeader: false, hasCookie: false → no Access app covers the path
|
||||
// access: aud mismatch → app exists, this list is stale
|
||||
// Read a tag without the dashboard: an Access-covered path 302s to the
|
||||
// login URL carrying its AUD as the `kid=` query parameter.
|
||||
"ACCESS_AUD": "ca93cf2c7444c20f13b0667a3063d4ac37f702a802e712721da53abb510f9194,a15d6e938cf3109651473e37c0c0fdf84a851248d8f57fdfda26808b40d3c3cb,bd54655abb62f1bb72f8541177ef2406a066007a4c5ac60eb654f90f6ea88754"
|
||||
// These are not secrets: any Access-covered path 302s to a login URL
|
||||
// carrying its own AUD as the `kid=` query parameter, readable with an
|
||||
// unauthenticated curl. Add a tag here whenever another Access application
|
||||
// is pointed at this Worker, or every request through it is refused with
|
||||
// `access: aud mismatch` in `wrangler tail`.
|
||||
//
|
||||
// ca93cf2c... /admin (email policy)
|
||||
// a15d6e93... /api/admin/* (email policy)
|
||||
"ACCESS_AUD_ADMIN": "ca93cf2c7444c20f13b0667a3063d4ac37f702a802e712721da53abb510f9194,a15d6e938cf3109651473e37c0c0fdf84a851248d8f57fdfda26808b40d3c3cb",
|
||||
|
||||
// bd54655a... /api/ingest/* (Service Auth, token f2p-discovery)
|
||||
"ACCESS_AUD_INGEST": "bd54655abb62f1bb72f8541177ef2406a066007a4c5ac60eb654f90f6ea88754"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user