diff --git a/packages/code/README.md b/packages/code/README.md index f11e0072..94da7d0f 100644 --- a/packages/code/README.md +++ b/packages/code/README.md @@ -386,7 +386,14 @@ standard Git LFS filters; hosts using LFS must install `git-lfs`, and checkout fails instead of silently leaving pointer files when it is unavailable. SRT replaces each real credential with a sentinel inside the sandbox and substitutes the real value in its host proxy only for the corresponding Git or -GitHub API host. TLS termination is enabled for that substitution. The worker +GitHub API host. On GitHub.com, the CLI token is also substituted for the exact +`uploads.github.com` host used by native issue and PR attachments. This does not +authorize credential injection into arbitrary GitHub subdomains or asset-storage +hosts. Use a GitHub CLI version that supports `--attach`; GitHub still decides +whether the credential has permission to upload, including which GitHub Apps it +accepts. Enterprise host substitution is unchanged. + +TLS termination is enabled for that substitution. The worker restores the parent environment immediately after constructing the sandbox command; it never writes credentials into the repository, a remote URL, Git config, or the GitHub CLI credential store. diff --git a/packages/code/src/github.test.ts b/packages/code/src/github.test.ts index 5d401515..fa461485 100644 --- a/packages/code/src/github.test.ts +++ b/packages/code/src/github.test.ts @@ -982,7 +982,7 @@ test('restricts Git and GitHub CLI credential substitution to their respective h { name: 'GH_TOKEN', extract: '^(.+)$', - injectHosts: ['api.github.com'], + injectHosts: ['api.github.com', 'uploads.github.com'], }, ]); assert.deepEqual(gitHubMaskedCredentialVariables('github.example.test'), [ @@ -999,6 +999,33 @@ test('restricts Git and GitHub CLI credential substitution to their respective h ]); }); +test('upload authentication does not authorize other GitHub subdomains or external hosts', () => { + const variables = gitHubMaskedCredentialVariables('github.com'); + const cli = variables.find(variable => variable.name === 'GH_TOKEN')!; + const git = variables.find( + variable => variable.name === GITHUB_CREDENTIAL_ENV_NAME, + )!; + + assert.ok(GITHUB_ALLOWED_DOMAINS.includes('*.github.com')); + assert.ok( + cli.injectHosts.includes( + new URL('https://uploads.github.com/user-attachments/assets').hostname, + ), + ); + assert.ok(!git.injectHosts.includes('uploads.github.com')); + for (const host of [ + 'github.com', + '*.github.com', + 'untrusted.github.com', + 'uploads.github.com.attacker.test', + 'uploads.githubusercontent.com', + 'github-cloud.s3.amazonaws.com', + 'attacker.test', + ]) { + assert.ok(!cli.injectHosts.includes(host), host); + } +}); + test('composes the masked credential with SRT Git configuration inside the sandbox', () => { const wrapped = wrapGitHubCredentialCommand( 'git push', diff --git a/packages/code/src/github.ts b/packages/code/src/github.ts index 8519938f..6f0ed674 100644 --- a/packages/code/src/github.ts +++ b/packages/code/src/github.ts @@ -711,7 +711,12 @@ export function gitHubMaskedCredentialVariables(host: string): Array<{ { name: gitHubCliTokenEnvironmentName(host), extract: '^(.+)$', - injectHosts: [gitHubApiHost(host)], + // Egress permission for *.github.com does not restore masked credentials. + // gh sends native PR/issue attachments to this separate HTTPS origin. + injectHosts: + host === 'github.com' + ? [gitHubApiHost(host), 'uploads.github.com'] + : [gitHubApiHost(host)], }, ]; } diff --git a/packages/code/src/native-sandbox.test.ts b/packages/code/src/native-sandbox.test.ts index 4b31658d..3d716243 100644 --- a/packages/code/src/native-sandbox.test.ts +++ b/packages/code/src/native-sandbox.test.ts @@ -35,6 +35,11 @@ import { restoreScratchTraversal } from './native-scratch.js'; import { browserSetupAttemptPath, browserSetupProtectedPaths } from './browser-setup.js'; import { WorkspaceToolError } from './workspace.js'; import { isWorkspaceToolResult } from './protocol.js'; +import { + GITHUB_ALLOWED_DOMAINS, + gitHubCommandCredentialEnvironment, + gitHubMaskedCredentialVariables, +} from './github.js'; const request = { protocolVersion: 1 as const, @@ -1064,6 +1069,51 @@ test('masks a host credential for only its injection host and restores the paren }); }); +test('passes upload credential routing to SRT while keeping the GitHub token masked', async t => { + const root = await mkdtemp(join(tmpdir(), 'librechat-code-native-')); + t.after(() => rm(root, { recursive: true, force: true })); + const original = process.env.GH_TOKEN; + const token = 'ghs_test_only_not_a_real_credential'; + const fake = fakeManager({ + wrappedEnvironment: { GH_TOKEN: 'fake_value_upload_sentinel' }, + beforeWrap: async () => { + assert.equal(process.env.GH_TOKEN, token); + }, + }); + const sandbox = new NativeSrtWorkspaceCommandSandbox({ + workspaceRoot: root, + allowedDomains: [...GITHUB_ALLOWED_DOMAINS], + maskedEnvironment: { + variables: gitHubMaskedCredentialVariables('github.com'), + async resolve() { + return gitHubCommandCredentialEnvironment({ value: token }); + }, + }, + manager: fake.manager, + }); + t.after(() => sandbox.close()); + + const result = await sandbox.execute({ + ...request, + command: 'printf %s "$GH_TOKEN"', + }); + + assert.equal(result.exitCode, 0); + assert.equal(result.stdout, 'fake_value_upload_sentinel'); + assert.equal(process.env.GH_TOKEN, original); + assert.deepEqual( + fake.config?.credentials?.envVars?.find(variable => variable.name === 'GH_TOKEN'), + { + name: 'GH_TOKEN', + extract: '^(.+)$', + injectHosts: ['api.github.com', 'uploads.github.com'], + mode: 'mask', + onExtractNoMatch: 'error', + }, + ); + assert.deepEqual(fake.config?.network.tlsTerminate, {}); +}); + test('keeps trusted public command context out of credential masking', async t => { const root = await mkdtemp(join(tmpdir(), 'librechat-code-native-')); t.after(() => rm(root, { recursive: true, force: true }));