cloud / core / secrets-worker / src / secret / durable-object.tsmain at aacbc74
// The authority over one secret. The mind's `/secrets/<name>` context is its record, which the
// mind's own code can withhold from or forge into, so nothing here reads it.
import { DurableObject, env } from "cloudflare:workers";
import type {
CloudClientName,
SecretBodyField,
SecretMaterial,
SecretOperation,
SecretProxy,
SecretRefresh,
} from "iterate/api";
import {
codedError,
errorCode,
fetchLeavingTheStack,
ITERATE_CAUSE_HEADER,
reportIssue,
} from "iterate/lib";
import { type Facts, withFacts } from "iterate/facts";
import { controlPlaneOriginOf, zoneOf } from "iterate/mind-config";
import {
assertSecretPath,
headerNamesASecret,
SECRET_OAUTH_CALLBACK_PATH,
SECRET_HELD_HEADER,
SECRET_USE_HEADER,
secretPathsIn,
VAULT_ANSWER_HEADER,
type VaultAnswerKind,
} from "iterate/secret-placeholder";
import type {
SecretAnswer,
SecretDescription,
SecretOAuthResult,
SecretRefusal,
SecretVerb,
SecretVerbs,
} from "iterate/workers";
import { z } from "zod";
import {
ITERATE_STATEMENT_HEADER,
mindFetchStatements,
signedRequestHeaders,
type Envelope,
} from "iterate/signed";
import type { PageCaller } from "../callback-page.ts";
import {
bodyToFill,
fieldsToFill,
leavesAsRead,
textOf,
withBodyFilled,
} from "../body-placement.ts";
import { askOf, brokered, cloudClientOf, cloudPinRefusal } from "../cloud-client.ts";
import { configOf, type Env } from "../env.ts";
import { decryptSecretMaterial, encryptSecretMaterial } from "../secret-at-rest.ts";
import {
accountOf,
beginSecretOAuth,
completeSecretOAuth,
grantCeilingOf,
grantRefusal,
provenanceRefusal,
normalizeSecretOAuth,
stateOf,
type PendingSecretOAuth,
} from "../secret-oauth.ts";
import { startLinkOf } from "../secret-oauth-start.ts";
import { stackSecretDescription, stackSecretOf, stackSecretRefusal } from "../stack-secrets.ts";
import {
accessTokenEnded,
clientSecretReferenceOf,
cloudOriginOf,
githubAppJwt,
hmacSha256Hex,
isRecord,
normalizeSecretRecord,
oauthTokenRequest,
originPinned,
type ProxiedSecretRecord,
proxiedSecretRecordOf,
secretProxyOf,
originsOf,
pinRefusal,
publicFieldsOf,
secretUseOf,
type SecretUse,
SecretRefused,
REFRESH_READS,
refreshOnlyFieldRefusal,
refreshSecretMaterial,
RefreshUnanswered,
secretMaterialStringOf,
secretsEqual,
sha256Hex,
substituteMindSecrets,
SecretHmacSigningInput,
SecretKeyDerivationInput,
signSecretHmac,
verifySecretEquals,
verifySecretHmac,
type SecretRecord,
} from "../secrets.ts";
import {
ApprovalRule,
bodyTextOf,
fillsOfText,
HELD_BODY_LIMIT,
HeldAnswer,
type HeldFills,
heldOfText,
isTightening,
METHOD_HEADERS,
methodsOf,
requestOfText,
requestText,
ruleMatches,
ruleNames,
} from "../approval.ts";
import { INDEX, SET } from "../approvals-index.ts";
import { type SealedCell, type SecretFact, type Signer, StoredSecret } from "./contract.ts";
import { lendRefusal, operationRefusal, placementRefusal } from "./refusals.ts";
import { runExchangeCode } from "./exchange-jail.ts";
import {
generatedSigningMaterial,
hmacKeyOf,
SecretKeyAgreement,
SecretKeyField,
SecretKeySignature,
SecretSealInput,
SecretStatementInput,
SecretUnsealInput,
sealedText,
signedStatement,
signingKeyOf,
underStatementPaths,
unsealedText,
x25519PublicKey,
x25519SharedSecret,
x25519Sign,
} from "./key-ops.ts";
type Current = StoredSecret;
/** The nonce of a secret that the stack made: it has no cell, and it is never refreshed. */
const STACK_NONCE = "stack";
// Per fixed minute, in memory: an eviction resets them. `verifyHmac` is not counted.
const OPERATIONS_PER_MINUTE = 300;
const FALSE_VERIFICATIONS_PER_MINUTE = 20;
const useOf = ({
placement,
operations,
lendTo,
statementPaths,
bodyFields,
}: SecretUse): SecretUse => ({
placement,
operations,
lendTo,
statementPaths,
bodyFields,
});
// `from` is the kernel's claim, never checked.
export type HeldRow = {
digest: string;
text: string;
at: number;
until: number;
from?: string;
state: "waiting" | "approved" | "rejected" | "sent";
signer?: Signer;
reason?: string;
};
export const HOLD_MS = 5 * 60_000;
export const MAX_WAITING = 50;
// A later approval serves an identical retry for a day.
const HELD_ROW_MS = 24 * 60 * 60_000;
// Each statement type is also the type of the fact it leaves.
export const APPROVED = "events.iterate.com/secret/request-approved";
export const REJECTED = "events.iterate.com/secret/request-rejected";
export const RULE_CONFIGURED = "events.iterate.com/secret/approval-rule-configured";
// `basedOn`: the version the attempt began on; a write since ends it. `ended`: the provider's
// `error` when its callback came with one (`recordOAuthError`).
type PendingAttempt = PendingSecretOAuth & {
basedOn: number;
authorizationUrl: string;
ended?: { status: "declined" | "failed"; error: string; at: number };
};
type Completed = {
nonce: string;
version: number;
scopes: string[];
account?: { id: string; name: string | null };
};
type Collecting = {
nonce: string;
urls: string[];
description?: string;
fields?: CollectField[];
redirectUrl?: string;
until: number;
use: SecretUse;
anyoneWithLink: boolean;
};
type CollectField = z.infer<typeof CollectField>;
const COLLECT_TTL_MS = 24 * 60 * 60_000;
const CollectField = z.object({
name: z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/),
label: z.string().trim().min(1).max(80),
multiline: z.boolean().optional(),
public: z.boolean().optional(),
placeholder: z.string().trim().min(1).max(120).optional(),
// HTML's pattern attribute, which the browser compiles with the `v` flag
pattern: z
.string()
.min(1)
.max(500)
.refine((pattern) => {
try {
new RegExp(pattern, "v");
return true;
} catch {
return false;
}
}, "pattern is a regular expression, as HTML's pattern attribute takes it")
.optional(),
});
const CollectInput = z.object({
egress: z.object({ urls: z.array(z.string()) }),
// the form's Markdown takes time that grows faster than its length on a hostile line
description: z.string().max(4000).optional(),
fields: z
.array(CollectField)
.min(1)
.max(10)
.refine(
(fields) => new Set(fields.map((field) => field.name)).size === fields.length,
"field names are unique",
)
.optional(),
redirectUrl: z
.url({ protocol: /^https?$/ })
.refine((value) => {
const url = new URL(value);
return !url.username && !url.password;
}, "redirectUrl is an http(s) URL without credentials")
.optional(),
anyoneWithLink: z.boolean().optional(),
});
// It arrives untyped.
const SecretHmacVerificationInput = z.object({
payload: z.union([z.string(), z.instanceof(Uint8Array)]),
signature: z.string(),
field: z.string().min(1).optional(),
derive: SecretKeyDerivationInput.optional(),
});
const deletedWhileStored = (path: string) =>
`secret ${path}: it was deleted while this value was being stored, and the value is not kept. Set it again if it is still wanted`;
/** What came of a refresh on word (`refreshGrant`). */
export type GrantRefresh =
| { outcome: "refreshed" | "fresh" | "not-a-brokered-grant" }
| { outcome: "failed"; error: string; answerLost?: true };
/** When a refresh on word last brought new tokens, and for how long a grant is fresh after it. */
const GRANT_REFRESHED_AT = "grant-refreshed-at";
const GRANT_FRESH_MS = 60 * 60_000;
/** How far ahead an export's proof may name its end (`#exportSealed`). */
const EXPORT_PROOF_MS = 5 * 60_000;
/** How often one refresh on word asks while no answer comes: once more, with the same token. */
const GRANT_TRIES = 2;
class RefreshFailed extends Error {
readonly facts: SecretFact[];
readonly lent: Facts;
/** No answer came (../secrets.ts `RefreshUnanswered`): the provider did not refuse. */
readonly answerLost: boolean;
/** One more try at once may help: no answer came, and the answer did not say "not at once". */
readonly askAgain: boolean;
// Not parameter properties: erasable syntax only, as in secrets.ts.
constructor(message: string, facts: SecretFact[], lent: Facts, unanswered?: RefreshUnanswered) {
super(message);
this.facts = facts;
this.lent = lent;
this.answerLost = Boolean(unanswered);
this.askAgain = Boolean(unanswered) && !unanswered?.notAtOnce;
}
}
class LendRefused extends Error {
readonly facts: Facts;
constructor(message: string, facts: Facts) {
super(message);
this.facts = facts;
}
}
// The page's `form-action` must name `redirectOrigin`: a browser holds a form's redirect to it.
export type CollectionForm = Pick<
Collecting,
"urls" | "description" | "fields" | "use" | "anyoneWithLink"
> & { redirectOrigin?: string };
/** A use refused because the refresh it needed failed at the provider: not the caller's mistake. */
class RefreshFailedRefusal extends SecretRefused {}
export class SecretDurableObject extends DurableObject<Env> {
// a warm use by proxy awaits nothing but its dispatch: the opened proxy and the statements
#proxyOpened: { nonce: string; proxy: SecretProxy } | undefined;
#statements = mindFetchStatements(64);
// Single-flight per cell: callers who 401 together share one mint. A caller holding a newer cell
// queues behind the running one.
#refreshing: { nonce: string; promise: Promise<SecretFact[]> } | undefined;
#minute = { start: 0, operations: 0, falseVerifications: 0 };
#waiting = new Map<string, Set<() => void>>();
#pathOf(path: string): string {
assertSecretPath(path);
const named = this.ctx.id.name;
if (named && named !== path) throw new Error(`this object is the secret ${named}, not ${path}`);
return path;
}
#current(): Current | undefined {
return this.ctx.storage.kv.get<Current>("current");
}
// Never goes back.
#version(): number {
return this.ctx.storage.kv.get<number>("version") ?? 0;
}
#key(): string {
return configOf(this.env).atRestKey.exposeSecret();
}
/** What the stack holds at this path, when the path is one whose value the deploy made
* (../stack-secrets.ts): the config is its value, and this object's storage is not read. */
#stackSecret(path: string) {
return stackSecretOf(configOf(this.env).cloudflareRealtime, path);
}
// The pages' own calls and another secret's `clientSecretFor` are this Worker's alone: no door
// outside it reaches them.
async secret(path: string, verb: string, input: unknown): Promise<unknown> {
const stack = this.#stackSecret(this.#pathOf(path));
if (stack) {
if (verb === "describe") return stackSecretDescription(stack, path);
throw stackSecretRefusal(path, verb);
}
const verbs: {
[V in SecretVerb]: (input: SecretVerbs[V]["input"]) => Promise<SecretVerbs[V]["answer"]>;
} = {
set: (input) => this.#set(path, input),
delete: () => this.#delete(path),
deleteIfProxy: () => this.#deleteIfProxy(path),
describe: () => this.#describe(path),
importSealed: (input) => this.#importSealed(path, input.cell, input.proof),
exportSealed: (input) => this.#exportSealed(path, input.proof, input.until),
beginOAuth: (input) => this.#beginOAuth(path, input),
oauthResult: (input) => this.#oauthResult(path, input),
collectFromUser: (input) => this.#collectFromUser(path, input),
signHmac: (input) => this.#signHmac(path, input),
verifyHmac: (input) => this.#verifyHmac(path, input),
verifyEquals: (input) => this.#verifyEquals(path, input),
deriveSharedSecret: (input) => this.#deriveSharedSecret(path, input),
publicKeyOf: (input) => this.#publicKeyOf(path, input),
signWithKey: (input) => this.#signWithKey(path, input),
signStatement: (input) => this.#signStatement(path, input),
generateSigningKey: (input) => this.#generateSigningKey(path, input),
seal: (input) => this.#seal(path, input),
unseal: (input) => this.#unseal(path, input),
};
if (!Object.hasOwn(verbs, verb))
throw codedError("INVALID_INPUT", `secrets: ${JSON.stringify(verb)} is no verb of a secret`);
return (verbs[verb as SecretVerb] as (input: unknown) => Promise<unknown>)(input);
}
// Material always travels with its complete policy: a value never inherits a pin or a strategy.
async #set(path: string, input: unknown): Promise<SecretAnswer<{ path: string }>> {
this.#pathOf(path);
if (!isRecord(input))
throw codedError(
"INVALID_INPUT",
"secrets.set: pass { material, urls, refresh?, public?, placement?, bodyFields?, operations?, lendTo? }, or { material: null, urls, proxy }",
);
if (input.proxy !== undefined)
return {
result: { path },
facts: [{ path, events: [await this.#write(path, proxiedSecretRecordOf(input), {})] }],
};
const record = normalizeSecretRecord(input.material, {
urls: input.urls,
refresh: input.refresh,
placement: input.placement,
operations: input.operations,
lendTo: input.lendTo,
statementPaths: input.statementPaths,
bodyFields: input.bodyFields,
});
const publicFields =
input.public === undefined ? undefined : publicFieldsOf(input.material, input.public);
return {
result: { path },
facts: [{ path, events: [await this.#write(path, record, { public: publicFields })] }],
};
}
async #delete(path: string): Promise<SecretAnswer<{ path: string }>> {
this.#pathOf(path);
if (this.#version() === 0)
throw codedError("SECRET_NOT_SET", `secret ${path}: never set — nothing to delete`);
return this.#drop(path, this.#current());
}
/** A DELETE ONLY OF A SECRET BY PROXY, decided with no await between the look and the delete:
* anything else (a value, a write that reserved a later version, a live OAuth attempt or key
* form, nothing) stays, and the answer says `deleted: false`. An attempt or a form that lapsed,
* by the test their own calls use, blocks nothing, and the delete removes it as `delete` does. */
async #deleteIfProxy(path: string): Promise<SecretAnswer<{ path: string; deleted: boolean }>> {
this.#pathOf(path);
const held = this.#current();
const now = Date.now();
const attempt = this.ctx.storage.kv.get<PendingAttempt>("pending");
const form = this.ctx.storage.kv.get<Collecting>("collecting");
const byProxy =
held?.cell.byProxy === true &&
this.#version() === held.cell.version &&
!(attempt && !attempt.ended && attempt.until > now) &&
!(form && form.until > now);
if (!byProxy) return { result: { path, deleted: false }, facts: [] };
const { facts } = await this.#drop(path, held);
return { result: { path, deleted: true }, facts };
}
/** The delete itself: its first part is synchronous, so a check just before it holds. */
async #drop(path: string, held: Current | undefined): Promise<SecretAnswer<{ path: string }>> {
// A write that reserved its version before this delete, and still seals, is earlier than the
// delete: `#write` reads this and keeps no value of it.
this.ctx.storage.kv.put("deleted-through", this.#version());
for (const key of ["current", "pending", "completed", "collecting"])
this.ctx.storage.kv.delete(key);
await this.keepListed(path);
// THE FACT SAYS WHAT THE OBJECT IS WHEN THE CALL ENDS. The call to the vault's list lets
// another call of this object in: a set that landed meanwhile is the later word, and its own
// fact says that the secret is set. A fact of this delete, which could land in the log after
// that one, would say that the secret is gone.
const events: SecretFact[] =
held && !this.#current()
? [{ type: "events.iterate.com/secret/deleted", payload: { path } }]
: [];
if (held) console.info({ event: "secret.deleted", path, version: held.cell.version });
return { result: { path }, facts: events.length ? [{ path, events }] : [] };
}
async #describe(path: string): Promise<SecretDescription> {
this.#pathOf(path);
const current = this.#current();
if (!current) return { path, set: false };
const { cell } = current;
// whose client refreshes it: an iterate Cloud's, by the Cloud's name for the client, and the
// Cloud's origin as the secret keeps it
const client = cell.refresh && "client" in cell.refresh ? cell.refresh.client : undefined;
const cloud = client && "cloud" in client ? client : undefined;
return {
path,
set: true,
urls: cell.urls,
...(cell.refresh && { refresh: cell.refresh.kind }),
cloud: cloud?.cloud,
cloudOrigin: cloud?.origin,
public: current.public,
...useOf(cell),
...(cell.byProxy && (await this.#proxyDescribed(path, current))),
};
}
// `proof` is an HMAC under the at-rest key of the path and the cell, checked before anything opens.
// The import writes over any value held: the old mind may have rotated a refresh token since.
/** A SEALED CELL, TAKEN IN: one that this vault handed out (`exportSealed`), which opens under
* this stack's own key with its own binding and comes back with the use and the public fields
* it had. */
async #importSealed(
path: string,
// the JSON text the proof was made over: its bytes, not its keys' order, crossed every hop
cell: string,
proof: string,
): Promise<SecretAnswer<{ path: string }>> {
this.#pathOf(path);
const sealed = StoredSecret.parse(JSON.parse(cell));
const { nonce } = sealed.cell;
// The proof first: a call that proves nothing keeps nothing. Were the cell's nonce kept before
// this check, a caller with no key could name a cell and shut its own import out for good, and
// fill this object with nonces.
if (
!(await secretsEqual(proof, await hmacSha256Hex(this.#key(), JSON.stringify([path, cell]))))
)
throw codedError(
"FORBIDDEN",
`secrets.importSealed: no proof under the at-rest key for ${path}`,
);
// A proof is good for ever, so each proven cell imports once. Read and kept with no await
// between, so two imports of one cell cannot both pass; kept by `delete`.
const imported = this.ctx.storage.kv.get<string[]>("imported-nonces") || [];
if (imported.includes(nonce))
throw codedError(
"FORBIDDEN",
`secrets.importSealed: this cell of ${path} was imported once already`,
);
this.ctx.storage.kv.put("imported-nonces", [...imported, nonce]);
const versionBefore = this.#version();
let refusedProxy = false;
try {
let record: SecretRecord | ProxiedSecretRecord;
let publicFields: Record<string, string> | undefined;
try {
record = await this.#recordOf(path, sealed.cell);
// `PUBLIC` IS BESIDE THE CELL AND OUTSIDE ITS SEAL, so whoever changes a backup can write
// another. Each field is taken only when the material has it with that very value. A
// changed copy can then neither say another value, nor name a secret field to make it
// public: it would have to know that field's value.
if (sealed.public) {
if ("proxy" in record) throw new Error("a secret by proxy has no public fields");
const given = Object.entries(sealed.public);
publicFields = publicFieldsOf(
record.material,
given.map(([name]) => name),
);
if (given.some(([name, value]) => publicFields?.[name] !== value))
throw new Error("a public field is not the material's");
}
} catch {
throw codedError(
"INVALID_INPUT",
`secrets.importSealed: the cell for ${path} does not open under this stack's at-rest key, or its public fields are not those of its material`,
);
}
const fact = await this.#write(path, record, {
imported: true,
public: publicFields,
refusedProxy: () => (refusedProxy = true),
});
return { result: { path }, facts: [{ path, events: [fact] }] };
} catch (error) {
// A CELL WHOSE VALUE IS NOT STORED WAS NOT IMPORTED, and its nonce is free again: it did
// not open, or its write failed, or a delete took the value while it was written. A changed
// copy has the true cell's nonce, and a restore holds no other copy of a cell: neither a
// damaged copy tried first nor a write that failed may shut the cell out for good. The
// guard reads what the object holds now, not what this import stored: a set that lands
// while a failing import is in flight keeps the nonce of a cell that was not imported.
// A proxy refused over a set or a rule stored nothing, whatever the version says.
if ((this.#current()?.cell.version ?? 0) <= versionBefore || refusedProxy)
this.ctx.storage.kv.put(
"imported-nonces",
(this.ctx.storage.kv.get<string[]>("imported-nonces") || []).filter(
(each) => each !== nonce,
),
);
throw error;
}
}
/** THE STORED CELL, HANDED OUT FOR A BACKUP, as the text that `importSealed` takes back. It is
* ciphertext, which only the at-rest key opens, and it goes to a caller that proves that key
* alone: so no ciphertext waits elsewhere for a day when the key leaks. The proof names the
* time it ends, a few minutes ahead at most: a proof passes the mind's Worker and may stay in
* a log, and one that was read there pulls nothing on a later day. It writes nothing here,
* and one fact says that the cell left. */
async #exportSealed(
path: string,
proof: string,
until: number,
): Promise<SecretAnswer<{ sealed: string }>> {
this.#pathOf(path);
const now = Date.now();
// a number alone: text would pass the comparisons below as the number it spells
if (!(typeof until === "number" && until > now && until <= now + EXPORT_PROOF_MS))
throw codedError(
"FORBIDDEN",
`secrets.exportSealed: the proof's time for ${path} is past, or more than five minutes ahead`,
);
const wanted = await hmacSha256Hex(this.#key(), JSON.stringify([path, "exportSealed", until]));
if (!(await secretsEqual(proof, wanted)))
throw codedError(
"FORBIDDEN",
`secrets.exportSealed: no proof under the at-rest key for ${path}`,
);
const current = this.#current();
if (!current)
throw codedError("SECRET_NOT_SET", `secrets.exportSealed: ${path} holds no material`);
console.info({ event: "secret.exported", path, version: current.cell.version });
return {
result: { sealed: JSON.stringify(current) },
facts: [
{ path, events: [{ type: "events.iterate.com/secret/exported", payload: { path } }] },
],
};
}
async #write(
path: string,
record: SecretRecord | ProxiedSecretRecord,
{
public: publicFields,
imported,
by,
empty,
refusedProxy,
}: {
public?: Record<string, string>;
imported?: true;
by?: PageCaller;
empty?: true;
refusedProxy?: () => void;
},
): Promise<SecretFact> {
// A SECRET MADE TO SIGN HOLDS A P-256 KEY AND NOTHING ELSE, and says its public half, however
// it was made: a key generated here, collected from a person, set or imported.
if ("material" in record && record.operations.includes("signStatement")) {
if (record.refresh)
throw codedError("INVALID_INPUT", `secrets: ${path} is made to sign, so it has no refresh`);
const { publicKey, keyId, material } = await signingKeyOf(record.material).catch(
(error: Error) => {
throw codedError("INVALID_INPUT", `secrets: ${path}: ${error.message}`);
},
);
// Kept in the material too, so an exported cell's public fields are its material's. These
// two are its only public fields: a caller's `public` could name `privateKey`.
record = { ...record, material };
publicFields = { publicKey, keyId };
}
// `empty`: no write since the last delete, one still in flight included. Read with the version
// below and no await between, so no write can come in after the check. A proxy is set only
// so; a later value's set takes the higher version, so the owner's own key always wins.
// a rule outlives a delete, and no rule holds a proxy: every use would be refused
if ("proxy" in record && this.#rule()) {
refusedProxy?.();
throw codedError(
"INVALID_INPUT",
`secrets.set with proxy: ${path} has an approval rule, which no use by proxy can pass; clear the rule first`,
);
}
if (
(empty || "proxy" in record) &&
this.#version() > (this.ctx.storage.kv.get<number>("deleted-through") ?? 0)
) {
if ("proxy" in record) refusedProxy?.();
throw codedError(
"INVALID_INPUT",
"proxy" in record
? `secrets.set with proxy: ${path} is set or being set; delete it first`
: `secrets: ${path} holds a value; delete it first`,
);
}
// reserved before the seal awaits, so two writes in flight never share a version
const version = this.#version() + 1;
this.ctx.storage.kv.put("version", version);
// Into the vault's list before the value is stored, so the list never lacks a set secret. It
// is awaited here, before the check below: between that check and the store nothing awaits.
await this.env.APPROVALS.getByName(SET).add(path);
const cell = await this.#sealCell(path, record, version);
// A DELETE THAT CAME IN WHILE THE SEAL AWAITED IS THE LATER WORD. With no value held, this
// write would store its own and undo the delete. It stores nothing and says so, and no fact
// says the secret was set.
if (version <= (this.ctx.storage.kv.get<number>("deleted-through") ?? 0)) {
// this write's early row in the vault's list goes with it
await this.keepListed(path);
throw codedError("SECRET_NOT_SET", deletedWhileStored(path));
}
const current = this.#current();
if (!current || current.cell.version < version)
this.ctx.storage.kv.put<Current>("current", { cell, public: publicFields });
console.info({
event: "secret.set",
path,
version,
urls: record.urls,
imported,
proxy: "proxy" in record ? record.proxy.url : undefined,
});
await this.keepListed(path);
const fact = await this.#setFact(path, record, publicFields, imported, by);
// As above, after the last await: a delete that came in while the list was written is the
// later word. The value is gone, so no fact says that the secret was set.
if (version <= (this.ctx.storage.kv.get<number>("deleted-through") ?? 0))
throw codedError("SECRET_NOT_SET", deletedWhileStored(path));
return fact;
}
/** THE VAULT'S LIST OF SET SECRETS SAYS WHAT THIS OBJECT IS (../secret-list.ts). The object
* writes its own row, and reads itself again after the write has landed: a set and a delete
* that cross each write once, in either order, and whichever lands last is checked by its own
* writer, which writes again when the object changed meanwhile. So the list ends as the
* object is. The vault's `listSecrets` calls it for a path whose row and object disagree. */
async keepListed(path: string): Promise<void> {
this.#pathOf(path);
const list = this.env.APPROVALS.getByName(SET);
for (;;) {
const set = Boolean(this.#current());
await (set ? list.add(path) : list.remove(path));
if (Boolean(this.#current()) === set) return;
}
}
async #setFact(
path: string,
record: Omit<SecretRecord, "material"> | ProxiedSecretRecord,
publicFields: Record<string, string> | undefined,
imported?: true,
by?: PageCaller,
): Promise<SecretFact> {
return {
type: "events.iterate.com/secret/set",
payload: {
path,
urls: record.urls,
refresh: record.refresh?.kind,
refreshSourceSha256:
record.refresh?.kind === "worker" ? await sha256Hex(record.refresh.source) : undefined,
public: publicFields,
imported,
placement: record.placement,
operations: record.operations,
lendTo: record.lendTo,
statementPaths: record.statementPaths,
bodyFields: record.bodyFields,
by: by ? { userId: by.userId, email: by.email } : undefined,
proxy: "proxy" in record ? record.proxy : undefined,
},
};
}
async #sealCell(
path: string,
record: SecretRecord | ProxiedSecretRecord,
version: number,
): Promise<SealedCell> {
const nonce = crypto.randomUUID();
const { urls, refresh, ...rest } = record;
const { clear, use } =
"proxy" in rest
? { clear: rest.proxy, use: { ...useOf(rest), byProxy: true as const } }
: { clear: rest.material, use: useOf(rest) };
const material = await encryptSecretMaterial(
clear,
{ context: path, urls, refresh, version, nonce, use },
this.#key(),
);
return { context: path, urls, refresh, version, nonce, material, ...use };
}
/** A cell of this path, opened with this stack's key: it opens only as what it was sealed as. */
async #recordOf(path: string, cell: SealedCell): Promise<SecretRecord | ProxiedSecretRecord> {
const material = await decryptSecretMaterial(
cell.material,
{
context: path,
urls: cell.urls,
refresh: cell.refresh,
version: cell.version,
nonce: cell.nonce,
use: { ...useOf(cell), byProxy: cell.byProxy },
},
this.#key(),
);
if (cell.byProxy)
return { proxy: secretProxyOf(material), urls: cell.urls, refresh: null, ...useOf(cell) };
return { material, urls: cell.urls, refresh: cell.refresh, ...useOf(cell) };
}
async #open(path: string, current: Current): Promise<SecretRecord> {
if (current.cell.byProxy)
throw new SecretRefused(`secrets: ${path} goes by proxy and holds no value here`);
const record = await this.#opened(path, current);
if ("proxy" in record) throw new SecretRefused(unopened(path));
return record;
}
// one cell that does not open fails no list and no daily refresh
async #proxyDescribed(path: string, current: Current) {
try {
return { proxy: await this.#openProxy(path, current) };
} catch {
return {
byProxy: true as const,
unreadable: `${path} goes by proxy, and its cell does not open under this stack's at-rest key: set it again`,
};
}
}
async #openProxy(path: string, current: Current): Promise<SecretProxy> {
if (this.#proxyOpened?.nonce === current.cell.nonce) return this.#proxyOpened.proxy;
const record = await this.#opened(path, current);
if (!("proxy" in record)) throw new SecretRefused(unopened(path));
this.#proxyOpened = { nonce: current.cell.nonce, proxy: record.proxy };
return record.proxy;
}
async #opened(path: string, current: Current) {
const { cell } = current;
try {
if (cell.context !== path || cell.version > this.#version())
throw new Error("not this secret's current cell");
return await this.#recordOf(path, cell);
} catch {
throw new SecretRefused(unopened(path));
}
}
// `verifyHmac` is the public webhook door's: anyone can make it answer false, so it is not counted
// and only a true answer leaves a fact; else a stranger could shut the secret and fill its log.
async #operate<T>(
path: string,
verb: SecretOperation,
run: (material: SecretMaterial) => Promise<T>,
): Promise<SecretAnswer<T> | SecretRefusal> {
this.#pathOf(path);
const current = this.#current();
if (!current) throw codedError("SECRET_NOT_SET", `${verb}: ${path} holds no material`);
const record = await this.#open(path, current);
const now = Date.now();
if (now - this.#minute.start >= 60_000)
this.#minute = { start: now, operations: 0, falseVerifications: 0 };
const counted = verb !== "verifyHmac";
// A SECRET THAT WAITS FOR APPROVAL SIGNS NOTHING ON THE SIDE. A rule holds the requests that
// leave with the secret. A signature or a shared secret made from it here would leave in a
// request of the mind's own, which the rule never sees. To check a signature, and to read a
// public key, sends nothing as the secret.
const signs =
verb === "signHmac" ||
verb === "signWithKey" ||
verb === "deriveSharedSecret" ||
verb === "signStatement" ||
verb === "seal";
const refusal = !record.operations.includes(verb)
? {
code: "FORBIDDEN" as const,
message: operationRefusal(verb, path, record.operations),
}
: signs && this.#rule()
? {
code: "FORBIDDEN" as const,
message: `${verb}: ${path} has an approval rule, so it signs nothing here: what it signed would leave in a request that the rule does not see. Keep a key that signs in a secret with no rule.`,
}
: (counted && this.#minute.operations >= OPERATIONS_PER_MINUTE) ||
(verb === "verifyEquals" &&
this.#minute.falseVerifications >= FALSE_VERIFICATIONS_PER_MINUTE)
? {
code: "FORBIDDEN" as const,
message: `${verb}: ${path} has answered its limit for this minute (${OPERATIONS_PER_MINUTE} operations, ${FALSE_VERIFICATIONS_PER_MINUTE} wrong guesses of its value); try again after ${new Date(this.#minute.start + 60_000).toISOString()}.`,
}
: null;
const factsOf = (outcome: "answered" | "refused") => [
{
path,
events: [
{
type: "events.iterate.com/secret/operated",
payload: { verb, outcome },
} satisfies SecretFact,
],
},
];
if (refusal) {
console.info({ event: "secret.operated", path, verb, outcome: "refused" });
return { refused: refusal, facts: counted ? factsOf("refused") : [] };
}
if (counted) this.#minute.operations += 1;
const result = await run(record.material);
if (result === false && verb === "verifyEquals") this.#minute.falseVerifications += 1;
console.info({ event: "secret.operated", path, verb, outcome: "answered" });
return { result, facts: counted || result !== false ? factsOf("answered") : [] };
}
#verifyHmac(path: string, input: unknown) {
const parsed = SecretHmacVerificationInput.parse(input);
return this.#operate(path, "verifyHmac", async (material) =>
verifySecretHmac(await hmacKeyOf(material, parsed), parsed),
);
}
#signHmac(path: string, input: unknown) {
const parsed = SecretHmacSigningInput.parse(input);
return this.#operate(path, "signHmac", async (material) => {
const signature = await signSecretHmac(await hmacKeyOf(material, parsed), parsed);
if (!signature)
throw codedError("SECRET_NOT_SET", "signHmac: this secret holds no key at that field");
return signature;
});
}
#verifyEquals(path: string, input: unknown) {
const parsed = z
.object({ value: z.string(), field: z.string().min(1).optional() })
.parse(input);
return this.#operate(path, "verifyEquals", (material) => verifySecretEquals(material, parsed));
}
#deriveSharedSecret(path: string, input: unknown) {
const { field, peerPublicHex } = SecretKeyAgreement.parse(input);
return this.#operate(path, "deriveSharedSecret", async (material) => {
const shared = await x25519SharedSecret(
this.#privateAt(material, field),
Buffer.from(peerPublicHex, "hex"),
);
return Buffer.from(shared).toString("hex");
});
}
#publicKeyOf(path: string, input: unknown) {
const { field } = SecretKeyField.parse(input);
return this.#operate(path, "publicKeyOf", async (material) =>
Buffer.from(await x25519PublicKey(this.#privateAt(material, field))).toString("hex"),
);
}
#signWithKey(path: string, input: unknown) {
const { field, messageHex } = SecretKeySignature.parse(input);
return this.#operate(path, "signWithKey", async (material) =>
Buffer.from(
x25519Sign(this.#privateAt(material, field), Buffer.from(messageHex, "hex")),
).toString("hex"),
);
}
#signStatement(path: string, input: unknown) {
const statement = SecretStatementInput.parse(input);
const paths = this.#current()?.cell.statementPaths;
if (paths && !underStatementPaths(statement.path, paths))
throw codedError(
"FORBIDDEN",
`signStatement: ${path} signs statements under ${paths.join(", ")} alone, not ${statement.path}`,
);
return this.#operate(path, "signStatement", (material) => signedStatement(material, statement));
}
#seal(path: string, input: unknown) {
const parsed = SecretSealInput.parse(input);
return this.#operate(path, "seal", (material) => sealedText(material, parsed));
}
#unseal(path: string, input: unknown) {
const parsed = SecretUnsealInput.parse(input);
return this.#operate(path, "unseal", (material) => unsealedText(material, parsed));
}
async #generateSigningKey(
path: string,
input: unknown,
): Promise<SecretAnswer<{ path: string; publicKey: string; keyId: string }>> {
this.#pathOf(path);
const options = isRecord(input) ? input : {};
const material = await generatedSigningMaterial();
const { publicKey, keyId } = await signingKeyOf(material);
const record = normalizeSecretRecord(material, {
// never sent anywhere (`fetch` refuses a signing secret), and pinned all the same
urls: [controlPlaneOriginOf(configOf(this.env))],
operations: ["signStatement"],
statementPaths: options.statementPaths,
});
// a key that other minds trust is never replaced by a slip: delete it first, on purpose
const fact = await this.#write(path, record, { empty: true });
// a write that began while this one was sealed is the later word, and its value is what is kept
if (this.#current()?.public?.keyId !== keyId)
throw codedError(
"SECRET_NOT_SET",
`generateSigningKey: a later write to ${path} replaced the key before it was kept`,
);
return { result: { path, publicKey, keyId }, facts: [{ path, events: [fact] }] };
}
#privateAt(material: SecretMaterial, field: string): Uint8Array {
let node: unknown = material;
for (const part of field.split(".")) node = isRecord(node) ? node[part] : undefined;
if (typeof node !== "string")
throw codedError("INVALID_INPUT", `key-ops: material has no hex key at "${field}"`);
return Buffer.from(node, "hex");
}
// Only this Worker calls it. The pin binds this use as it binds every other.
async clientSecretFor(
path: string,
input: { borrower: string; origin: string; field?: string },
): Promise<SecretAnswer<string> | SecretRefusal> {
this.#pathOf(path);
const { borrower, origin } = input;
const current = this.#current();
if (!current)
throw codedError(
"INVALID_INPUT",
`secrets: ${path} holds no secret — collect the client secret there first (itx.secrets.collectFromUser)`,
);
const { lendTo } = useOf(current.cell);
const refreshOnly = refreshOnlyFieldRefusal(current.cell.refresh, path, input.field);
const refusal = current.cell.byProxy
? `secrets: ${path} goes by proxy and holds no value, so it is lent to no other secret`
: refreshOnly
? `secrets: ${refreshOnly}`
: this.#rule()
? // a secret that waits for approval is spent with one alone
`secrets: ${path} has an approval rule, so it is lent to no other secret's token request`
: current.cell.operations?.includes("signStatement")
? `secrets: ${path} is a signing key, so it is lent to no other secret`
: lendTo && !lendTo.includes(borrower)
? lendRefusal(path, borrower, lendTo)
: !originPinned(origin, current.cell.urls)
? `secrets: the secret ${path} is pinned to ${current.cell.urls.join(", ")}, not ${origin} — so it is never sent there as a client secret`
: null;
const outcome = refusal ? "refused" : "answered";
console.info({ event: "secret.operated", path, verb: "clientSecretFor", borrower, outcome });
if (refusal)
return {
refused: { code: "FORBIDDEN", message: refusal },
facts: [
{
path,
events: [
{
type: "events.iterate.com/secret/operated",
payload: { verb: "clientSecretFor", outcome, borrower },
} satisfies SecretFact,
],
},
],
};
let record: SecretRecord;
try {
record = await this.#open(path, current);
} catch (error) {
if (!(error instanceof SecretRefused)) throw error;
throw codedError("INVALID_INPUT", error.message.replace(/^itx\.fetch: /, "secrets: "));
}
const value = secretMaterialStringOf(record.material, input.field);
if (value) return { result: value, facts: [] };
throw codedError(
"INVALID_INPUT",
input.field
? `secrets: ${path} has no string at field "${input.field}"`
: typeof record.material === "string"
? `secrets: ${path} is empty`
: `secrets: ${path} is a JSON object: name its field, getSecret("${path}", { field: "…" })`,
);
}
/** A fetch toward the broker of the secret's own Cloud: `client` is what the secret (or the
* attempt that makes it) keeps, never what a call names. A request for any other origin
* throws before anything is signed or sent (../cloud-client.ts `brokered`). */
#brokered(client: CloudClientName, asks: "token" | "installation-token" | "ask") {
return brokered(
(input) => this.env.AUTH.sign(input),
client.origin,
client.cloud,
`events.iterate.com/integration/${asks}-requested`,
);
}
#clientSecretOf(path: string) {
return async (clientSecret: string, tokenEndpoint: string): Promise<string> => {
const reference = clientSecretReferenceOf(clientSecret);
if (!reference) return clientSecret;
if (reference.path === path)
throw codedError(
"INVALID_INPUT",
`secrets: the client secret getSecret("${reference.path}") names ${path} itself — collect the client secret into a secret of its own`,
);
// RPC's stub types cannot say which answer the object gives
const answer = (await this.env.SECRET.getByName(reference.path).clientSecretFor(
reference.path,
{ borrower: path, origin: new URL(tokenEndpoint).origin, field: reference.field },
)) as SecretAnswer<string> | SecretRefusal;
if ("refused" in answer) throw new LendRefused(answer.refused.message, answer.facts);
return answer.result;
};
}
async #beginOAuth(
path: string,
options: unknown,
): Promise<SecretAnswer<{ authorizationUrl: string; nonce: string }> | SecretRefusal> {
this.#pathOf(path);
const config = configOf(this.env);
const controlPlaneOrigin = controlPlaneOriginOf(config);
// `next` may be on the control-plane hostname, or on any hostname of any zone of the mind
const next = isRecord(options) && typeof options.next === "string" ? options.next : null;
const nextOrigin = next && URL.canParse(next) ? new URL(next) : null;
const mindOrigin =
nextOrigin?.protocol === "https:" && zoneOf(config, nextOrigin.hostname)
? nextOrigin.origin
: null;
// a client of an iterate Cloud's: the caller names the Cloud, by its origin
const client = isRecord(options) && isRecord(options.client) ? options.client : null;
const origin =
typeof client?.cloud === "string"
? cloudOriginOf(client.origin, "secrets.beginOAuth: client.origin")
: null;
const cloud = origin
? { ...(await cloudClientOf(origin, String(client?.cloud))), origin }
: null;
// the stack makes no OAuth client at Cloudflare: a config that names it is told the way
if (client && "stack" in client)
throw codedError(
"INVALID_INPUT",
"secrets.beginOAuth: the stack has no OAuth client at Cloudflare any more. A mind connects through Cloudflare's MCP server, as its own client: the default template's cloudflare.ts `cloudflareConnect`.",
);
const normalized = normalizeSecretOAuth(
options,
[controlPlaneOrigin, ...(mindOrigin ? [mindOrigin] : [])],
cloud,
);
const nonce = crypto.randomUUID();
// a placeholder that cannot resolve is refused now, before a human is sent to consent
try {
await this.#clientSecretOf(path)(normalized.clientSecret, normalized.tokenEndpoint);
} catch (error) {
if (!(error instanceof LendRefused)) throw error;
return { refused: { code: "FORBIDDEN", message: error.message }, facts: error.facts };
}
const { pending, authorizationUrl } = await beginSecretOAuth(normalized, {
// a Cloud client's one redirect URI is the Cloud's callback, which sends the code on here
redirectUri: cloud?.redirectUri || `${controlPlaneOrigin}${SECRET_OAUTH_CALLBACK_PATH}`,
state: stateOf({ path, nonce }),
nonce,
});
this.ctx.storage.kv.put<PendingAttempt>("pending", {
...pending,
basedOn: this.#version(),
authorizationUrl,
});
return {
result: { authorizationUrl: startLinkOf(controlPlaneOrigin, path, nonce), nonce },
facts: [],
};
}
async oauthStartTarget(
path: string,
input: { nonce: string },
): Promise<{
authorizationUrl: string;
urls: string[];
use: SecretUse;
anyoneWithLink: boolean;
straightToProvider?: true;
returnTo?: string;
beginAt?: string;
} | null> {
this.#pathOf(path);
const pending = this.ctx.storage.kv.get<PendingAttempt>("pending");
if (!pending || pending.nonce !== input.nonce || pending.until <= Date.now()) return null;
const { urls, use, anyoneWithLink, straightToProvider, cloud } = pending.options;
const callback = `${controlPlaneOriginOf(configOf(this.env))}${SECRET_OAUTH_CALLBACK_PATH}`;
return {
authorizationUrl: pending.authorizationUrl,
urls,
use,
anyoneWithLink,
straightToProvider,
// the Cloud's page says which accounts' installations the mind asks for
...(cloud && {
returnTo: callback,
beginAt:
cloud.beginEndpoint && cloud.accounts?.length
? `${cloud.beginEndpoint}?accounts=${cloud.accounts.join(",")}`
: cloud.beginEndpoint,
}),
};
}
// Never a token, a cell or the provider's URL.
async #oauthResult(path: string, input: { nonce: string }): Promise<SecretOAuthResult> {
this.#pathOf(path);
const nonce = String(input?.nonce || "");
const pending = this.ctx.storage.kv.get<PendingAttempt>("pending");
if (pending?.nonce === nonce && pending.until > Date.now())
return pending.ended
? { status: pending.ended.status, error: pending.ended.error }
: { status: "pending" };
const completed = this.ctx.storage.kv.get<Completed>("completed");
if (completed?.nonce !== nonce || completed.version !== this.#version())
return { status: "unavailable" };
return {
status: "complete",
scopes: completed.scopes,
account: completed.account,
};
}
/** THE PROVIDER'S CALLBACK CAME WITH `error` (RFC 6749 §4.1.2.1). `access_denied` is the person's
* no: `declined`. Any other error is the provider's or the client's fault: `failed`. The attempt
* keeps it, so `oauthResult` says so at once rather than `pending` until the attempt runs out.
* The attempt stays: a consent on the same link later still completes it. Who may end it so is
* who may complete it. */
async recordOAuthError(
path: string,
input: { nonce: string; error: string; by: PageCaller },
): Promise<{ status: "declined" | "failed"; next: string | null }> {
this.#pathOf(path);
const pending = this.ctx.storage.kv.get<PendingAttempt>("pending");
if (!pending || pending.nonce !== input.nonce || pending.until <= Date.now())
throw new Error("no pending attempt matches this callback — begin again");
if (!pending.options.anyoneWithLink && !input.by?.member)
throw new Error("only a member of the mind, signed in, may answer this attempt");
// the provider's word, from a URL: kept short
const error = String(input.error).slice(0, 200);
const status = error === "access_denied" ? "declined" : "failed";
this.ctx.storage.kv.put<PendingAttempt>("pending", {
...pending,
ended: { status, error, at: Date.now() },
});
return { status, next: pending.options.next };
}
// A stale or foreign callback fails without touching the live attempt.
async completeOAuth(
path: string,
input: { code: string; nonce: string; by: PageCaller; cloudProof?: string },
): Promise<SecretAnswer<{ next: string | null; scopes: string[] }> | SecretRefusal> {
this.#pathOf(path);
const { by } = input;
const pending = this.ctx.storage.kv.get<PendingAttempt>("pending");
if (!pending || pending.nonce !== input.nonce)
throw new Error("no pending attempt matches this callback — begin again");
// the page admitted the browser; this holds whatever called
if (!pending.options.anyoneWithLink && !by?.member)
throw new Error("only a member of the mind, signed in, may complete this attempt");
if (pending.until <= Date.now()) {
this.ctx.storage.kv.delete("pending");
throw new Error("the attempt expired — begin again");
}
if (pending.basedOn !== this.#version()) {
this.ctx.storage.kv.delete("pending");
throw new Error("the secret was changed since this attempt began — begin again");
}
let scopes: string[] = [];
let granted: string | undefined;
const record = await completeSecretOAuth(
pending,
input.code,
async (exchange) => {
const { cloud } = pending.options;
// a Cloud's broker checks its own origin
const allowed = !!cloud || originPinned(exchange.url, pending.options.urls);
if (!allowed)
throw new Error(`the token endpoint ${new URL(exchange.url).origin} is outside the pin`);
const response = await (
cloud ? this.#brokered({ cloud: cloud.name, origin: cloud.origin }, "token") : dispatch
)(exchange);
const answer: unknown = await response
.clone()
.json()
.catch(() => null);
granted = isRecord(answer) && typeof answer.scope === "string" ? answer.scope : undefined;
scopes = granted
? granted.split(/[\s,]+/).filter(Boolean)
: (pending.options.scope || "").split(/\s+/).filter(Boolean);
return response;
},
this.#clientSecretOf(path),
input.cloudProof,
);
// A REFUSAL BEFORE ANYTHING IS KEPT (`#refuseGrant`): the new tokens go nowhere.
const refuse = (words: string) => this.#refuseGrant(path, pending, input.nonce, record, words);
// the ceiling: only scopes of the list, and the answer must name them
const ceiling = grantCeilingOf(pending.options.grantedWithin, [
pending.options.tokenEndpoint,
...pending.options.urls,
]);
const overCeiling = ceiling && grantRefusal(granted, ceiling);
if (overCeiling) return refuse(overCeiling);
let account: { id: string; name: string | null } | undefined;
const lookup = pending.options.account;
if (lookup) {
const identity = await dispatch(
new Request(lookup.url, {
method: lookup.method || "GET",
headers: {
authorization: `Bearer ${secretMaterialStringOf(record.material, "accessToken")}`,
...(lookup.body && {
"content-type": "application/json",
// an MCP server takes a request that accepts both
accept: "application/json, text/event-stream",
}),
},
body: lookup.body,
redirect: "manual",
}),
);
if (!identity.ok) {
await identity.body?.cancel();
if (pending.options.expectAccount)
return refuse(
`The provider did not say which account the grant is for (it answered ${identity.status}). Nothing was kept.`,
);
throw new Error(`the account lookup answered ${identity.status}`);
}
const named: unknown = await identity.json().catch(() => null);
try {
account = accountOf(named, lookup);
} catch (error) {
if (!pending.options.expectAccount) throw error;
return refuse(
`The provider named no one account for the grant. It must be for ${pending.options.expectAccount} alone. Nothing was kept.`,
);
}
}
const expected = pending.options.expectAccount;
if (expected && account?.id !== expected)
return refuse(
`The grant is for ${account ? `"${account.id}"` : "an account that was not named"}, and this connection is for ${expected} alone. Nothing was kept. Connect again, and choose only that account.`,
);
// a write, or a newer attempt, that landed while the provider was answering wins
if (
this.#version() !== pending.basedOn ||
this.ctx.storage.kv.get<PendingAttempt>("pending")?.nonce !== input.nonce
)
throw new Error(
"the secret was changed while the provider was answering — the tokens were discarded; begin again",
);
const fact = await this.#write(path, record, { by });
this.ctx.storage.kv.delete("pending");
this.ctx.storage.kv.put<Completed>("completed", {
nonce: input.nonce,
version: this.#version(),
scopes,
account,
});
return { result: { next: pending.options.next, scopes }, facts: [{ path, events: [fact] }] };
}
/** A GRANT THAT THE VAULT REFUSED: nothing is kept. The attempt ends with the words that the
* person's page shows, unless a newer attempt took its place while the provider answered; the
* refused tokens go back to the provider when the connect named its revocation endpoint
* (RFC 7009); and the `secret/grant-refused` fact tells the mind's code. */
async #refuseGrant(
path: string,
pending: PendingAttempt,
nonce: string,
record: SecretRecord,
words: string,
): Promise<SecretRefusal> {
const now = this.ctx.storage.kv.get<PendingAttempt>("pending");
if (now?.nonce === nonce)
this.ctx.storage.kv.put<PendingAttempt>("pending", {
...now,
ended: { status: "failed", error: words, at: Date.now() },
});
const revoked = await this.#revoke(path, pending.options, record.material);
console.info({ event: "secret.grant-refused", path, revoked });
return {
refused: { code: "FORBIDDEN", message: words },
facts: [
{
path,
events: [
{
type: "events.iterate.com/secret/grant-refused",
payload: { nonce, reason: words, revoked },
},
],
},
],
};
}
/** RFC 7009: the refresh token, then the access token, given back at the connect's revocation
* endpoint. True when the provider took each; false when there is no endpoint or one failed. */
async #revoke(
path: string,
options: PendingSecretOAuth["options"],
material: SecretMaterial,
): Promise<boolean> {
const endpoint = options.revocationEndpoint;
// the refused tokens go back to the provider that issued them, and to no other host
if (
!endpoint ||
options.cloud ||
new URL(endpoint).origin !== new URL(options.tokenEndpoint).origin
)
return false;
try {
const clientSecret = await this.#clientSecretOf(path)(options.clientSecret, endpoint);
let revoked = true;
for (const hint of ["refreshToken", "accessToken"] as const) {
const token = secretMaterialStringOf(material, hint);
if (!token) continue;
const response = await dispatch(
oauthTokenRequest({
tokenEndpoint: endpoint,
clientId: options.clientId,
clientSecret,
clientAuth: options.clientAuth,
params: {
token,
token_type_hint: hint === "refreshToken" ? "refresh_token" : "access_token",
},
}),
);
await response.body?.cancel();
revoked &&= response.ok;
}
return revoked;
} catch (error) {
console.info({ event: "secret.revoke-failed", path, error: String(error) });
return false;
}
}
async #collectFromUser(path: string, input: unknown): Promise<{ path: string; url: string }> {
this.#pathOf(path);
const collected = CollectInput.parse(input);
const urls = originsOf(collected.egress.urls);
if (urls.length === 0)
throw new Error("itx.secrets.collectFromUser: egress.urls must name at least one URL");
const unsafeUrl = collected.egress.urls.find((value) => {
const url = new URL(value);
return !["http:", "https:"].includes(url.protocol) || Boolean(url.username || url.password);
});
if (unsafeUrl)
throw new Error(
`itx.secrets.collectFromUser: egress URL ${JSON.stringify(unsafeUrl)} must be an http(s) URL without credentials`,
);
const nonce = crypto.randomUUID();
this.ctx.storage.kv.put<Collecting>("collecting", {
nonce,
urls,
use: secretUseOf(isRecord(input) ? input : undefined, urls),
anyoneWithLink: collected.anyoneWithLink === true,
description: collected.description,
fields: collected.fields,
...(collected.redirectUrl && { redirectUrl: new URL(collected.redirectUrl).href }),
until: Date.now() + COLLECT_TTL_MS,
});
const url = new URL("/secrets/collect", controlPlaneOriginOf(configOf(this.env)));
url.searchParams.set("path", path);
url.searchParams.set("nonce", nonce);
return { path, url: url.href };
}
async collectionForm(path: string, input: { nonce: string }): Promise<CollectionForm | null> {
this.#pathOf(path);
const collecting = this.#collecting(input.nonce);
if (!collecting) return null;
const { urls, description, fields, redirectUrl, use, anyoneWithLink } = collecting;
return {
urls,
description,
fields,
use,
anyoneWithLink,
...(redirectUrl && { redirectOrigin: new URL(redirectUrl).origin }),
};
}
async collect(
path: string,
input: { nonce: string; values: Record<string, string>; by: PageCaller },
): Promise<SecretAnswer<{ redirectUrl: string | null }>> {
this.#pathOf(path);
const collecting = this.#collecting(input.nonce);
if (!collecting)
throw new Error("this link was used already, or it lapsed — ask for a new one");
// the page admitted the browser; this holds whatever called
if (!collecting.anyoneWithLink && !input.by?.member)
throw new Error("only a member of the mind, signed in, may fill this form");
let material: SecretMaterial;
let publicNames: string[] = [];
if (!collecting.fields) {
const value = input.values.value || "";
if (!value) throw new Error("enter the value");
material = value;
} else {
const object: Record<string, string> = {};
for (const field of collecting.fields) {
const value = input.values[field.name] ?? "";
if (!value) throw new Error(`enter ${field.label}`);
if (field.pattern && !new RegExp(`^(?:${field.pattern})$`, "v").test(value))
throw new Error(`${field.label} is not in the form it asks for`);
object[field.name] = value;
}
material = object;
publicNames = collecting.fields.filter((field) => field.public).map((field) => field.name);
}
const fact = await this.#write(
path,
{ material, urls: collecting.urls, refresh: null, ...collecting.use },
{
public: publicNames.length ? publicFieldsOf(material, publicNames) : undefined,
by: input.by,
},
);
this.ctx.storage.kv.delete("collecting");
return {
result: { redirectUrl: collecting.redirectUrl || null },
facts: [{ path, events: [fact] }],
};
}
#collecting(nonce: string): Collecting | null {
const collecting = this.ctx.storage.kv.get<Collecting>("collecting");
return collecting?.nonce === nonce && collecting.until > Date.now() ? collecting : null;
}
#rule(): ApprovalRule | null {
return this.ctx.storage.kv.get<ApprovalRule>("approval-rule") ?? null;
}
#setRule(path: string, rule: ApprovalRule | null) {
if (rule) this.ctx.storage.kv.put("approval-rule", rule);
else this.ctx.storage.kv.delete("approval-rule");
console.info({ event: "secret.approval-rule-configured", path, rule });
}
/** THE REQUESTS THAT ARE HELD HERE, as every reader gets them: the page, the count of those
* that wait, a member's approval and a wait call. A row ends with its day. It ends also once
* its request can leave no more (../body-placement.ts `leavesAsRead`, the rule of `fetch`):
* the secret was given again since the request was held to fill its body, or the secret names
* a body field now for a request that was held to fill none. Such a row is deleted here, as
* one whose day ended is: the same request, sent again, is a new one. */
#heldRows(): HeldRow[] {
const now = Date.now();
const cell = this.#current()?.cell;
const canLeave = (text: string) => {
const held = heldOfText(text);
return leavesAsRead(held.fills, cell?.version, fieldsToFill(cell?.bodyFields, held));
};
const rows: HeldRow[] = [];
// its day first: the text of a row whose day ended is not read
for (const [key, row] of this.ctx.storage.kv.list<HeldRow>({ prefix: "held:" }))
if (row.until > now && canLeave(row.text)) rows.push(row);
else this.ctx.storage.kv.delete(key);
return rows;
}
/** A PERSON'S GRANT THAT THE ITERATE CLOUD BROKERS, REFRESHED ON WORD. A refresh runs otherwise
* only after the provider refused the token (the retry in `#fetchWithSecret`);
* ../grant-refresh.ts says why such a grant needs one on word. The answer says what came of it:
* - `refreshed`: the provider gave a new token, and the secret holds it;
* - `failed`: the refresh did not end well, with the error's words. The secret is as it was:
* a refused refresh deletes nothing, and the Cloud ends what the person connected;
* - `failed` with `answerLost`: no decision of the provider came (../secrets.ts
* `RefreshUnanswered`). One more try is made at once with the same token, unless the
* answer was 429 or 504: the Cloud may hold the answer that was lost on its way here;
* - `fresh`: a refresh on word brought new tokens within the last hour. A provider's refresh
* token works once, and each refresh risks an answer that is lost: one is not spent for
* nothing;
* - `not-a-brokered-grant`: nothing is set here, or what is set is no such grant, or it holds
* no refresh token, so nothing is there to refresh.
* A refresh that runs already for a use is joined, never doubled. */
async refreshGrant(path: string): Promise<SecretAnswer<GrantRefresh>> {
this.#pathOf(path);
const current = this.#current();
const refresh = current?.cell.refresh;
if (
!current ||
refresh?.kind !== "oauth-refresh-token" ||
!refresh.client ||
!("cloud" in refresh.client)
)
return { result: { outcome: "not-a-brokered-grant" }, facts: [] };
const last = this.ctx.storage.kv.get<number>(GRANT_REFRESHED_AT) ?? 0;
if (Date.now() - last < GRANT_FRESH_MS) return { result: { outcome: "fresh" }, facts: [] };
// A provider that gave no refresh token gave a token that does not end (a Slack workspace
// that does not rotate its tokens). A failure each day would say that a good grant is broken.
const { material } = await this.#open(path, current);
if (!isRecord(material) || typeof material.refreshToken !== "string")
return { result: { outcome: "not-a-brokered-grant" }, facts: [] };
// the facts of a try whose answer was lost, kept for the answer of the try after it
const lost: SecretFact[] = [];
for (let tries = 1; ; tries += 1)
try {
const events = [...lost, ...(await this.#refresh(path, current.cell.nonce))];
this.ctx.storage.kv.put(GRANT_REFRESHED_AT, Date.now());
return { result: { outcome: "refreshed" }, facts: events.length ? [{ path, events }] : [] };
} catch (error) {
if (!(error instanceof RefreshFailed)) throw error;
if (error.askAgain && tries < GRANT_TRIES) {
lost.push(...error.facts);
continue;
}
return {
result: {
outcome: "failed",
error: error.message,
...(error.answerLost && { answerLost: true }),
},
facts: [{ path, events: [...lost, ...error.facts] }, ...error.lent],
};
}
}
/** `bodyFields`: the secret's own (iterate/api `SecretUses`). With a rule, a request that fills
* one waits whatever the rule names (`fetch`), which the approvals page says in words. */
async approvals(
path: string,
): Promise<{ rule: ApprovalRule | null; held: HeldRow[]; bodyFields?: SecretBodyField[] }> {
this.#pathOf(path);
const held = this.#heldRows().filter(
(row) => row.state === "waiting" || row.state === "approved",
);
return { rule: this.#rule(), held, bodyFields: this.#current()?.cell.bodyFields };
}
// A loosening needs a signed statement.
async tightenRule(
path: string,
input: { rule: unknown; by: { userId: string; email: string } },
): Promise<SecretAnswer<{ rule: ApprovalRule }>> {
this.#pathOf(path);
const parsed = ApprovalRule.safeParse(input.rule);
if (!parsed.success)
throw codedError("INVALID_INPUT", parsed.error.issues.map((i) => i.message).join("; "));
const rule = parsed.data;
if (this.#current()?.cell.byProxy) throw codedError("INVALID_INPUT", proxyHoldsNoRule(path));
if (!isTightening(this.#rule(), rule))
throw codedError(
"FORBIDDEN",
`the rule of ${path} would hold fewer requests than the rule it has now. That takes your key, whether the rules for mail and calls are locked or not: go back, and press "Save with passkey" in the block of ${path}`,
);
this.#setRule(path, rule);
await this.env.APPROVALS.getByName(INDEX).add(path);
const by = { userId: input.by.userId, email: input.by.email };
return {
result: { rule },
facts: [{ path, events: [{ type: RULE_CONFIGURED, payload: { rule, by } }] }],
};
}
// A statement the auth Worker refused is a log line alone, since anyone can send one.
async decide(path: string, envelope: Envelope): Promise<SecretAnswer<{ error?: string }>> {
this.#pathOf(path);
const verified = await this.env.AUTH.verify(envelope);
if ("refused" in verified) {
console.info({ event: "secret.statement-refused", path, reason: verified.refused });
return { result: { error: verified.refused }, facts: [] };
}
const { statement } = verified;
const signer: Signer = {
principal: verified.signer.principal,
key: verified.signer.key,
label: verified.signer.label,
userVerified: verified.signer.userVerified,
};
const answer = (fact: SecretFact, error?: string) => {
console.info({ event: fact.type.replace("events.iterate.com/secret/", "secret."), path });
return { result: error ? { error } : {}, facts: [{ path, events: [fact] }] };
};
const refuse = (reason: string) =>
answer(
{
type: "events.iterate.com/secret/statement-refused",
payload: { statementType: statement.type, reason, signer },
},
reason,
);
if (statement.path !== path) return refuse(`the statement is about ${statement.path}`);
if (!verified.signer.member)
return refuse("only a member's key approves, rejects or changes a rule");
// as a sign-in asks: a passkey that checked nobody is anyone's who holds the device
if (verified.signer.userVerified === false)
return refuse("the passkey did not check that it is you, by a PIN, a fingerprint or a face");
if (statement.type === RULE_CONFIGURED) {
const given = statement.payload?.rule;
const parsed = given === null ? null : ApprovalRule.safeParse(given);
if (parsed && !parsed.success)
return refuse(parsed.error.issues.map((issue) => issue.message).join("; "));
const rule = parsed?.data || null;
if (rule && this.#current()?.cell.byProxy) return refuse(proxyHoldsNoRule(path));
this.#setRule(path, rule);
if (rule) await this.env.APPROVALS.getByName(INDEX).add(path);
return answer({ type: RULE_CONFIGURED, payload: { rule, signer, signed: envelope } });
}
if (statement.type !== APPROVED && statement.type !== REJECTED)
return refuse(`the vault does not act on ${statement.type}`);
const digests = statement.about || [];
if (!digests.length) return refuse("the statement names no request");
const live = this.#heldRows();
const rows = digests.map((digest) => live.find((row) => row.digest === digest));
if (rows.some((row) => row?.state !== "waiting"))
return refuse("a request it names does not wait here");
const reason = statement.payload?.reason;
if (statement.type === REJECTED && (typeof reason !== "string" || !reason.trim()))
return refuse("a rejection names its reason");
for (const row of rows as HeldRow[])
this.ctx.storage.kv.put<HeldRow>(`held:${row.digest}`, {
...row,
state: statement.type === APPROVED ? "approved" : "rejected",
signer,
...(statement.type === REJECTED && { reason: String(reason) }),
});
for (const digest of digests) this.#wake(digest);
return answer(
statement.type === APPROVED
? { type: APPROVED, payload: { digests, signer, signed: envelope } }
: {
type: REJECTED,
payload: { digests, signer, signed: envelope, reason: String(reason) },
},
);
}
// A call with new bytes is answered `held` at once, so the mind's log hears of it. The wait call
// names a digest; nothing else it carries is read. `fills`: what the secret fills in the
// request's body now. The answer's is what the request was held with.
async #hold(
path: string,
request: Request,
waitFor: string | null,
facts: SecretFact[],
fills: HeldFills | undefined,
): Promise<{ request: Request; digest: string; release: () => void; fills?: HeldFills }> {
const find = (digest: string) => this.#heldRows().find((held) => held.digest === digest);
const ended = (digest: string | null) =>
new HeldAnswer(
404,
"refused",
`itx.fetch: no request ${digest} waits with ${path}. A held request ends after a day, and one whose body the secret fills ends when the secret is given again. Send the request again.`,
);
let row = waitFor ? find(waitFor) : await this.#rowOf(path, request, facts, fills);
if (!row) throw ended(waitFor);
const { digest, text } = row;
const key = `held:${digest}`;
if (row.state === "waiting") {
await this.#settled(digest, HOLD_MS);
// as every reader gets it (`#heldRows`): a row that ended while this call waited is no row
row = find(digest);
if (!row) throw ended(digest);
}
if (row.state === "sent") throw sentBefore(digest);
if (row.state === "rejected")
throw new HeldAnswer(
403,
"rejected",
`itx.fetch: ${row.signer?.principal.email || row.signer?.principal.actor || "a member"} rejected this request (${digest}): ${row.reason}`,
);
if (row.state === "waiting") {
console.info({ event: "secret.hold-expired", path, digest });
facts.push({ type: "events.iterate.com/secret/hold-expired", payload: { digest } });
throw new HeldAnswer(
403,
"waiting",
`itx.fetch: this request with ${path} waits for a member's approval (${digest}), at /secrets/approvals?digest=${digest} on the platform's origin. Once it is approved, send the same request again: it goes then.`,
);
}
const release = () => {
// no await from here to the dispatch: the mark and the send are one step
const now = this.ctx.storage.kv.get<HeldRow>(key);
if (now?.state !== "approved") throw sentBefore(digest);
this.ctx.storage.kv.put<HeldRow>(key, { ...now, state: "sent" });
};
return { request: requestOfText(text), digest, release, fills: fillsOfText(text) };
}
async #rowOf(
path: string,
request: Request,
facts: SecretFact[],
fills: HeldFills | undefined,
): Promise<HeldRow> {
if (Number(request.headers.get("content-length") || 0) > HELD_BODY_LIMIT)
throw new HeldAnswer(413, "refused", tooLarge(path));
const bytes = new Uint8Array(await request.arrayBuffer());
if (bytes.length > HELD_BODY_LIMIT) throw new HeldAnswer(413, "refused", tooLarge(path));
let body: string;
try {
body = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
} catch {
throw new HeldAnswer(
415,
"refused",
`itx.fetch: a request with ${path} waits for approval, and its body must be text (UTF-8)`,
);
}
const text = requestText({
method: request.method,
url: request.url,
headers: request.headers,
body,
fills,
});
const digest = await sha256Hex(text);
const rows = this.#heldRows();
const kept = rows.find((held) => held.digest === digest);
if (kept) return kept;
// each row keeps a body of up to `HELD_BODY_LIMIT` for a day
if (rows.filter((held) => held.state === "waiting").length >= MAX_WAITING)
throw new HeldAnswer(
429,
"refused",
`itx.fetch: ${MAX_WAITING} requests with ${path} wait for approval already; approve or reject some first`,
);
const now = Date.now();
const from = claimedOrigin(request.headers.get(ITERATE_CAUSE_HEADER));
this.ctx.storage.kv.put<HeldRow>(`held:${digest}`, {
digest,
text,
at: now,
until: now + HELD_ROW_MS,
from,
state: "waiting",
});
const shown = { method: request.method, url: request.url, bodyStart: body.slice(0, 500) };
console.info({
event: "secret.approval-requested",
path,
digest,
method: shown.method,
url: shown.url,
from,
});
facts.push({
type: "events.iterate.com/secret/approval-requested",
payload: { digest, ...shown, from },
});
await this.env.APPROVALS.getByName(INDEX).add(path);
throw new HeldAnswer(202, "held", JSON.stringify({ digest }));
}
#settled(digest: string, ms: number): Promise<void> {
return new Promise((settle) => {
const waiters = this.#waiting.get(digest) ?? new Set();
this.#waiting.set(digest, waiters);
const done = () => {
clearTimeout(timer);
waiters.delete(done);
settle();
};
const timer = setTimeout(done, ms);
waiters.add(done);
});
}
#wake(digest: string) {
for (const done of [...(this.#waiting.get(digest) ?? [])]) done();
this.#waiting.delete(digest);
}
// Every refusal goes to the caller, never to the destination: a 403 when the vault's own rules
// refuse the request as it is written (the pin, the placement, a secret that is not set), and a
// 502 when a refresh the request needed failed at its provider. A 403 is the caller's to fix, so
// it is no error line in the Workers' logs; a failed refresh is one. A substituted value is cut
// out of the answer's headers; the body goes back as the host sent it.
override async fetch(incoming: Request): Promise<Response> {
const path = this.#pathOf(incoming.headers.get(SECRET_USE_HEADER) ?? "");
const headers = new Headers(incoming.headers);
headers.delete(SECRET_USE_HEADER);
const waitFor = headers.get(SECRET_HELD_HEADER);
headers.delete(SECRET_HELD_HEADER);
let request = new Request(incoming, { headers });
const facts: SecretFact[] = [];
const lent: Facts = [];
const substituted = new Set<string>();
// whether this request's body holds the value, and whether the request was sent
let bodyFilled = false;
let dispatched = false;
const answered = (response: Response, own?: VaultAnswerKind) =>
answerOf(response, [{ path, events: facts }, ...lent], substituted, own);
// An ask of the Cloud uses no value of the secret: nothing is substituted or pinned. It is
// still a request that the secret's connection makes, so the secret's rule holds it as any
// other. Asked of the request that is sent: a wait call sends the stored text.
const stack = this.#stackSecret(path);
const askFor = (sent: Request) => {
// a secret that the stack made has no connection: whatever this object stored is not read
const connected = stack ? undefined : this.#current()?.cell.refresh;
return askOf(
sent,
connected?.kind === "oauth-refresh-token" && connected.client && "cloud" in connected.client
? connected.client
: undefined,
);
};
let held: { digest: string; release?: () => void; fills?: HeldFills } | undefined;
// read below, and only for a rule that names strings of the body
let body: string | null = null;
const upgrades = upgradesOf(request);
let proxied: string | undefined;
// cancelled on a refusal: workerd reports a body a Durable Object left unread
let unread: Request | undefined;
// the request's copy for its one retry after a refresh: it holds the other half of the body
let retry: Request | null = null;
// The pin is checked at every read: after a refresh it may have moved.
// `version`: of the stored cell. A refresh keeps it; a value that is given again has a new one.
const read = async (): Promise<
| { nonce: string; version: number; record: SecretRecord }
| { nonce: string; proxy: SecretProxy; proxied: string }
| null
> => {
proxied = undefined;
// a secret that the stack made is the config's, and one that it did not make is not set
const current = stack ? undefined : this.#current();
const held = stack
? "record" in stack && {
nonce: STACK_NONCE,
urls: stack.record.urls,
open: async () => stack.record,
}
: current && {
nonce: current.cell.nonce,
urls: current.cell.urls,
open: () => this.#open(path, current),
};
if (!held) return null;
if (!originPinned(request.url, held.urls)) throw pinRefusal(path, request.url, held.urls);
// only a stored cell goes by proxy: a secret that the stack made never does
if (current?.cell.byProxy) {
if (secretPathsIn(request.url).length)
throw new SecretRefused(
`itx.fetch: ${path} goes by proxy, so its placeholder sits in a header alone: in the URL it would go to the proxy`,
);
const target = new URL(request.url);
if (target.hostname.split(".").some((label) => label === "" || label === ".."))
throw new SecretRefused(
`itx.fetch: ${path} goes by proxy, and ${target.hostname} is no host to send it to`,
);
// `proxiedUrlOf` cuts the href at the origin's length, and the origin leaves out userinfo
if (target.username || target.password)
throw new SecretRefused(
`itx.fetch: ${path} goes by proxy, and a use by proxy takes no credentials in its URL: send them in a header`,
);
const proxy = await this.#openProxy(path, current);
proxied = proxiedUrlOf(proxy, target);
return { nonce: current.cell.nonce, proxy, proxied };
}
const record = await held.open();
if (record.operations.includes("signStatement"))
throw new SecretRefused(
`itx.fetch: the secret ${path} is a signing key: it signs (signStatement) and is never sent`,
);
// a secret placed in headers alone is never spliced into a URL, where a host may log it,
// redirect with it or quote it back
if (record.placement === "header" && secretPathsIn(request.url).length)
throw new SecretRefused(placementRefusal(path));
return { nonce: held.nonce, version: current?.cell.version ?? 0, record };
};
const readValue = async () => {
const stored = await read();
if (stored && "proxy" in stored)
throw new SecretRefused(`itx.fetch: ${path} went by proxy while it was being sent`);
return stored;
};
const usedFact = (status: number): SecretFact => ({
type: "events.iterate.com/secret/used",
payload: {
method: request.method,
url: request.url,
status,
...(held && { digest: held.digest }),
proxy: proxied,
// only of a request that left: a refusal after the fill sent the value nowhere
...(bodyFilled && dispatched && { inBody: true as const }),
},
});
// `nonce`: a use by proxy leaves only for the cell it read
const send = async (outbound: Request, retried: boolean, nonce?: string) => {
// No await from here to the dispatch: a rule set while this request was being prepared (a
// refresh can take seconds) holds it too.
if (nonce && this.#current()?.cell.nonce !== nonce)
throw new HeldAnswer(
409,
"refused",
`itx.fetch: ${path} was set again while this request was being signed for its proxy; send it again`,
);
const ruleNow = this.#rule();
// WITH A RULE, WHAT SAYS WHAT A REQUEST IS LEAVES AS THE RULE READ IT. A rule reads a
// request as it is written, and a placeholder in a header is a field of the secret by now.
// So two things are read once more, of the request that leaves: whether it opens a
// WebSocket, where no rule reads what the socket then carries, and the methods that a
// host may take it for (`methodsOf`). A request that arrives under a rule with a
// placeholder in such a header is refused there, before a member is asked; here the same
// holds for a rule that was set since, and for a request that waited. The rest of what a
// rule reads cannot change: no placeholder moves a host, a path with one is never a free
// path, and a body that is filled always waits.
if (ruleNow && upgradesOf(outbound)) throw new SecretRefused(noWebSocket(path));
if (ruleNow && JSON.stringify(methodsOf(outbound)) !== JSON.stringify(methodsOf(request)))
throw new SecretRefused(noPlaceholder(path, "a header that names another method"));
if (
!held &&
ruleNow &&
(bodyFilled || ruleMatches(ruleNow, methodsOf(request), request.url, body))
)
throw new HeldAnswer(
409,
"refused",
`itx.fetch: ${path} got an approval rule that holds this request while it was being sent; send it again`,
);
held?.release?.();
if (held) held.release = undefined;
// the record no mind can drop, written before the request leaves
console.info({
event: "secret.used",
path,
method: request.method,
url: request.url,
retried,
...(held && { digest: held.digest }),
proxy: proxied,
...(bodyFilled && { inBody: true }),
});
dispatched = true;
const response = await dispatch(outbound, nonce ? "the secret proxy" : undefined);
facts.push(usedFact(response.status));
return response;
};
try {
unread = request;
let ask = askFor(request);
// an ask goes to the Cloud, outside the secret's pinned hosts: no value is read for it
let stored = ask ? null : await read();
// A REQUEST THAT FILLS ONE OF THE SECRET'S BODY FIELDS (../body-placement.ts): few are. Its
// body is read first, within the limit of a held request, and one with no place to fill
// is refused before a member is asked about it. (What the placeholder names is asked when
// the request leaves: another secret's is held first, and refused then.) From here on the
// body is its parse written again, and its content type the one JSON type that it names:
// as it will leave but for the value, so a rule reads, a member approves and the host
// gets one text. The caller sends the body as slowly as it likes, so the secret is read
// again after it: one that was deleted or given again meanwhile sends nothing.
let fills: HeldFills | undefined;
const fillable =
stored && "record" in stored && !waitFor
? fieldsToFill(stored.record.bodyFields, request)
: [];
if (stored && "record" in stored && fillable.length) {
const checked = bodyToFill(request.headers, await textOf(request.body), fillable);
fills = { fields: checked.places.map(({ field }) => field), version: stored.version };
const typed = new Headers(request.headers);
typed.set("content-type", checked.type);
request = new Request(request, { headers: typed, body: JSON.stringify(checked.json) });
unread = request;
stored = await read();
if (!stored || !("record" in stored) || stored.version !== fills.version)
throw new HeldAnswer(409, "refused", givenAgain(path));
}
// A wait call is held whatever the rule says now: what it may send is the stored text alone.
const rule = this.#rule();
if (stored && "proxy" in stored && (rule || waitFor))
throw new SecretRefused(`itx.fetch: ${proxyHoldsNoRule(path)}`);
// As it is written, before a member is asked: a WebSocket, and a placeholder in a header
// that says what the request is. No rule can read what such a header will say, and an
// approval of the request could never send it. `send` reads both again as it leaves.
if (rule && upgrades) throw new SecretRefused(noWebSocket(path));
const filled =
rule &&
["upgrade", ...METHOD_HEADERS].find((name) =>
headerNamesASecret(request.headers.get(name) || ""),
);
if (filled) throw new SecretRefused(noPlaceholder(path, `its ${filled} header`));
// (a request that fills waits whatever its body holds: nothing to read for)
if (
(stored || ask) &&
!waitFor &&
!fills &&
rule?.jsonStrings &&
ruleNames(rule, methodsOf(request), request.url)
)
body = await bodyTextOf(request);
// WITH A RULE, THE VALUE GOES INTO A BODY ONLY IN A REQUEST THAT A MEMBER APPROVED, whatever
// the rule names (`fills`): a host that shows the value back gives it to the mind's code,
// which could then send what the rule holds with no secrets Worker between. The request
// is held with the fields that it fills and the secret's version, and leaves only so
// (below). `send` refuses a filled body that this did not hold: one of a rule that was
// set after this.
if (
(stored || ask) &&
(waitFor || (rule && (fills || ruleMatches(rule, methodsOf(request), request.url, body))))
) {
const approved = await this.#hold(path, request, waitFor, facts, fills);
held = { digest: approved.digest, release: approved.release, fills: approved.fills };
request = approved.request;
unread = request;
ask = askFor(request);
stored = ask ? null : await read();
if (stored && "proxy" in stored)
throw new SecretRefused(`itx.fetch: ${proxyHoldsNoRule(path)}`);
}
if (stored && "proxy" in stored) {
unread = await this.#byProxy(request, stored.proxy, stored.proxied, upgrades);
substituted.add(unread.headers.get(ITERATE_STATEMENT_HEADER) ?? "");
return answered(await send(unread, false, stored.nonce));
}
if (ask) {
// a request that was held to fill a body is no ask: the secret was connected anew since
if (held?.fills) throw new HeldAnswer(409, "refused", givenAgain(path));
// every header that names the secret is taken off: the Cloud gets no placeholder
const bare = new Headers(request.headers);
for (const [name, value] of request.headers)
if (headerNamesASecret(value)) bare.delete(name);
// no await from the mark to the dispatch, as in `send`
held?.release?.();
if (held) held.release = undefined;
console.info({
event: "secret.used",
path,
method: request.method,
url: request.url,
...(held && { digest: held.digest }),
});
const response = await this.#brokered(ask, "ask")(new Request(request, { headers: bare }));
facts.push(usedFact(response.status));
return answered(response);
}
let valued = stored;
// Refused here too, not only at egress: this object is the boundary that holds the bytes.
const resolve = (named: string, field: string | undefined) => {
if (named !== path)
throw new SecretRefused(
`itx.fetch: getSecret(${JSON.stringify(named)}) does not belong to the secret ${path}`,
);
const refreshOnly = valued && refreshOnlyFieldRefusal(valued.record.refresh, path, field);
if (refreshOnly) throw new SecretRefused(`itx.fetch: ${refreshOnly}`);
return valued?.record.material || null;
};
// The cast: workers-types' Request<Cf> against the bare Request the pure half takes.
retry = valued?.record.refresh ? (request.clone() as unknown as Request) : null;
const refreshed = async () => {
try {
facts.push(...(await this.#refresh(path, valued!.nonce)));
return null;
} catch (cause) {
if (cause instanceof RefreshFailed) {
facts.push(...cause.facts);
lent.push(...cause.lent);
}
return cause instanceof Error ? cause : new Error(String(cause));
}
};
// An access token whose material says that it has ended is refreshed before the request
// goes: a provider may answer an ended token with a status that starts no refresh here. A
// refresh that fails is in the facts, and the request goes once with what the secret holds:
// its answer starts no second refresh.
let refreshFailed = false;
if (retry && valued && accessTokenEnded(valued.record.material)) {
refreshFailed = Boolean(await refreshed());
valued = await readValue();
}
// At most two attempts: a mintable miss or a 401 from the pinned host mints, then one retry.
for (let attempt = 0; ; attempt += 1) {
const last = attempt > 0 || !retry || !valued || refreshFailed;
let outbound: Request;
try {
// the body first, by the request's headers as written: a placeholder is no content type
let written = attempt ? retry! : request;
const toFill = fieldsToFill(valued?.record.bodyFields, request);
// A REQUEST LEAVES WITH WHAT ITS FIRST READ FOUND, OR NOT AT ALL
// (../body-placement.ts `leavesAsRead`): some fields to fill, from one version of the
// secret, or none. For a request that waited, that is what a member approved. A held
// request that can leave no more is held no more (`#heldRows`), so this refuses only
// a request whose secret was given again while the request was on its way: while its
// body arrived, during a refresh, or right after a member's approval.
if (!leavesAsRead(held ? held.fills : fills, valued?.version, toFill))
throw new HeldAnswer(409, "refused", givenAgain(path));
if (toFill.length) {
written = await withBodyFilled(written, toFill, resolve);
bodyFilled = true;
}
outbound = await substituteMindSecrets(written, resolve, substituted);
} catch (error) {
if (!(error instanceof SecretRefused) || !error.mintable || last) throw error;
const failed = await refreshed();
if (failed)
throw new RefreshFailedRefusal(
`${error.message}; the refresh failed: ${failed.message}`,
);
valued = await readValue();
continue;
}
// the body is this request's now: a refusal in `send` cancels it there
unread = outbound;
const response = await send(outbound, attempt > 0);
if (response.status !== 401 || last || (await refreshed())) return answered(response);
await response.body?.cancel();
valued = await readValue();
}
} catch (error) {
if (!(error instanceof SecretRefused) && !(error instanceof HeldAnswer)) throw error;
const status =
error instanceof HeldAnswer
? error.status
: error instanceof RefreshFailedRefusal
? 502
: 403;
// only a use that was dispatched names the digest of the request that waited
held = undefined;
// Each body that nothing reads any more is cancelled: of the request that holds the
// caller's body now, and of its copy for the retry. By the Streams standard the cancel of
// one half of a copied body settles only with the other's. So the cancels are awaited
// when they will settle: there is no copy, or both halves are cancelled here. One half
// alone is cancelled with no wait: a host that took the other half may never end it, and
// nothing below depends on the cancel.
const unreadBodies = [unread, retry].flatMap(
(left) => (left && !left.bodyUsed && left.body) || [],
);
const cancels = Promise.allSettled(unreadBodies.map((left) => left.cancel()));
if (!retry || unreadBodies.length === 2) await cancels;
console.info({
event: "secret.used",
path,
method: request.method,
url: request.url,
status,
proxy: proxied,
});
facts.push(usedFact(status));
const kind = error instanceof HeldAnswer ? error.kind : "refused";
return answered(
kind === "held"
? new Response(error.message, { status, headers: { "content-type": "application/json" } })
: new Response(`${error.message}\n`, { status }),
kind,
);
}
}
#statementFor(mind: string, method: string, proxied: string): Promise<string> {
// any fault of the stack's in signing, the auth Worker's RPC included, is a 503, reported
const unsigned = (error: unknown) => {
reportIssue("secret-proxy.statement-unsigned", error, { mind });
return new HeldAnswer(
503,
"refused",
`itx.fetch: the request to the secret proxy of ${mind} could not be signed now: ${error instanceof Error ? error.message : String(error)}`,
);
};
const sign = (input: Parameters<Env["AUTH"]["sign"]>[0]) =>
this.env.AUTH.sign(input).catch((error: unknown) => {
throw unsigned(error);
});
const from = configOf(this.env).controlPlaneHostname;
return this.#statements(sign, { from, mind, method, url: proxied }).catch((error: unknown) => {
if (error instanceof HeldAnswer) throw error;
// the memo's length check alone is a 414
if (errorCode(error) === "INVALID_INPUT")
throw new HeldAnswer(
414,
"refused",
error instanceof Error ? error.message : String(error),
);
throw unsigned(error);
});
}
/** A use by proxy as it goes to the proxy: no header that names a secret, the statement added,
* the body streamed unread (nothing signs it). */
async #byProxy(request: Request, proxy: SecretProxy, proxied: string, upgrades: boolean) {
const headers = signedRequestHeaders(request.headers);
for (const [name, value] of request.headers)
if (headerNamesASecret(value)) headers.delete(name);
headers.set(
ITERATE_STATEMENT_HEADER,
await this.#statementFor(proxy.mind, request.method, proxied),
);
// workerd refuses a body on a GET or a HEAD (as iterate-context-durable-object.ts rebuilds one)
const bodyless = request.method === "GET" || request.method === "HEAD";
return new Request(proxied, {
method: request.method,
headers,
body: bodyless ? null : request.body,
// not for an upgrade, whose socket outlives the request that opened it
signal: upgrades ? undefined : request.signal,
// Node (undici) needs it for a streamed body; RequestInit does not declare it
duplex: "half",
} as RequestInit);
}
#refresh(path: string, nonce: string): Promise<SecretFact[]> {
const inFlight = this.#refreshing;
if (inFlight?.nonce === nonce) return inFlight.promise.then(() => []);
const previous = inFlight?.promise.catch(() => {}) ?? Promise.resolve();
const promise = previous
.then(() => this.#doRefresh(path, nonce))
.finally(() => {
if (this.#refreshing?.promise === promise) this.#refreshing = undefined;
});
this.#refreshing = { nonce, promise };
return promise;
}
// A stale mint must never resurrect material a write replaced: the cell's nonce fences it. A mint
// is sealed at the same version.
async #doRefresh(path: string, nonce: string): Promise<SecretFact[]> {
const current = this.#current();
if (current?.cell.nonce !== nonce) return [];
const record = await this.#open(path, current);
const { refresh, urls } = record;
if (!refresh) throw new Error("no refresh strategy");
const pinnedDispatch = (exchange: Request) => {
if (!originPinned(exchange.url, urls))
throw new Error(`the exchange endpoint ${new URL(exchange.url).origin} is outside the pin`);
console.info({ event: "secret.refresh-request", path, url: new URL(exchange.url).origin });
return dispatch(exchange);
};
let next: Record<string, unknown>;
try {
if (refresh.kind === "github-app-installation")
next = {
...(isRecord(record.material) && record.material),
accessToken: await this.#githubInstallationToken(path, refresh, record, pinnedDispatch),
};
else if (refresh.kind === "worker")
// The cast: the generated `ctx.exports` types do not see ../worker.ts's entrypoints.
next = await runExchangeCode({
loader: this.env.LOADER,
pinnedOutbound: (
this.ctx.exports as unknown as {
PinnedOutbound: (options: { props: { urls: string[] } }) => Fetcher;
}
).PinnedOutbound({ props: { urls } }),
context: path,
urls,
source: refresh.source,
material: record.material,
});
else if (provenanceRefusal(refresh.tokenEndpoint, urls))
// a record older than the rule: the scope answer would be another host's word
throw new Error(
`${refresh.kind}: ${provenanceRefusal(refresh.tokenEndpoint, urls)}: the grant is not refreshed. Connect again.`,
);
else if (refresh.client && "stack" in refresh.client)
// A grant through the stack's own client at Cloudflare: the stack holds no such client's
// secret, so it cannot refresh. A member connects again.
throw new Error(
"this grant went through the stack's own OAuth client at Cloudflare, which the stack no longer has: connect again (the default template's cloudflareConnect)",
);
else
next = await refreshSecretMaterial(
refresh,
record.material,
refresh.client && "cloud" in refresh.client
? this.#brokered(refresh.client, "token")
: pinnedDispatch,
this.#clientSecretOf(path),
grantCeilingOf(refresh.grantedWithin, [refresh.tokenEndpoint, ...urls]),
);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
const answerLost = error instanceof RefreshUnanswered;
console.info({ event: "secret.refreshed", path, ok: false, answerLost, error: message });
throw new RefreshFailed(
message,
[
{
type: "events.iterate.com/secret/refreshed",
// `answerLost` on every failed refresh: false says that the provider refused
payload: { kind: refresh.kind, ok: false, error: message, answerLost },
},
],
error instanceof LendRefused ? error.facts : [],
answerLost ? error : undefined,
);
}
const before = this.#current();
if (before?.cell.nonce !== nonce) return [];
const cell = await this.#sealCell(path, { ...record, material: next }, before.cell.version);
// again, after the seal awaited
const now = this.#current();
if (now?.cell.nonce !== nonce) return [];
this.ctx.storage.kv.put<Current>("current", { ...now, cell });
console.info({ event: "secret.refreshed", path, ok: true, version: cell.version });
return [
{
type: "events.iterate.com/secret/refreshed",
payload: { kind: refresh.kind, ok: true },
},
];
}
// `appId` and `privateKey` may each be a placeholder naming another secret, read at every mint as
// a client secret is.
async #githubInstallationToken(
path: string,
refresh: Extract<SecretRefresh, { kind: "github-app-installation" }>,
record: SecretRecord,
pinnedDispatch: (request: Request) => Promise<Response>,
): Promise<string> {
// iterate Cloud's App key stays at the Cloud, which mints the token
if ("cloud" in refresh.client) {
const { cloud, origin } = refresh.client;
const client = await cloudClientOf(origin, cloud);
const { installationTokenEndpoint } = client;
if (!installationTokenEndpoint) throw new Error(`${refresh.kind}: ${cloud} is no GitHub App`);
// here and not at `set`, so it holds for every cell, an imported one too, and asks nothing
// of the Cloud before a write takes its version
const refusal = cloudPinRefusal(record.urls, client);
if (refusal) throw new Error(`${refresh.kind}: ${refusal}`);
const minted = await this.#brokered(
refresh.client,
"installation-token",
)(
new Request(installationTokenEndpoint, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ installationId: refresh.installationId }),
}),
);
const answer: unknown = await minted.json().catch(() => null);
if (!minted.ok || !isRecord(answer) || typeof answer.token !== "string" || !answer.token)
throw new Error(`${refresh.kind}: the Cloud answered ${minted.status} with no token`);
return answer.token;
}
const material = REFRESH_READS[refresh.kind].safeParse(record.material);
if (!material.success)
throw new Error(`${refresh.kind}: the secret's material holds no "appId" and "privateKey"`);
const clientSecretOf = this.#clientSecretOf(path);
const app = {
appId: await clientSecretOf(material.data.appId, refresh.apiOrigin),
privateKey: await clientSecretOf(material.data.privateKey, refresh.apiOrigin),
};
const jwt = await githubAppJwt(app.appId, app.privateKey);
const response = await pinnedDispatch(
new Request(
`${refresh.apiOrigin}/app/installations/${encodeURIComponent(refresh.installationId)}/access_tokens`,
{
method: "POST",
headers: {
accept: "application/vnd.github+json",
authorization: `Bearer ${jwt}`,
"user-agent": "iterate",
},
},
),
);
const data: unknown = await response.json().catch(() => null);
if (!response.ok || !isRecord(data) || typeof data.token !== "string" || !data.token)
throw new Error(`${refresh.kind}: GitHub answered ${response.status} with no token`);
return data.token;
}
}
// A hostile mind may set it to anything: shown as a claim, never checked.
function claimedOrigin(cause: string | null): string | undefined {
if (!cause) return undefined;
try {
const { parent, chain } = JSON.parse(cause) as { parent?: unknown; chain?: unknown };
if (typeof parent === "string") return parent.slice(0, 200);
if (typeof chain === "string")
return chain
.replace(/^.*? with /, "")
.replace(/ ~\S+$/, "")
.slice(0, 200);
} catch {
// not the kernel's JSON
}
return cause.slice(0, 200);
}
const sentBefore = (digest: string) =>
new HeldAnswer(
409,
"sent-before",
`itx.fetch: this request (${digest}) was approved and sent once already; identical bytes are sent once`,
);
// the proxy's URL, then the target's host, path and query (an empty `?` kept)
const proxiedUrlOf = (proxy: SecretProxy, target: URL) => {
const written = new URL(target);
written.hash = "";
const rest = written.href.slice(written.origin.length);
return new URL(`${proxy.url}/${written.host}${rest}`).href;
};
const proxyHoldsNoRule = (path: string) =>
`${path} goes by proxy, so no approval rule holds it here: the mind that holds its key holds its requests`;
const unopened = (path: string) =>
`itx.fetch: the stored material of ${path} cannot be opened (another key, or another secret's record) — set the secret again`;
const tooLarge = (path: string) =>
`itx.fetch: a request with ${path} waits for approval, and its body may be at most ${HELD_BODY_LIMIT / 1024 / 1024} MiB`;
/** Whether a request asks for a WebSocket, as the runtime reads its `Upgrade` header: this one
* word in any letter case, and nothing else in the value. A value such as `websocket, h2c`
* leaves as a plain request with no `Upgrade` header (local workerd, measured 2026-11-21). */
const upgradesOf = (request: { headers: Headers }) =>
request.headers.get("upgrade")?.toLowerCase() === "websocket";
const noWebSocket = (path: string) =>
`itx.fetch: ${path} has an approval rule, so it opens no WebSocket`;
/** `where`: the header that would be filled, as the caller reads it. */
const noPlaceholder = (path: string, where: string) =>
`itx.fetch: ${path} has an approval rule, which reads a request as it is written: ${where} takes no placeholder`;
/** A request whose body the secret fills, and a secret that is no longer the one it was for. */
const givenAgain = (path: string) =>
`itx.fetch: ${path} was given again or deleted since this request arrived, and its value goes into this request's body; send the request again`;
// `VAULT_ANSWER_HEADER` is the vault's alone: an upstream's copy goes. A 101 keeps its socket.
function answerOf(
response: Response,
facts: Facts,
secrets: ReadonlySet<string>,
own?: VaultAnswerKind,
): Response {
const headers = new Headers(response.headers);
let changed = response.headers.has(VAULT_ANSWER_HEADER) || Boolean(own);
headers.delete(VAULT_ANSWER_HEADER);
if (own) headers.set(VAULT_ANSWER_HEADER, own);
for (const [name, value] of response.headers) {
if (name === VAULT_ANSWER_HEADER) continue;
let clean = value;
for (const secret of secrets)
// a shorter value would cut ordinary text, a length or a date, out of a header
if (secret.length >= 8)
for (const spelling of [secret, encodeURIComponent(secret)])
clean = clean.replaceAll(spelling, "[redacted]");
if (clean !== value) {
headers.set(name, clean);
changed = true;
}
}
const answer = !changed
? response
: response.webSocket
? new Response(null, { status: 101, webSocket: response.webSocket, headers })
: new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers,
});
return withFacts(
answer,
facts.filter(({ events }) => events.length),
);
}
const UPSTREAM_ANSWER_MS = 50_000;
// No redirect is followed: it would carry a substituted secret to another origin. The runtime's own
// error quotes the URL, which may carry the secret. No abort signal: an upgrade's socket outlives it.
const dispatch = async (request: Request, place = "the pinned host"): Promise<Response> => {
const { origin } = new URL(request.url);
let overdue: ReturnType<typeof setTimeout> | undefined;
try {
return await Promise.race([
fetchLeavingTheStack(env, request, { redirect: "manual" }),
new Promise<Response>((answer) => {
overdue = setTimeout(
() =>
answer(
new Response(
`itx.fetch: ${place} ${origin} sent no answer within ${String(UPSTREAM_ANSWER_MS / 1000)} s\n`,
{ status: 504 },
),
),
UPSTREAM_ANSWER_MS,
);
}),
]);
} catch (error) {
// The origin and the error's code, when it has one. The error's own text may quote the URL.
console.error({ event: "secret.send-failed", origin, code: errorCode(error) });
const unreachable = `itx.fetch: ${place} ${origin} could not be reached`;
// a proxy's failure is a gateway's
throw place === "the pinned host"
? new SecretRefused(unreachable)
: new HeldAnswer(502, "refused", unreachable);
} finally {
clearTimeout(overdue);
}
};