Keep git inside the checkout folder so an empty work dir cannot hijack the outer repo.
An empty .work/<mr>/repo inside a git checkout made rev-parse find the outer repository, so set-url, fetch and checkout --force ran there; GIT_CEILING_DIRECTORIES pins git to the folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -132,7 +132,11 @@ function gitAccess(mr: MrInfo): { url: string; env: NodeJS.ProcessEnv } {
|
|||||||
|
|
||||||
export async function checkout(mr: MrInfo, dir: string) {
|
export async function checkout(mr: MrInfo, dir: string) {
|
||||||
const access = gitAccess(mr);
|
const access = gitAccess(mr);
|
||||||
const git = (...args: string[]) => run('git', args, { cwd: dir, env: access.env, maxBuffer: 16 * 1024 * 1024 });
|
// Git must never walk up from the checkout: an empty folder inside another repository (a
|
||||||
|
// LABUH_HOME that is itself a git checkout) would otherwise send init, set-url and
|
||||||
|
// `checkout --force` to that outer repository.
|
||||||
|
const env = { ...access.env, GIT_CEILING_DIRECTORIES: path.dirname(path.resolve(dir)) };
|
||||||
|
const git = (...args: string[]) => run('git', args, { cwd: dir, env, maxBuffer: 16 * 1024 * 1024 });
|
||||||
try {
|
try {
|
||||||
const { stdout } = await git('rev-parse', 'HEAD');
|
const { stdout } = await git('rev-parse', 'HEAD');
|
||||||
if (stdout.trim() === mr.sha) {
|
if (stdout.trim() === mr.sha) {
|
||||||
|
|||||||
42
test/checkout.test.ts
Normal file
42
test/checkout.test.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import { test } from 'node:test';
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { execFileSync } from 'node:child_process';
|
||||||
|
import { mkdtempSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from 'node:fs';
|
||||||
|
import os from 'node:os';
|
||||||
|
import path from 'node:path';
|
||||||
|
import { checkout, type MrInfo } from '../src/gitlab.ts';
|
||||||
|
|
||||||
|
const git = (cwd: string, ...args: string[]) => execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', ...args], { cwd, encoding: 'utf8' }).trim();
|
||||||
|
|
||||||
|
test('an empty checkout folder inside another repository never touches that repository', async () => {
|
||||||
|
delete process.env.GITLAB_TOKEN;
|
||||||
|
const root = mkdtempSync(path.join(os.tmpdir(), 'labuh-co-'));
|
||||||
|
// The reviewed project, with the MR head under GitLab's ref name.
|
||||||
|
const src = path.join(root, 'src');
|
||||||
|
mkdirSync(src);
|
||||||
|
git(src, 'init', '-q');
|
||||||
|
writeFileSync(path.join(src, 'app.txt'), 'reviewed\n');
|
||||||
|
git(src, 'add', '.');
|
||||||
|
git(src, 'commit', '-qm', 'mr');
|
||||||
|
const sha = git(src, 'rev-parse', 'HEAD');
|
||||||
|
git(src, 'update-ref', 'refs/merge-requests/7/head', sha);
|
||||||
|
// LABUH_HOME is itself a repository, and the work folder already has an empty repo/.
|
||||||
|
const home = path.join(root, 'home');
|
||||||
|
mkdirSync(home);
|
||||||
|
git(home, 'init', '-q');
|
||||||
|
writeFileSync(path.join(home, 'own.txt'), 'labuh\n');
|
||||||
|
git(home, 'add', '.');
|
||||||
|
git(home, 'commit', '-qm', 'home');
|
||||||
|
const homeHead = git(home, 'rev-parse', 'HEAD');
|
||||||
|
const dir = path.join(home, '.work', 'p-7', 'repo');
|
||||||
|
mkdirSync(dir, { recursive: true });
|
||||||
|
|
||||||
|
await checkout({ iid: 7, sha, sshUrl: src, httpUrl: src } as MrInfo, dir);
|
||||||
|
|
||||||
|
assert.equal(git(dir, 'rev-parse', '--show-toplevel'), realpathSync(dir), 'the checkout is its own repository');
|
||||||
|
assert.equal(git(dir, 'rev-parse', 'HEAD'), sha);
|
||||||
|
assert.equal(readFileSync(path.join(dir, 'app.txt'), 'utf8'), 'reviewed\n');
|
||||||
|
assert.equal(git(home, 'rev-parse', 'HEAD'), homeHead, 'the outer repository keeps its HEAD');
|
||||||
|
assert.equal(git(home, 'remote'), '', 'the outer repository gets no remote');
|
||||||
|
assert.equal(readFileSync(path.join(home, 'own.txt'), 'utf8'), 'labuh\n');
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user