- MCP
- blame on this site's /mcp
- CLI
- abso blame client/src/web-session.ts --json
- JSON
- /christopher/abso-core/blame/client/src/web-session.ts?format=json
141 linesbox-grants 100%Last change box-grants in version 306,
141 lines from 7 changes in 3 versions.
box-grants302abso 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.Compare1// abso login --web: a staging website session for the person a box is signed in as, opened in an agent's own browser, with
2// 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)
3// and answers a one-use link that works for a minute; opening it signs that browser in. Only the link is printed: never the
4// box's sign-in, the PAT or the session cookie, which the website sets in the browser itself.
6import { readFileSync } from "node:fs";
7import { UsageError } from "./args.ts";
9/** The website beside each staging server. Prod has none here: agents never sign in to the website on prod. */
10const STAGING_SITES: Readonly<Record<string, string>> = {
11 "https://abso-staging.chrisazy.workers.dev": "https://abso-website-staging.chrisazy.workers.dev",
12 "https://avcs-staging.chrisazy.workers.dev": "https://abso-website-staging.chrisazy.workers.dev",
13};
15/** What the website answers: the link, when it stops working, and who and until when it signs in. */
16export interface Handoff {
17 readonly url: string;
18 readonly expiresAt: string;
19 readonly account: string;
20 readonly name: string;
21 readonly sessionEnds: string;
22}
box-grants306abso 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.Compare24/**
25 * The website a handoff's credential goes to (Security condition 6, Code Review): the staging website beside the folder's own
26 * staging server, from the fixed list above, and never an origin from a flag. A server that isn't on the list is refused,
27 * 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
28 * takes no --site at all.
29 */
30export function siteFor(server: string, site: string | undefined, kind: "box" | "pat"): string {
box-grants302abso 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.Compare31 const origin = new URL(server).origin;
32 const known = STAGING_SITES[origin];
33 if (known === undefined)
34 throw new UsageError(
box-grants306abso 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.Compare35 `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.`,
box-grants302abso 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.Compare36 );
box-grants306abso 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.Compare37 if (site === undefined) return known;
38 if (kind === "box")
39 throw new UsageError(`--site isn't taken with a box's sign-in, which goes only to ${known}. Nothing was sent.`);
40 let url: URL | null = null;
41 try {
42 url = new URL(site);
43 } catch {
44 // Not an address: refused below.
45 }
46 if (url === null || url.protocol !== "http:" || (url.hostname !== "localhost" && url.hostname !== "127.0.0.1"))
47 throw new UsageError(
48 "--site, with --pat-file, names this machine only (http://localhost:<port> or http://127.0.0.1:<port>). Nothing was sent.",
49 );
50 return url.origin;
51}
53/**
54 * abso login --web: where the credential would go is settled before it is read, so a refusal reads and sends nothing
55 * (Security). `credential` reads the box's sign-in or the PAT only once the website is one it may go to.
56 */
57export async function webLogin(options: {
58 readonly server: string;
59 readonly site: string | undefined;
60 /** A box's sign-in, or a test account's PAT (--pat-file). */
61 readonly kind: "box" | "pat";
62 readonly credential: () => string;
63 readonly fetcher?: typeof fetch;
64}): Promise<{ handoff: Handoff; site: string }> {
65 const site = siteFor(options.server, options.site, options.kind);
66 return { handoff: await webHandoff(site, options.credential(), options.fetcher), site };
box-grants302abso 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.Compare67}
69/** A PAT from the file --pat-file names: read here, never printed. */
70export function patFromFile(path: string): string {
71 let text: string;
72 try {
73 text = readFileSync(path, "utf8").trim();
74 } catch {
75 throw new UsageError(`--pat-file names ${path}, which abso can't read`);
76 }
77 if (!/^abso_pat_[A-Za-z0-9_-]{16,200}$/.test(text))
78 throw new UsageError(`--pat-file names ${path}, which doesn't hold a PAT`);
79 return text;
80}
82/** Words from the website, with any secret this call sent taken out. */
83function scrubbed(text: string, secret: string): string {
84 return text.split(secret).join("[the credential]").slice(0, 300);
85}
87/** Asks the website for a handoff link with `credential` (a box sign-in or a PAT) in Authorization. */
88export async function webHandoff(site: string, credential: string, fetcher: typeof fetch = fetch): Promise<Handoff> {
89 let res: Response;
90 try {
91 res = await fetcher(`${site}/login/handoff`, {
92 method: "POST",
93 headers: { authorization: `Bearer ${credential}`, accept: "application/json" },
94 redirect: "error",
95 signal: AbortSignal.timeout(30_000),
96 });
97 } catch (err) {
98 throw new UsageError(`couldn't reach ${site}: ${scrubbed((err as Error).message, credential)}`);
99 }
100 const text = await res.text().catch(() => "");
101 let body: Partial<Handoff> & { error?: unknown } = {};
102 try {
103 body = JSON.parse(text) as typeof body;
104 } catch {
105 // Not JSON: the status says enough.
106 }
107 if (!res.ok) {
108 const why = typeof body.error === "string" ? body.error : `it answered ${res.status}`;
109 throw new UsageError(
110 res.status === 404
111 ? `${site} doesn't hand out sign-in links (its key sign-in is off, or it isn't staging)`
112 : `${site} refused: ${scrubbed(why, credential)}`,
113 );
114 }
115 if (
116 typeof body.url !== "string" ||
117 !body.url.startsWith(`${site}/login/handoff#`) ||
118 typeof body.expiresAt !== "string" ||
119 typeof body.account !== "string"
120 )
121 throw new UsageError(`${site} answered something that isn't a sign-in link`);
122 return {
123 url: body.url,
124 expiresAt: body.expiresAt,
125 account: body.account,
126 name: typeof body.name === "string" ? body.name : body.account,
127 sessionEnds: typeof body.sessionEnds === "string" ? body.sessionEnds : body.expiresAt,
128 };
129}
box-grants304abso 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).Compare131/** What abso login --web says: the one link, how long it works, who it signs in as, and what it reaches (Security). */
132export function handoffText(h: Handoff, site: string, boxed = true, now = Date.now()): string {
box-grants302abso 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.Compare133 const seconds = Math.max(0, Math.round((Date.parse(h.expiresAt) - now) / 1000));
box-grants304abso 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).Compare134 const reach = boxed
135 ? "only to the projects this box was granted, at most as a writer, until the box is revoked or ends"
136 : "at most as a writer";
box-grants302abso 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.Compare137 return [
box-grants304abso 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).Compare138 `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}:`,
box-grants302abso 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.Compare139 h.url,
140 ].join("\n");
141}