mirror of
https://github.com/openai/codex-security.git
synced 2026-11-26 01:25:21 +08:00
Support GitHub Enterprise Cloud checkout origins and host-aware routing, share Git remote parsing between the Action and SDK, and preserve packaged modules and GitLab IPv6 authorities. Integrate main while retaining its Action dependency patches, publication changes, and validation fixes.
245 lines
15 KiB
TypeScript
245 lines
15 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { mkdtemp, realpath, writeFile, mkdir, rm, symlink } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { dirname, join } from 'node:path';
|
|
import { parseInputs } from '../src/inputs.js';
|
|
import { resolveTarget, validateEvent, git, gitEnvironment, type EventContext } from '../src/targets.js';
|
|
|
|
async function fixture(t: any) {
|
|
const path = await realpath(await mkdtemp(join(tmpdir(),'action-target-')));
|
|
t.after(() => rm(path,{recursive:true,force:true}));
|
|
const run = (...args: string[]) => execFileSync('/usr/bin/git', ['-c','user.name=Test','-c','user.email=test@example.invalid',...args], {cwd:path,env:gitEnvironment(),encoding:'utf8',stdio:['ignore','pipe','pipe']}).trim();
|
|
run('init','-b','main'); run('remote','add','origin','https://github.com/example/repo.git');
|
|
await mkdir(join(path,'src')); await writeFile(join(path,'src/app.ts'),'export const n = 1;\n');
|
|
await writeFile(join(path,'SECURITY.md'),'Only authorized boundaries count.\n');
|
|
run('add','.'); run('commit','-m','base'); const base = run('rev-parse','HEAD');
|
|
const change = async (file = 'src/app.ts', text = 'export const n = 2;\n') => {
|
|
await writeFile(join(path,file),text); run('add','.'); run('commit','-m','change'); return run('rev-parse','HEAD');
|
|
};
|
|
const event = (head: string, eventName = 'pull_request'): EventContext => ({eventName, repository:'example/repo',
|
|
sha:head, ref:eventName === 'pull_request' ? 'refs/pull/7/merge' : 'refs/heads/main',serverUrl:'https://github.com',
|
|
payload:{number:7,pull_request:{number:7,head:{sha:head,repo:{full_name:'example/repo'}},base:{sha:base,repo:{full_name:'example/repo'}}}}});
|
|
const inputs = (values: Record<string,string>={scope:'diff'}) => parseInputs(key=>values[key]??'',path);
|
|
return {path,run,base,change,event,inputs};
|
|
}
|
|
test('Git children retain the runner process-tracking marker', async t => {
|
|
const f = await fixture(t);
|
|
const previous = process.env.RUNNER_TRACKING_ID;
|
|
t.after(() => {
|
|
if (previous === undefined) delete process.env.RUNNER_TRACKING_ID;
|
|
else process.env.RUNNER_TRACKING_ID = previous;
|
|
});
|
|
process.env.RUNNER_TRACKING_ID = 'synthetic-git-runner-tracking';
|
|
f.run('config', 'alias.capture-tracking', '!printf "%s" "$RUNNER_TRACKING_ID"');
|
|
assert.equal(await git(f.path, ['capture-tracking']), 'synthetic-git-runner-tracking');
|
|
});
|
|
test('PR target resolves merge base and preserves exact upload head identity', async t => {
|
|
const f=await fixture(t); const head=await f.change();
|
|
f.run('checkout','-b','updated-base',f.base);
|
|
const updatedBase=await f.change('SECURITY.md','Updated policy.\n');
|
|
f.run('checkout','--detach',head);
|
|
const event=f.event(head);
|
|
event.payload.pull_request.base.sha=updatedBase;
|
|
const target=await resolveTarget(f.inputs(),event);
|
|
assert.equal(target.diffBase,f.base); assert.equal(target.diffHead,head);
|
|
assert.equal(target.analysisRef,'refs/pull/7/head'); assert.equal(target.publishable,true);
|
|
});
|
|
test('GitHub Cloud checkout origins match the event host for HTTPS and SSH', async t => {
|
|
const f = await fixture(t); const head = await f.change();
|
|
for (const [host, sshUser] of [['github.com', 'git'], ['example-enterprise.ghe.com', 'example-enterprise']]) {
|
|
for (const origin of [
|
|
`https://${host}/example/repo.git`, `https://${host}:443/example/repo`,
|
|
`https://${host.toUpperCase()}/Example/Repo.git/`,
|
|
`${sshUser}@${host}:example/repo.git`, `${sshUser}@${host.toUpperCase()}:Example/Repo/`,
|
|
`ssh://${sshUser}@${host}/example/repo`, `ssh://${sshUser}@${host.toUpperCase()}:22/Example/Repo.git/`,
|
|
]) {
|
|
f.run('remote', 'set-url', 'origin', origin);
|
|
for (const eventName of ['schedule', 'pull_request']) {
|
|
const event = {...f.event(head, eventName), serverUrl:`https://${host}`};
|
|
const target = await resolveTarget(f.inputs({scope:eventName === 'schedule' ? 'repository' : 'diff'}), event);
|
|
assert.equal(target.scannedSha, head); assert.equal(target.publishable, true);
|
|
assert.equal(target.analysisRef, eventName === 'schedule' ? 'refs/heads/main' : 'refs/pull/7/head');
|
|
assert.equal(target.diffBase, eventName === 'schedule' ? undefined : f.base);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
test('GitHub.com checkout origins support its SSH-over-443 endpoint', async t => {
|
|
const f = await fixture(t);
|
|
f.run('remote', 'set-url', 'origin', 'ssh://git@ssh.github.com:443/example/repo.git');
|
|
const target = await resolveTarget(f.inputs({scope:'repository'}), f.event(f.base, 'schedule'));
|
|
assert.equal(target.scannedSha, f.base);
|
|
assert.equal(target.publishable, true);
|
|
|
|
const enterpriseEvent = {...f.event(f.base), serverUrl:'https://example-enterprise.ghe.com'};
|
|
await assert.rejects(resolveTarget(f.inputs(), enterpriseEvent), /origin does not match/);
|
|
for (const origin of ['https://ssh.github.com/example/repo.git', 'ssh://git@ssh.github.com:22/example/repo.git',
|
|
'ssh://git@ssh.github.com:443/other/repo.git', 'ssh://other@ssh.github.com:443/example/repo.git']) {
|
|
f.run('remote', 'set-url', 'origin', origin);
|
|
await assert.rejects(resolveTarget(f.inputs(), f.event(f.base)), /origin does not match/);
|
|
}
|
|
});
|
|
test('SCP checkout paths remain literal when compared with the triggering repository', async t => {
|
|
const f = await fixture(t);
|
|
for (const origin of ['git@github.com:example/repo.git#suffix', 'git@github.com:example/repo.git?suffix',
|
|
'git@github.com:other/../example/repo.git']) {
|
|
f.run('remote', 'set-url', 'origin', origin);
|
|
await assert.rejects(resolveTarget(f.inputs(), f.event(f.base)), /origin does not match/);
|
|
}
|
|
});
|
|
test('unsupported server URLs still fail eligibility', async t => {
|
|
const f = await fixture(t);
|
|
for (const serverUrl of ['', 'https://github.example.invalid', 'http://example-enterprise.ghe.com',
|
|
'https://ghe.com', 'https://example-enterprise.ghe.com.example.invalid', 'https://example-enterprise-ghe.com']) {
|
|
assert.throws(() => validateEvent({...f.event(f.base), serverUrl}), /supports GitHub.com and GitHub Enterprise Cloud/);
|
|
}
|
|
});
|
|
test('enterprise checkout origins cannot select another host or repository', async t => {
|
|
const f = await fixture(t);
|
|
const event = {...f.event(f.base), serverUrl:'https://example-enterprise.ghe.com'};
|
|
for (const origin of ['https://github.com/example/repo.git', 'https://other-enterprise.ghe.com/example/repo.git',
|
|
'other-enterprise@other-enterprise.ghe.com:example/repo.git', 'https://example-enterprise.ghe.com/other/repo.git',
|
|
'example-enterprise@example-enterprise.ghe.com:other/repo.git']) {
|
|
f.run('remote', 'set-url', 'origin', origin);
|
|
await assert.rejects(resolveTarget(f.inputs(), event), /origin does not match/);
|
|
}
|
|
f.run('remote', 'set-url', 'origin', 'https://example-enterprise.ghe.com/example/repo.git');
|
|
await assert.rejects(resolveTarget(f.inputs(), f.event(f.base)), /origin does not match/);
|
|
});
|
|
test('manual diff scans resolve an explicit base against the checked-out head', async t => {
|
|
const f=await fixture(t);
|
|
f.run('tag','scan-base');
|
|
const head=await f.change();
|
|
const target=await resolveTarget(f.inputs({scope:'diff','diff-base':'scan-base'}),f.event(head,'workflow_dispatch'));
|
|
assert.equal(target.diffBase,f.base); assert.equal(target.diffHead,head);
|
|
assert.equal(target.scannedSha,head); assert.equal(target.emptyDiff,false);
|
|
});
|
|
test('large diffs are nonempty without collecting every changed pathname', async t => {
|
|
const f = await fixture(t);
|
|
const blob = f.run('rev-parse', `${f.base}:src/app.ts`);
|
|
const entries = Array.from({length:20_000}, (_, index) =>
|
|
`100644 blob ${blob}\tsrc-${String(index).padStart(5, '0')}-${'x'.repeat(210)}.ts\n`).join('');
|
|
const tree = execFileSync('/usr/bin/git', ['mktree'], {
|
|
cwd:f.path, env:gitEnvironment(), input:entries, encoding:'utf8',
|
|
}).trim();
|
|
const base = f.run('commit-tree', tree, '-p', f.base, '-m', 'Synthetic large diff base');
|
|
const head = f.run('commit-tree', f.run('rev-parse', `${f.base}^{tree}`), '-p', base, '-m', 'Synthetic large diff head');
|
|
f.run('reset', '--hard', head);
|
|
for (const eventName of ['pull_request', 'workflow_dispatch']) {
|
|
const event = f.event(head, eventName);
|
|
event.payload.pull_request.base.sha = base;
|
|
const inputs = f.inputs(eventName === 'pull_request' ? {scope:'diff'} : {scope:'diff', 'diff-base':base});
|
|
const target = await resolveTarget(inputs, event);
|
|
assert.equal(target.diffBase, base);
|
|
assert.equal(target.diffHead, head);
|
|
assert.equal(target.emptyDiff, false);
|
|
}
|
|
});
|
|
test('empty diff is a verified no-op and schedule has no PR dependency', async t => {
|
|
const f=await fixture(t);
|
|
assert.equal((await resolveTarget(f.inputs(),f.event(f.base))).emptyDiff,true);
|
|
const target=await resolveTarget(f.inputs({scope:'repository'}),f.event(f.base,'schedule'));
|
|
assert.equal(target.analysisRef,'refs/heads/main'); assert.equal(target.diffBase,undefined);
|
|
});
|
|
test('repository scans accept file and directory paths inside the checkout', async t => {
|
|
const f=await fixture(t);
|
|
const inputs=f.inputs({scope:'repository',paths:'src\nsrc/app.ts'});
|
|
const target=await resolveTarget(inputs,f.event(f.base,'schedule'));
|
|
assert.equal(target.scannedSha,f.base);
|
|
});
|
|
test('scan paths must stay inside the checkout without traversing symlinks', async t => {
|
|
const f=await fixture(t);
|
|
const outside=await fixture(t);
|
|
await symlink(outside.path,join(f.path,'linked-src'));
|
|
f.run('add','.'); f.run('commit','-m','linked source');
|
|
const head=f.run('rev-parse','HEAD');
|
|
const event=f.event(head,'schedule');
|
|
await assert.rejects(resolveTarget({...f.inputs({scope:'repository'}),paths:[outside.path]},event),/escapes the repository/);
|
|
await assert.rejects(resolveTarget(f.inputs({scope:'repository',paths:'linked-src/src/app.ts'}),event),/must not traverse symlinks/);
|
|
});
|
|
test('wrong checkout, local changes, missing diff base and option revisions are refused',async t=>{
|
|
const f=await fixture(t); const head=await f.change();
|
|
await assert.rejects(resolveTarget(f.inputs(),f.event(f.base)),/must check out/);
|
|
await assert.rejects(resolveTarget(f.inputs({scope:'repository'}),f.event(f.base,'schedule')),/HEAD must match GITHUB_SHA/);
|
|
await assert.rejects(resolveTarget(f.inputs(),f.event(head,'workflow_dispatch')),/diff-base is required/);
|
|
await assert.rejects(resolveTarget(f.inputs({scope:'diff','diff-base':'--help'}),f.event(head)),/Git rev-parse failed/);
|
|
await writeFile(join(f.path,'src/app.ts'),'dirty');
|
|
await assert.rejects(resolveTarget(f.inputs(),f.event(head)),/local changes/);
|
|
await assert.rejects(resolveTarget(f.inputs({scope:'repository'}),f.event(head,'schedule')),/local changes/);
|
|
});
|
|
test('forks and privileged events fail eligibility',async t=>{
|
|
const f=await fixture(t); const event=f.event(f.base);
|
|
event.payload.pull_request.head.repo.full_name='fork/repo'; assert.throws(()=>validateEvent(event),/Fork PR/);
|
|
for(const eventName of ['pull_request_target','workflow_run']) assert.throws(()=>validateEvent({...event,eventName}),/Unsupported event/);
|
|
});
|
|
test('persisted Git credentials and origin userinfo are refused without revealing secrets',async t=>{
|
|
const f=await fixture(t);
|
|
f.run('config','http.https://github.com/.extraheader','AUTHORIZATION: bearer CANARY');
|
|
await assert.rejects(resolveTarget(f.inputs(),f.event(f.base)),error=> /persist-credentials/.test(String(error))&&!String(error).includes('CANARY'));
|
|
f.run('config','--unset','http.https://github.com/.extraheader');
|
|
f.run('remote','set-url','origin','https://CANARY@github.com/example/repo.git');
|
|
await assert.rejects(resolveTarget(f.inputs(),f.event(f.base)),error=>/origin/.test(String(error))&&!String(error).includes('CANARY'));
|
|
});
|
|
test('enterprise checkouts retain the Git credential protections', async t => {
|
|
const f = await fixture(t);
|
|
const event = {...f.event(f.base), serverUrl:'https://example-enterprise.ghe.com'};
|
|
f.run('remote', 'set-url', 'origin', `${event.serverUrl}/example/repo.git`);
|
|
const key = `http.${event.serverUrl}/.extraheader`;
|
|
f.run('config', key, 'AUTHORIZATION: bearer synthetic-token');
|
|
await assert.rejects(resolveTarget(f.inputs(), event), /persist-credentials/);
|
|
f.run('config', '--unset', key);
|
|
for (const origin of ['https://synthetic-token@example-enterprise.ghe.com/example/repo.git',
|
|
'https://example-enterprise:synthetic-token@example-enterprise.ghe.com/example/repo.git',
|
|
'ssh://example-enterprise:synthetic-token@example-enterprise.ghe.com/example/repo.git']) {
|
|
f.run('remote', 'set-url', 'origin', origin);
|
|
await assert.rejects(resolveTarget(f.inputs(), event), /origin does not match/);
|
|
}
|
|
});
|
|
|
|
test('worktree configuration is optional and clean linked checkouts are accepted', async t => {
|
|
const f = await fixture(t);
|
|
f.run('config', 'extensions.worktreeConfig', 'true');
|
|
assert.equal((await resolveTarget(f.inputs(), f.event(f.base))).scannedSha, f.base);
|
|
const root = await mkdtemp(join(tmpdir(), 'action-linked-worktree-'));
|
|
t.after(() => rm(root, {recursive:true, force:true}));
|
|
const checkout = join(root, 'checkout');
|
|
f.run('worktree', 'add', '--detach', checkout, f.base);
|
|
f.run('-C', checkout, 'config', '--worktree', 'core.editor', 'synthetic-editor');
|
|
const inputs = {...f.inputs({scope:'repository'}), repository:checkout};
|
|
const target = await resolveTarget(inputs, f.event(f.base, 'schedule'));
|
|
assert.equal(target.repository, await realpath(checkout));
|
|
assert.equal(target.scannedSha, f.base);
|
|
for (const [key, value] of [
|
|
['http.https://github.com/.extraheader', 'AUTHORIZATION: bearer synthetic-worktree-key'],
|
|
['include.path', join(f.path, 'synthetic-external-config')],
|
|
]) {
|
|
f.run('-C', checkout, 'config', '--worktree', key, value);
|
|
await assert.rejects(resolveTarget(inputs, f.event(f.base, 'schedule')), /persist-credentials/);
|
|
f.run('-C', checkout, 'config', '--worktree', '--unset', key);
|
|
}
|
|
});
|
|
|
|
test('Git failures preserve diagnostic details', async t => {
|
|
const f = await fixture(t);
|
|
await assert.rejects(git(f.path, ['show', 'missing-synthetic-revision']), /unknown revision|ambiguous argument/);
|
|
});
|
|
|
|
|
|
test('selected paths require literal filesystem entries without glob expansion', async t => {
|
|
const f = await fixture(t);
|
|
const paths = ['src/[slug]/page.tsx', 'src/star*file.ts', 'src/question?file.ts'];
|
|
for (const path of [...paths, 'src/s/page.tsx', 'src/starOtherfile.ts', 'src/questionXfile.ts']) {
|
|
await mkdir(dirname(join(f.path, path)), {recursive:true});
|
|
await writeFile(join(f.path, path), 'export const synthetic = 1;\n');
|
|
}
|
|
f.run('add', '.'); f.run('commit', '-m', 'Synthetic literal paths');
|
|
const head = f.run('rev-parse', 'HEAD');
|
|
const event = f.event(head, 'schedule');
|
|
const target = await resolveTarget(f.inputs({scope:'repository', paths:[...paths, 'src/[slug]'].join('\n')}), event);
|
|
assert.equal(target.scannedSha, head);
|
|
await assert.rejects(resolveTarget(f.inputs({scope:'repository', paths:'src/[sl]/page.tsx'}), event), /ENOENT/);
|
|
await assert.rejects(resolveTarget(f.inputs({scope:'repository', paths:'src/*.tsx'}), event), /ENOENT/);
|
|
});
|