Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions packages/core/src/policy/policy-engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,39 @@ describe('PolicyEngine', () => {
});

describe('shell command parsing failure', () => {
it('should return ALLOW in YOLO mode for dangerous commands due to heuristics override', async () => {
// Create an engine with YOLO mode and a sandbox manager that flags a command as dangerous
const rules: PolicyRule[] = [
{
toolName: '*',
decision: PolicyDecision.ALLOW,
priority: 999,
modes: [ApprovalMode.YOLO],
},
];

const mockSandboxManager = new NoopSandboxManager();
mockSandboxManager.isDangerousCommand = vi.fn().mockReturnValue(true);
mockSandboxManager.isKnownSafeCommand = vi.fn().mockReturnValue(false);

engine = new PolicyEngine({
rules,
approvalMode: ApprovalMode.YOLO,
sandboxManager: mockSandboxManager,
});

const result = await engine.check(
{
name: 'run_shell_command',
args: { command: 'powershell echo "dangerous"' },
},
undefined,
);

// Even though the command is flagged as dangerous, YOLO mode should preserve the ALLOW decision
expect(result.decision).toBe(PolicyDecision.ALLOW);
});

it('should return ALLOW in YOLO mode even if shell command parsing fails', async () => {
const { splitCommands } = await import('../utils/shell-utils.js');
const rules: PolicyRule[] = [
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/policy/policy-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ export class PolicyEngine {
const parsedArgs = parsedObjArgs.map(extractStringFromParseEntry);

if (this.sandboxManager.isDangerousCommand(parsedArgs)) {
if (this.approvalMode === ApprovalMode.YOLO) {
debugLogger.debug(
`[PolicyEngine.check] Command evaluated as dangerous, but YOLO mode is active. Preserving decision: ${command}`,
);
return decision;
}

debugLogger.debug(
`[PolicyEngine.check] Command evaluated as dangerous, forcing ASK_USER: ${command}`,
);
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/sandbox/linux/bwrapArgsBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ describe.skipIf(os.platform() === 'win32')('buildBwrapArgs', () => {
expect(args[args.indexOf(worktreeGitDir) - 1]).toBe('--ro-bind-try');
});

it('git worktree read-only bindings should override previous policyWrite bindings', async () => {
it('explicit policyWrite bindings should override git worktree read-only bindings', async () => {
const worktreeGitDir = '/custom/worktree/.git';

const args = await buildBwrapArgs({
Expand All @@ -389,11 +389,11 @@ describe.skipIf(os.platform() === 'win32')('buildBwrapArgs', () => {
}),
});

const writeBindIndex = args.indexOf('/custom/worktree');
const worktreeBindIndex = args.lastIndexOf(worktreeGitDir);
const writeBindIndex = args.lastIndexOf('/custom/worktree');
const worktreeBindIndex = args.indexOf(worktreeGitDir);

expect(writeBindIndex).toBeGreaterThan(-1);
expect(worktreeBindIndex).toBeGreaterThan(-1);
expect(worktreeBindIndex).toBeGreaterThan(writeBindIndex);
expect(writeBindIndex).toBeGreaterThan(worktreeBindIndex);
});
});
11 changes: 7 additions & 4 deletions packages/core/src/sandbox/linux/bwrapArgsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ export async function buildBwrapArgs(
bwrapArgs.push('--ro-bind-try', p, p);
}

for (const p of resolvedPaths.policyWrite) {
bwrapArgs.push('--bind-try', p, p);
}

for (const file of GOVERNANCE_FILES) {
const filePath = join(workspace.original, file.path);
const realPath = join(workspace.resolved, file.path);
Expand All @@ -115,6 +111,13 @@ export async function buildBwrapArgs(
}
}

// Apply explicit additional write permissions.
// We apply these after GOVERNANCE_FILES and gitWorktree rules so that if a user explicitly requests
// write access to a protected directory (like .git), the explicit allow takes precedence in bwrap.
for (const p of resolvedPaths.policyWrite) {
bwrapArgs.push('--bind-try', p, p);
}
Comment thread
galdawave marked this conversation as resolved.
Resolved

for (const p of resolvedPaths.forbidden) {
if (!fs.existsSync(p)) continue;
try {
Expand Down
27 changes: 25 additions & 2 deletions packages/core/src/sandbox/macos/seatbeltArgsBuilder.ts
Comment thread
galdawave marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ export function buildSeatbeltProfile(options: SeatbeltArgsOptions): string {
);
const realGovernanceFile = resolveToRealPath(governanceFile);

// Skip deny if explicitly allowed by additional write permissions
const isExplicitlyAllowed = resolvedPaths.policyWrite.some(
(p) =>
p === governanceFile ||
p === realGovernanceFile ||
governanceFile.startsWith(p + path.sep) ||
realGovernanceFile.startsWith(p + path.sep),
);

if (isExplicitlyAllowed) {
continue;
}
Comment thread
galdawave marked this conversation as resolved.
Resolved

// Determine if it should be treated as a directory (subpath) or a file (literal).
// .git is generally a directory, while ignore files are literals.
let isDirectory = GOVERNANCE_FILES[i].isDirectory;
Expand All @@ -175,10 +188,20 @@ export function buildSeatbeltProfile(options: SeatbeltArgsOptions): string {
if (resolvedPaths.gitWorktree) {
const { worktreeGitDir, mainGitDir } = resolvedPaths.gitWorktree;
if (worktreeGitDir) {
profile += `(deny file-write* (subpath "${escapeSchemeString(worktreeGitDir)}"))\n`;
const isExplicitlyAllowed = resolvedPaths.policyWrite.some(
(p) => p === worktreeGitDir || worktreeGitDir.startsWith(p + path.sep),
);
Comment thread
galdawave marked this conversation as resolved.
Resolved
if (!isExplicitlyAllowed) {
profile += `(deny file-write* (subpath "${escapeSchemeString(worktreeGitDir)}"))\n`;
}
}
if (mainGitDir) {
profile += `(deny file-write* (subpath "${escapeSchemeString(mainGitDir)}"))\n`;
const isExplicitlyAllowed = resolvedPaths.policyWrite.some(
(p) => p === mainGitDir || mainGitDir.startsWith(p + path.sep),
);
Comment thread
galdawave marked this conversation as resolved.
Resolved
if (!isExplicitlyAllowed) {
profile += `(deny file-write* (subpath "${escapeSchemeString(mainGitDir)}"))\n`;
}
}
}

Expand Down
Loading