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
59 changes: 31 additions & 28 deletions packages/core/src/sandbox/macos/MacOsSandboxManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,12 @@ describe('MacOsSandboxManager', () => {
policy: mockPolicy,
});

expect(seatbeltArgsBuilder.buildSeatbeltProfile).toHaveBeenCalledWith({
workspace: mockWorkspace,
allowedPaths: mockAllowedPaths,
forbiddenPaths: [],
networkAccess: mockNetworkAccess,
workspaceWrite: false,
additionalPermissions: {
fileSystem: {
read: [],
write: [],
},
network: true,
},
});
expect(seatbeltArgsBuilder.buildSeatbeltProfile).toHaveBeenCalledWith(
expect.objectContaining({
networkAccess: true,
workspaceWrite: false,
}),
);

expect(result.program).toBe('/usr/bin/sandbox-exec');
expect(result.args[0]).toBe('-f');
Expand Down Expand Up @@ -155,11 +147,10 @@ describe('MacOsSandboxManager', () => {

expect(seatbeltArgsBuilder.buildSeatbeltProfile).toHaveBeenCalledWith(
expect.objectContaining({
additionalPermissions: expect.objectContaining({
fileSystem: expect.objectContaining({
read: expect.not.arrayContaining(['/']),
write: expect.not.arrayContaining(['/']),
}),
workspaceWrite: true,
resolvedPaths: expect.objectContaining({
policyRead: expect.not.arrayContaining(['/']),
policyWrite: expect.not.arrayContaining(['/']),
}),
}),
);
Expand Down Expand Up @@ -213,7 +204,11 @@ describe('MacOsSandboxManager', () => {
// The seatbelt builder internally handles governance files, so we simply verify
// it is invoked correctly with the right workspace.
expect(seatbeltArgsBuilder.buildSeatbeltProfile).toHaveBeenCalledWith(
expect.objectContaining({ workspace: mockWorkspace }),
expect.objectContaining({
resolvedPaths: expect.objectContaining({
workspace: { resolved: mockWorkspace, original: mockWorkspace },
}),
}),
);
});
});
Expand All @@ -233,10 +228,12 @@ describe('MacOsSandboxManager', () => {

expect(seatbeltArgsBuilder.buildSeatbeltProfile).toHaveBeenCalledWith(
expect.objectContaining({
allowedPaths: expect.arrayContaining([
'/tmp/allowed1',
'/tmp/allowed2',
]),
resolvedPaths: expect.objectContaining({
policyAllowed: expect.arrayContaining([
'/tmp/allowed1',
'/tmp/allowed2',
]),
}),
}),
);
});
Expand All @@ -258,7 +255,9 @@ describe('MacOsSandboxManager', () => {

expect(seatbeltArgsBuilder.buildSeatbeltProfile).toHaveBeenCalledWith(
expect.objectContaining({
forbiddenPaths: expect.arrayContaining(['/tmp/forbidden1']),
resolvedPaths: expect.objectContaining({
forbidden: expect.arrayContaining(['/tmp/forbidden1']),
}),
}),
);
});
Expand All @@ -278,7 +277,9 @@ describe('MacOsSandboxManager', () => {

expect(seatbeltArgsBuilder.buildSeatbeltProfile).toHaveBeenCalledWith(
expect.objectContaining({
forbiddenPaths: expect.arrayContaining(['/tmp/does-not-exist']),
resolvedPaths: expect.objectContaining({
forbidden: expect.arrayContaining(['/tmp/does-not-exist']),
}),
}),
);
});
Expand All @@ -301,8 +302,10 @@ describe('MacOsSandboxManager', () => {

expect(seatbeltArgsBuilder.buildSeatbeltProfile).toHaveBeenCalledWith(
expect.objectContaining({
allowedPaths: [],
forbiddenPaths: expect.arrayContaining(['/tmp/conflict']),
resolvedPaths: expect.objectContaining({
policyAllowed: [],
forbidden: expect.arrayContaining(['/tmp/conflict']),
}),
}),
);
});
Expand Down
22 changes: 10 additions & 12 deletions packages/core/src/sandbox/macos/MacOsSandboxManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,26 @@ export class MacOsSandboxManager implements SandboxManager {
false,
};

const resolvedPaths = await resolveSandboxPaths(
this.options,
const { command: finalCommand, args: finalArgs } = handleReadWriteCommands(
req,
mergedAdditional,
this.options.workspace,
[
...(req.policy?.allowedPaths || []),
...(this.options.includeDirectories || []),
],
);
const { command: finalCommand, args: finalArgs } = handleReadWriteCommands(

const resolvedPaths = await resolveSandboxPaths(
this.options,
req,
mergedAdditional,
this.options.workspace,
req.policy?.allowedPaths,
);

const sandboxArgs = buildSeatbeltProfile({
workspace: this.options.workspace,
allowedPaths: [
...resolvedPaths.policyAllowed,
...(this.options.includeDirectories || []),
],
forbiddenPaths: resolvedPaths.forbidden,
resolvedPaths,
Comment thread
This conversation was marked as resolved.
networkAccess: mergedAdditional.network,
workspaceWrite,
additionalPermissions: mergedAdditional,
});

const tempFile = this.writeProfileToTempFile(sandboxArgs);
Expand Down
144 changes: 41 additions & 103 deletions packages/core/src/sandbox/macos/seatbeltArgsBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import {
buildSeatbeltProfile,
escapeSchemeString,
} from './seatbeltArgsBuilder.js';
import * as fsUtils from '../utils/fsUtils.js';
import type { ResolvedSandboxPaths } from '../../services/sandboxManager.js';
import fs from 'node:fs';
import os from 'node:os';

vi.mock('../utils/fsUtils.js', async () => {
const actual = await vi.importActual('../utils/fsUtils.js');
return {
...actual,
tryRealpath: vi.fn((p) => p),
resolveGitWorktreePaths: vi.fn(() => ({})),
};
});
const defaultResolvedPaths: ResolvedSandboxPaths = {
workspace: {
resolved: '/Users/test/workspace',
original: '/Users/test/raw-workspace',
},
forbidden: [],
globalIncludes: [],
policyAllowed: [],
policyRead: [],
policyWrite: [],
};

describe.skipIf(os.platform() === 'win32')('seatbeltArgsBuilder', () => {
afterEach(() => {
Expand All @@ -35,12 +38,8 @@ describe.skipIf(os.platform() === 'win32')('seatbeltArgsBuilder', () => {

describe('buildSeatbeltProfile', () => {
it('should build a strict allowlist profile allowing the workspace', () => {
vi.mocked(fsUtils.tryRealpath).mockImplementation((p) => p);

const profile = buildSeatbeltProfile({
workspace: '/Users/test/workspace',
allowedPaths: [],
forbiddenPaths: [],
resolvedPaths: defaultResolvedPaths,
});

expect(profile).toContain('(version 1)');
Expand All @@ -51,19 +50,18 @@ describe.skipIf(os.platform() === 'win32')('seatbeltArgsBuilder', () => {
});

it('should allow network when networkAccess is true', () => {
vi.mocked(fsUtils.tryRealpath).mockImplementation((p) => p);
const profile = buildSeatbeltProfile({
workspace: '/test',
allowedPaths: [],
forbiddenPaths: [],
resolvedPaths: {
...defaultResolvedPaths,
workspace: { resolved: '/test', original: '/test' },
},
networkAccess: true,
});
expect(profile).toContain('(allow network-outbound)');
});

describe('governance files', () => {
it('should inject explicit deny rules for governance files', () => {
vi.mocked(fsUtils.tryRealpath).mockImplementation((p) => p.toString());
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
vi.spyOn(fs, 'lstatSync').mockImplementation(
(p) =>
Expand All @@ -74,9 +72,13 @@ describe.skipIf(os.platform() === 'win32')('seatbeltArgsBuilder', () => {
);

const profile = buildSeatbeltProfile({
workspace: '/test/workspace',
allowedPaths: [],
forbiddenPaths: [],
resolvedPaths: {
...defaultResolvedPaths,
workspace: {
resolved: '/test/workspace',
original: '/test/workspace',
},
},
});

expect(profile).toContain(
Expand All @@ -87,48 +89,16 @@ describe.skipIf(os.platform() === 'win32')('seatbeltArgsBuilder', () => {
`(deny file-write* (subpath "/test/workspace/.git"))`,
);
});

it('should protect both the symlink and the real path if they differ', () => {
vi.mocked(fsUtils.tryRealpath).mockImplementation((p) => {
if (p === '/test/workspace/.gitignore')
return '/test/real/.gitignore';
return p.toString();
});
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
vi.spyOn(fs, 'lstatSync').mockImplementation(
() =>
({
isDirectory: () => false,
isFile: () => true,
}) as unknown as fs.Stats,
);

const profile = buildSeatbeltProfile({
workspace: '/test/workspace',
allowedPaths: [],
forbiddenPaths: [],
});

expect(profile).toContain(
`(deny file-write* (literal "/test/workspace/.gitignore"))`,
);
expect(profile).toContain(
`(deny file-write* (literal "/test/real/.gitignore"))`,
);
});
});

describe('allowedPaths', () => {
it('should embed allowed paths and normalize them', () => {
vi.mocked(fsUtils.tryRealpath).mockImplementation((p) => {
if (p === '/test/symlink') return '/test/real_path';
return p;
});

it('should embed allowed paths', () => {
const profile = buildSeatbeltProfile({
workspace: '/test',
allowedPaths: ['/custom/path1', '/test/symlink'],
forbiddenPaths: [],
resolvedPaths: {
...defaultResolvedPaths,
workspace: { resolved: '/test', original: '/test' },
policyAllowed: ['/custom/path1', '/test/real_path'],
},
});

expect(profile).toContain(`(subpath "/custom/path1")`);
Expand All @@ -138,59 +108,27 @@ describe.skipIf(os.platform() === 'win32')('seatbeltArgsBuilder', () => {

describe('forbiddenPaths', () => {
it('should explicitly deny forbidden paths', () => {
vi.mocked(fsUtils.tryRealpath).mockImplementation((p) => p);

const profile = buildSeatbeltProfile({
workspace: '/test',
allowedPaths: [],
forbiddenPaths: ['/secret/path'],
resolvedPaths: {
...defaultResolvedPaths,
workspace: { resolved: '/test', original: '/test' },
forbidden: ['/secret/path'],
},
});

expect(profile).toContain(
`(deny file-read* file-write* (subpath "/secret/path"))`,
);
});

it('resolves forbidden symlink paths to their real paths', () => {
vi.mocked(fsUtils.tryRealpath).mockImplementation((p) => {
if (p === '/test/symlink' || p === '/test/missing-dir') {
return '/test/real_path';
}
return p;
});

const profile = buildSeatbeltProfile({
workspace: '/test',
allowedPaths: [],
forbiddenPaths: ['/test/symlink'],
});

expect(profile).toContain(
`(deny file-read* file-write* (subpath "/test/real_path"))`,
);
});

it('explicitly denies non-existent forbidden paths to prevent creation', () => {
vi.mocked(fsUtils.tryRealpath).mockImplementation((p) => p);

const profile = buildSeatbeltProfile({
workspace: '/test',
allowedPaths: [],
forbiddenPaths: ['/test/missing-dir/missing-file.txt'],
});

expect(profile).toContain(
`(deny file-read* file-write* (subpath "/test/missing-dir/missing-file.txt"))`,
);
});

it('should override allowed paths if a path is also in forbidden paths', () => {
vi.mocked(fsUtils.tryRealpath).mockImplementation((p) => p);

const profile = buildSeatbeltProfile({
workspace: '/test',
allowedPaths: ['/custom/path1'],
forbiddenPaths: ['/custom/path1'],
resolvedPaths: {
...defaultResolvedPaths,
workspace: { resolved: '/test', original: '/test' },
policyAllowed: ['/custom/path1'],
forbidden: ['/custom/path1'],
},
});

const allowString = `(allow file-read* file-write* (subpath "/custom/path1"))`;
Expand Down
Loading
Loading