|
1 | 1 | import { test, describe } from "node:test" |
2 | 2 | import assert from "node:assert/strict" |
3 | 3 | import { execFileSync, spawnSync } from "node:child_process" |
4 | | -import { mkdtempSync, mkdirSync, rmSync, readdirSync, readFileSync, writeFileSync, symlinkSync, unlinkSync, existsSync } from "node:fs" |
| 4 | +import { mkdtempSync, mkdirSync, rmSync, readdirSync, readFileSync, writeFileSync, symlinkSync, unlinkSync, existsSync, copyFileSync, chmodSync } from "node:fs" |
5 | 5 | import { tmpdir } from "node:os" |
6 | 6 | import { dirname, join } from "node:path" |
7 | 7 | import { |
@@ -29,6 +29,9 @@ import { |
29 | 29 | savingFor, |
30 | 30 | leaveOut, |
31 | 31 | commitPaths, |
| 32 | + parseLsTree, |
| 33 | + treeChanges, |
| 34 | + checkTreeAgainstBase, |
32 | 35 | startedOnMergeCommit, |
33 | 36 | checkHandoffDescends, |
34 | 37 | REWROTE_HISTORY, |
@@ -1282,3 +1285,84 @@ describe("a staged rename is a move, not a copy", () => { |
1282 | 1285 | rmSync(dir, { recursive: true, force: true }) |
1283 | 1286 | }) |
1284 | 1287 | }) |
| 1288 | + |
| 1289 | +describe("the base is checked against a listing taken before the agent ran", () => { |
| 1290 | + const identity = { name: "Test Companion", email: BOT_EMAIL } |
| 1291 | + const ls = (git, rev) => parseLsTree(spawnSync("git", ["ls-tree", "-r", "-z", "--full-tree", rev], { cwd: git.dir, encoding: "utf8" }).stdout) |
| 1292 | + |
| 1293 | + test("listings compare path by path", () => { |
| 1294 | + const before = parseLsTree("100644 blob aaa\ta.ts\x00100644 blob bbb\tb.ts\0") |
| 1295 | + const after = parseLsTree("100644 blob aaa\ta.ts\x00100644 blob ccc\tb.ts\x00120000 blob ddd\tlink\0") |
| 1296 | + assert.deepEqual(treeChanges(before, after), ["b.ts", "link"]) |
| 1297 | + assert.match(checkTreeAgainstBase(before, after, { allowed: ["b.ts"] }), /nobody chose: `link`/) |
| 1298 | + assert.match(checkTreeAgainstBase(before, after, { allowed: ["b.ts", "link"] }), /never commits: `link`/) |
| 1299 | + assert.equal(checkTreeAgainstBase(before, parseLsTree("100644 blob aaa\ta.ts\x00100644 blob ccc\tb.ts\0"), { allowed: ["b.ts"] }), undefined) |
| 1300 | + assert.match(checkTreeAgainstBase(new Map(), after), /couldn't be listed/) |
| 1301 | + }) |
| 1302 | + |
| 1303 | + test("git itself refuses a forged object, loose or packed", () => { |
| 1304 | + const { dir, git, tip } = repoWithBranch({ "a.ts": "new\n" }) |
| 1305 | + const tree = git(["rev-parse", `${tip}^{tree}`]).out |
| 1306 | + const file = join(dir, ".git/objects", tree.slice(0, 2), tree.slice(2)) |
| 1307 | + // Swap the base tree's object file for another tree's. |
| 1308 | + const other = git(["mktree"], {}, "").out |
| 1309 | + chmodSync(file, 0o644) |
| 1310 | + copyFileSync(join(dir, ".git/objects", other.slice(0, 2), other.slice(2)), file) |
| 1311 | + const r = git(["ls-tree", "-r", tip]) |
| 1312 | + assert.equal(r.ok, false) |
| 1313 | + assert.match(r.err, /hash mismatch/) |
| 1314 | + rmSync(dir, { recursive: true, force: true }) |
| 1315 | + }) |
| 1316 | + |
| 1317 | + test("a commit built on a base other than the one listed is refused", () => { |
| 1318 | + // What a forged read of the base would produce: a commit whose tree differs from the |
| 1319 | + // listed base in more than the chosen files. |
| 1320 | + const { dir, git, base, tip } = repoWithBranch({ "a.ts": "new\n", ".github/actions/x/action.yml": "runs: evil\n" }) |
| 1321 | + git.dir = dir |
| 1322 | + const listedBase = ls(git, base) |
| 1323 | + writeFileSync(join(dir, "a.ts"), "newer\n") |
| 1324 | + const out = commitPaths({ git, base: tip, paths: ["a.ts"], identity, message: "m", indexFile: join(dir, "scratch") }) |
| 1325 | + assert.ok(out.sha, JSON.stringify(out)) |
| 1326 | + assert.match(checkTreeAgainstBase(listedBase, ls(git, out.sha), { allowed: ["a.ts"] }), /nobody chose: `\.github\/actions\/x\/action\.yml`/) |
| 1327 | + rmSync(dir, { recursive: true, force: true }) |
| 1328 | + }) |
| 1329 | + |
| 1330 | + test("commit-graph and multi-pack-index files are not used", () => { |
| 1331 | + const settings = GIT_SAFE_ARGS.filter((a) => a !== "-c") |
| 1332 | + assert.ok(settings.includes("core.commitGraph=false")) |
| 1333 | + assert.ok(settings.includes("core.multiPackIndex=false")) |
| 1334 | + }) |
| 1335 | + |
| 1336 | + test("git never inherits a variable that points it at other objects", () => { |
| 1337 | + const got = gitEnvFrom({ GIT_ALTERNATE_OBJECT_DIRECTORIES: "/tmp/evil", GIT_OBJECT_DIRECTORY: "/tmp/evil", GIT_DIR: "/tmp/evil", GIT_INDEX_FILE: "/tmp/evil" }) |
| 1338 | + for (const k of ["GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_OBJECT_DIRECTORY", "GIT_DIR", "GIT_INDEX_FILE"]) assert.equal(got[k], undefined, k) |
| 1339 | + }) |
| 1340 | + |
| 1341 | + test("an alternates file feeds git objects from another folder, which is why it's checked", () => { |
| 1342 | + const { dir, git } = repoWithBranch({ "a.ts": "new\n" }) |
| 1343 | + const other = mkdtempSync(join(tmpdir(), "tc-alt-")) |
| 1344 | + spawnSync("git", ["init", "-q"], { cwd: other }) |
| 1345 | + const foreign = spawnSync("git", ["hash-object", "-w", "--stdin"], { cwd: other, encoding: "utf8", input: "foreign\n" }).stdout.trim() |
| 1346 | + assert.equal(git(["cat-file", "-e", foreign]).ok, false) |
| 1347 | + mkdirSync(join(dir, ".git/objects/info"), { recursive: true }) |
| 1348 | + writeFileSync(join(dir, ".git/objects/info/alternates"), `${join(other, ".git/objects")}\n`) |
| 1349 | + assert.equal(git(["cat-file", "-e", foreign]).ok, true) |
| 1350 | + rmSync(dir, { recursive: true, force: true }) |
| 1351 | + rmSync(other, { recursive: true, force: true }) |
| 1352 | + }) |
| 1353 | + |
| 1354 | + test("the wiring: listed before the agent, alternates and listings checked before anything is pushed", () => { |
| 1355 | + const main = readFileSync(new URL("../src/main.mjs", import.meta.url), "utf8") |
| 1356 | + const start = main.indexOf('headBeforeRun = git(["rev-parse", "HEAD"]).out') |
| 1357 | + assert.ok(main.indexOf("baseListing = headBeforeRun ? parseLsTree(", start) > start) |
| 1358 | + assert.ok(main.indexOf("alternatesBefore = readAlternates(", start) > start) |
| 1359 | + assert.ok(main.indexOf("baseListing = headBeforeRun", start) < main.indexOf("runLockedDown(", start), "listed before the agent runs") |
| 1360 | + assert.match(main, /const ALTERNATES_FILES = \["objects\/info\/alternates", "objects\/info\/http-alternates"\]/) |
| 1361 | + const save = main.slice(main.indexOf("function saveWork("), main.indexOf("function isolateAgent(")) |
| 1362 | + assert.ok(save.indexOf("alternatesChanged()") < save.indexOf('"status", "--porcelain"')) |
| 1363 | + assert.ok(save.indexOf("checkTreeAgainstBase(baseListing") < save.indexOf('git(["push"')) |
| 1364 | + const hand = main.slice(main.indexOf("function openHandedOffPr("), main.indexOf("function openHandedOffPr(") + 7000) |
| 1365 | + assert.ok(hand.indexOf("alternatesChanged()") < hand.indexOf("refs/heads/${req.branch}^{commit}")) |
| 1366 | + assert.ok(hand.indexOf("checkTreeAgainstBase(baseListing") < hand.indexOf('git(["push"')) |
| 1367 | + }) |
| 1368 | +}) |
0 commit comments