Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/agent/__tests__/wizard-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
15 changes: 15 additions & 0 deletions src/agent/runner/harness/pi/__tests__/security.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/agent/runner/harness/pi/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/agent/tools/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/cli/runners/run-non-interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
3 changes: 2 additions & 1 deletion src/cli/runners/run-wizard.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down
Loading