{"file":"client/src/web-session.ts","version":318,"total":141,"from":1,"to":141,"lines":[{"n":1,"text":"// abso login --web: a staging website session for the person a box is signed in as, opened in an agent's own browser, with","atom":"a2925","author":"box-grants","version":302},{"n":2,"text":"// no secret in the agent's hands. The website's /login/handoff takes this box's sign-in (or a test account's PAT, from a file)","atom":"a2925","author":"box-grants","version":302},{"n":3,"text":"// and answers a one-use link that works for a minute; opening it signs that browser in. Only the link is printed: never the","atom":"a2925","author":"box-grants","version":302},{"n":4,"text":"// box's sign-in, the PAT or the session cookie, which the website sets in the browser itself.","atom":"a2925","author":"box-grants","version":302},{"n":5,"text":"","atom":"a2925","author":"box-grants","version":302},{"n":6,"text":"import { readFileSync } from \"node:fs\";","atom":"a2925","author":"box-grants","version":302},{"n":7,"text":"import { UsageError } from \"./args.ts\";","atom":"a2925","author":"box-grants","version":302},{"n":8,"text":"","atom":"a2925","author":"box-grants","version":302},{"n":9,"text":"/** The website beside each staging server. Prod has none here: agents never sign in to the website on prod. */","atom":"a2925","author":"box-grants","version":302},{"n":10,"text":"const STAGING_SITES: Readonly<Record<string, string>> = {","atom":"a2925","author":"box-grants","version":302},{"n":11,"text":"\t\"https://abso-staging.chrisazy.workers.dev\": \"https://abso-website-staging.chrisazy.workers.dev\",","atom":"a2925","author":"box-grants","version":302},{"n":12,"text":"\t\"https://avcs-staging.chrisazy.workers.dev\": \"https://abso-website-staging.chrisazy.workers.dev\",","atom":"a2925","author":"box-grants","version":302},{"n":13,"text":"};","atom":"a2925","author":"box-grants","version":302},{"n":14,"text":"","atom":"a2925","author":"box-grants","version":302},{"n":15,"text":"/** What the website answers: the link, when it stops working, and who and until when it signs in. */","atom":"a2925","author":"box-grants","version":302},{"n":16,"text":"export interface Handoff {","atom":"a2925","author":"box-grants","version":302},{"n":17,"text":"\treadonly url: string;","atom":"a2925","author":"box-grants","version":302},{"n":18,"text":"\treadonly expiresAt: string;","atom":"a2925","author":"box-grants","version":302},{"n":19,"text":"\treadonly account: string;","atom":"a2925","author":"box-grants","version":302},{"n":20,"text":"\treadonly name: string;","atom":"a2925","author":"box-grants","version":302},{"n":21,"text":"\treadonly sessionEnds: string;","atom":"a2925","author":"box-grants","version":302},{"n":22,"text":"}","atom":"a2925","author":"box-grants","version":302},{"n":23,"text":"","atom":"a2925","author":"box-grants","version":302},{"n":24,"text":"/**","atom":"a2940","author":"box-grants","version":306},{"n":25,"text":" * The website a handoff's credential goes to (Security condition 6, Code Review): the staging website beside the folder's own","atom":"a2945","author":"box-grants","version":306},{"n":26,"text":" * staging server, from the fixed list above, and never an origin from a flag. A server that isn't on the list is refused,","atom":"a2945","author":"box-grants","version":306},{"n":27,"text":" * prod included. --site is only for a test account's PAT on this machine (http://localhost or 127.0.0.1); a box's sign-in","atom":"a2945","author":"box-grants","version":306},{"n":28,"text":" * takes no --site at all.","atom":"a2945","author":"box-grants","version":306},{"n":29,"text":" */","atom":"a2940","author":"box-grants","version":306},{"n":30,"text":"export function siteFor(server: string, site: string | undefined, kind: \"box\" | \"pat\"): string {","atom":"a2945","author":"box-grants","version":306},{"n":31,"text":"\tconst origin = new URL(server).origin;","atom":"a2925","author":"box-grants","version":302},{"n":32,"text":"\tconst known = STAGING_SITES[origin];","atom":"a2925","author":"box-grants","version":302},{"n":33,"text":"\tif (known === undefined)","atom":"a2925","author":"box-grants","version":302},{"n":34,"text":"\t\tthrow new UsageError(","atom":"a2925","author":"box-grants","version":302},{"n":35,"text":"\t\t\t`abso login --web is for staging: ${origin} has no staging website beside it that abso knows, and agents never sign in to the website on prod. Nothing was sent.`,","atom":"a2945","author":"box-grants","version":306},{"n":36,"text":"\t\t);","atom":"a2925","author":"box-grants","version":302},{"n":37,"text":"\tif (site === undefined) return known;","atom":"a2940","author":"box-grants","version":306},{"n":38,"text":"\tif (kind === \"box\")","atom":"a2945","author":"box-grants","version":306},{"n":39,"text":"\t\tthrow new UsageError(`--site isn't taken with a box's sign-in, which goes only to ${known}. Nothing was sent.`);","atom":"a2953","author":"box-grants","version":306},{"n":40,"text":"\tlet url: URL | null = null;","atom":"a2945","author":"box-grants","version":306},{"n":41,"text":"\ttry {","atom":"a2940","author":"box-grants","version":306},{"n":42,"text":"\t\turl = new URL(site);","atom":"a2940","author":"box-grants","version":306},{"n":43,"text":"\t} catch {","atom":"a2940","author":"box-grants","version":306},{"n":44,"text":"\t\t// Not an address: refused below.","atom":"a2945","author":"box-grants","version":306},{"n":45,"text":"\t}","atom":"a2940","author":"box-grants","version":306},{"n":46,"text":"\tif (url === null || url.protocol !== \"http:\" || (url.hostname !== \"localhost\" && url.hostname !== \"127.0.0.1\"))","atom":"a2945","author":"box-grants","version":306},{"n":47,"text":"\t\tthrow new UsageError(","atom":"a2945","author":"box-grants","version":306},{"n":48,"text":"\t\t\t\"--site, with --pat-file, names this machine only (http://localhost:<port> or http://127.0.0.1:<port>). Nothing was sent.\",","atom":"a2945","author":"box-grants","version":306},{"n":49,"text":"\t\t);","atom":"a2945","author":"box-grants","version":306},{"n":50,"text":"\treturn url.origin;","atom":"a2945","author":"box-grants","version":306},{"n":51,"text":"}","atom":"a2945","author":"box-grants","version":306},{"n":52,"text":"","atom":"a2945","author":"box-grants","version":306},{"n":53,"text":"/**","atom":"a2945","author":"box-grants","version":306},{"n":54,"text":" * abso login --web: where the credential would go is settled before it is read, so a refusal reads and sends nothing","atom":"a2945","author":"box-grants","version":306},{"n":55,"text":" * (Security). `credential` reads the box's sign-in or the PAT only once the website is one it may go to.","atom":"a2945","author":"box-grants","version":306},{"n":56,"text":" */","atom":"a2945","author":"box-grants","version":306},{"n":57,"text":"export async function webLogin(options: {","atom":"a2945","author":"box-grants","version":306},{"n":58,"text":"\treadonly server: string;","atom":"a2945","author":"box-grants","version":306},{"n":59,"text":"\treadonly site: string | undefined;","atom":"a2945","author":"box-grants","version":306},{"n":60,"text":"\t/** A box's sign-in, or a test account's PAT (--pat-file). */","atom":"a2947","author":"box-grants","version":306},{"n":61,"text":"\treadonly kind: \"box\" | \"pat\";","atom":"a2947","author":"box-grants","version":306},{"n":62,"text":"\treadonly credential: () => string;","atom":"a2947","author":"box-grants","version":306},{"n":63,"text":"\treadonly fetcher?: typeof fetch;","atom":"a2945","author":"box-grants","version":306},{"n":64,"text":"}): Promise<{ handoff: Handoff; site: string }> {","atom":"a2945","author":"box-grants","version":306},{"n":65,"text":"\tconst site = siteFor(options.server, options.site, options.kind);","atom":"a2947","author":"box-grants","version":306},{"n":66,"text":"\treturn { handoff: await webHandoff(site, options.credential(), options.fetcher), site };","atom":"a2947","author":"box-grants","version":306},{"n":67,"text":"}","atom":"a2925","author":"box-grants","version":302},{"n":68,"text":"","atom":"a2925","author":"box-grants","version":302},{"n":69,"text":"/** A PAT from the file --pat-file names: read here, never printed. */","atom":"a2925","author":"box-grants","version":302},{"n":70,"text":"export function patFromFile(path: string): string {","atom":"a2925","author":"box-grants","version":302},{"n":71,"text":"\tlet text: string;","atom":"a2925","author":"box-grants","version":302},{"n":72,"text":"\ttry {","atom":"a2925","author":"box-grants","version":302},{"n":73,"text":"\t\ttext = readFileSync(path, \"utf8\").trim();","atom":"a2925","author":"box-grants","version":302},{"n":74,"text":"\t} catch {","atom":"a2925","author":"box-grants","version":302},{"n":75,"text":"\t\tthrow new UsageError(`--pat-file names ${path}, which abso can't read`);","atom":"a2925","author":"box-grants","version":302},{"n":76,"text":"\t}","atom":"a2925","author":"box-grants","version":302},{"n":77,"text":"\tif (!/^abso_pat_[A-Za-z0-9_-]{16,200}$/.test(text))","atom":"a2928","author":"box-grants","version":302},{"n":78,"text":"\t\tthrow new UsageError(`--pat-file names ${path}, which doesn't hold a PAT`);","atom":"a2928","author":"box-grants","version":302},{"n":79,"text":"\treturn text;","atom":"a2925","author":"box-grants","version":302},{"n":80,"text":"}","atom":"a2925","author":"box-grants","version":302},{"n":81,"text":"","atom":"a2925","author":"box-grants","version":302},{"n":82,"text":"/** Words from the website, with any secret this call sent taken out. */","atom":"a2925","author":"box-grants","version":302},{"n":83,"text":"function scrubbed(text: string, secret: string): string {","atom":"a2925","author":"box-grants","version":302},{"n":84,"text":"\treturn text.split(secret).join(\"[the credential]\").slice(0, 300);","atom":"a2925","author":"box-grants","version":302},{"n":85,"text":"}","atom":"a2925","author":"box-grants","version":302},{"n":86,"text":"","atom":"a2925","author":"box-grants","version":302},{"n":87,"text":"/** Asks the website for a handoff link with `credential` (a box sign-in or a PAT) in Authorization. */","atom":"a2925","author":"box-grants","version":302},{"n":88,"text":"export async function webHandoff(site: string, credential: string, fetcher: typeof fetch = fetch): Promise<Handoff> {","atom":"a2925","author":"box-grants","version":302},{"n":89,"text":"\tlet res: Response;","atom":"a2925","author":"box-grants","version":302},{"n":90,"text":"\ttry {","atom":"a2925","author":"box-grants","version":302},{"n":91,"text":"\t\tres = await fetcher(`${site}/login/handoff`, {","atom":"a2925","author":"box-grants","version":302},{"n":92,"text":"\t\t\tmethod: \"POST\",","atom":"a2925","author":"box-grants","version":302},{"n":93,"text":"\t\t\theaders: { authorization: `Bearer ${credential}`, accept: \"application/json\" },","atom":"a2925","author":"box-grants","version":302},{"n":94,"text":"\t\t\tredirect: \"error\",","atom":"a2925","author":"box-grants","version":302},{"n":95,"text":"\t\t\tsignal: AbortSignal.timeout(30_000),","atom":"a2925","author":"box-grants","version":302},{"n":96,"text":"\t\t});","atom":"a2925","author":"box-grants","version":302},{"n":97,"text":"\t} catch (err) {","atom":"a2925","author":"box-grants","version":302},{"n":98,"text":"\t\tthrow new UsageError(`couldn't reach ${site}: ${scrubbed((err as Error).message, credential)}`);","atom":"a2925","author":"box-grants","version":302},{"n":99,"text":"\t}","atom":"a2925","author":"box-grants","version":302},{"n":100,"text":"\tconst text = await res.text().catch(() => \"\");","atom":"a2925","author":"box-grants","version":302},{"n":101,"text":"\tlet body: Partial<Handoff> & { error?: unknown } = {};","atom":"a2925","author":"box-grants","version":302},{"n":102,"text":"\ttry {","atom":"a2925","author":"box-grants","version":302},{"n":103,"text":"\t\tbody = JSON.parse(text) as typeof body;","atom":"a2925","author":"box-grants","version":302},{"n":104,"text":"\t} catch {","atom":"a2925","author":"box-grants","version":302},{"n":105,"text":"\t\t// Not JSON: the status says enough.","atom":"a2925","author":"box-grants","version":302},{"n":106,"text":"\t}","atom":"a2925","author":"box-grants","version":302},{"n":107,"text":"\tif (!res.ok) {","atom":"a2925","author":"box-grants","version":302},{"n":108,"text":"\t\tconst why = typeof body.error === \"string\" ? body.error : `it answered ${res.status}`;","atom":"a2925","author":"box-grants","version":302},{"n":109,"text":"\t\tthrow new UsageError(","atom":"a2925","author":"box-grants","version":302},{"n":110,"text":"\t\t\tres.status === 404","atom":"a2925","author":"box-grants","version":302},{"n":111,"text":"\t\t\t\t? `${site} doesn't hand out sign-in links (its key sign-in is off, or it isn't staging)`","atom":"a2925","author":"box-grants","version":302},{"n":112,"text":"\t\t\t\t: `${site} refused: ${scrubbed(why, credential)}`,","atom":"a2925","author":"box-grants","version":302},{"n":113,"text":"\t\t);","atom":"a2925","author":"box-grants","version":302},{"n":114,"text":"\t}","atom":"a2925","author":"box-grants","version":302},{"n":115,"text":"\tif (","atom":"a2925","author":"box-grants","version":302},{"n":116,"text":"\t\ttypeof body.url !== \"string\" ||","atom":"a2925","author":"box-grants","version":302},{"n":117,"text":"\t\t!body.url.startsWith(`${site}/login/handoff#`) ||","atom":"a2925","author":"box-grants","version":302},{"n":118,"text":"\t\ttypeof body.expiresAt !== \"string\" ||","atom":"a2925","author":"box-grants","version":302},{"n":119,"text":"\t\ttypeof body.account !== \"string\"","atom":"a2925","author":"box-grants","version":302},{"n":120,"text":"\t)","atom":"a2925","author":"box-grants","version":302},{"n":121,"text":"\t\tthrow new UsageError(`${site} answered something that isn't a sign-in link`);","atom":"a2925","author":"box-grants","version":302},{"n":122,"text":"\treturn {","atom":"a2925","author":"box-grants","version":302},{"n":123,"text":"\t\turl: body.url,","atom":"a2925","author":"box-grants","version":302},{"n":124,"text":"\t\texpiresAt: body.expiresAt,","atom":"a2925","author":"box-grants","version":302},{"n":125,"text":"\t\taccount: body.account,","atom":"a2925","author":"box-grants","version":302},{"n":126,"text":"\t\tname: typeof body.name === \"string\" ? body.name : body.account,","atom":"a2925","author":"box-grants","version":302},{"n":127,"text":"\t\tsessionEnds: typeof body.sessionEnds === \"string\" ? body.sessionEnds : body.expiresAt,","atom":"a2925","author":"box-grants","version":302},{"n":128,"text":"\t};","atom":"a2925","author":"box-grants","version":302},{"n":129,"text":"}","atom":"a2925","author":"box-grants","version":302},{"n":130,"text":"","atom":"a2925","author":"box-grants","version":302},{"n":131,"text":"/** What abso login --web says: the one link, how long it works, who it signs in as, and what it reaches (Security). */","atom":"a2937","author":"box-grants","version":304},{"n":132,"text":"export function handoffText(h: Handoff, site: string, boxed = true, now = Date.now()): string {","atom":"a2937","author":"box-grants","version":304},{"n":133,"text":"\tconst seconds = Math.max(0, Math.round((Date.parse(h.expiresAt) - now) / 1000));","atom":"a2925","author":"box-grants","version":302},{"n":134,"text":"\tconst reach = boxed","atom":"a2937","author":"box-grants","version":304},{"n":135,"text":"\t\t? \"only to the projects this box was granted, at most as a writer, until the box is revoked or ends\"","atom":"a2937","author":"box-grants","version":304},{"n":136,"text":"\t\t: \"at most as a writer\";","atom":"a2937","author":"box-grants","version":304},{"n":137,"text":"\treturn [","atom":"a2925","author":"box-grants","version":302},{"n":138,"text":"\t\t`Open this link in your browser within ${seconds} seconds. It signs that browser in to ${site} as ${h.name}, until ${h.sessionEnds}, and works once. It reaches ${reach}:`,","atom":"a2937","author":"box-grants","version":304},{"n":139,"text":"\t\th.url,","atom":"a2925","author":"box-grants","version":302},{"n":140,"text":"\t].join(\"\\n\");","atom":"a2925","author":"box-grants","version":302},{"n":141,"text":"}","atom":"a2925","author":"box-grants","version":302}],"atoms":[{"atom":"a2925","author":"box-grants","message":"","time":1792172479405,"timeIso":"2026-10-16T17:41:19.405Z","publish":{"draft":null,"version":302,"agent":"box-grants","message":"abso login --web: a staging website session in an agent's own browser with no secret in its hands. It sends this box's sign-in (or, with --pat-file, a test account's PAT read from that file) to the staging website's /login/handoff and prints only the one-use link it answers, which signs whichever browser opens it in as that person, within a minute. Prod has no website for it: agents never sign in there. Tests: client/test/web-session.test.ts.","time":1792172641933,"timeIso":"2026-10-16T17:44:01.933Z"}},{"atom":"a2928","author":"box-grants","message":"","time":1792172641491,"timeIso":"2026-10-16T17:44:01.491Z","publish":{"draft":null,"version":302,"agent":"box-grants","message":"abso login --web: a staging website session in an agent's own browser with no secret in its hands. It sends this box's sign-in (or, with --pat-file, a test account's PAT read from that file) to the staging website's /login/handoff and prints only the one-use link it answers, which signs whichever browser opens it in as that person, within a minute. Prod has no website for it: agents never sign in there. Tests: client/test/web-session.test.ts.","time":1792172641933,"timeIso":"2026-10-16T17:44:01.933Z"}},{"atom":"a2937","author":"box-grants","message":"","time":1792174936266,"timeIso":"2026-10-16T18:22:16.266Z","publish":{"draft":null,"version":304,"agent":"box-grants","message":"abso login --web says what the link reaches: a box's only the projects the box was granted, at most as a writer, until the box is revoked or ends (abso 1464, website 1125, Security); a PAT's at most as a writer. The help says the same, and that it writes issues, replies, answers, groups and announcements and changes no members, roles, visibility, keys, boxes or settings. Tests: client/test/web-session.test.ts (4).","time":1792174936899,"timeIso":"2026-10-16T18:22:16.899Z"}},{"atom":"a2940","author":"box-grants","message":"","time":1792178451261,"timeIso":"2026-10-16T19:20:51.261Z","publish":{"draft":null,"version":306,"agent":"box-grants","message":"abso login --web sends a credential only to the staging website beside the folder's own staging server (Security condition 6, Code Review's Medium on 302). The pairing is the fixed list in web-session.ts; any other server, prod included, is refused and nothing is sent. A box's sign-in takes no --site at all. --site is only for --pat-file, and only http://localhost or 127.0.0.1. webLogin settles the website before it reads the credential, so a refusal reads and sends nothing. Tests: client/test/web-session.test.ts (5), including that every refused case reads no credential and sends no request; tsc exits 0.","time":1792178581623,"timeIso":"2026-10-16T19:23:01.623Z"}},{"atom":"a2945","author":"box-grants","message":"","time":1792178512041,"timeIso":"2026-10-16T19:21:52.041Z","publish":{"draft":null,"version":306,"agent":"box-grants","message":"abso login --web sends a credential only to the staging website beside the folder's own staging server (Security condition 6, Code Review's Medium on 302). The pairing is the fixed list in web-session.ts; any other server, prod included, is refused and nothing is sent. A box's sign-in takes no --site at all. --site is only for --pat-file, and only http://localhost or 127.0.0.1. webLogin settles the website before it reads the credential, so a refusal reads and sends nothing. Tests: client/test/web-session.test.ts (5), including that every refused case reads no credential and sends no request; tsc exits 0.","time":1792178581623,"timeIso":"2026-10-16T19:23:01.623Z"}},{"atom":"a2947","author":"box-grants","message":"","time":1792178522798,"timeIso":"2026-10-16T19:22:02.798Z","publish":{"draft":null,"version":306,"agent":"box-grants","message":"abso login --web sends a credential only to the staging website beside the folder's own staging server (Security condition 6, Code Review's Medium on 302). The pairing is the fixed list in web-session.ts; any other server, prod included, is refused and nothing is sent. A box's sign-in takes no --site at all. --site is only for --pat-file, and only http://localhost or 127.0.0.1. webLogin settles the website before it reads the credential, so a refusal reads and sends nothing. Tests: client/test/web-session.test.ts (5), including that every refused case reads no credential and sends no request; tsc exits 0.","time":1792178581623,"timeIso":"2026-10-16T19:23:01.623Z"}},{"atom":"a2953","author":"box-grants","message":"","time":1792178581121,"timeIso":"2026-10-16T19:23:01.121Z","publish":{"draft":null,"version":306,"agent":"box-grants","message":"abso login --web sends a credential only to the staging website beside the folder's own staging server (Security condition 6, Code Review's Medium on 302). The pairing is the fixed list in web-session.ts; any other server, prod included, is refused and nothing is sent. A box's sign-in takes no --site at all. --site is only for --pat-file, and only http://localhost or 127.0.0.1. webLogin settles the website before it reads the credential, so a refusal reads and sends nothing. Tests: client/test/web-session.test.ts (5), including that every refused case reads no credential and sends no request; tsc exits 0.","time":1792178581623,"timeIso":"2026-10-16T19:23:01.623Z"}}]}