R21.2: enforce the AppCaller brand at runtime with a module-private WeakSet
The symbol brand on AppCaller stopped a TypeScript importer from
writing the `{ appSlug }` literal, but nothing enforced it at
runtime: no handler inspected the branded field, so a cast
(`as unknown as AppCaller`) reached refreshAppSession/startOAuthFlow's
caller gate with a forged value carrying the right appSlug. A brand
that claims an authority it does not enforce is worse than none --
this closes that gap by making the brand real instead of documenting
what it does not do.
brandCaller (app-caller.ts) now registers every AppCaller it mints in
a module-private WeakSet; isAppCaller is the only way a consumer may
trust one. startOAuthFlow and refreshAppSession call isAppCaller(caller)
instead of trusting the AppCaller | null type alone at their two gate
checks.
Flips Soul's forged-caller it.fails to a passing it.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
ea804ddbbe
commit
3427bdda1e
3 changed files with 34 additions and 17 deletions
|
|
@ -18,10 +18,13 @@ import { type HeimdallConfig } from "./config.js";
|
|||
|
||||
// `{ appSlug }` used to satisfy this type structurally, so any module could
|
||||
// construct a caller by writing the literal — nothing but grep discipline
|
||||
// stopped an importer from forging one. The brand is a symbol that never
|
||||
// leaves this module, so only the two constructors below (resolveAppCaller
|
||||
// for HTTP, callerFromOpenedEnvelope for the private command plane) can
|
||||
// produce a value TypeScript will accept as an AppCaller.
|
||||
// stopped an importer from forging one. The symbol brand stops that at
|
||||
// compile time, but a cast (`as unknown as AppCaller`) or a JSON round-trip
|
||||
// defeats a type-level check trivially; nothing enforced it at runtime. The
|
||||
// module-private WeakSet below is the enforcement: brandCaller registers
|
||||
// every value it mints, and isAppCaller is the only way a consumer may trust
|
||||
// one. A forged literal — even one carrying the symbol key by hand — was
|
||||
// never added to the set and is rejected.
|
||||
const callerBrand = Symbol("AppCaller");
|
||||
|
||||
export interface AppCaller {
|
||||
|
|
@ -29,8 +32,23 @@ export interface AppCaller {
|
|||
readonly [callerBrand]: true;
|
||||
}
|
||||
|
||||
const mintedCallers = new WeakSet<object>();
|
||||
|
||||
function brandCaller(appSlug: AppSlug): AppCaller {
|
||||
return { appSlug, [callerBrand]: true };
|
||||
const caller: AppCaller = { appSlug, [callerBrand]: true };
|
||||
mintedCallers.add(caller);
|
||||
return caller;
|
||||
}
|
||||
|
||||
/**
|
||||
* The runtime half of the brand. `startOAuthFlow` and `refreshAppSession`
|
||||
* call this instead of trusting the `AppCaller | null` type alone — a value
|
||||
* that is not exactly the object `resolveAppCaller` or
|
||||
* `callerFromOpenedEnvelope` returned is never in the set, regardless of
|
||||
* shape or cast.
|
||||
*/
|
||||
export function isAppCaller(value: unknown): value is AppCaller {
|
||||
return typeof value === "object" && value !== null && mintedCallers.has(value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { createHmac, randomUUID } from "node:crypto";
|
||||
import cors from "@fastify/cors";
|
||||
import Fastify, { type FastifyInstance } from "fastify";
|
||||
import { type AppCaller, resolveAppCaller } from "./app-caller.js";
|
||||
import { type AppCaller, isAppCaller, resolveAppCaller } from "./app-caller.js";
|
||||
import { deliverBackendHandoff, type BackendHandoffPayload } from "./backend-handoff.js";
|
||||
import {
|
||||
appSlugs,
|
||||
|
|
@ -586,7 +586,7 @@ export async function startOAuthFlow(
|
|||
if (options?.trustedBrowserAttemptId && handoff.kind === "browser_completion") {
|
||||
handoff = { kind: "browser_completion", attemptId: options.trustedBrowserAttemptId };
|
||||
}
|
||||
if (entitlementPolicy && (!caller || caller.appSlug !== profile.slug)) {
|
||||
if (entitlementPolicy && (!isAppCaller(caller) || caller.appSlug !== profile.slug)) {
|
||||
return { statusCode: 401, body: { error: "app_auth_required" } };
|
||||
}
|
||||
|
||||
|
|
@ -661,7 +661,7 @@ export async function refreshAppSession(
|
|||
},
|
||||
};
|
||||
}
|
||||
if (entitlementPolicies.length > 0 && (!caller || caller.appSlug !== appSlug)) {
|
||||
if (entitlementPolicies.length > 0 && (!isAppCaller(caller) || caller.appSlug !== appSlug)) {
|
||||
return { statusCode: 401, body: { error: "app_auth_required" } };
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -340,15 +340,14 @@ describe("isAllowedReturnOrigin: origin comparison, not string matching", () =>
|
|||
});
|
||||
});
|
||||
|
||||
describe("AppCaller brand: compile-time only", () => {
|
||||
// Hole, by construction rather than by accident: the symbol brand stops a
|
||||
// TypeScript importer from writing `{ appSlug }`, and nothing else. No
|
||||
// handler inspects the brand at runtime, so a forged literal that reaches
|
||||
// refreshAppSession via `as unknown as AppCaller`, plain JS, or a JSON
|
||||
// round-trip is honoured. The barrier is type-checking plus the two
|
||||
// constructor call sites, and the grep test in caller-identity-soul.test.ts
|
||||
// is what stands guard over those call sites.
|
||||
it.fails("a forged caller literal is refused at runtime", async () => {
|
||||
describe("AppCaller brand: enforced by a module-private WeakSet (R21.2)", () => {
|
||||
// Closed. brandCaller (app-caller.ts) registers every AppCaller it mints in
|
||||
// a WeakSet that never leaves the module; isAppCaller is the only way a
|
||||
// consumer may trust one, and startOAuthFlow / refreshAppSession now call
|
||||
// it instead of trusting the AppCaller | null type alone. A forged literal
|
||||
// — even one carrying the callerBrand symbol key by hand — was never added
|
||||
// to the set and is rejected regardless of shape or cast.
|
||||
it("a forged caller literal is refused at runtime", async () => {
|
||||
const { app } = await harness();
|
||||
const context = (app as unknown as { heimdallContext: Parameters<typeof refreshAppSession>[0] }).heimdallContext;
|
||||
const forged = { appSlug: "repixelizer" } as unknown as AppCaller;
|
||||
|
|
|
|||
Loading…
Reference in a new issue