From 9de82bade374594b1da7861ffdd1db726955559a Mon Sep 17 00:00:00 2001 From: Zhenhao Liu <176368005+LikC1606@users.noreply.github.com> Date: Mon, 30 Nov 2026 02:08:17 +0800 Subject: [PATCH] Follow top-level links of harness read roots An app worktree outside the home links node_modules to a checkout inside it. The allowlist granted the worktree but not the link target, so the sandbox masked the dependencies and `import("zod")` failed. Resolve the top-level symlinks of each extra read root; deeper links are not scanned. Co-Authored-By: Claude Opus 5.5 --- src/runtime/harnessProcess.ts | 17 ++++++++++++++--- tests/harnessProcess.test.ts | 27 ++++++++++++++++++++------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/runtime/harnessProcess.ts b/src/runtime/harnessProcess.ts index 4995b357..0d81483c 100644 --- a/src/runtime/harnessProcess.ts +++ b/src/runtime/harnessProcess.ts @@ -172,16 +172,27 @@ function linkedTargets(directory: string): string[] { return targets; } +// A granted read root such as an app worktree links its dependencies (`node_modules`) from its top level. +// Only that level is followed: a full scan of a large root would exhaust the link budget. +function rootLinks(root: string): string[] { + let entries: fs.Dirent[]; + try { entries = fs.readdirSync(root, { withFileTypes: true }); } catch { return []; } + return entries.filter(entry => entry.isSymbolicLink()).flatMap(entry => { + try { const real = fs.realpathSync(path.join(root, entry.name)); return isMathPathWithin(root, real) ? [] : [real]; } + catch { return []; /* dangling */ } + }); +} + /** * Home paths a harness launch may read: its PATH (with the installation prefix of each `bin`), the Lean, - * cache and tool roots its environment names, the launcher's package and the targets of symlinks in its - * workspace and profile. Everything else in a home is masked. + * cache and tool roots its environment names, the launcher's package, the targets of symlinks in its + * workspace and profile and of top-level symlinks in its extra read roots. Everything else in a home is masked. */ export function harnessReadRoots(environment: NodeJS.ProcessEnv, directories: readonly string[] = [], executable?: string, extra: readonly string[] = [], homes: readonly string[] = hostHomes()): string[] { const valid = validHomes(homes), roots = new Set(); const add = (value: string | undefined) => { if (value && path.isAbsolute(value)) roots.add(path.resolve(value)); }; - for (const root of extra) add(root); + for (const root of extra) if (path.isAbsolute(root)) { add(root); for (const target of rootLinks(root)) add(target); } for (const key of READ_ROOT_DIRECTORIES) add(environment[key]); // Lean tools fall back to the default elan installation. if (!environment.ELAN_HOME) for (const home of valid) add(path.join(home, '.elan')); diff --git a/tests/harnessProcess.test.ts b/tests/harnessProcess.test.ts index 0bf6fbea..d3aab450 100644 --- a/tests/harnessProcess.test.ts +++ b/tests/harnessProcess.test.ts @@ -70,19 +70,23 @@ describe('shared native harness process isolation', () => { it('hides unlisted home files from shell children while its PATH, toolchain and launch roots stay readable', () => { const home = path.join(root, 'home'), linked = path.join(outside, 'linked-store'), app = path.join(home, 'app'); - for (const directory of ['.config/app', '.codex/packages', '.ssh', '.elan/bin', '.local/bin', 'services/relay', 'app']) - fs.mkdirSync(path.join(home, directory), { recursive: true }); + // An app worktree outside the home links its dependencies from a checkout inside it. + const worktree = path.join(outside, 'worktree'); + for (const directory of ['.config/app', '.codex/packages', '.ssh', '.elan/bin', '.local/bin', 'services/relay', 'app', + 'checkout/node_modules/dep']) fs.mkdirSync(path.join(home, directory), { recursive: true }); + fs.mkdirSync(worktree); fs.symlinkSync(path.join(home, 'checkout/node_modules'), path.join(worktree, 'node_modules')); fs.mkdirSync(linked); fs.writeFileSync(path.join(linked, 'token'), 'linked secret'); fs.symlinkSync(linked, path.join(home, '.config/linked')); const secrets = { '.config/app/token': 'config secret', '.codex/auth.json': 'codex secret', '.codex/packages/tool': 'unlisted', - '.ssh/id': 'ssh secret', 'services/relay/credentials.txt': 'relay secret' }; + '.ssh/id': 'ssh secret', 'services/relay/credentials.txt': 'relay secret', 'checkout/.env': 'checkout secret' }; for (const [file, text] of Object.entries(secrets)) fs.writeFileSync(path.join(home, file), text); - const visible = { '.elan/bin/lean': 'elan', '.local/bin/tool': 'path tool', 'app/main.js': 'app' }; + const visible = { '.elan/bin/lean': 'elan', '.local/bin/tool': 'path tool', 'app/main.js': 'app', + 'checkout/node_modules/dep/index.js': 'dependency' }; for (const [file, text] of Object.entries(visible)) fs.writeFileSync(path.join(home, file), text); vi.stubEnv('HOME', home); try { const environment = { ...process.env, PATH: `${path.join(home, '.local/bin')}:${process.env.PATH}`, ELAN_HOME: '' }; - const launch = prepareHarnessProcess({ workspace, profile, environment, readableRoots: [app] }, process.execPath, ['-e', + const launch = prepareHarnessProcess({ workspace, profile, environment, readableRoots: [app, worktree] }, process.execPath, ['-e', `const fs=require('fs'), cp=require('child_process'); for (const file of ${JSON.stringify([...Object.keys(secrets).map(file => path.join(home, file)), path.join(linked, 'token')])}) { let text=''; try { text=fs.readFileSync(file,'utf8'); } catch(e) { if(e.code!=='ENOENT'&&e.code!=='EACCES') throw e; } @@ -90,12 +94,14 @@ describe('shared native harness process isolation', () => { if(cp.spawnSync('/bin/cat',[file]).stdout.length) process.exit(8); } for (const [file, text] of ${JSON.stringify(Object.entries(visible).map(([file, text]) => [path.join(home, file), text]))}) - if(fs.readFileSync(file,'utf8')!==text) process.exit(9);`]); + if(fs.readFileSync(file,'utf8')!==text) process.exit(9); + if(fs.readFileSync(${JSON.stringify(path.join(worktree, 'node_modules/dep/index.js'))},'utf8')!=='dependency') process.exit(10);`]); const result = spawnSync(launch.command, launch.args, { env: launch.env, encoding: 'utf8', timeout: 10_000 }); expect(result.status, result.stderr).toBe(0); expect(launch.isolationReceipt.hiddenRoots).toEqual(expect.arrayContaining([path.join(home, '.config'), path.join(home, '.codex'), path.join(home, 'services'), linked])); - expect(launch.isolationReceipt.homeReadRoots).toEqual(expect.arrayContaining([app, path.join(home, '.elan'), path.join(home, '.local/bin')])); + expect(launch.isolationReceipt.homeReadRoots).toEqual(expect.arrayContaining([app, worktree, + path.join(home, 'checkout/node_modules'), path.join(home, '.elan'), path.join(home, '.local/bin')])); } finally { vi.unstubAllEnvs(); } expect(fs.readFileSync(path.join(home, '.codex/auth.json'), 'utf8')).toBe('codex secret'); }); @@ -126,6 +132,13 @@ describe('shared native harness process isolation', () => { path.join(home, 'app'), cache, gitdir, path.join(home, 'repo/.git')])); // Neither a home nor its `.local` counts as an installation prefix. for (const personal of [home, path.join(home, '.local'), '/', 'relative']) expect(roots).not.toContain(personal); + // A read root's top-level links are followed; deeper links are not scanned. + const deps = path.join(home, 'deps'), deep = path.join(home, 'deep'), app = path.join(home, 'app'); + for (const directory of [deps, deep, path.join(app, 'sub')]) fs.mkdirSync(directory, { recursive: true }); + fs.symlinkSync(deps, path.join(app, 'node_modules')); fs.symlinkSync(deep, path.join(app, 'sub/deep')); + fs.symlinkSync('sub', path.join(app, 'inner')); fs.symlinkSync(path.join(home, 'missing'), path.join(app, 'dangling')); + expect(harnessReadRoots({ ELAN_HOME: path.join(root, 'elan') }, [], undefined, [app], [home])) + .toEqual([path.join(root, 'elan'), app, deps].sort()); expect(harnessReadRoots({ ELAN_HOME: path.join(root, 'elan') }, [], undefined, [], [home])).toEqual([path.join(root, 'elan')]); // A script launcher brings its interpreter. const node = path.join(home, '.local/node-v1/bin/node'), script = path.join(outside, 'native');