kernel / core / auth-worker / src / asking-app.tsmain at 949894f
// asking-app.ts — THE APP OF THIS MIND THAT A SIGN-IN IS FOR. A page of an app's host
// (a hostname of one of the mind's zones) that asks who is there sends the browser to this issuer with an
// authorization request of the host's own: its resource is the host, its redirect is the host's
// own callback, and its client is the one this stack made for the host (./oauth.ts
// `hostClientOf`). The sign-in pages then say which app the person signs in to.
//
// WHAT IS TRUSTED. `next`, the request that a sign-in goes on to, is a query that anyone can
// write. So the app is NAMED from it only when its three parts agree with what this stack holds:
// the test by which the authorization endpoint completes a host's own sign-in with no consent
// page. A link that passes it is the link that the host's own page makes.
//
// WHAT THE APP SAYS OF ITSELF, in the document that its own code answers at
// `/.well-known/iterate-app` on its own host (iterate/app-description; iterate/sdk
// `serveAppDescription`): its name, its icon, whether it is for the mind's members alone, and
// how its people sign in:
//
// { "name": "Guestbook", "icon": "/icon.svg", "signIn": ["email", "passkey"] }
//
// This Worker reads it over the public internet, as it reads an outside client's document, follows
// no redirect, and asks again after five minutes. An icon counts only from the app's own origin,
// so the page's policy allows that one origin. An app that says nothing is shown by its host and
// its initials. An app that DOES NOT ANSWER (no answer in two seconds, or a 5xx) has said nothing
// new: it is still what it last said, and it is asked again after a minute. So is an app whose
// description is a JSON object that is wrong. With nothing kept of such an app, the issuer takes
// the passkey alone for it until it says (`UNHEARD`): it never opens an app that it has not heard.
//
// "For members" changes the page's words and the default of `signIn`, and nothing else: who may
// come is the guard in the app's own code (the mind's worker.ts), which this Worker does not
// read.
//
// HOW THE APP'S PEOPLE SIGN IN (`signIn`) is the one part that this Worker enforces: the
// authorization endpoint gives a code for the app's host only to a session whose method the app
// names, or to a passkey's (./authorize.ts, `appTakes`), and the sign-in pages offer those
// methods alone, in the app's order (`signInOf`). It is read from the app, never from the
// request, so an MCP client that signs a person in to the app gets the same list.
//
// WHOSE ADDRESS A MAILED CODE SIGNS IN (`allowedEmails`, `allowedEmailPatterns`) is enforced
// with it, by the rule that says who owns a mind (iterate/mind-config `isOwnerEmail`): addresses,
// each whole, and patterns `*@<domain>`. For an app that names either, no code is mailed to an
// address that neither names (./email-sign-in.ts), and a session that a mailed code opened for
// such an address gets no code for the app's host (`appTakes`). So the list holds at the sign-up,
// and not only at the app's own guard. It binds the method "email" alone; a list that changes
// binds the grants made after it.
//
// AN APP IS AN OAUTH CLIENT, as Codex is: the host's client holds the app's name and icon (its
// `clientName` and `logoUri`), written here whenever the description is read and says something
// else. So the list of a person's sign-ins by client (./oauth.ts `sessionsByClientOf`) names the
// app as the sign-in pages do, and asks no app for it.
import { APP_DESCRIPTION_PATH, AppDescription } from "iterate/app-description";
import { domainOfOwnerEmailPattern, isOwnerEmail } from "iterate/mind-config";
import type { SignInMethod } from "iterate/principal";
import { admitted } from "./device/limits.ts";
import { configOf, type Env, issuerOf } from "./env.ts";
import {
AUTHORIZE_ENDPOINT,
hostCallbackOf,
keptHostClientOf,
knownResourceOf,
mindHostResourceOf,
oauthHelpers,
} from "./oauth.ts";
export type AskingApp = {
/** The app's hostname, in one of the mind's zones. */
host: string;
origin: string;
/** What the app calls itself, or its host when it says nothing. */
name: string;
/** Its icon, on its own origin, when it names one. */
icon: string | null;
/** The app says that it is for the mind's members alone. */
members: boolean;
/** How the app's people sign in, in the app's order: what it names, or the default for whom it
* is for (`saidIn`). The sign-in page opens on the first. */
signIn: SignInMethod[];
/** Whose address a mailed code signs in to the app (`takesAddress`), or null: any address. */
allowedEmails: AllowedEmails | null;
};
const text = (value: string) =>
value.replace(/[&<>"']/g, (character) => `&#${character.charCodeAt(0)};`);
/** HOW A PERSON SIGNS IN TO THE CONTROL PLANE ITSELF, and for a client of the mind's API or its
* MCP server: a member's passkey, and nothing else. The mailed code is for the people of the
* mind's apps, and opens no way into the mind (./email-sign-in.ts). */
const CONTROL_PLANE_SIGN_IN: SignInMethod[] = ["passkey"];
/** THE SIGN-IN THAT `next` GOES ON TO (a path of this issuer), as its pages show it: the methods
* that they offer, in order, and the app that they name. An authorization for a mind hostname
* takes the methods of that hostname's app, whichever client asks, since the authorization
* endpoint holds the session to the same list; the pages name the app only for its own sign-in
* (`askingAppOf`). Anything else is the control plane's own sign-in. Null: too many hostnames
* that the stack holds nothing of were asked about just now (`appOf`). */
export async function signInOf(
env: Env,
next: string,
): Promise<{
app: AskingApp | null;
methods: SignInMethod[];
/** For a mind hostname's sign-in: the app's name, and whose address a mailed code signs in to
* it (`takesAddress`). The pages' refusal names the app by it, whichever client asks. */
mailedCode: Pick<AskingApp, "name" | "allowedEmails"> | null;
} | null> {
const authorization = hostAuthorizationIn(env, next);
if (!authorization) return { app: null, methods: CONTROL_PLANE_SIGN_IN, mailedCode: null };
const app = await appOf(env, authorization.origin);
if (!app) return null;
return {
app: (await isOwnSignIn(env, authorization)) ? app : null,
methods: app.signIn,
mailedCode: app,
};
}
/** The app whose own sign-in `next` goes on to (a path of this issuer), or null: for any other
* request, and for one whose parts this stack does not hold. */
export async function askingAppOf(env: Env, next: string): Promise<AskingApp | null> {
const authorization = hostAuthorizationIn(env, next);
if (!authorization || !(await isOwnSignIn(env, authorization))) return null;
return appOf(env, authorization.origin);
}
/** WHETHER AN APP TAKES A SESSION, for a code for the app's host: a passkey's counts at every
* app, and any other when the app names its method. A mailed code's counts only for an address
* that the app takes (`takesAddress`). */
export const appTakes = (
app: Pick<AskingApp, "signIn" | "allowedEmails">,
session: { method: SignInMethod; email: string },
) =>
session.method === "passkey" ||
(app.signIn.includes(session.method) &&
(session.method !== "email" || takesAddress(app, session.email)));
/** The addresses and the patterns (`*@<domain>`) that an app names for the mailed code. */
export type AllowedEmails = { emails: string[]; patterns: string[] };
/** WHETHER A MAILED CODE SIGNS `email` IN TO THE APP: the app names nobody, or the address is one
* that it names, by the one rule that says who owns a mind (iterate/mind-config
* `isOwnerEmail`). */
export const takesAddress = (app: Pick<AskingApp, "allowedEmails">, email: string) =>
!app.allowedEmails ||
isOwnerEmail(
{ ownerEmails: app.allowedEmails.emails, ownerEmailPatterns: app.allowedEmails.patterns },
email,
);
/** `next` as an authorization request of this issuer for a mind hostname: the hostname's origin,
* and the request's parameters. Null for anything else. */
function hostAuthorizationIn(env: Env, next: string) {
const issuer = issuerOf(env);
if (!URL.canParse(next, issuer)) return null;
const url = new URL(next, issuer);
if (url.origin !== issuer || url.pathname !== AUTHORIZE_ENDPOINT) return null;
const asked = url.searchParams.get("resource");
const origin = asked && mindHostResourceOf(configOf(env), knownResourceOf(env, asked));
return origin ? { origin, parameters: url.searchParams } : null;
}
/** Whether an authorization for a mind hostname is that host's own sign-in: its code goes to
* the host's own callback, and its client is the one this stack made for the host. */
async function isOwnSignIn(
env: Env,
{ origin, parameters }: { origin: string; parameters: URLSearchParams },
) {
if (parameters.get("redirect_uri") !== hostCallbackOf(origin)) return false;
const client = await keptHostClientOf(env, new URL(origin).host);
return Boolean(client) && client === parameters.get("client_id");
}
/** A HOST OF THIS MIND AS AN APP: `origin` is one that `mindHostResourceOf` let through. Anyone
* can ask about any hostname of the mind's zones, in the `next` of a sign-in page, so a hostname
* that the stack holds nothing of (no description, no client of its own) is counted as a new
* host client is (./oauth.ts `hostClientOf`): null when too many were asked about just now. */
export async function appOf(env: Env, origin: string): Promise<AskingApp | null> {
const { host } = new URL(origin);
const key = `iterate-app-description:${host}`;
let kept = inThisShape(await env.OAUTH_KV.get<Kept & DomainsRow>(key, "json"));
if (
!kept &&
!(await keptHostClientOf(env, host)) &&
!(await admitted(env, "host-client", new Request(origin)))
)
return null;
if (!kept || kept.askAfter <= Date.now()) {
const document = `${origin}${APP_DESCRIPTION_PATH}`;
const heard = await fetch(document, {
headers: { accept: "application/json" },
redirect: "manual",
signal: AbortSignal.timeout(2000),
})
// What the app says: its description's text is empty for an app that has none (a 404, a
// sign-in). Null: the app did not answer, its answer broke off, or its description is
// wrong (`saidIn`). None of those is the app's word.
.then(async (answer) => {
// a 5xx, and a 4xx that says "not now" (408, 425, 429), are no answer
if (answer.status >= 500 || [408, 425, 429].includes(answer.status)) return null;
return saidIn(answer.status === 200 ? await answer.text() : "", document);
})
.catch(() => null);
kept = heard
? // "nothing" is kept too: an app with no description is not asked at each page
{ ...heard, askAfter: Date.now() + 5 * 60_000 }
: // no word of the app: it is what it last said, or `UNHEARD`, and is asked again after a
// minute
{ ...(kept || UNHEARD), askAfter: Date.now() + 60_000 };
// The row goes a day after it was last written: an app that is asked about is written again
// within five minutes, and a hostname that nobody asks about any more leaves no row.
await env.OAUTH_KV.put(key, JSON.stringify(kept), { expirationTtl: 24 * 3600 });
if (heard) await keepOnClient(env, origin, { name: kept.name || host, icon: kept.icon });
}
return {
host,
origin,
name: kept.name || host,
icon: kept.icon,
members: kept.members,
signIn: kept.signIn,
allowedEmails: kept.allowedEmails,
};
}
/** A ROW OF AN ISSUER THAT READ A LIST OF DOMAINS (`emailDomains`) where this one reads
* `allowedEmails`. The mind's KV outlives a deploy, so this issuer may read such a row for the
* five minutes until the app is asked again. */
type DomainsRow = { emailDomains?: string[] | null; allowedEmails?: AllowedEmails | null };
/** Such a row as this issuer keeps it: a domain is the pattern `*@<domain>`, which names the same
* addresses. So a row that held a list never reads as "any address". */
function inThisShape(row: (Kept & DomainsRow) | null): Kept | null {
if (!row) return null;
const { emailDomains, ...kept } = row;
if (kept.allowedEmails || !emailDomains)
return { ...kept, allowedEmails: kept.allowedEmails || null };
return {
...kept,
allowedEmails: { emails: [], patterns: emailDomains.map((domain) => `*@${domain}`) },
};
}
/** What an app said of itself (`saidIn`), and when to ask it again. */
type Kept = typeof NOTHING & { askAfter: number };
/** The host's own client says what the app says: written when it says something else. */
async function keepOnClient(env: Env, origin: string, said: { name: string; icon: string | null }) {
const clientId = await keptHostClientOf(env, new URL(origin).host);
if (!clientId) return;
const helpers = oauthHelpers(env, origin);
const client = await helpers.lookupClient(clientId);
if (!client || (client.clientName === said.name && (client.logoUri || null) === said.icon))
return;
await helpers.updateClient(clientId, { clientName: said.name, logoUri: said.icon || undefined });
}
/** An app that says nothing: it is shown by its host, and it is for anyone, whose people sign in
* by the mailed code first. */
const NOTHING: {
name: string | null;
icon: string | null;
members: boolean;
signIn: SignInMethod[];
allowedEmails: AllowedEmails | null;
} = { name: null, icon: null, members: false, signIn: ["email", "passkey"], allowedEmails: null };
/** AN APP THAT THE ISSUER HAS NO WORD OF YET: it did not answer, or its description is wrong, and
* nothing that it said before is kept. The issuer does not know whom the app takes, so it takes
* the passkey alone until the app says: a code that is mailed meanwhile, or a grant for an
* address, could be one that the app's own list refuses (`signIn`, `allowedEmails`). */
const UNHEARD: typeof NOTHING = { ...NOTHING, signIn: ["passkey"] };
/** WHAT THE TEXT OF AN APP'S DESCRIPTION SAYS (iterate/app-description `AppDescription`): a name
* of at most sixty characters, its icon when that is on the description's own origin, over
* https, whether the app is for members, and how its people sign in: the list it names, as it is
* written, or the passkey alone for an app for members (a member signs in with nothing else),
* and email then passkey for any other, and whose address a mailed code signs in. A text that is
* no JSON object says nothing: an app with no description answers with a page, or with none.
* NULL: A JSON OBJECT THAT IS NO `AppDescription`, such as one that names a method this issuer
* does not know, or a pattern that is none, and one that broke off or is over 20,000 characters. The app wrote a description and it is wrong, so it is
* held to no list that it did not write, and it is not opened to anyone either: the caller keeps
* what the app last said (`appOf`). */
export function saidIn(descriptionText: string, descriptionUrl: string): typeof NOTHING | null {
// An object that is too long to read, or that broke off, is a description that did not arrive
// whole. Any other text (a page, of any length) is none.
const unread = descriptionText.trimStart().startsWith("{") ? null : NOTHING;
if (!descriptionText) return NOTHING;
if (descriptionText.length > 20_000) return unread;
let json: unknown;
try {
json = JSON.parse(descriptionText);
} catch {
return unread;
}
if (typeof json !== "object" || !json || Array.isArray(json)) return NOTHING;
// A list of domains (`emailDomains`) is a restriction that this issuer does not read. The parse
// would drop it and leave the app for any address, so such a description is wrong.
if ("emailDomains" in json) return null;
const parsed = AppDescription.safeParse(json);
if (!parsed.success) return null;
const name = parsed.data.name
// a name is one line of text
.replace(/[\p{Cc}\p{Cf}]/gu, " ")
.trim()
.slice(0, 60);
const { origin } = new URL(descriptionUrl);
const icon = parsed.data.icon ? new URL(parsed.data.icon, descriptionUrl) : null;
const members = parsed.data.for === "members";
const { allowedEmails, allowedEmailPatterns } = parsed.data;
return {
name: name || null,
// a path that begins `//` names another host: an icon is the app's own origin's alone
icon: icon?.protocol === "https:" && icon.origin === origin ? icon.href : null,
members,
signIn: [
...new Set<SignInMethod>(parsed.data.signIn || (members ? ["passkey"] : NOTHING.signIn)),
],
allowedEmails:
allowedEmails || allowedEmailPatterns
? {
emails: [...new Set(allowedEmails || [])],
patterns: [...new Set(allowedEmailPatterns || [])],
}
: null,
};
}
/** WHOM AN APP'S MAILED CODE IS FOR, IN WORDS, or "" for an app that takes any address. The
* pages name the domains of the patterns, and never an address that the app names: the page is
* for anyone. */
export function allowedEmailsWords(app: Pick<AskingApp, "name" | "allowedEmails">): string {
if (!app.allowedEmails) return "";
const { emails, patterns } = app.allowedEmails;
const domains = new Intl.ListFormat("en", { type: "disjunction" }).format(
patterns.map(domainOfOwnerEmailPattern),
);
if (!patterns.length) return `${app.name} takes only the addresses that it names.`;
return emails.length
? `${app.name} takes an address at ${domains}, and the addresses that it names, only.`
: `${app.name} takes an address at ${domains} only.`;
}
/** The app's tile (plain-page.css `.tile`): its icon, or its initials. Never a built-in mark of
* ./client-mark.ts: an app of a mind that calls itself "Codex" is not OpenAI's. `small` is for a
* row of a list. */
export function appTile(
app: Pick<AskingApp, "name" | "icon">,
{ small = false }: { small?: boolean } = {},
): string {
const tile = small ? "tile small" : "tile";
if (app.icon)
return `<div class="${tile}" data-app-mark="icon"><img src="${text(app.icon)}" alt="" referrerpolicy="no-referrer"></div>`;
const initials = [...app.name.replace(/[^\p{L}\p{N}]/gu, "")].slice(0, 2).join("");
return `<div class="${tile}" data-app-mark="initials">${text(initials.toUpperCase() || "?")}</div>`;
}