From 760f5c34ec9f643f723b7da60bba779df124d2c7 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 30 May 2026 09:37:17 -0700 Subject: [PATCH] perf: classify relay bulk discard paths safely (#3770) --- src/relay/git-handler.test.ts | 30 ++++++++++++++++++++++++++++-- src/relay/git-handler.ts | 8 +++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/relay/git-handler.test.ts b/src/relay/git-handler.test.ts index 326e42059..7d7d2fc25 100644 --- a/src/relay/git-handler.test.ts +++ b/src/relay/git-handler.test.ts @@ -20,14 +20,14 @@ import { describe('GitHandler', () => { let dispatcher: MockDispatcher + let handler: GitHandler let tmpDir: string beforeEach(() => { tmpDir = mkdtempSync(path.join(tmpdir(), 'relay-git-')) dispatcher = createMockDispatcher() const ctx = new RelayContext() - // eslint-disable-next-line no-new - new GitHandler(dispatcher as unknown as RelayDispatcher, ctx) + handler = new GitHandler(dispatcher as unknown as RelayDispatcher, ctx) }) afterEach(async () => { @@ -498,6 +498,32 @@ describe('GitHandler', () => { await expect(fs.access(path.join(tmpDir, 'new.txt'))).rejects.toThrow() }) + it('handles large tracked path lists during bulk discard classification', async () => { + const trackedStdout = Array.from({ length: 150_000 }, (_, index) => `docs/file-${index}.ts`) + .join('\0') + .concat('\0') + const gitMock = vi + .spyOn( + handler as unknown as { + git: (args: string[], cwd: string) => Promise<{ stdout: string; stderr: string }> + }, + 'git' + ) + .mockResolvedValueOnce({ stdout: trackedStdout, stderr: '' }) + .mockResolvedValueOnce({ stdout: '', stderr: '' }) + + await dispatcher.callRequest('git.bulkDiscard', { + worktreePath: tmpDir, + filePaths: ['docs'] + }) + + expect(gitMock).toHaveBeenNthCalledWith( + 2, + ['restore', '--worktree', '--source=HEAD', '--', ':(literal)docs'], + tmpDir + ) + }) + it('rejects path traversal', async () => { gitInit(tmpDir) await expect( diff --git a/src/relay/git-handler.ts b/src/relay/git-handler.ts index b532ab070..e14b72e28 100644 --- a/src/relay/git-handler.ts +++ b/src/relay/git-handler.ts @@ -276,7 +276,13 @@ export class GitHandler { ['ls-files', '-z', '--', ...chunk.map((p) => this.literalPathspec(p))], worktreePath ) - trackedPathSpecs.push(...stdout.split('\0').filter(Boolean)) + // Why: selecting a tracked directory can make `ls-files -z` return + // enough descendants for push(...split) to exceed the argument limit. + for (const trackedPathSpec of stdout.split('\0')) { + if (trackedPathSpec) { + trackedPathSpecs.push(trackedPathSpec) + } + } } const trackedPaths = filePaths.filter((filePath) =>