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
17 changes: 14 additions & 3 deletions src/runtime/harnessProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
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'));
Expand Down
27 changes: 20 additions & 7 deletions tests/harnessProcess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,38 @@ 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; }
if(text) process.exit(7);
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');
});
Expand Down Expand Up @@ -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');
Expand Down
Loading