test(gateway): make the KBN-101-02a child PATH an allowlist and enumerate it
PATH was the last part of the child environment still built as a denylist: the two workspace node_modules/.bin directories, dirname(process.execPath) and the shell's directory. Measured on this host that is 4,093 distinct executable names, and apps/gateway/node_modules/.bin carries real agent runtimes this repo depends on directly — pi (@mariozechner/pi-coding-agent), pi-ai, openai, anthropic-ai-sdk. The comment on that construction claimed it prevented an unrelated same-name executable from answering, which is the resolution it permitted. PATH is now one fixture-owned directory in the per-run sandbox holding only symlinks: node, tsx, and dirname/sed/uname (the pnpm tsx launcher is a /bin/sh script that calls all three to locate its own entrypoint). The launcher's relative ../tsx/dist/cli.mjs lookup is served by one directory symlink inside the sandbox, off PATH. The shell is invoked absolutely and needs no entry; env needs none because the shebang invokes it absolutely. The fixture control now enumerates the directories on the child's observed PATH and asserts the reachable executable names EQUAL the allowlist. The seven harness-name assertions are kept and commented redundant-by-design: a name enumeration finds pi and misses the other three actually present, which is the empirical argument against the denylist shape. Symlink targets are asserted to resolve to the exact expected binaries. Measured: 4,093 -> 5 reachable names; `command -v pi` resolves the real .bin/pi under the old construction and is not-found under the new one; `tsx --version` still succeeds on the narrowed PATH. Bounds recorded rather than overclaimed: this narrows command resolution, not module resolution — the tsx launcher exports NODE_PATH naming four real repository module roots, the last exposing 775 packages including the same agent runtimes. And the shell-arm key-set measurement describes the control's own arm, not the GREEN route. RED unchanged: 83 failing / 17 passing (100 cases, 6 files), no Errors row, zero /tmp/kbn-02a-* residue, dev:local still absent.
This commit is contained in:
1 parent
3d348772ad
commit
a266c88b7f
2 files changed
+195
-39
No files matched your search
@@ -11,7 +11,18 @@
|
||||
*/
|
||||
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
||||
import {
|
||||
accessSync,
|
||||
constants as fsConstants,
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
realpathSync,
|
||||
statSync,
|
||||
symlinkSync,
|
||||
} from 'node:fs';
|
||||
import { readFile, mkdtemp, realpath, rm } from 'node:fs/promises';
|
||||
import { homedir, tmpdir } from 'node:os';
|
||||
import { dirname, join } from 'node:path';
|
||||
@@ -315,11 +326,9 @@ describe('KBN-101-02a local-dev — the package route refuses, not merely the mo
|
||||
* The POSIX shell the packaged route is executed through, as a validated
|
||||
* absolute path.
|
||||
*
|
||||
* A literal `'sh'` resolved here only because this host keeps Node and the
|
||||
* shell in the same directory, so the constructed `PATH` happened to contain
|
||||
* both. That is a coincidence of layout, not a property of the fixture: on a
|
||||
* host where Node lives elsewhere the constructed `PATH` would not reach a
|
||||
* shell at all, and widening `PATH` to find one would reopen the executable-
|
||||
* The shell is not resolved through `PATH`. It is invoked by this validated
|
||||
* absolute path, so it does not need an entry in the fixture-owned command
|
||||
* directory. Widening `PATH` to find a shell would reopen the executable-
|
||||
* resolution hole the allowlist exists to close.
|
||||
*/
|
||||
function resolveTrustedShell(): string {
|
||||
@@ -333,6 +342,40 @@ describe('KBN-101-02a local-dev — the package route refuses, not merely the mo
|
||||
|
||||
const TRUSTED_SHELL = resolveTrustedShell();
|
||||
|
||||
function resolveFixtureCommand(name: string): string {
|
||||
for (const directory of ['/usr/bin', '/bin']) {
|
||||
const candidate = join(directory, name);
|
||||
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
throw new Error(`fixture command ${name} was not found in /usr/bin or /bin`);
|
||||
}
|
||||
|
||||
function resolveTsxExecutable(): string {
|
||||
for (const candidate of [
|
||||
join(GATEWAY_DIR, 'node_modules', '.bin', 'tsx'),
|
||||
join(REPO_ROOT, 'node_modules', '.bin', 'tsx'),
|
||||
]) {
|
||||
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
throw new Error('tsx was not found in the gateway or repository node_modules/.bin');
|
||||
}
|
||||
|
||||
const TSX_EXECUTABLE = resolveTsxExecutable();
|
||||
const PATH_COMMAND_TARGETS = Object.freeze({
|
||||
node: process.execPath,
|
||||
tsx: TSX_EXECUTABLE,
|
||||
// The pnpm-generated `tsx` launcher uses these three commands to locate and
|
||||
// dispatch its JavaScript entrypoint.
|
||||
dirname: resolveFixtureCommand('dirname'),
|
||||
sed: resolveFixtureCommand('sed'),
|
||||
uname: resolveFixtureCommand('uname'),
|
||||
});
|
||||
const PATH_COMMAND_NAMES = Object.freeze(Object.keys(PATH_COMMAND_TARGETS).sort());
|
||||
|
||||
/**
|
||||
* Every variable name the child is permitted to receive, before the per-case
|
||||
* declarations are layered on.
|
||||
@@ -376,12 +419,13 @@ describe('KBN-101-02a local-dev — the package route refuses, not merely the mo
|
||||
* The complete base environment for one child. Built from nothing; nothing is
|
||||
* inherited. A case may add only the explicitly declarable route inputs.
|
||||
*
|
||||
* `PATH` is constructed rather than inherited: the two workspace `.bin`
|
||||
* directories the package route resolves `tsx` from, the directory holding the
|
||||
* Node binary already running this suite, and the trusted shell's own
|
||||
* directory (which is what supplies the coreutils a `#!/usr/bin/env node`
|
||||
* shebang needs). A host `PATH` would let an unrelated executable of the same
|
||||
* name answer, which is the executable-resolution half of the same problem.
|
||||
* `PATH` names one fixture-owned directory containing symlinks only to the
|
||||
* commands the route needs. The workspace `.bin` directory carries real agent
|
||||
* runtimes this repository depends on, while Node ordinarily lives in
|
||||
* `/usr/bin`; the former construction therefore put thousands of executables
|
||||
* on `PATH` while claiming to prevent an unrelated same-name executable from
|
||||
* answering. A `#!/usr/bin/env node` shebang needs `node` on `PATH`, but `env`
|
||||
* itself needs no entry because the shebang invokes it by absolute path.
|
||||
*
|
||||
* `XDG_CONFIG_DIRS` and `XDG_DATA_DIRS` are set to private empty directories
|
||||
* rather than left unset. Unset is not neutral for the XDG directory *lists*:
|
||||
@@ -396,12 +440,6 @@ describe('KBN-101-02a local-dev — the package route refuses, not merely the mo
|
||||
extraEnv: Record<string, string> = {},
|
||||
): Record<string, string> {
|
||||
const home = join(sandbox, 'home');
|
||||
const pathEntries = [
|
||||
join(GATEWAY_DIR, 'node_modules', '.bin'),
|
||||
join(REPO_ROOT, 'node_modules', '.bin'),
|
||||
dirname(process.execPath),
|
||||
dirname(TRUSTED_SHELL),
|
||||
];
|
||||
const base: Record<string, string> = {
|
||||
HOME: home,
|
||||
// Every XDG root, not only the config one. Leaving any of them ambient
|
||||
@@ -414,12 +452,12 @@ describe('KBN-101-02a local-dev — the package route refuses, not merely the mo
|
||||
XDG_CONFIG_DIRS: join(sandbox, 'xdg', 'config-dirs'),
|
||||
XDG_DATA_DIRS: join(sandbox, 'xdg', 'data-dirs'),
|
||||
TMPDIR: join(sandbox, 'tmp'),
|
||||
PATH: [...new Set(pathEntries)].join(':'),
|
||||
PATH: join(sandbox, 'bin'),
|
||||
};
|
||||
return { ...base, ...extraEnv };
|
||||
}
|
||||
|
||||
/** A private root for one child run: home, XDG trees, tmp, runtime dir. */
|
||||
/** A private root for one child run: home, XDG trees, tmp, runtime dir, bin. */
|
||||
function makeChildSandbox(): string {
|
||||
const sandbox = mkdtempSync(join(tmpdir(), 'kbn-02a-child-'));
|
||||
tempRoots.push(sandbox);
|
||||
@@ -432,12 +470,44 @@ describe('KBN-101-02a local-dev — the package route refuses, not merely the mo
|
||||
'tmp',
|
||||
join('xdg', 'config-dirs'),
|
||||
join('xdg', 'data-dirs'),
|
||||
'bin',
|
||||
]) {
|
||||
mkdirSync(join(sandbox, relative), { recursive: true });
|
||||
}
|
||||
|
||||
const sandboxBin = join(sandbox, 'bin');
|
||||
for (const [name, target] of Object.entries(PATH_COMMAND_TARGETS)) {
|
||||
symlinkSync(target, join(sandboxBin, name));
|
||||
}
|
||||
// pnpm's shell launcher locates `../tsx/dist/cli.mjs` relative to `$0`.
|
||||
// Preserve that layout outside PATH after moving `$0` into the sandbox bin.
|
||||
symlinkSync(join(dirname(TSX_EXECUTABLE), '..', 'tsx'), join(sandbox, 'tsx'), 'dir');
|
||||
|
||||
return sandbox;
|
||||
}
|
||||
|
||||
function enumerateExecutablePathEntries(pathValue: string): Array<{
|
||||
directory: string;
|
||||
name: string;
|
||||
path: string;
|
||||
}> {
|
||||
const entries: Array<{ directory: string; name: string; path: string }> = [];
|
||||
for (const directory of pathValue.split(':')) {
|
||||
for (const name of readdirSync(directory)) {
|
||||
const path = join(directory, name);
|
||||
try {
|
||||
accessSync(path, fsConstants.X_OK);
|
||||
if (statSync(path).isFile()) {
|
||||
entries.push({ directory, name, path });
|
||||
}
|
||||
} catch {
|
||||
// A directory entry that cannot be executed is not reachable as a command.
|
||||
}
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerates a case's declarations once, then validates and snapshots that
|
||||
* same entry array. The single enumeration removes the double-read that two
|
||||
@@ -672,15 +742,42 @@ describe('KBN-101-02a local-dev — the package route refuses, not merely the mo
|
||||
expect(readdirSync(value), isolationReason).toEqual([]);
|
||||
}
|
||||
|
||||
// PATH is the intended directories, in order, and nothing else.
|
||||
expect(observed['PATH']?.split(':')).toEqual([
|
||||
...new Set([
|
||||
join(GATEWAY_DIR, 'node_modules', '.bin'),
|
||||
join(REPO_ROOT, 'node_modules', '.bin'),
|
||||
dirname(process.execPath),
|
||||
dirname(TRUSTED_SHELL),
|
||||
]),
|
||||
]);
|
||||
// PATH is the fixture-owned directory, in order, and nothing else.
|
||||
expect(observed['PATH']?.split(':')).toEqual([join(direct.sandbox, 'bin')]);
|
||||
const observedPath = observed['PATH'];
|
||||
expect(observedPath, 'the marker child had no PATH to enumerate').toBeDefined();
|
||||
if (observedPath === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const reachable = enumerateExecutablePathEntries(observedPath);
|
||||
expect([...new Set(reachable.map(({ name }) => name))].sort()).toEqual(PATH_COMMAND_NAMES);
|
||||
|
||||
// Redundant by design: exact set equality above catches every added command,
|
||||
// while these messages identify the agent-runtime hazard that prompted it.
|
||||
for (const harness of [
|
||||
'pi',
|
||||
'pi-ai',
|
||||
'openai',
|
||||
'anthropic-ai-sdk',
|
||||
'claude',
|
||||
'opencode',
|
||||
'codex',
|
||||
]) {
|
||||
const offenders = reachable.filter(({ name }) => name === harness);
|
||||
expect(
|
||||
offenders,
|
||||
`${harness} is resolvable from fixture PATH in: ${offenders.map(({ directory }) => directory).join(', ')}`,
|
||||
).toEqual([]);
|
||||
}
|
||||
|
||||
for (const [name, expectedTarget] of Object.entries(PATH_COMMAND_TARGETS)) {
|
||||
const fixtureCommand = join(direct.sandbox, 'bin', name);
|
||||
expect(
|
||||
realpathSync(fixtureCommand),
|
||||
`${name} did not resolve to the exact expected binary ${expectedTarget}`,
|
||||
).toBe(realpathSync(expectedTarget));
|
||||
}
|
||||
|
||||
// Executed through the trusted shell, which is the route arm's own shape.
|
||||
// The shell adds names of its own; the claim is not that the final process
|
||||
|
||||
@@ -491,13 +491,65 @@ no better — the specification says a set-but-empty list falls back to the same
|
||||
value has to be a real directory that happens to be empty.
|
||||
|
||||
The shell is resolved to a validated absolute path (`/bin/sh` or `/usr/bin/sh`, checked to exist
|
||||
and to be a file) instead of the literal `sh`. The literal resolved only because this host keeps
|
||||
Node and the shell in the same directory, so the constructed `PATH` happened to contain both. That
|
||||
is a property of the host's layout, not of the fixture; where Node lives elsewhere the constructed
|
||||
`PATH` would reach no shell at all, and widening `PATH` to find one would reopen the
|
||||
executable-resolution hole the allowlist exists to close. `PATH` itself is the two workspace `.bin`
|
||||
directories, the directory of the running Node binary, and the trusted shell's own directory,
|
||||
deduplicated.
|
||||
and to be a file) instead of the literal `sh`. The literal resolved only because an earlier
|
||||
construction of `PATH` happened to contain the shell's own directory, which was a property of this
|
||||
host's layout rather than of the fixture. The shell is now invoked by that validated absolute path
|
||||
and needs no `PATH` entry at all; widening `PATH` to find one would reopen the
|
||||
executable-resolution hole the allowlist exists to close.
|
||||
|
||||
**`PATH` was the last part of the child environment still built as a denylist, and calling it an
|
||||
allowlist was false in a way that a measurement settles.** It named the two workspace
|
||||
`node_modules/.bin` directories, the directory of the running Node binary, and the shell's
|
||||
directory. On this host that is 4,093 distinct executable names — because `dirname(process.execPath)`
|
||||
is `/usr/bin`. Worse than the count: `apps/gateway/node_modules/.bin` carries real agent-runtime
|
||||
executables this repository depends on directly — `pi` (`@mariozechner/pi-coding-agent`), `pi-ai`,
|
||||
`openai`, `anthropic-ai-sdk`. At GREEN, any accidental resolution of one of those names from the
|
||||
package script or its process tree reaches a real agent runtime with real capability. The hard
|
||||
timeout bounds how long that runs; it does not prevent it. A directory list is not a capability
|
||||
allowlist, and the comment on that construction claimed it stopped exactly the resolution it
|
||||
permitted.
|
||||
|
||||
`PATH` is now a single fixture-owned directory inside the per-run sandbox, holding symlinks and
|
||||
nothing else: `node` (to `process.execPath`), `tsx` (to the executable the package route actually
|
||||
resolves), and `dirname`, `sed`, `uname`. The last three are not decoration — the pnpm-generated
|
||||
`tsx` launcher is a `/bin/sh` script that calls all three to locate its own JavaScript entrypoint,
|
||||
so a `PATH` without them does not run the route at all. The launcher also finds
|
||||
`../tsx/dist/cli.mjs` relative to `$0`, so the sandbox carries one directory symlink to the `tsx`
|
||||
package at that relative location; it is not on `PATH`. `env` needs no entry either, because a
|
||||
`#!/usr/bin/env node` shebang invokes `env` by absolute path and only `node` is resolved through
|
||||
`PATH`. Measured: five reachable executable names, down from 4,093; `command -v pi` reports the real
|
||||
`.bin/pi` under the old construction and not-found under the new one; `tsx --version` still succeeds
|
||||
on the narrowed `PATH`.
|
||||
|
||||
The control does not assert that in prose. It splits the child's **observed** `PATH`, reads each
|
||||
directory, collects every entry that is an executable file, and asserts that name set **equals**
|
||||
the allowlist. Equality is the point: a "does not contain" check is the denylist shape, and the
|
||||
denylist shape is what failed here — an enumeration of known harness names finds `pi` and misses
|
||||
`pi-ai`, `openai` and `anthropic-ai-sdk`, three of the four actually present. The seven harness
|
||||
names are still asserted individually, and that assertion is redundant by design and commented as
|
||||
such: set equality catches every addition including ones nobody enumerated, while the named
|
||||
assertion is the one whose failure message a maintainer will understand. Each symlink's target is
|
||||
also asserted to resolve to the exact expected binary, so a fixture-owned `tsx` cannot quietly
|
||||
become something else.
|
||||
|
||||
Two bounds on that claim, stated because the recurring defect in this item is prose crediting a
|
||||
mechanism with a property it lacks. First, the claim is about **command resolution**, not about
|
||||
everything the route can reach: the pnpm `tsx` launcher exports `NODE_PATH` naming four real
|
||||
repository module roots into the process it starts, and the last of them
|
||||
(`node_modules/.pnpm/node_modules`) exposes 775 packages — `@mariozechner/pi-coding-agent`,
|
||||
`@anthropic-ai` and `openai` among them. Narrowing `PATH` removes those runtimes as _commands_; it
|
||||
does not remove them as _modules_, and nothing at this layer could, because the route legitimately
|
||||
runs repository code against repository `node_modules`. Second, executable enumeration is a
|
||||
mechanical coverage check and is separate from reading source: the review that passed this fixture
|
||||
before the hole was found was bounded by paths whose directory contents it never listed, which is
|
||||
precisely why the guarantee is now measured by the fixture rather than observed by a reviewer.
|
||||
|
||||
This is the third real-runtime-reachability incident in this window — the FL-09 ambient OpenCode
|
||||
fall-through, the review command-substitution Claude launch, and now real agent executables on a
|
||||
fixture `PATH` alongside the system bin directory. Taken together they argue for two standing
|
||||
properties rather than three separate patches: fail-loud semantics when a real fall-through is
|
||||
possible, and capability-minimal fixture `PATH`s. Private `HOME`/XDG roots and a hard timeout
|
||||
reduce exposure; neither proves that no runtime or auth state was read.
|
||||
|
||||
The claim is **not** that the final route process receives the builder's key set exactly. The route
|
||||
is executed through that shell, and a POSIX shell sets some of its own bookkeeping names. The
|
||||
@@ -509,7 +561,10 @@ derived from the invocation — `PWD` from `cwd`, `SHLVL` from a counter, `_` fr
|
||||
the command — so none carries host state, but they are additions and the document says so rather
|
||||
than rounding them away. The measured claim is: the shell-mediated child receives every allowlisted
|
||||
name with the value the builder constructed for it, and every name beyond the allowlist is one of
|
||||
those five.
|
||||
those five. That measurement is of the control's own arm, which runs `sh -c 'exec node -e …'`. It
|
||||
does not describe the GREEN route, which runs through the pnpm `tsx` launcher and will therefore
|
||||
also carry the `NODE_PATH` that launcher exports. What the route's own process ends up holding is
|
||||
unmeasured until there is a GREEN to measure.
|
||||
|
||||
**There is exactly one place in the file where a child process is created**, and that is the
|
||||
mechanism, not tidiness. A control that made its own spawn would leave the route's spawn free to be
|
||||
@@ -589,9 +644,13 @@ one site: directly, where the observed key set must equal the allowlist exactly
|
||||
the route's own shape, where every allowlisted name must be present, must still carry the value the
|
||||
builder constructed for that spawn's own sandbox, and every added name must be one of the shell's
|
||||
bookkeeping names. Presence alone was not enough: a shell-mediated child that changed `HOME`,
|
||||
`PATH` or an XDG root would keep every name and stay green. It is green by construction and is a **harness control, not
|
||||
`PATH` or an XDG root would keep every name and stay green. Beyond the key set, it enumerates the
|
||||
directories on the child's observed `PATH` and asserts the reachable executable names **equal** the
|
||||
allowlist, asserts the seven known agent-runtime names are absent, and asserts each symlink resolves
|
||||
to its exact expected binary. It is green by construction and is a **harness control, not
|
||||
coverage** — it asserts nothing about the code under test. Its purpose is to redden if the fixture
|
||||
is widened back toward the ambient environment.
|
||||
is widened back toward the ambient environment, including by a new executable appearing on `PATH`
|
||||
that nobody thought to name.
|
||||
|
||||
`does not leak the parent ambient environment into the child` is the route-arm half, and it is
|
||||
**pending execution until GREEN**. At RED head there is no `dev:local` script, so the manifest
|
||||
|
||||
Reference in new issue
Block a user