diff --git a/src/agent/__tests__/wizard-tools.test.ts b/src/agent/__tests__/wizard-tools.test.ts index 0a0cd9e75..9be1f9436 100644 --- a/src/agent/__tests__/wizard-tools.test.ts +++ b/src/agent/__tests__/wizard-tools.test.ts @@ -88,6 +88,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/agent/runner/harness/pi/__tests__/security.test.ts b/src/agent/runner/harness/pi/__tests__/security.test.ts index b1dc5d2fb..d6c0d7bb4 100644 --- a/src/agent/runner/harness/pi/__tests__/security.test.ts +++ b/src/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/agent/runner/harness/pi/security.ts b/src/agent/runner/harness/pi/security.ts index 796e8a458..b52e716d6 100644 --- a/src/agent/runner/harness/pi/security.ts +++ b/src/agent/runner/harness/pi/security.ts @@ -143,8 +143,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/agent/tools/tools.ts b/src/agent/tools/tools.ts index 7f57602f0..1a917cc74 100644 --- a/src/agent/tools/tools.ts +++ b/src/agent/tools/tools.ts @@ -427,13 +427,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; diff --git a/src/cli/runners/run-non-interactive.ts b/src/cli/runners/run-non-interactive.ts index 57f80dd74..db3eb5793 100644 --- a/src/cli/runners/run-non-interactive.ts +++ b/src/cli/runners/run-non-interactive.ts @@ -59,9 +59,7 @@ export function runNonInteractive( configureLogFileFromEnvironment(); const env = readEnvironment(); - 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); withSignals(async (signal) => { const { runHeadless } = await import('@headless'); return runHeadless(config, { diff --git a/src/cli/runners/run-wizard.ts b/src/cli/runners/run-wizard.ts index 85b9fc8a6..18b72cabd 100644 --- a/src/cli/runners/run-wizard.ts +++ b/src/cli/runners/run-wizard.ts @@ -1,6 +1,7 @@ /** The TUI command runner: parse the launch values, start the TUI host, and exit with its code. */ import type { ProgramConfig, SessionArgs } from '@programs/types'; import type { Harness, Sequence } from '@shared/constants'; +import { resolve } from 'node:path'; import { runtimeEnv } from '@env'; import { resolveNoTelemetry } from './resolve-no-telemetry'; import { withSignals } from './signals'; @@ -14,7 +15,7 @@ export function tuiSessionArgs( localDev: options.localDev as boolean | undefined, localMcp: options.localMcp as boolean | undefined, localPosthog: options.localPosthog as boolean | undefined, - installDir: (options.installDir as string) || process.cwd(), + installDir: resolve((options.installDir as string) || process.cwd()), ci: false, signup: options.signup as boolean | undefined, apiKey: options.apiKey as string | undefined,