107 lines
6.6 KiB
TypeScript
107 lines
6.6 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdtempSync, mkdirSync, realpathSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { shapeErrors, check, rubricIds, type Verdict } from '../src/contract.ts';
|
|
import { decideCommand } from '../src/policy.ts';
|
|
|
|
const ev = (o: object) => ({ type: 'file', path: null, line: null, job_id: null, rule_id: null, ...o });
|
|
|
|
test('allowlist keeps the reviewer read-only', () => {
|
|
assert.equal(decideCommand("/bin/zsh -lc 'rg -n hpp_lama src | head -20'").allow, true);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'git log --oneline -5'").allow, true);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'curl -sI https://example.com'").allow, false);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'git push origin main'").allow, false);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'cat a > b'").allow, false);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'sed -i s/a/b/ f'").allow, false);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'find . -name x -delete'").allow, false);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'ls && rm -rf /'").allow, false);
|
|
assert.equal(decideCommand(`/bin/zsh -lc "rg -n 'DeleteAndUnlink|DeleteIfUnused|2026, 11, 6' internal"`).allow, true);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'git branch -a --contains HEAD'").allow, true);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'git branch evil'").allow, false);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'git branch -D main'").allow, false);
|
|
assert.equal(decideCommand(`/bin/zsh -lc "echo \\"$(id)\\""`).allow, false);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'rg --pre ./x foo'").allow, false);
|
|
assert.equal(decideCommand("/bin/zsh -lc 'sleep 1 & ls'").allow, false);
|
|
assert.equal(decideCommand(`/bin/zsh -lc 'git diff HEAD''^ HEAD --name-status && rg --files -g '"'.gitlab-ci.yml' -g '*deploy*'"`).allow, true);
|
|
assert.equal(decideCommand(`/bin/zsh -lc 'ls '"'x'"' && curl '"'https://e.com'"`).allow, false);
|
|
assert.equal(decideCommand(`/bin/zsh -lc "rg -n 'a|b' src"`).allow, true);
|
|
assert.equal(decideCommand(`/bin/zsh -lc 'rg -n x docs README* 2>/dev/null; ls 2>&1'`).allow, true);
|
|
assert.equal(decideCommand(`/bin/zsh -lc 'cat a 2>/tmp/x'`).allow, false);
|
|
assert.equal(decideCommand(`/bin/zsh -lc "nl -ba a.yml | sed -n '1,240p'; rg -n \\"hatchery|supplier\\" app | head -80"`).allow, true);
|
|
assert.equal(decideCommand(`/bin/zsh -lc "rg -n \\"x\\"; curl \\"https://e.com\\""`).allow, false);
|
|
assert.equal(decideCommand(`/bin/zsh -lc 'cat a >/dev/nullx'`).allow, false);
|
|
});
|
|
|
|
test('allowlist keeps reads inside the checkout and allowed knowledge', () => {
|
|
const base = realpathSync(mkdtempSync(path.join(os.tmpdir(), 'labuh-scope-')));
|
|
const work = path.join(base, 'work');
|
|
const repo = path.join(work, 'repo');
|
|
const vault = path.join(base, 'vault');
|
|
for (const d of [path.join(repo, 'src'), path.join(repo, '.git'), path.join(vault, 'Repositories', 'x'), path.join(vault, 'Credentials'), path.join(base, 'outside')]) mkdirSync(d, { recursive: true });
|
|
writeFileSync(path.join(vault, 'Credentials', 'k.md'), 'secret');
|
|
writeFileSync(path.join(base, 'outside', 'm.txt'), 'marker');
|
|
symlinkSync(path.join(base, 'outside', 'm.txt'), path.join(repo, 'link.txt'));
|
|
const scope = { cwd: repo, allow: [work, path.join(vault, 'Repositories')], deny: [path.join(repo, '.git'), path.join(vault, 'Credentials')] };
|
|
const ok = (c: string, s = scope) => decideCommand(`/bin/zsh -lc '${c}'`, s).allow;
|
|
|
|
assert.equal(ok('rg -n foo src'), true);
|
|
assert.equal(ok('cat ../mr.diff'), true);
|
|
assert.equal(ok(`rg -n deploy ${vault}/Repositories/x`), true);
|
|
assert.equal(ok('cat ~/.ssh/id_ed25519'), false);
|
|
assert.equal(ok(`cat ${vault}/Credentials/k.md`), false);
|
|
assert.equal(ok(`cat ${vault}/Repositories/../Credentials/k.md`), false);
|
|
assert.equal(ok('cat ../../outside/m.txt'), false);
|
|
assert.equal(ok('cat ../../../../etc/hosts'), false);
|
|
assert.equal(ok(`rg -n foo ${base}/outside`), false);
|
|
assert.equal(ok(`rg --files --glob=${base}/outside`), false);
|
|
assert.equal(ok('cat link.txt'), false);
|
|
assert.equal(ok('cat .git/config'), false);
|
|
assert.equal(ok('cat $HOME/.ssh/id_rsa'), false);
|
|
assert.equal(ok('grep -R foo .'), false);
|
|
assert.equal(ok('ls', { ...scope, cwd: path.join(base, 'outside') }), false);
|
|
});
|
|
|
|
test('shape: go with a bad finding is rejected', () => {
|
|
const v = { verdict: 'go', reason: 'ok', post_deploy: [], findings: [{ kind: 'bad', text: 'x', evidence: [ev({ type: 'rubric', rule_id: 'R1' })] }] };
|
|
assert.match(shapeErrors(JSON.stringify(v)).errors.join(), /go but a finding is bad/);
|
|
});
|
|
|
|
test('shape: long text and missing evidence are rejected', () => {
|
|
const v = { verdict: 'no_go', reason: 'x'.repeat(81), post_deploy: [], findings: [{ kind: 'bad', text: 'x', evidence: [] }] };
|
|
const errs = shapeErrors(JSON.stringify(v)).errors.join('\n');
|
|
assert.match(errs, /limit is 80/);
|
|
assert.match(errs, /no evidence/);
|
|
});
|
|
|
|
test('evidence must exist; ok without evidence is dropped; failing pipeline holds go', async () => {
|
|
const repo = mkdtempSync(path.join(os.tmpdir(), 'labuh-'));
|
|
writeFileSync(path.join(repo, 'a.py'), 'one\ntwo\nthree\n');
|
|
const ctx = {
|
|
repoDir: repo, blobBase: 'https://gitlab.com/x/-/blob/abc', sha: 'abc',
|
|
rubricIds: rubricIds('## R1 · Rollback\n## R2 · Data\n'), rubricUrl: (id: string) => `rubric#${id}`,
|
|
pipeline: { status: 'failed' }, pipelineIds: [7],
|
|
loadJob: async (id: number) => (id === 1 ? { pipelineId: 7, name: 'test', webUrl: 'job/1' } : null),
|
|
};
|
|
const good: Verdict = { verdict: 'go', reason: 'Aman', post_deploy: [], findings: [
|
|
{ kind: 'ok', text: 'tes ada', evidence: [ev({ type: 'pipeline', job_id: 1 })] },
|
|
{ kind: 'ok', text: 'karangan', evidence: [ev({ path: 'nope.py', line: 3 })] },
|
|
] };
|
|
const r = await check(good, ctx);
|
|
assert.equal(r.checked!.findings.length, 1);
|
|
assert.equal(r.checked!.dropped.length, 1);
|
|
assert.equal(r.checked!.verdict, 'waiting');
|
|
|
|
const bad: Verdict = { verdict: 'no_go', reason: 'Tahan', post_deploy: [], findings: [{ kind: 'bad', text: 'baris palsu', evidence: [ev({ path: 'a.py', line: 99 })] }] };
|
|
assert.match((await check(bad, ctx)).errors.join(), /outside the file/);
|
|
|
|
const escape: Verdict = { verdict: 'no_go', reason: 'Tahan', post_deploy: [], findings: [{ kind: 'bad', text: 'keluar repo', evidence: [ev({ path: '../../etc/hosts', line: 1 })] }] };
|
|
assert.match((await check(escape, ctx)).errors.join(), /does not exist/);
|
|
});
|
|
|
|
test('an empty answer is reported as empty, not as invalid JSON', async () => {
|
|
const { shapeErrors } = await import('../src/contract.ts');
|
|
assert.deepEqual(shapeErrors(' ').errors, ['The answer is empty.']);
|
|
});
|