A sandbox's git helper answers a host of its own mind alone
Jonas Templestein, 2026-11-26 13:13 UTC · 175d66101f209a7cd22b07ba130d970cb011c1e0 · parent 917cc37
3 files changed
M core/lib/src/cli/cli.ts
@@ -47,6 +47,7 @@ gitCredential, type GitCredentialIo, gitHostOf, + isHostOfMind, mindOfGitHost, personIsThere, writeGitConfig, @@ -240,8 +241,11 @@ }, // in a sandbox of this mind: the mind's own token for the repo, by the sandbox's own session ...(ownSandboxOf(process.env, address) && { - async repoToken(repo) { + async repoToken(repo, host) { using connection = await connectMind({ address }); + const mindUrl = await connection.itx.url(); + if (!isHostOfMind(host, mindUrl)) + throw new Error(`${host} is no host of this mind (${mindUrl}), so it gets no token`); const token = await connection.itx.cfArtifacts.get(repo).createToken("write", 300); return token.plaintext; }, @@ -638,6 +642,12 @@ // in a sandbox of the mind there is nobody to sign in: the helper asks the mind itself const told = process.env.ITERATE_MIND?.trim().toLowerCase() || ""; if (!input.mind && ownSandboxOf(process.env, told)) { + using connection = await connectMind({ address: told }); + const mindUrl = await connection.itx.url(); + if (!isHostOfMind(host, mindUrl)) + throw new Error( + `${host} is no host of this sandbox's mind (${mindUrl}): a sandbox's git is set up for its own mind's host alone.`, + ); writeGitConfig({ host, helper: gitHelperCommandOf(told), namesRepo: true }); return { message: `Git is set up for https://${host} in this sandbox, with no sign-in: it asks ${told} for each repo's token.`,
M core/lib/src/cli/git.test.ts
@@ -15,6 +15,7 @@ gitCredential, gitHostOf, HOST_TOKEN_LEAST_LEFT_MS, + isHostOfMind, mindOfGitHost, personIsThere, repoOfGitPath, @@ -64,7 +65,8 @@ const minted: string[] = []; const io = { ...issuer.io, - repoToken: async (repo: string) => { + repoToken: async (repo: string, host: string) => { + expect(host).toBe("git.example.com"); minted.push(repo); if (repo === "/repos/away") throw new Error("no such repo"); return `token-of-${repo}`; @@ -105,6 +107,16 @@ ]).toEqual(["git.example.com", "git.example.com", "example.com"]); for (const typed of ["localhost", "http://git.example.com", "git.example.com:8443"]) expect(() => gitHostOf(typed)).toThrow("no https host"); +}); + +test("a host of the mind is its apex or a name under it, and nothing that only looks like it", () => { + const mind = "https://example.com"; + expect( + ["git.example.com", "example.com", "GIT.Example.com", "git.example.com.evil.test", "evilexample.com", "git.other.test"].map( + (host) => isHostOfMind(host, mind), + ), + ).toEqual([true, true, true, false, false, false]); + expect(isHostOfMind("git.example.com", "not an address")).toBe(false); }); test("the path that git names is a repo of the mind, or none", () => {
M core/lib/src/cli/git.ts
@@ -26,7 +26,9 @@ // for a repo as the mind's other code does (`itx.cfArtifacts`). So the helper answers git a write // token for the one repo that git names, which lasts five minutes and is kept nowhere, and the // host passes it on. `setup` there has git name the repo to the helper, and signs nobody in. A -// sandbox whose requests the mind filters has no such session, and so no git. +// sandbox whose requests the mind filters has no such session, and so no git. THE TOKEN GOES TO A +// HOST OF THE MIND ALONE (`isHostOfMind`, asked of the mind at each use and at the setup): a +// helper that was set for any other host answers it nothing. import { spawnSync } from "node:child_process"; import { z } from "zod"; import { isCodingAgent } from "./coding-agent.ts"; @@ -155,9 +157,19 @@ say(line: string): void; /** IN A SANDBOX OF THE MIND ALONE: a write token of the mind for the one repo at `repo` * (`/repos/<name>`), through the sandbox's own session. Absent on any other computer. */ - repoToken?(repo: string): Promise<string>; + repoToken?(repo: string, host: string): Promise<string>; }; +/** Whether `host` is a host of the mind whose own address is `mindUrl` (`itx.url()`, its apex): + * that hostname, or a name under it. A sandbox's helper hands a repo's token to no other host: + * git asks the helper for whatever host its config names. */ +export function isHostOfMind(host: string, mindUrl: string): boolean { + if (!URL.canParse(mindUrl)) return false; + const apex = new URL(mindUrl).hostname.toLowerCase(); + const asked = host.toLowerCase(); + return asked === apex || asked.endsWith(`.${apex}`); +} + /** The repo that git names to the helper as `path` (`repos/config.git`), as the mind names it * (`/repos/config`), or null for what is no repo's path. */ export function repoOfGitPath(path: string | undefined): string | null { @@ -201,7 +213,7 @@ const repo = repoOfGitPath(fields.path); if (!repo) return stop(`git named no repo of ${fields.host} to its helper.`); try { - return credentialOf(await input.io.repoToken(repo)); + return credentialOf(await input.io.repoToken(repo, fields.host)); } catch (error) { input.io.say( `iterate: this sandbox got no token for ${repo} from ${input.address}: ${error instanceof Error ? error.message : String(error)}`,