From ab05e187a9097dacc0f74b07e4928e30e850bfab Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 4 Dec 2026 19:58:39 -0500 Subject: [PATCH 1/2] test(worker): Verify checkpoint directory isolation across native command views --- .github/workflows/ci.yml | 3 +- .../src/checkpoint-isolation-live.test.ts | 119 ++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 packages/code/src/checkpoint-isolation-live.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c39caf27..67fe7c69 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/packages/code/src/checkpoint-isolation-live.test.ts b/packages/code/src/checkpoint-isolation-live.test.ts new file mode 100644 index 00000000..c2d080dd --- /dev/null +++ b/packages/code/src/checkpoint-isolation-live.test.ts @@ -0,0 +1,119 @@ +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`, + `if (printf changed >> ${quote(path)}) 2>/dev/null; then echo appended; fi`, + ]), + `(printf new > ${quote(fresh)}) 2>/dev/null || :`, + ].join('\n'); + 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); + } + + 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}`); + } + } 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' }); +}); From 5dc1741ee279ca8f5376348adb709d7dbdc2d8ef Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 4 Dec 2026 20:02:31 -0500 Subject: [PATCH 2/2] Check checkpoint host integrity across sandbox views --- packages/code/src/checkpoint-isolation-live.test.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/code/src/checkpoint-isolation-live.test.ts b/packages/code/src/checkpoint-isolation-live.test.ts index c2d080dd..ef083127 100644 --- a/packages/code/src/checkpoint-isolation-live.test.ts +++ b/packages/code/src/checkpoint-isolation-live.test.ts @@ -75,10 +75,16 @@ test('native Linux command views hide a protected checkpoint directory', { `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`, - `if (printf changed >> ${quote(path)}) 2>/dev/null; then echo appended; 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, @@ -95,6 +101,7 @@ test('native Linux command views hide a protected checkpoint directory', { }); assert.equal(result.exitCode, 0, `${workspace}: ${result.stderr}`); assert.equal(result.stdout, 'visible\n', workspace); + await assertStoreIntact(); } const sandbox = new NativeSrtWorkspaceCommandSandbox({ ...options, workspaceRoot: checkout }); @@ -106,6 +113,7 @@ test('native Linux command views hide a protected checkpoint directory', { ); 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();