{"file":"server/src/keys.ts","version":318,"total":253,"from":1,"to":253,"lines":[{"n":1,"text":"// Per-agent keys, ported from the platform's worker/src/keys.ts. They run when AGENT_KEYS is \"on\" and","atom":"a1449","author":"speed","version":150},{"n":2,"text":"// KEY_HMAC_SECRET is set. Each key belongs to one agent of one project; the project's Durable Object keeps only the","atom":"a1449","author":"speed","version":150},{"n":3,"text":"// key's HMAC, never the key. Without keys, the shared token (ABSO_TOKEN) is the only way in.","atom":"a1449","author":"speed","version":150},{"n":4,"text":"","atom":"a1449","author":"speed","version":150},{"n":5,"text":"export const KEY_PREFIX = \"abso_sk_\";","atom":"a1449","author":"speed","version":150},{"n":6,"text":"const FAIL_LIMIT = 8;","atom":"a1449","author":"speed","version":150},{"n":7,"text":"const FAIL_WINDOW_MS = 10 * 60 * 1000;","atom":"a1449","author":"speed","version":150},{"n":8,"text":"const AUDIT_CAP = 200;","atom":"a1449","author":"speed","version":150},{"n":9,"text":"const DAY_MS = 24 * 60 * 60 * 1000;","atom":"a1449","author":"speed","version":150},{"n":10,"text":"","atom":"a1449","author":"speed","version":150},{"n":11,"text":"export interface KeyEnv {","atom":"a1449","author":"speed","version":150},{"n":12,"text":"\tAGENT_KEYS?: string;","atom":"a1449","author":"speed","version":150},{"n":13,"text":"\tKEY_HMAC_SECRET?: string;","atom":"a1449","author":"speed","version":150},{"n":14,"text":"}","atom":"a1449","author":"speed","version":150},{"n":15,"text":"","atom":"a1449","author":"speed","version":150},{"n":16,"text":"/** What a key may do in its project: the admin's choice when it was issued. */","atom":"a1449","author":"speed","version":150},{"n":17,"text":"export type KeyRole = \"owner\" | \"writer\" | \"reader\";","atom":"a1449","author":"speed","version":150},{"n":18,"text":"","atom":"a1449","author":"speed","version":150},{"n":19,"text":"export interface KeyRow {","atom":"a1449","author":"speed","version":150},{"n":20,"text":"\tid: string;","atom":"a1449","author":"speed","version":150},{"n":21,"text":"\thmac: string;","atom":"a1449","author":"speed","version":150},{"n":22,"text":"\tagent: string;","atom":"a1449","author":"speed","version":150},{"n":23,"text":"\tissuedBy: string;","atom":"a1449","author":"speed","version":150},{"n":24,"text":"\tat: number;","atom":"a1449","author":"speed","version":150},{"n":25,"text":"\tlastUsed: number | null;","atom":"a1449","author":"speed","version":150},{"n":26,"text":"\trevokedAt: number | null;","atom":"a1449","author":"speed","version":150},{"n":27,"text":"\texpiresAt?: number | null;","atom":"a1449","author":"speed","version":150},{"n":28,"text":"\t/** Absent on a key issued before roles existed: it counts as reader, so it fails closed. */","atom":"a1449","author":"speed","version":150},{"n":29,"text":"\trole?: KeyRole;","atom":"a1449","author":"speed","version":150},{"n":30,"text":"}","atom":"a1449","author":"speed","version":150},{"n":31,"text":"","atom":"a1449","author":"speed","version":150},{"n":32,"text":"export interface FailRow {","atom":"a1449","author":"speed","version":150},{"n":33,"text":"\tn: number;","atom":"a1449","author":"speed","version":150},{"n":34,"text":"\tat: number;","atom":"a1449","author":"speed","version":150},{"n":35,"text":"}","atom":"a1449","author":"speed","version":150},{"n":36,"text":"","atom":"a1449","author":"speed","version":150},{"n":37,"text":"export interface AuditRow {","atom":"a1449","author":"speed","version":150},{"n":38,"text":"\tt: number;","atom":"a1449","author":"speed","version":150},{"n":39,"text":"\tkind: \"issue\" | \"revoke\" | \"use\" | \"fail\";","atom":"a1449","author":"speed","version":150},{"n":40,"text":"\tid: string | null;","atom":"a1449","author":"speed","version":150},{"n":41,"text":"\taddr: string;","atom":"a1449","author":"speed","version":150},{"n":42,"text":"}","atom":"a1449","author":"speed","version":150},{"n":43,"text":"","atom":"a1449","author":"speed","version":150},{"n":44,"text":"export interface KeyState {","atom":"a1449","author":"speed","version":150},{"n":45,"text":"\trows: KeyRow[];","atom":"a1449","author":"speed","version":150},{"n":46,"text":"\tfails: Record<string, FailRow>;","atom":"a1449","author":"speed","version":150},{"n":47,"text":"\tseen: Record<string, string>;","atom":"a1449","author":"speed","version":150},{"n":48,"text":"\taudit: AuditRow[];","atom":"a1449","author":"speed","version":150},{"n":49,"text":"}","atom":"a1449","author":"speed","version":150},{"n":50,"text":"","atom":"a1449","author":"speed","version":150},{"n":51,"text":"export function emptyKeyState(): KeyState {","atom":"a1449","author":"speed","version":150},{"n":52,"text":"\treturn { rows: [], fails: {}, seen: {}, audit: [] };","atom":"a1449","author":"speed","version":150},{"n":53,"text":"}","atom":"a1449","author":"speed","version":150},{"n":54,"text":"","atom":"a1449","author":"speed","version":150},{"n":55,"text":"/** Keys run when the switch and the secret are both set. */","atom":"a1449","author":"speed","version":150},{"n":56,"text":"export function keysEnabled(env: KeyEnv): boolean {","atom":"a1449","author":"speed","version":150},{"n":57,"text":"\treturn env.AGENT_KEYS === \"on\" && !!env.KEY_HMAC_SECRET;","atom":"a1449","author":"speed","version":150},{"n":58,"text":"}","atom":"a1449","author":"speed","version":150},{"n":59,"text":"","atom":"a1449","author":"speed","version":150},{"n":60,"text":"/** 90 days when omitted. 1d to 365d otherwise. */","atom":"a1449","author":"speed","version":150},{"n":61,"text":"export function expiresAt(now: number, raw: string | null): number | { error: string } {","atom":"a1449","author":"speed","version":150},{"n":62,"text":"\tif (raw === null || raw === \"\") return now + 90 * DAY_MS;","atom":"a1449","author":"speed","version":150},{"n":63,"text":"\tconst match = /^(\\d+)d$/.exec(raw);","atom":"a1449","author":"speed","version":150},{"n":64,"text":"\tif (!match) return { error: \"expires is a number of days, like 7d, from 1d to 365d\" };","atom":"a1449","author":"speed","version":150},{"n":65,"text":"\tconst days = Number(match[1]);","atom":"a1449","author":"speed","version":150},{"n":66,"text":"\tif (days < 1 || days > 365) return { error: \"expires is from 1d to 365d\" };","atom":"a1449","author":"speed","version":150},{"n":67,"text":"\treturn now + days * DAY_MS;","atom":"a1449","author":"speed","version":150},{"n":68,"text":"}","atom":"a1449","author":"speed","version":150},{"n":69,"text":"","atom":"a1449","author":"speed","version":150},{"n":70,"text":"export function isAgentKey(presented: string | undefined): boolean {","atom":"a1449","author":"speed","version":150},{"n":71,"text":"\treturn typeof presented === \"string\" && presented.startsWith(KEY_PREFIX) && presented.length > KEY_PREFIX.length;","atom":"a1449","author":"speed","version":150},{"n":72,"text":"}","atom":"a1449","author":"speed","version":150},{"n":73,"text":"","atom":"a1449","author":"speed","version":150},{"n":74,"text":"export async function hmac(secret: string, message: string): Promise<string> {","atom":"a1449","author":"speed","version":150},{"n":75,"text":"\tconst key = await crypto.subtle.importKey(","atom":"a1449","author":"speed","version":150},{"n":76,"text":"\t\t\"raw\",","atom":"a1449","author":"speed","version":150},{"n":77,"text":"\t\tnew TextEncoder().encode(secret),","atom":"a1449","author":"speed","version":150},{"n":78,"text":"\t\t{ name: \"HMAC\", hash: \"SHA-256\" },","atom":"a1449","author":"speed","version":150},{"n":79,"text":"\t\tfalse,","atom":"a1449","author":"speed","version":150},{"n":80,"text":"\t\t[\"sign\"],","atom":"a1449","author":"speed","version":150},{"n":81,"text":"\t);","atom":"a1449","author":"speed","version":150},{"n":82,"text":"\tconst mac = new Uint8Array(await crypto.subtle.sign(\"HMAC\", key, new TextEncoder().encode(message)));","atom":"a1449","author":"speed","version":150},{"n":83,"text":"\treturn [...mac].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");","atom":"a1449","author":"speed","version":150},{"n":84,"text":"}","atom":"a1449","author":"speed","version":150},{"n":85,"text":"","atom":"a1449","author":"speed","version":150},{"n":86,"text":"/** Whether two strings are equal, taking the same time wherever they differ. */","atom":"a1449","author":"speed","version":150},{"n":87,"text":"export function timingSame(a: string, b: string): boolean {","atom":"a1449","author":"speed","version":150},{"n":88,"text":"\tif (a.length !== b.length) return false;","atom":"a1449","author":"speed","version":150},{"n":89,"text":"\tlet diff = 0;","atom":"a1449","author":"speed","version":150},{"n":90,"text":"\tfor (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);","atom":"a1449","author":"speed","version":150},{"n":91,"text":"\treturn diff === 0;","atom":"a1449","author":"speed","version":150},{"n":92,"text":"}","atom":"a1449","author":"speed","version":150},{"n":93,"text":"","atom":"a1449","author":"speed","version":150},{"n":94,"text":"function randomHex(bytes: number): string {","atom":"a1449","author":"speed","version":150},{"n":95,"text":"\tconst b = new Uint8Array(bytes);","atom":"a1449","author":"speed","version":150},{"n":96,"text":"\tcrypto.getRandomValues(b);","atom":"a1449","author":"speed","version":150},{"n":97,"text":"\treturn [...b].map((n) => n.toString(16).padStart(2, \"0\")).join(\"\");","atom":"a1449","author":"speed","version":150},{"n":98,"text":"}","atom":"a1449","author":"speed","version":150},{"n":99,"text":"","atom":"a1449","author":"speed","version":150},{"n":100,"text":"export function newKey(): { id: string; key: string } {","atom":"a1449","author":"speed","version":150},{"n":101,"text":"\tconst raw = new Uint8Array(32);","atom":"a1449","author":"speed","version":150},{"n":102,"text":"\tcrypto.getRandomValues(raw);","atom":"a1449","author":"speed","version":150},{"n":103,"text":"\tconst body = btoa(String.fromCharCode(...raw))","atom":"a1449","author":"speed","version":150},{"n":104,"text":"\t\t.replaceAll(\"+\", \"-\")","atom":"a1449","author":"speed","version":150},{"n":105,"text":"\t\t.replaceAll(\"/\", \"_\")","atom":"a1449","author":"speed","version":150},{"n":106,"text":"\t\t.replace(/=+$/, \"\");","atom":"a1449","author":"speed","version":150},{"n":107,"text":"\treturn { id: `k_${randomHex(4)}`, key: `${KEY_PREFIX}${body}` };","atom":"a1449","author":"speed","version":150},{"n":108,"text":"}","atom":"a1449","author":"speed","version":150},{"n":109,"text":"","atom":"a1449","author":"speed","version":150},{"n":110,"text":"function note(state: KeyState, row: AuditRow): void {","atom":"a1449","author":"speed","version":150},{"n":111,"text":"\tstate.audit.push(row);","atom":"a1449","author":"speed","version":150},{"n":112,"text":"\tif (state.audit.length > AUDIT_CAP) state.audit.splice(0, state.audit.length - AUDIT_CAP);","atom":"a1449","author":"speed","version":150},{"n":113,"text":"}","atom":"a1449","author":"speed","version":150},{"n":114,"text":"","atom":"a1449","author":"speed","version":150},{"n":115,"text":"export interface Issued {","atom":"a1449","author":"speed","version":150},{"n":116,"text":"\tid: string;","atom":"a1449","author":"speed","version":150},{"n":117,"text":"\tkey: string;","atom":"a1449","author":"speed","version":150},{"n":118,"text":"\tagent: string;","atom":"a1449","author":"speed","version":150},{"n":119,"text":"}","atom":"a1449","author":"speed","version":150},{"n":120,"text":"","atom":"a1449","author":"speed","version":150},{"n":121,"text":"/** Issues a key. The returned value is the only copy; the state keeps the HMAC. */","atom":"a1449","author":"speed","version":150},{"n":122,"text":"export async function issueKey(","atom":"a1449","author":"speed","version":150},{"n":123,"text":"\tstate: KeyState,","atom":"a1449","author":"speed","version":150},{"n":124,"text":"\tsecret: string,","atom":"a1449","author":"speed","version":150},{"n":125,"text":"\tagent: string,","atom":"a1449","author":"speed","version":150},{"n":126,"text":"\tby: string,","atom":"a1449","author":"speed","version":150},{"n":127,"text":"\tnow: number,","atom":"a1449","author":"speed","version":150},{"n":128,"text":"\taddress: string,","atom":"a1449","author":"speed","version":150},{"n":129,"text":"\texpires: number,","atom":"a1449","author":"speed","version":150},{"n":130,"text":"\trole: KeyRole,","atom":"a1449","author":"speed","version":150},{"n":131,"text":"): Promise<Issued> {","atom":"a1449","author":"speed","version":150},{"n":132,"text":"\tconst made = newKey();","atom":"a1449","author":"speed","version":150},{"n":133,"text":"\tconst addr = await hmac(secret, address);","atom":"a1449","author":"speed","version":150},{"n":134,"text":"\tstate.rows.push({","atom":"a1449","author":"speed","version":150},{"n":135,"text":"\t\tid: made.id,","atom":"a1449","author":"speed","version":150},{"n":136,"text":"\t\thmac: await hmac(secret, made.key),","atom":"a1449","author":"speed","version":150},{"n":137,"text":"\t\tagent,","atom":"a1449","author":"speed","version":150},{"n":138,"text":"\t\tissuedBy: by,","atom":"a1449","author":"speed","version":150},{"n":139,"text":"\t\tat: now,","atom":"a1449","author":"speed","version":150},{"n":140,"text":"\t\tlastUsed: null,","atom":"a1449","author":"speed","version":150},{"n":141,"text":"\t\trevokedAt: null,","atom":"a1449","author":"speed","version":150},{"n":142,"text":"\t\texpiresAt: expires,","atom":"a1449","author":"speed","version":150},{"n":143,"text":"\t\trole,","atom":"a1449","author":"speed","version":150},{"n":144,"text":"\t});","atom":"a1449","author":"speed","version":150},{"n":145,"text":"\tnote(state, { t: now, kind: \"issue\", id: made.id, addr });","atom":"a1449","author":"speed","version":150},{"n":146,"text":"\treturn { id: made.id, key: made.key, agent };","atom":"a1449","author":"speed","version":150},{"n":147,"text":"}","atom":"a1449","author":"speed","version":150},{"n":148,"text":"","atom":"a1449","author":"speed","version":150},{"n":149,"text":"export function revokeKey(state: KeyState, id: string, now: number, addr: string): boolean {","atom":"a1449","author":"speed","version":150},{"n":150,"text":"\tconst row = state.rows.find((r) => r.id === id);","atom":"a1449","author":"speed","version":150},{"n":151,"text":"\tif (!row || row.revokedAt !== null) return false;","atom":"a1449","author":"speed","version":150},{"n":152,"text":"\trow.revokedAt = now;","atom":"a1449","author":"speed","version":150},{"n":153,"text":"\tnote(state, { t: now, kind: \"revoke\", id, addr });","atom":"a1449","author":"speed","version":150},{"n":154,"text":"\treturn true;","atom":"a1449","author":"speed","version":150},{"n":155,"text":"}","atom":"a1449","author":"speed","version":150},{"n":156,"text":"","atom":"a1449","author":"speed","version":150},{"n":157,"text":"export function listKeys(state: KeyState, agent?: string) {","atom":"a1449","author":"speed","version":150},{"n":158,"text":"\treturn state.rows","atom":"a1449","author":"speed","version":150},{"n":159,"text":"\t\t.filter((r) => agent === undefined || r.agent === agent)","atom":"a1449","author":"speed","version":150},{"n":160,"text":"\t\t.map(({ id, agent: a, issuedBy, at, lastUsed, revokedAt, expiresAt: exp, role }) => ({","atom":"a1449","author":"speed","version":150},{"n":161,"text":"\t\t\tid,","atom":"a1449","author":"speed","version":150},{"n":162,"text":"\t\t\tagent: a,","atom":"a1449","author":"speed","version":150},{"n":163,"text":"\t\t\trole: role ?? \"reader\",","atom":"a1449","author":"speed","version":150},{"n":164,"text":"\t\t\tissuedBy,","atom":"a1449","author":"speed","version":150},{"n":165,"text":"\t\t\tat,","atom":"a1449","author":"speed","version":150},{"n":166,"text":"\t\t\tlastUsed,","atom":"a1449","author":"speed","version":150},{"n":167,"text":"\t\t\trevokedAt,","atom":"a1449","author":"speed","version":150},{"n":168,"text":"\t\t\texpiresAt: exp ?? null,","atom":"a1449","author":"speed","version":150},{"n":169,"text":"\t\t}));","atom":"a1449","author":"speed","version":150},{"n":170,"text":"}","atom":"a1449","author":"speed","version":150},{"n":171,"text":"","atom":"a1449","author":"speed","version":150},{"n":172,"text":"export type KeyDecision =","atom":"a1449","author":"speed","version":150},{"n":173,"text":"\t| { ok: true; id: string; as: string; role: KeyRole; dirty: boolean }","atom":"a1449","author":"speed","version":150},{"n":174,"text":"\t| { ok: false; status: number; error: string; dirty: boolean };","atom":"a1449","author":"speed","version":150},{"n":175,"text":"","atom":"a1449","author":"speed","version":150},{"n":176,"text":"/**","atom":"a1449","author":"speed","version":150},{"n":177,"text":" * Checks a presented key against one project. The URL's agent must match the key's agent: a mismatch is a 403, and","atom":"a1449","author":"speed","version":150},{"n":178,"text":" * the agent is not rewritten. A missing secret is a 503 that names no key. Failures are counted per address and","atom":"a1449","author":"speed","version":150},{"n":179,"text":" * slowed after a few. Nothing returned or recorded contains the key.","atom":"a1449","author":"speed","version":150},{"n":180,"text":" */","atom":"a1449","author":"speed","version":150},{"n":181,"text":"export async function checkKey(","atom":"a1449","author":"speed","version":150},{"n":182,"text":"\tstate: KeyState,","atom":"a1449","author":"speed","version":150},{"n":183,"text":"\tsecret: string | undefined,","atom":"a1449","author":"speed","version":150},{"n":184,"text":"\tpresented: string,","atom":"a1449","author":"speed","version":150},{"n":185,"text":"\tagent: string | undefined,","atom":"a1449","author":"speed","version":150},{"n":186,"text":"\tnow: number,","atom":"a1449","author":"speed","version":150},{"n":187,"text":"\taddress: string,","atom":"a1449","author":"speed","version":150},{"n":188,"text":"): Promise<KeyDecision> {","atom":"a1449","author":"speed","version":150},{"n":189,"text":"\tif (!secret) return { ok: false, status: 503, error: \"the key secret (KEY_HMAC_SECRET) is not set\", dirty: false };","atom":"a1449","author":"speed","version":150},{"n":190,"text":"\tconst addr = await hmac(secret, address);","atom":"a1449","author":"speed","version":150},{"n":191,"text":"\tconst fail = state.fails[addr];","atom":"a1449","author":"speed","version":150},{"n":192,"text":"\tif (fail && now - fail.at < FAIL_WINDOW_MS && fail.n >= FAIL_LIMIT) {","atom":"a1449","author":"speed","version":150},{"n":193,"text":"\t\treturn { ok: false, status: 429, error: \"too many failed keys; try again shortly\", dirty: false };","atom":"a1449","author":"speed","version":150},{"n":194,"text":"\t}","atom":"a1449","author":"speed","version":150},{"n":195,"text":"\tconst mac = await hmac(secret, presented);","atom":"a1449","author":"speed","version":150},{"n":196,"text":"\tconst row = state.rows.find((r) => timingSame(r.hmac, mac));","atom":"a1449","author":"speed","version":150},{"n":197,"text":"\tconst bump = () => {","atom":"a1449","author":"speed","version":150},{"n":198,"text":"\t\tconst cur = state.fails[addr];","atom":"a1449","author":"speed","version":150},{"n":199,"text":"\t\tstate.fails[addr] = !cur || now - cur.at >= FAIL_WINDOW_MS ? { n: 1, at: now } : { n: cur.n + 1, at: cur.at };","atom":"a1449","author":"speed","version":150},{"n":200,"text":"\t\tnote(state, { t: now, kind: \"fail\", id: row?.id ?? null, addr });","atom":"a1449","author":"speed","version":150},{"n":201,"text":"\t};","atom":"a1449","author":"speed","version":150},{"n":202,"text":"\tif (!row || row.revokedAt !== null || (row.expiresAt != null && now >= row.expiresAt)) {","atom":"a1449","author":"speed","version":150},{"n":203,"text":"\t\tbump();","atom":"a1449","author":"speed","version":150},{"n":204,"text":"\t\tconst error = !row","atom":"a1449","author":"speed","version":150},{"n":205,"text":"\t\t\t? \"that key is not for this project\"","atom":"a1449","author":"speed","version":150},{"n":206,"text":"\t\t\t: row.revokedAt !== null","atom":"a1449","author":"speed","version":150},{"n":207,"text":"\t\t\t\t? \"that key was revoked\"","atom":"a1449","author":"speed","version":150},{"n":208,"text":"\t\t\t\t: \"that key has expired\";","atom":"a1449","author":"speed","version":150},{"n":209,"text":"\t\treturn { ok: false, status: 403, error, dirty: true };","atom":"a1449","author":"speed","version":150},{"n":210,"text":"\t}","atom":"a1449","author":"speed","version":150},{"n":211,"text":"\tif (agent !== undefined && row.agent !== agent) {","atom":"a1449","author":"speed","version":150},{"n":212,"text":"\t\tbump();","atom":"a1449","author":"speed","version":150},{"n":213,"text":"\t\treturn { ok: false, status: 403, error: \"that key is not for this agent\", dirty: true };","atom":"a1449","author":"speed","version":150},{"n":214,"text":"\t}","atom":"a1449","author":"speed","version":150},{"n":215,"text":"\tlet dirty = false;","atom":"a1449","author":"speed","version":150},{"n":216,"text":"\tif (state.fails[addr]) {","atom":"a1449","author":"speed","version":150},{"n":217,"text":"\t\tdelete state.fails[addr];","atom":"a1449","author":"speed","version":150},{"n":218,"text":"\t\tdirty = true;","atom":"a1449","author":"speed","version":150},{"n":219,"text":"\t}","atom":"a1449","author":"speed","version":150},{"n":220,"text":"\tif (state.seen[row.id] !== addr) {","atom":"a1449","author":"speed","version":150},{"n":221,"text":"\t\tstate.seen[row.id] = addr;","atom":"a1449","author":"speed","version":150},{"n":222,"text":"\t\tnote(state, { t: now, kind: \"use\", id: row.id, addr });","atom":"a1449","author":"speed","version":150},{"n":223,"text":"\t\tdirty = true;","atom":"a1449","author":"speed","version":150},{"n":224,"text":"\t}","atom":"a1449","author":"speed","version":150},{"n":225,"text":"\tif (row.lastUsed === null || now - row.lastUsed >= 60_000) {","atom":"a1449","author":"speed","version":150},{"n":226,"text":"\t\trow.lastUsed = now;","atom":"a1449","author":"speed","version":150},{"n":227,"text":"\t\tdirty = true;","atom":"a1449","author":"speed","version":150},{"n":228,"text":"\t}","atom":"a1449","author":"speed","version":150},{"n":229,"text":"\treturn { ok: true, id: row.id, as: `key ${row.id}`, role: row.role ?? \"reader\", dirty };","atom":"a1449","author":"speed","version":150},{"n":230,"text":"}","atom":"a1449","author":"speed","version":150},{"n":231,"text":"","atom":"a1449","author":"speed","version":150},{"n":232,"text":"/** Project routes only an owner's key may use, whatever the method. */","atom":"a1449","author":"speed","version":150},{"n":233,"text":"const OWNER_ONLY = new Set([\"git-remote\", \"git-sync\", \"export\"]);","atom":"a1449","author":"speed","version":150},{"n":234,"text":"","atom":"a1449","author":"speed","version":150},{"n":235,"text":"/**","atom":"a1449","author":"speed","version":150},{"n":236,"text":" * Whether a key's role allows a request. A reader only reads. A writer also writes, but only on its own agent's","atom":"a1449","author":"speed","version":150},{"n":237,"text":" * routes (/a/:agent/..., where checkKey has matched the agent), and blob uploads, which a record needs. Owner-only","atom":"a1449","author":"speed","version":150},{"n":238,"text":" * routes need an owner's key.","atom":"a1449","author":"speed","version":150},{"n":239,"text":" */","atom":"a1449","author":"speed","version":150},{"n":240,"text":"export function keyAllows(","atom":"a1449","author":"speed","version":150},{"n":241,"text":"\trole: KeyRole,","atom":"a1449","author":"speed","version":150},{"n":242,"text":"\trequest: { write: boolean; agent: string | undefined; route: string | undefined; method?: string },","atom":"a1449","author":"speed","version":150},{"n":243,"text":"): { ok: true } | { ok: false; status: number; error: string } {","atom":"a1449","author":"speed","version":150},{"n":244,"text":"\tif (role === \"owner\") return { ok: true };","atom":"a1449","author":"speed","version":150},{"n":245,"text":"\tif (request.route !== undefined && OWNER_ONLY.has(request.route))","atom":"a1449","author":"speed","version":150},{"n":246,"text":"\t\treturn { ok: false, status: 403, error: `${request.route} needs an owner's key` };","atom":"a1449","author":"speed","version":150},{"n":247,"text":"\tif (!request.write) return { ok: true };","atom":"a1449","author":"speed","version":150},{"n":248,"text":"\tif (role === \"reader\") return { ok: false, status: 403, error: \"this key only reads\" };","atom":"a1449","author":"speed","version":150},{"n":249,"text":"\tif (request.method === \"PUT\" && /^blobs\\/[0-9a-f]{64}$/.test(request.route ?? \"\")) return { ok: true };","atom":"a1449","author":"speed","version":150},{"n":250,"text":"\tif (request.agent === undefined)","atom":"a1449","author":"speed","version":150},{"n":251,"text":"\t\treturn { ok: false, status: 403, error: \"a writer's key writes only on its own agent's routes\" };","atom":"a1449","author":"speed","version":150},{"n":252,"text":"\treturn { ok: true };","atom":"a1449","author":"speed","version":150},{"n":253,"text":"}","atom":"a1449","author":"speed","version":150}],"atoms":[{"atom":"a1449","author":"speed","message":"","time":1791906712454,"timeIso":"2026-10-13T15:51:52.454Z","publish":{"draft":null,"version":150,"agent":"speed","message":"Open server: the project Worker, Durable Object and Artifacts (issue 17)","time":1791907522179,"timeIso":"2026-10-13T16:05:22.179Z"}}]}