Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 28 additions & 1 deletion packages/code/src/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'), [
Expand All @@ -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',
Expand Down
7 changes: 6 additions & 1 deletion packages/code/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
},
];
}
Expand Down
50 changes: 50 additions & 0 deletions packages/code/src/native-sandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }));
Expand Down
Loading