Skip to content

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.

fix(swarm): accept GitHub's stateless installation tokens, and redact them (#1817)

0xken1549e2e2a9c8d3
diff --git a/backend/src/superTabs/credentials.test.ts b/backend/src/superTabs/credentials.test.ts
index f0f561ab1..025ae3005 100644
--- a/backend/src/superTabs/credentials.test.ts
+++ b/backend/src/superTabs/credentials.test.ts
@@ -22,6 +22,8 @@ const fake = {
jwe: () => [b64url({ alg: 'RSA-OAEP', enc: 'A256GCM' }), noise(40, B64URL, 2), noise(16, B64URL, 3), noise(30, B64URL, 4), noise(22, B64URL, 5)].join('.'),
harness: (prefix: string, seed = 11) => `${prefix}_${noise(43, B64URL, seed)}`,
github: (prefix: string, seed = 12) => `${prefix}_${noise(36, ALNUM, seed)}`,
+ // GitHub's stateless installation token: ghs_, the app id, and a signed JWT of about 520 characters in all.
+ githubStateless: () => `ghs_123456_${fake.jwt(20)}${noise(380, B64URL, 21)}`,
githubPat: () => ['github', 'pat', `${noise(22, ALNUM, 13)}_${noise(59, ALNUM, 14)}`].join('_'),
anthropic: () => ['sk', 'ant', 'api03', noise(93, B64URL, 15)].join('-'),
openai: (kind = '') => ['sk', ...(kind ? [kind] : []), noise(48, ALNUM, 16)].join('-'),
@@ -49,6 +51,7 @@ describe('findCredentials: every kind it removes', () => {
['github', 'a GitHub user-to-server token', fake.github('ghu')],
['github', 'a GitHub server-to-server token', fake.github('ghs')],
['github', 'a GitHub refresh token', fake.github('ghr')],
+ ['github', 'a stateless GitHub App installation token', fake.githubStateless()],
['github', 'a fine-grained GitHub token', fake.githubPat()],
['anthropic', 'an Anthropic key', fake.anthropic()],
['openai', 'a legacy OpenAI key', fake.openai()],
@@ -114,6 +117,7 @@ describe('findCredentials: every kind it removes', () => {
expect(redactCredentials(`GITHUB_TOKEN=${fake.github('ghp')}`)).toEqual({ text: `GITHUB_TOKEN=${CREDENTIAL_MARKER}`, removed: ['github'] })
expect(redactCredentials(`Authorization: Bearer ${fake.jwt()}`)).toEqual({ text: `Authorization: Bearer ${CREDENTIAL_MARKER}`, removed: ['jwt'] })
expect(redactCredentials(`git clone https://x-access-token:${fake.github('ghs')}@github.com/example/repo`).removed).toEqual(['github'])
+ expect(redactCredentials(`GH_TOKEN=${fake.githubStateless()}`)).toEqual({ text: `GH_TOKEN=${CREDENTIAL_MARKER}`, removed: ['github'] })
expect(redactCredentials(`aws_access_key_id = ${fake.awsKeyId()}`).removed).toEqual(['aws-key-id'])
})
diff --git a/backend/src/superTabs/credentials.ts b/backend/src/superTabs/credentials.ts
index 077df0282..c90c6e911 100644
--- a/backend/src/superTabs/credentials.ts
+++ b/backend/src/superTabs/credentials.ts
@@ -89,6 +89,9 @@ const ASSIGN = String.raw`["']?[ \t]*(?:=>|:=|[:=])[ \t]*["'\x60]?`
/** Most specific first: a GitHub token inside `token=…` is a GitHub token. */
const RULES: Rule[] = [
+ // A stateless GitHub App installation token, ghs_APPID_ and a JWT: before the JWT rule, which would miss it.
+ { kind: 'github', pattern: /(?<![A-Za-z0-9_])ghs_\d+_[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}(?![A-Za-z0-9_-])/dg,
+ accept: token => random(token.slice(token.lastIndexOf('.') + 1)) },
{ kind: 'jwt', pattern: /(?<![A-Za-z0-9_\-.+/])[A-Za-z0-9_-]{10,}(?:\.[A-Za-z0-9_-]*){2,5}(?![A-Za-z0-9_\-+/=])/dg, accept: token => {
// A full stop after the token ends the sentence; three segments are signed (JWS), five encrypted (JWE).
const trimmed = token.endsWith('.') ? token.slice(0, -1) : token
diff --git a/cli/src/lib/agentHandoff.spec.ts b/cli/src/lib/agentHandoff.spec.ts
index 20e7b5400..f1a781d97 100644
--- a/cli/src/lib/agentHandoff.spec.ts
+++ b/cli/src/lib/agentHandoff.spec.ts
@@ -137,6 +137,7 @@ describe('redactHandoffSecrets', () => {
const removed: Array<[string, string]> = [
['a GitHub token', `ghp_${'A'.repeat(36)}`],
['a fine-grained GitHub token', 'github_pat_11ABCDEFG0123456789_abcdefghijklmnop'],
+ ['a stateless GitHub App installation token', `ghs_123456_eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiIxMjM0NTYifQ.${'s'.repeat(342)}-x_y`],
['an AWS key id', 'AKIAIOSFODNN7EXAMPLE'],
['a Slack token', 'xoxb-1234567890-abcdefghij'],
['a Google API key', `AIza${'b'.repeat(35)}`],
diff --git a/cli/src/lib/agentHandoff.ts b/cli/src/lib/agentHandoff.ts
index 60a829bc6..03e945c31 100644
--- a/cli/src/lib/agentHandoff.ts
+++ b/cli/src/lib/agentHandoff.ts
@@ -183,7 +183,7 @@ function blankOrphanKeyBodies(text: string): string {
export function redactHandoffSecrets(text: string): string {
return blankOrphanKeyBodies(text
.replace(/-----BEGIN [A-Z0-9 ]+-----[\s\S]*?(?:-----END [A-Z0-9 ]+-----|$)/g, '-----BEGIN <redacted>-----'))
- .replace(/\b(?:gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})/g, '<redacted>')
+ .replace(/\b(?:ghs_\d+_[A-Za-z0-9_-]{8,}(?:\.[A-Za-z0-9_-]{8,}){2}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})/g, '<redacted>')
.replace(/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g, '<redacted>')
.replace(/\bxox[a-z]-[A-Za-z0-9-]{10,}/g, '<redacted>')
.replace(/\bAIza[0-9A-Za-z_-]{35}/g, '<redacted>')
diff --git a/cli/src/lib/swarmGitHub.spec.ts b/cli/src/lib/swarmGitHub.spec.ts
index 518925720..6e50f4b1d 100644
--- a/cli/src/lib/swarmGitHub.spec.ts
+++ b/cli/src/lib/swarmGitHub.spec.ts
@@ -13,6 +13,8 @@ let made = 0
const fresh = (name: string) => { const dir = join(root, `${name}-${made++}`); mkdirSync(dir, { recursive: true }); return dir }
const home = fresh('home')
const script = (file: string, body: string) => { writeFileSync(file, `#!/bin/sh\n${body}\n`); chmodSync(file, 0o755); return file }
+/** GitHub's stateless installation token: ghs_, the app id, and a JWT, about 520 characters with dots and dashes. */
+const STATELESS = `ghs_123456_eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiIxMjM0NTYifQ.${'s'.repeat(470)}-x_y`
const git = (cwd: string, args: string[], env: Record<string, string> = {}, input?: string) => spawnSync('git', args, {
cwd, input, encoding: 'utf8', env: { PATH: process.env.PATH!, HOME: home, GIT_CONFIG_NOSYSTEM: '1', GIT_AUTHOR_NAME: 'Fixture', GIT_AUTHOR_EMAIL: '[email protected]',
GIT_COMMITTER_NAME: 'Fixture', GIT_COMMITTER_EMAIL: '[email protected]', ...env },
@@ -71,6 +73,8 @@ describe('the shared helper and hooks', () => {
expect(run(['ls-remote', '--get-url', 'https://github.com/acme/app.git']).stdout.trim()).toBe('https://github.com/acme/app.git')
expect(run(['ls-remote', '--get-url', '[email protected]:acme/app.git']).stdout.trim()).toBe('https://github.com/acme/app.git')
expect(run(['config', '--get-all', 'credential.helper']).stdout).not.toContain('owner-helper')
+ // GitHub's stateless installation token, ghs_APPID_ and a JWT of about 520 characters, is in GitHub's form.
+ expect(swarmGitPrepareEnv(STATELESS).HARNESS_SWARM_GIT_TOKEN).toBe(STATELESS)
// A token not in GitHub's form is refused before any git runs with it.
expect(() => swarmGitPrepareEnv('not a token; rm -rf /')).toThrow('not in the form GitHub gives')
})
@@ -177,6 +181,8 @@ describe('a session\'s gh login and trailers', () => {
expect(readFileSync(join(dir, 'gh', 'hosts.yml'), 'utf8')).toBe('{}\n')
// Trailers are kept: who the work is for does not change with the token.
expect(readFileSync(join(dir, 'trailers'), 'utf8')).toBe('Co-authored-by: Ada <[email protected]>\n')
+ writeSessionGitHub(dir, STATELESS)
+ expect(readFileSync(join(dir, 'gh', 'hosts.yml'), 'utf8')).toContain(` oauth_token: ${STATELESS}\n`)
writeSessionGitHub(dir, 'not a token\n oauth_token: other')
expect(readFileSync(join(dir, 'gh', 'hosts.yml'), 'utf8')).toBe('{}\n')
})
diff --git a/cli/src/lib/swarmGitHub.ts b/cli/src/lib/swarmGitHub.ts
index 9a1f5e506..ca5125e31 100644
--- a/cli/src/lib/swarmGitHub.ts
+++ b/cli/src/lib/swarmGitHub.ts
@@ -160,8 +160,9 @@ export function writeSwarmGitHubTools(tools: SwarmGitHubTools): { helper: string
}
const GH_CONFIG = 'version: "1"\n'
-/** gh's own token format, and nothing that could break out of the YAML around it. */
-const TOKEN = /^[A-Za-z0-9_]{20,255}$/
+/** GitHub's token formats, the stateless installation token (ghs_APPID_JWT, about 520 characters, with dots and dashes)
+ * among them, and nothing that could break out of the YAML around it or a shell. */
+const TOKEN = /^[A-Za-z0-9_][A-Za-z0-9_.-]{19,4095}$/
/** The session's gh login: [token] for github.com, or none. And, when given, the trailers its commits get. */
export function writeSessionGitHub(dir: string, token: string | null, trailers?: readonly string[]): void {