/** * Which RPC methods need write access, and the check the RPC route applies. * * The authentication layer (`auth.js`, in front of the whole UI) decides each * request's access and passes it in the React Router load context as `auth`. * Read access can load every page, including decrypted data, but cannot change * a run or a hook. */ /** The authentication decision for one request, from `auth.js`. */ export interface WebAuthContext { /** `local`, `none`, `jwt` or `oidc`. */ mode: string; access: 'read' | 'write'; /** The token's or session's `sub` claim. */ subject?: string; email?: string; name?: string; } /** * RPC methods that change runs or hooks. `fetchHookToken` is among them: a * hook's token is enough to resume the hook through the application's own * webhook route. */ export const WRITE_RPC_METHODS: ReadonlySet = new Set([ 'cancelRun', 'bulkCancelRuns', 'recreateRun', 'reenqueueRun', 'wakeUpRun', 'resumeHook', 'fetchHookToken', ]); /** * Whether `method` names one of `handlers`' own methods. Only a string that is * an own key passes, so the access check and the dispatch see the same name. */ export function isRpcMethod( method: unknown, handlers: H ): method is Extract { return typeof method === 'string' && Object.hasOwn(handlers, method); } const MAX_REDACT_DEPTH = 32; /** * The value a read-only caller receives: a copy without any `token` property, * at any depth of plain objects and arrays. Hooks carry their token, and * `hook_created` events carry it in their data, and a token is enough to * resume the hook through the application's webhook route. Serialized * payloads (byte arrays) and other non-plain values are kept as they are. * Write access gets the value unchanged. */ export function redactForAccess( value: T, auth: WebAuthContext | undefined ): T { if (auth?.access !== 'read') return value; return redactTokens(value, 0) as T; } function redactTokens(value: unknown, depth: number): unknown { if (depth > MAX_REDACT_DEPTH || value === null || typeof value !== 'object') { return value; } if (Array.isArray(value)) return value.map((v) => redactTokens(v, depth + 1)); const proto = Object.getPrototypeOf(value); if (proto !== Object.prototype && proto !== null) return value; const out: Record = {}; for (const [key, v] of Object.entries(value)) { if (key === 'token') continue; out[key] = redactTokens(v, depth + 1); } return out; } /** * Returns an error message when `auth` may not call `method`, or undefined * when it may. Without an auth decision (the package's own `react-router dev` * server), every method is allowed. */ export function rpcAccessError( method: string, auth: WebAuthContext | undefined ): string | undefined { if (auth?.access === 'read' && WRITE_RPC_METHODS.has(method)) { return `Read-only access: ${method} is not allowed.`; } return undefined; } /** * The audit line for an authenticated write, or undefined when there is no * one to attribute it to (no authentication, or not a write). It names the * caller by the `sub` claim only: no email addresses in logs. */ export function rpcAuditLine( method: string, params: Record | undefined, auth: WebAuthContext | undefined ): string | undefined { if (!auth || !WRITE_RPC_METHODS.has(method)) return undefined; if (auth.mode !== 'jwt' && auth.mode !== 'oidc') return undefined; const who = auth.subject ?? 'unknown subject'; const target = typeof params?.runId === 'string' ? ` runId=${params.runId}` : Array.isArray(params?.runIds) ? ` runs=${params.runIds.length}` : ''; return `[workflow-web] ${who} called ${method}${target}`; }