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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,14 @@ jobs:
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
- run: npm ci
- run: npm run build
- name: Linked worktree lane and root Git metadata containment
- name: Linked worktree lane and root Git metadata containment and checkpoint isolation
env:
LIBRECHAT_CODE_LIVE_SRT_TESTS: '1'
run: >-
node --test
dist/linked-worktrees-live.test.js
dist/root-git-denies-live.test.js
dist/checkpoint-isolation-live.test.js

lambda-microvm-provisioning:
name: Lambda MicroVM Provisioning
Expand Down
127 changes: 127 additions & 0 deletions packages/code/src/checkpoint-isolation-live.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import assert from 'node:assert/strict';
import { execFile } from 'node:child_process';
import { mkdir, mkdtemp, readFile, realpath, rm, stat, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { promisify } from 'node:util';
import test from 'node:test';

import { verifyLinkedWorktree } from './linked-worktrees.js';
import { NativeWorkspaceCommandPool } from './native-pool.js';
import { NativeSrtWorkspaceCommandSandbox } from './native-sandbox.js';
import { resolveNativeSrtCommandPolicy } from './native-policy.js';

const exec = promisify(execFile);

function quote(path: string): string {
return `'${path.replaceAll("'", "'\\''")}'`;
}

// This covers effective Linux command policy for a pre-existing, disjoint
// directory. It does not authorize a store or cover host-side file/relay APIs.
test('native Linux command views hide a protected checkpoint directory', {
skip: process.platform !== 'linux' || process.env.LIBRECHAT_CODE_LIVE_SRT_TESTS !== '1',
timeout: 90_000,
}, async (t) => {
const base = await realpath(await mkdtemp(join(tmpdir(), 'checkpoint-isolation-')));
t.after(() => rm(base, { recursive: true, force: true }));
const home = join(base, 'home');
const checkout = join(home, 'checkout');
const second = join(home, 'second');
const store = join(base, 'checkpoints');
await mkdir(checkout, { recursive: true });
await mkdir(second);
await mkdir(store, { mode: 0o700 });
const manifest = join(store, 'manifest');
const lock = join(store, 'lock');
const fresh = join(store, 'new-entry');
const neighbor = join(base, 'public');
await writeFile(manifest, 'fixture manifest', { mode: 0o600 });
await writeFile(lock, 'fixture lock', { mode: 0o600 });
await writeFile(neighbor, 'visible');

const git = (...args: string[]) => exec('git', [
'-c', 'user.name=fixture', '-c', 'user.email=fixture@example.com',
'-c', 'commit.gpgsign=false', ...args,
], { cwd: checkout });
await git('init', '-q', '-b', 'main');
await writeFile(join(checkout, 'tracked'), 'checkout');
await git('add', 'tracked');
await git('commit', '-qm', 'initial');
await git('worktree', 'add', '-q', '-b', 'task', '.worktrees/task');
const lane = await verifyLinkedWorktree(checkout, 'task');

const options = {
homeDirectory: home,
protectedPaths: [store],
commandPolicy: resolveNativeSrtCommandPolicy('trusted-vm'),
};
const pool = new NativeWorkspaceCommandPool(new Map([
['checkout', { ...options, workspaceRoot: checkout }],
['second', { ...options, workspaceRoot: second }],
]), 4);
try {
await pool.registerRoot('lane', {
...options,
workspaceRoot: lane.root,
workspaceIdentity: lane.identity,
linkedWorktree: lane,
});
// Report every access independently. A failed sandbox launch or a broken
// shell must not look like a successful denial.
const command = (writable: string) => [
`cat ${quote(neighbor)} && printf '\\n' || exit 10`,
`printf ok > ${writable} || exit 11`,
`if [ -n "$(ls -A ${quote(store)} 2>/dev/null)" ]; then echo listed; fi`,
...[manifest, lock].flatMap((path) => [
`if cat ${quote(path)} >/dev/null 2>&1; then echo read; fi`,
`(printf changed >> ${quote(path)}) 2>/dev/null || :`,
]),
`(printf new > ${quote(fresh)}) 2>/dev/null || :`,
].join('\n');
// A write to an empty sandbox mask is harmless; verify the host bytes after each view.
const assertStoreIntact = async () => {
assert.equal(await readFile(manifest, 'utf8'), 'fixture manifest');
assert.equal(await readFile(lock, 'utf8'), 'fixture lock');
await assert.rejects(stat(fresh), { code: 'ENOENT' });
};
const request = (workspaceId: string, writable = 'canary') => ({
protocolVersion: 1 as const,
operation: 'execute_command' as const,
workspaceId,
command: command(writable),
timeoutMs: 10_000,
maxOutputBytes: 4096,
});
for (const workspace of ['checkout', 'second', 'lane', 'detached'] as const) {
const detached = workspace === 'detached';
const result = await pool.execute({
...request(detached ? 'checkout' : workspace, detached ? 'detached-canary' : 'canary'),
...(detached ? { detached: true as const } : {}),
});
assert.equal(result.exitCode, 0, `${workspace}: ${result.stderr}`);
assert.equal(result.stdout, 'visible\n', workspace);
await assertStoreIntact();
}

const sandbox = new NativeSrtWorkspaceCommandSandbox({ ...options, workspaceRoot: checkout });
try {
const data = await sandbox.createExecutionDirectory();
for (const probe of [false, true]) {
const result = await sandbox.executeProgrammatic(
request('checkout', '"$LIBRECHAT_CODE_DATA_DIR/canary"'), data, undefined, { probe },
);
assert.equal(result.exitCode, 0, `programmatic probe=${probe}: ${result.stderr}`);
assert.equal(result.stdout, 'visible\n', `programmatic probe=${probe}`);
await assertStoreIntact();
}
} finally {
await sandbox.close();
}
} finally {
await pool.close();
}
assert.equal(await readFile(manifest, 'utf8'), 'fixture manifest');
assert.equal(await readFile(lock, 'utf8'), 'fixture lock');
await assert.rejects(stat(fresh), { code: 'ENOENT' });
});
Loading