Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: validate the mkdtemp path, not git's respelling, in worktreeReap resets (#6003)
Adam Eivyf56c6504fff8
diff --git a/server/lib/gitTestRepo.js b/server/lib/gitTestRepo.jsindex edc3f963d7..729c30391b 100644--- a/server/lib/gitTestRepo.js+++ b/server/lib/gitTestRepo.js@@ -169,9 +169,19 @@ export async function materializeGitRepo(dest, { identity } = {}) {* again. `initialHead` is the sha `repo` was at right after it was built* (capture it once with `execGit(['rev-parse', 'HEAD'], repo)` in `beforeAll`,* before any test has run).+ *+ * `assertPath` (default `repo`) is what gets checked against `os.tmpdir()` —+ * pass the plain path a caller `mkdtemp()`'d rather than `repo` itself when+ * `repo` is git's OWN respelling of that same directory (e.g. from+ * `git rev-parse --show-toplevel`, as `worktreeReap.test.js`'s `initRepo()`+ * returns). On Windows those can disagree — `%TEMP%` is sometimes the 8.3+ * short form (`RUNNER~1`) while git always reports the long form+ * (`runneradmin`) — and `assertTempPath`'s realpath-based canonicalization+ * does not bridge that gap (nothing does but asking git; see the identical+ * problem worked around in `worktreeManager.js`'s `listWorktrees()`).*/-export async function resetGitSandbox({ scratch, repo, initialHead }) {- assertTempPath(repo, 'git sandbox reset');+export async function resetGitSandbox({ scratch, repo, initialHead, assertPath = repo }) {+ assertTempPath(assertPath, 'git sandbox reset');// A test can delete `repo` itself (simulating a checkout that vanished// mid-run) — rebuild it from the template rather than handing every git// call below a cwd that no longer exists. The template's own working copy@@ -217,13 +227,14 @@ export async function resetGitSandbox({ scratch, repo, initialHead }) {}/**- * Same contract as `resetGitSandbox()`, plus tearing down every real- * `git worktree add` checkout `repo` has grown since the last reset- * (including a locked one) — for suites whose tests exercise worktree- * creation/removal directly rather than just branches and commits.+ * Same contract as `resetGitSandbox()` — including the `assertPath` override+ * for a `repo` that's git's own respelling of an `mkdtemp()` path — plus+ * tearing down every real `git worktree add` checkout `repo` has grown since+ * the last reset (including a locked one), for suites whose tests exercise+ * worktree creation/removal directly rather than just branches and commits.*/-export async function resetGitWorktreeSandbox(repo, initialHead) {- assertTempPath(repo, 'git worktree sandbox reset');+export async function resetGitWorktreeSandbox(repo, initialHead, assertPath = repo) {+ assertTempPath(assertPath, 'git worktree sandbox reset');// A missing `repo` has no worktrees to list — let resetGitSandbox()'s own// rebuild-from-template handle it below instead of handing `worktree list`// a cwd that doesn't exist.@@ -250,7 +261,7 @@ export async function resetGitWorktreeSandbox(repo, initialHead) {}if (entries.length > 1) await execGit(['worktree', 'prune'], repo, { ignoreExitCode: true });}- await resetGitSandbox({ repo, initialHead });+ await resetGitSandbox({ repo, initialHead, assertPath });}export async function destroyGitSandbox(scratch) {diff --git a/server/lib/gitTestRepo.test.js b/server/lib/gitTestRepo.test.jsindex 6605661451..f60d0fb91c 100644--- a/server/lib/gitTestRepo.test.js+++ b/server/lib/gitTestRepo.test.js@@ -132,6 +132,38 @@ describe('resetGitSandbox', () => {expect((await execGit(['rev-parse', 'HEAD'], dest)).stdout.trim()).toBe(initialHead);expect((await execGit(['branch', '--format=%(refname:short)'], dest)).stdout.trim()).toBe('main');});++ it('checks assertPath instead of repo when a caller passes a respelled repo path', async () => {+ // Simulates a `repo` that's git's own respelling of an mkdtemp() path+ // (worktreeReap.test.js's initRepo() does this via `rev-parse+ // --show-toplevel`) — on Windows that spelling can disagree with+ // os.tmpdir()'s own (8.3 short form vs git's long form), which+ // assertTempPath's realpath-based check cannot bridge (#6003).+ const dest = await mkdtemp(join(tmpdir(), 'portos-git-fx-reset-assertpath-'));+ sandboxes.push(dest);+ await materializeGitRepo(dest);+ const initialHead = (await execGit(['rev-parse', 'HEAD'], dest)).stdout.trim();+ const respelled = (await execGit(['rev-parse', '--show-toplevel'], dest)).stdout.trim();++ // repo=respelled would still resolve to the real directory (git's+ // respelling is never actually outside tmpdir — this only stands in for+ // the case where an OS spelling mismatch would make it look that way to+ // assertTempPath) — the point is that passing assertPath overrides which+ // string gets checked, without changing which directory git operates on.+ await resetGitSandbox({ repo: respelled, initialHead, assertPath: dest });++ expect((await execGit(['rev-parse', 'HEAD'], dest)).stdout.trim()).toBe(initialHead);+ });++ it('rejects an unsafe assertPath even when repo itself is a real temp dir', async () => {+ const dest = await mkdtemp(join(tmpdir(), 'portos-git-fx-reset-unsafe-'));+ sandboxes.push(dest);+ await materializeGitRepo(dest);+ const initialHead = (await execGit(['rev-parse', 'HEAD'], dest)).stdout.trim();++ await expect(resetGitSandbox({ repo: dest, initialHead, assertPath: '/etc' }))+ .rejects.toThrow(/refusing to run/);+ });});describe('resetGitWorktreeSandbox', () => {diff --git a/server/services/worktreeReap.test.js b/server/services/worktreeReap.test.jsindex 173104015f..72190faaca 100644--- a/server/services/worktreeReap.test.js+++ b/server/services/worktreeReap.test.js@@ -41,32 +41,44 @@ async function commitFile(dir, name, content, message) {await execGit(['commit', '-m', message], dir);}+/**+ * @returns {Promise<{dir: string, safePath: string}>} `dir` is the repo root+ * spelled the way git itself reports it — needed for the reaper's own+ * `startsWith()` location checks and our path assertions (see below).+ * `safePath` is the plain `mkdtemp()` path, still in whatever spelling+ * `os.tmpdir()` uses — pass THIS to resetGitSandbox()/resetGitWorktreeSandbox()'s+ * `assertTempPath` check, since on Windows it and `dir` can disagree (8.3+ * short form `RUNNER~1` vs git's long form `runneradmin`) in a way nothing+ * but git itself can bridge.+ */async function initRepo() {// realpath-resolve: on macOS mkdtemp returns a /var symlink while// `git worktree list` records the canonical /private/var path, which would// break the reaper's startsWith() location checks and our path assertions.- const created = realpathSync(await mkdtemp(join(tmpdir(), 'portos-reap-')));- await materializeGitRepo(created, { identity: { email: '[email protected]', name: 'Test' } });+ const safePath = realpathSync(await mkdtemp(join(tmpdir(), 'portos-reap-')));+ await materializeGitRepo(safePath, { identity: { email: '[email protected]', name: 'Test' } });// Adopt git's spelling of the root. `git worktree list` reports paths the way// git normalized them, and the reaper's containment check compares those// against a root derived from this value — so any disagreement (8.3 short// names like C:\\Users\\RUNNER~1, drive-letter case) makes every worktree look// like it lives somewhere unmanaged and nothing is reaped.- return (await execGit(['rev-parse', '--show-toplevel'], created)).stdout.trim() || created;+ const dir = (await execGit(['rev-parse', '--show-toplevel'], safePath)).stdout.trim() || safePath;+ return { dir, safePath };}describe.skipIf(SKIP_HEAVY_INTEGRATION)('isBranchMergedInto', () => {let dir;+ let safePath;let initialHead;// One real repo for the whole describe, reset in place between tests// instead of a fresh mkdtemp + materializeGitRepo (fs.cp) + rm per test// (#5902) — the slow part on a Windows filesystem is that copy/delete// cycle, not the handful of git commands each test runs.beforeAll(async () => {- dir = await initRepo();+ ({ dir, safePath } = await initRepo());initialHead = (await execGit(['rev-parse', 'HEAD'], dir)).stdout.trim();});- beforeEach(async () => { await resetGitSandbox({ repo: dir, initialHead }); });+ beforeEach(async () => { await resetGitSandbox({ repo: dir, initialHead, assertPath: safePath }); });afterAll(async () => { await rm(dir, { recursive: true, force: true }); });it('detects a normal (--no-ff) merge', async () => {@@ -129,6 +141,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('isBranchMergedInto', () => {describe.skipIf(SKIP_HEAVY_INTEGRATION)('reapMergedWorktrees', () => {let dir;+ let safePath;// One root OUTSIDE the repo for the includeUnmanagedTrees cases, torn down// alongside the repo so a held tree can't leak out of the run.let externalRoot;@@ -141,11 +154,11 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('reapMergedWorktrees', () => {// locked one), wherever it lives, so externalRoot is naturally emptied// back out along with dir's own `.claude/worktrees` trees.beforeAll(async () => {- dir = await initRepo();+ ({ dir, safePath } = await initRepo());externalRoot = realpathSync(await mkdtemp(join(tmpdir(), 'portos-reap-ext-')));initialHead = (await execGit(['rev-parse', 'HEAD'], dir)).stdout.trim();});- beforeEach(async () => { await resetGitWorktreeSandbox(dir, initialHead); });+ beforeEach(async () => { await resetGitWorktreeSandbox(dir, initialHead, safePath); });afterAll(async () => {await rm(dir, { recursive: true, force: true });await rm(externalRoot, { recursive: true, force: true });