The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(cli): an unusable token answer is revoked, and a GitHub App GitHub refuses fails a start at once (#2396)
harness-for-autonomous[bot]dfb1783663d2
diff --git a/cli/src/superTabs/github.spec.ts b/cli/src/superTabs/github.spec.tsindex 330e029a3..5da637629 100644--- a/cli/src/superTabs/github.spec.ts+++ b/cli/src/superTabs/github.spec.ts@@ -91,6 +91,20 @@ describe('a local agent\'s GitHub token', () => {expect(hosts()).toBe('{}\n')})+ // Security review of #2390: a server whose app GitHub refuses (backend github/app.ts `misconfigured`: GitHub answered 401+ // to the app's own credentials) answers a 503 the backend decided, unlike GitHub not answering (GITHUB_UNAVAILABLE).+ it('empties the gh login when GitHub refuses the server\'s app, and keeps it while GitHub does not answer', async () => {+ let reply = issued('ghs_fixtureToken0000004')+ const { core, hosts } = setup((method) => method === 'GET' ? ok(hostForm([row('tab-1', 'agent-1')])) : reply)+ await githubToken(core, 'harness-1', sourcesOf(core)())+ reply = refuse(503, 'GITHUB_UNAVAILABLE')+ await githubToken(core, 'harness-1', sourcesOf(core)())+ expect(hosts()).toContain('ghs_fixtureToken0000004')+ reply = refuse(503, 'GITHUB_APP_MISCONFIGURED')+ expect(await githubToken(core, 'harness-1', sourcesOf(core)())).toMatchObject({ error: 'GITHUB_APP_MISCONFIGURED', status: 503 })+ expect(hosts()).toBe('{}\n')+ })+it('refuses a harness that is not running here, one in no Swarm, and a reply without a token', async () => {const { core } = setup(method => method === 'GET' ? ok(hostForm([])) : ok({}))expect(await githubToken(core, 'harness-9', sourcesOf(core)())).toMatchObject({ error: 'AGENT_UNAVAILABLE' })diff --git a/cli/src/superTabs/github.ts b/cli/src/superTabs/github.tsindex cdd830d78..c3a0cec73 100644--- a/cli/src/superTabs/github.ts+++ b/cli/src/superTabs/github.ts@@ -27,9 +27,12 @@ function githubDir(core: CoreApi, session: Session | undefined): string | null {return skill?.github ? swarmGitHubSessionDir(core.dataDir, skill.key) : null}-/** Refusals that mean the session must not keep a token: anything the backend decided, and no app on the server. A- * backend that could not be reached decides nothing, and an unexpired token stays. */-export const decided = (error: SuperTabHostError) => (error.status >= 400 && error.status < 500) || error.code === 'GITHUB_APP_NOT_CONFIGURED'+/** Refusals that mean the session must not keep a token: anything the backend decided, no app on the server, and an app+ * GitHub refuses (backend github/app.ts `misconfigured`: GitHub answered 401 to the app's own credentials; security review+ * of #2390). A backend that could not be reached, or GitHub not answering it (GITHUB_UNAVAILABLE), decides nothing, and an+ * unexpired token stays. */+const DECIDED_5XX = new Set(['GITHUB_APP_NOT_CONFIGURED', 'GITHUB_APP_MISCONFIGURED'])+export const decided = (error: SuperTabHostError) => (error.status >= 400 && error.status < 500) || DECIDED_5XX.has(error.code)/*** The token of [harnessId]'s task: asked as each Swarm membership this machine's roster lists for that harness (one,diff --git a/cli/src/superTabs/machines.spec.ts b/cli/src/superTabs/machines.spec.tsindex 640c60b3d..f4bf9efcc 100644--- a/cli/src/superTabs/machines.spec.ts+++ b/cli/src/superTabs/machines.spec.ts@@ -260,7 +260,9 @@ describe('contributed machine executor', () => {it('fails at once when the backend refuses it or the server has no GitHub App', async () => {for (const refusal of [new SuperTabHostError('FORBIDDEN', 'This start is not this machine\'s.', 403),- new SuperTabHostError('GITHUB_APP_NOT_CONFIGURED', 'This server has no GitHub App.', 503)]) {+ new SuperTabHostError('GITHUB_APP_NOT_CONFIGURED', 'This server has no GitHub App.', 503),+ // Security review of #2390: GitHub refused the server's app, which the backend decided; it waited a minute.+ new SuperTabHostError('GITHUB_APP_MISCONFIGURED', 'GitHub refused the Harness GitHub App\'s credentials.', 503)]) {const h = teamHost(() => { throw refusal })await h.pass()expect(h.failures()).toEqual([{ operationId: 'prepare-failed-start', command: { type: 'agent.startFailed', startId: 'start',diff --git a/cli/src/superTabs/projectFolder.spec.ts b/cli/src/superTabs/projectFolder.spec.tsindex 1ec2e7ddd..c0803cfa5 100644--- a/cli/src/superTabs/projectFolder.spec.ts+++ b/cli/src/superTabs/projectFolder.spec.ts@@ -183,13 +183,40 @@ describe('preparing another person\'s start with the Harness GitHub App', () =>expect([f.prepare.mock.calls.length, f.fetch.mock.calls.length, f.revoke.mock.calls.length]).toEqual([0, 0, 0])}for (const error of [new SuperTabHostError('FORBIDDEN', 'Not this machine\'s start.', 403), new SuperTabHostError('UPDATE_REQUIRED', 'Update Harness.', 426),- new SuperTabHostError('GITHUB_APP_NOT_CONFIGURED', 'No GitHub App.', 503)]) {+ new SuperTabHostError('GITHUB_APP_NOT_CONFIGURED', 'No GitHub App.', 503), new SuperTabHostError('GITHUB_APP_MISCONFIGURED', 'GitHub refused the app.', 503)]) {const thrown = await hosted(error).ready(true).catch(caught => caught)expect([thrown instanceof PrepareRetryError, thrown.message]).toEqual([false,`This machine could not get a Harness GitHub App token for this project, so it was not prepared with its owner's own GitHub login either: ${error.message}`])}})+ // Security review of #2390: an answer with a token but no repository or requester failed the start and left the token good+ // for its hour. A token in an answer that is not used is revoked, never logged, and the start still fails.+ it('revokes a token the server sent in an answer it cannot use, and still fails the start', async () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})+ for (const answer of [{ github: true, token: TOKEN, repository: 'example/project', requestedBy: {} }, { github: true, token: TOKEN, requestedBy: { email: '[email protected]' } },+ { github: 'yes', token: TOKEN, repository: 'example/project', requestedBy: { email: '[email protected]' } }]) {+ const f = hosted(answer)+ await expect(f.ready(true)).rejects.toThrow('answered without a GitHub token')+ expect([f.revoke.mock.calls, f.prepare.mock.calls.length, f.fetch.mock.calls.length]).toEqual([[[TOKEN]], 0, 0])+ }+ // The owner's own project with a token beside its answer: prepared with the owner's login, the token revoked.+ const own = hosted({ github: false, token: TOKEN })+ expect(await own.ready()).toBeNull()+ expect(own.revoke.mock.calls).toEqual([[TOKEN]])+ // A revocation GitHub refuses still fails the start for its answer, and says so without the token.+ const refused = hosted({ github: true, token: TOKEN })+ refused.revoke.mockRejectedValueOnce(new Error('GitHub answered 500 to revoking the token'))+ await expect(refused.ready(true)).rejects.toThrow('answered without a GitHub token')+ expect(warn.mock.calls.map(([line]) => String(line))).toEqual(['[super-tabs] could not revoke the clone token: GitHub answered 500 to revoking the token'])+ expect(JSON.stringify(warn.mock.calls)).not.toContain(TOKEN)+ // No token in it: nothing to revoke.+ const none = hosted({ github: true })+ await expect(none.ready(true)).rejects.toThrow('answered without a GitHub token')+ expect(none.revoke).not.toHaveBeenCalled()+ warn.mockRestore()+ })+// Kept out of this change by its Lead (10-30): a default branch that could not be fetched never fails or holds a start; the// worktree starts from what the checkout has, as before.it('starts from what the checkout has when the default branch could not be fetched', async () => {diff --git a/cli/src/superTabs/projectFolder.ts b/cli/src/superTabs/projectFolder.tsindex eb6ad1b50..605fcdf4f 100644--- a/cli/src/superTabs/projectFolder.ts+++ b/cli/src/superTabs/projectFolder.ts@@ -30,7 +30,8 @@ export class PrepareRetryError extends Error {}* Anything the backend refuses is thrown, to fail the start; a call it gave no answer to, as a PrepareRetryError: never this* machine's own login instead.*/-export async function startGitHub(core: CoreApi, row: Json, start: Json, deps: { backend: Backend; team: boolean }): Promise<StartGitHub | null> {+export async function startGitHub(core: CoreApi, row: Json, start: Json,+ deps: { backend: Backend; team: boolean; revoke?: (token: string) => Promise<void> }): Promise<StartGitHub | null> {const machineId = core.daemon.machineId()if (row.githubApp !== true) {if (deps.team) throw new Error(NO_APP_ON_TEAM)@@ -46,12 +47,20 @@ export async function startGitHub(core: CoreApi, row: Json, start: Json, deps: {if (error instanceof SuperTabHostError && decided(error)) throw new Error(said)throw new PrepareRetryError(error instanceof SuperTabHostError ? `${said} (${error.code})` : said)}- if (answer.github === false && !deps.team) return nullconst requester = object(answer.requestedBy).email- if (answer.github !== true || typeof answer.token !== 'string' || typeof answer.repository !== 'string' || typeof requester !== 'string' || !requester) {- throw new Error('The Harness server answered without a GitHub token for this project.')+ if (answer.github === true && typeof answer.token === 'string' && typeof answer.repository === 'string' && typeof requester === 'string' && requester) {+ return { token: answer.token, repository: answer.repository, requester }}- return { token: answer.token, repository: answer.repository, requester }+ // A token in an answer that is not used is revoked, never logged: one that came without its repository or requester failed+ // the start and stayed good for its hour (security review of #2390).+ if (typeof answer.token === 'string') await revokeQuietly(answer.token, deps.revoke)+ if (answer.github === false && !deps.team) return null+ throw new Error('The Harness server answered without a GitHub token for this project.')+}++/** Revokes a read-only clone token; one GitHub will not revoke now still expires within the hour, and fails nothing. */+async function revokeQuietly(token: string, revoke = revokeGitHubToken): Promise<void> {+ await revoke(token).catch(error => console.warn(`[super-tabs] could not revoke the clone token: ${(error as Error).message}`))}/**@@ -73,7 +82,7 @@ export async function prepareSwarmStart(core: CoreApi, row: Json, start: Json,}} finally {// The read-only token is done with: the agent gets a token of its own for its task (superTabs/github.ts).- if (github) await (deps.revoke ?? revokeGitHubToken)(github.token).catch(error => console.warn(`[super-tabs] could not revoke the clone token: ${(error as Error).message}`))+ if (github) await revokeQuietly(github.token, deps.revoke)}return github ? { requester: github.requester } : null}