From bf9cf78a47bb2e83666aaec4e3d4d41f1a83a006 Mon Sep 17 00:00:00 2001 From: Edwin Lim Date: Wed, 16 Sep 2026 11:36:59 -0700 Subject: [PATCH] fix: noramlize path for working directory --- src/lib/__tests__/wizard-tools.test.ts | 18 ++++++++++++++++++ .../harness/pi/__tests__/security.test.ts | 15 +++++++++++++++ src/lib/agent/runner/harness/pi/security.ts | 7 +++++-- src/lib/runners/run-non-interactive.ts | 4 +--- src/lib/runners/run-wizard.ts | 6 ++++-- src/lib/wizard-tools/tools.ts | 11 +++++------ 6 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/lib/__tests__/wizard-tools.test.ts b/src/lib/__tests__/wizard-tools.test.ts index 901a1d025..02b179619 100644 --- a/src/lib/__tests__/wizard-tools.test.ts +++ b/src/lib/__tests__/wizard-tools.test.ts @@ -86,6 +86,24 @@ describe('resolveEnvPath', () => { 'Path traversal rejected', ); }); + + it('accepts in-workspace paths when the working directory is not canonical', () => { + // Regression guard: a trailing-slash or relative install dir rejected + // every write, ".env" included (reported as PHW_AGENT_ORCHESTRATOR_TASKS_FAILED). + expect(resolveEnvPath('/project/', '.env')).toBe( + path.resolve('/project', '.env'), + ); + expect(resolveEnvPath('/project/sub/..', '.env')).toBe( + path.resolve('/project', '.env'), + ); + expect(resolveEnvPath('.', '.env')).toBe(path.resolve('.env')); + expect(() => resolveEnvPath('/project/', '../etc/passwd')).toThrow( + 'Path traversal rejected', + ); + expect(() => resolveEnvPath('.', '../escape/.env')).toThrow( + 'Path traversal rejected', + ); + }); }); describe('parseEnvKeys', () => { diff --git a/src/lib/agent/runner/harness/pi/__tests__/security.test.ts b/src/lib/agent/runner/harness/pi/__tests__/security.test.ts index f73405721..5eee84f7b 100644 --- a/src/lib/agent/runner/harness/pi/__tests__/security.test.ts +++ b/src/lib/agent/runner/harness/pi/__tests__/security.test.ts @@ -580,6 +580,21 @@ describe('pi-security: plain rm matches the anthropic arm', () => { } }); + test('a non-canonical working directory does not block in-project deletes', async () => { + const trailing = await evaluateToolCall( + 'bash', + { command: 'rm src/tmp/plan.json' }, + { workingDirectory: `${ROOT}${path.sep}` }, + ); + expect(trailing.block).toBe(false); + const escape = await evaluateToolCall( + 'bash', + { command: 'rm ../outside.txt' }, + { workingDirectory: `${ROOT}${path.sep}` }, + ); + expect(escape.block).toBe(true); + }); + test('can NEVER resolve outside the project root', async () => { for (const c of [ 'rm /etc/passwd', // absolute diff --git a/src/lib/agent/runner/harness/pi/security.ts b/src/lib/agent/runner/harness/pi/security.ts index 0ff5c811c..fe5d3ec85 100644 --- a/src/lib/agent/runner/harness/pi/security.ts +++ b/src/lib/agent/runner/harness/pi/security.ts @@ -135,8 +135,11 @@ function isDeletableProjectFile( if (/[*?[\]~]/.test(target)) return false; // glob / home expansion if (p.basename(target).startsWith('.env')) return false; // secrets - const resolved = p.resolve(root, target); - return resolved !== root && resolved.startsWith(root + p.sep); + const canonicalRoot = p.resolve(root); + const resolved = p.resolve(canonicalRoot, target); + return ( + resolved !== canonicalRoot && resolved.startsWith(canonicalRoot + p.sep) + ); } /** diff --git a/src/lib/runners/run-non-interactive.ts b/src/lib/runners/run-non-interactive.ts index 5fb00abca..c4730b362 100644 --- a/src/lib/runners/run-non-interactive.ts +++ b/src/lib/runners/run-non-interactive.ts @@ -117,9 +117,7 @@ export function runNonInteractive( const env = readEnvironment(); const apiKey = (options.apiKey as string) ?? readApiKeyFromEnv() ?? undefined; - const installDir = path.isAbsolute(options.installDir as string) - ? (options.installDir as string) - : path.join(process.cwd(), options.installDir as string); + const installDir = path.resolve(options.installDir as string); const session = buildSession({ debug: options.debug as boolean | undefined, diff --git a/src/lib/runners/run-wizard.ts b/src/lib/runners/run-wizard.ts index 435cfb9d4..ac9b13bf8 100644 --- a/src/lib/runners/run-wizard.ts +++ b/src/lib/runners/run-wizard.ts @@ -17,7 +17,7 @@ import { classifyRunFailure, emitWizardError } from '@lib/errors'; import { isRunFailure } from '@ui/mint-failure'; import { getUI } from '@ui'; import { analytics } from '@utils/analytics'; -import { join } from 'node:path'; +import { join, resolve } from 'node:path'; const WIZARD_VERSION = VERSION; @@ -81,7 +81,9 @@ export function runWizard( void (async () => { try { - const installDir = (options.installDir as string) || process.cwd(); + const installDir = resolve( + (options.installDir as string) || process.cwd(), + ); const { startTUI } = await import('@ui/tui/start-tui'); const { buildSession, RunPhase } = await import('@lib/wizard-session'); diff --git a/src/lib/wizard-tools/tools.ts b/src/lib/wizard-tools/tools.ts index cacb1491d..07bb29710 100644 --- a/src/lib/wizard-tools/tools.ts +++ b/src/lib/wizard-tools/tools.ts @@ -583,13 +583,12 @@ export function resolveEnvPath( workingDirectory: string, filePath: string, ): string { - const resolved = path.resolve(workingDirectory, filePath); - if ( - !resolved.startsWith(workingDirectory + path.sep) && - resolved !== workingDirectory - ) { + // Canonicalize first: a trailing-slash or relative root fails the prefix check for every path. + const root = path.resolve(workingDirectory); + const resolved = path.resolve(root, filePath); + if (!resolved.startsWith(root + path.sep) && resolved !== root) { throw new Error( - `Path traversal rejected: "${filePath}" resolves outside working directory`, + `Path traversal rejected: "${filePath}" resolves outside working directory "${root}"`, ); } return resolved;