Files
PaperClipAI/packages/paperclip-runner/src/drivers/codex/codex-security-config.test.ts
T
Nicky LeachandPaperclip fa9f3f8a08 feat(runner): support preconfigured user homes for Codex and ACPX (#16486)
## Thinking Path

> - Paperclip manages AI agents and their work.
> - Runner launches the selected provider and keeps durable session
state.
> - Runner currently prepares private provider homes for each session.
> - Operators can already have working login, skills, and MCP
configuration on the execution machine.
> - This PR adds an explicit mode that uses that existing setup.
> - Runner keeps its task bridge and session ownership while leaving the
user's configuration in place.

## Linked Issues or Issue Description

**Problem or motivation:** Rebuilding a provider home is unnecessary
when the execution account is already configured.

**Proposed solution:** Add `homeMode: "preconfigured"` to Runner adapter
config and runnerd transport options. Keep `"isolated"` as the default.
Initially support native Codex and ACPX Claude.

**Alternatives considered:** Continue copying user configuration into a
private home, or add an approval workflow. Both add work outside this
feature's scope.

**Roadmap alignment:** This extends the existing cross-provider Runner.
It does not add another runtime.

Related work: #14056 covers managed Codex credentials. #14474 covers
command environment delivery. This mode uses user-owned credentials and
retains the existing launch boundaries.

## What Changed

- Add a closed v7 launch contract for the selected home mode. Existing
contracts retain their behavior.
- Keep homeMode under operator control. Reject task overrides and
agent-authenticated changes, including removal through replacements,
adapter changes, or rollback. Ignore stored task values and exclude it
from automatic hire inheritance.
- Skip managed AI login, assigned-skill materialization, and external
MCP setup in preconfigured mode. Preserve the Runner task/completion
bridge.
- Keep protocol state and provider lifetime fences scoped to the Runner
session. Different sessions can share a home.
- Fail preconfigured ACPX planning/read-only launches before provider
admission until a read-only user-home launch is qualified. Use isolated
mode or native Codex for planning.
- Reject ACPX approval requests. Keep native Codex on `never`. Local
permission rules and hooks remain effective. Initially require the
default Runner ACPX `approve-all` setting; other Runner policies are
unqualified in this mode and fail before launch.
- Keep the internal ACPX bridge in a stable session namespace,
preserving an existing user MCP server named `paperclip`.
- Reject preconfigured ACPX Codex before launch until a session
permission profile can enforce Runner-state denial. Remove the
unprotected full-access mode switch; use native Codex or isolated mode
for that model family.
- Bump Pi to profile 23 because its attestation covers shared Runner
source changed here. Preserve profile 22 for historical recovery, and
update Docker/package verification inputs.
- Fence recovery on mode and home bindings. Fail missing
provider-history recovery without starting a replacement session.
Exclude the external Codex home from backups and credential cleanup.
- Discover remote HOME on the execution machine. Do not project
controller HOME or XDG paths onto it.
- Separate task instructions from Codex ACP isolation and update the
generated provider digest.
- Keep remote configured-environment manifests consistent when dropping
controller paths. Preserve exact Claude model IDs through per-launch
environment settings, without editing the user home or patching the
provider.

## Verification

- Targeted local checks cover native contracts and launch allowlists,
user-config preservation, provider admission, permissions, session
recovery, remote environment projection, heartbeat preparation, and
operator-only configuration. Runner, shared, and server TypeScript
checks and the Rust binary build pass; generated ACPX profiles are
current.
- Following the latest master integration, all 757 server
configuration/executor tests and 115 Runner admission/recovery tests
passed. Additional review-fix checks passed: 103 planning contract/host
tests, 78 provider-backend tests, 95 route/preparation/inheritance
tests, 43 task-validator tests, and 43 workspace-policy tests. Counts
describe separate runs and can overlap.
- The real pinned Claude ACP/SDK regression reproduces exact-model
aliasing without the fix (`claude-sonnet-5-5` becomes `sonnet`) and
passes with the fix. All 89 adapter tests pass. User settings remain
byte-for-byte unchanged.
- Localhost-only Claude SDK probes with synthetic credentials verify the
private bridge runs, local `ask` reaches the existing reject callback,
local `deny` blocks without a callback, and a user MCP server named
`paperclip` remains available. No user model allowlist is required.
- Review regressions cover fail-closed provider admission, saved-mode
recovery before routing, controller-home path removal without
configured-environment manifest drift, pre-launch rejection of
unqualified read-only ACPX launches, and denial of task/agent changes to
the operator-owned home mode.
- Before rebase, `d4d4ee2aebe4cb88c4f4d9ef8e22fed4b36b87be` received
standard Greptile 5/5 with no unresolved threads. Prior Apex findings
are fixed and covered by regressions. No manual Greptile retrigger is
requested for this rebase, per reviewer direction.
- The prior PR head and master hit the same GitHub runner encoding crash
in workspace-b on three PR attempts, including diagnostic logging
([prior PR
CI](https://github.com/paperclipai/paperclip/actions/runs/38818474563/job/116714538405),
[prior master
CI](https://github.com/paperclipai/paperclip/actions/runs/38819528748/job/116712423193)).
Rebased onto `origin/master` at `c3ffa3f0dd` to pull in its bridge-test
index correction. The rebased head
`edd153db410bea063b645e0000aa68649f287b00` passes all CI checks ([CI
run](https://github.com/paperclipai/paperclip/actions/runs/38824676914)),
including workspace-b. Greptile's automatic standard review is 5/5 on
this exact head, with no unresolved threads; no manual review was
requested.
- Local workspace-b investigation passed 911 shared tests and 3,868
tests across the remaining 15 workspace projects. The catalog run
separately hit an existing ignored
`.agents/skills/design-guide/SKILL.md` description-budget violation in
this checkout; that file is untracked, outside the PR, and left
untouched. Repeating shared tests with CI reporting enabled also passed.
- Rebase verification: the final Git tree exactly matches the previously
reviewed branch combined with current master. All 192 Runner
admission/contract tests, 61 server preparation/provider tests, and 68
bridge tests pass, as do Runner/server TypeScript and generated-profile
checks. The real Claude adapter fixture hit an `ENOTEMPTY` cleanup race
on its initial run; all 89 adapter tests passed on retry.
- Apex on rebased head `edd153db` returned 2/5: ACPX Codex selected full
access without enforcing Runner-state denial, and agent configuration
replacement could remove the operator mode. Both are fixed in
`26e14dbbf`. Preconfigured ACPX Codex is temporarily unqualified;
re-enabling it requires session permission enforcement on thread/turn
operations and a real filesystem-denial probe. The config guard covers
effective replacements, adapter changes, and rollback while allowing
ordinary agent merges and operator removal. All 190 focused Runner
tests, 103 server tests, both typechecks, and generated-profile checks
pass. Standard review and CI passed before the next Apex request.
- CI on `26e14dbbf` passed 1,967 Runner-shard tests but hit the
previously observed `ENOTEMPTY` fixture cleanup race after successful
Claude model admission. `eb83284fb` adds bounded filesystem retries to
that fixture's teardown; all 87 adapter tests pass. Assertions and
production cleanup behavior are unchanged. Head
`eb83284fb42ee19a15d9a93b106c1bbb07e1695e` passes all 59 CI checks ([CI
run](https://github.com/paperclipai/paperclip/actions/runs/38829500857))
and standard Greptile at 5/5 with no unresolved threads. Apex also
completed at **5/5** on that exact head ([Apex
result](https://github.com/paperclipai/paperclip/pull/16486#issuecomment-6201559527)).
All review threads are resolved, the branch has no merge conflicts, and
the PR remains unmerged.
- No live paid-provider or remote-machine smoke was performed. The
lockfile remains unchanged under the repository's refresh-bot policy;
local dependency installation resolved the changed patch hash.

## Risks

This explicit opt-in grants Codex tools access to the execution home
(read-only in planning), including credentials. Native Codex separately
denies Runner-owned protocol state. ACPX Claude retains its existing
provider trust boundary, and the isolated default is unchanged.
Providers may refresh credentials and update their own metadata.

User configuration can block tools or the completion reporter. Existing
blocked results and failure diagnostics remain the recovery path. Edit
the local settings and retry; no new approval workflow is added.

External provider history remains on the execution machine and is not
backed up by Runner. Remote HOME discovery requires `getent` and fails
explicitly when unavailable. Other harnesses need separate
qualification.

The Codex ACP bundle digest changes. Existing sessions tied to the old
bundle may need a fresh session. No database migration is required.

## Model Used

OpenAI Codex, GPT-6, with reasoning and code execution. The exact
runtime model ID was not exposed.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have described the issue in-PR following the feature issue
template
- [x] I have not referenced internal/instance-local Paperclip issues or
links
- [x] My branch name describes the change and contains no internal
ticket id
- [x] I have run targeted tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented risks above

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-10-15 17:57:58 -07:00

492 lines
31 KiB
TypeScript

import { createRequire } from "node:module";
import { execFileSync } from "node:child_process";
import { dirname, resolve } from "node:path";
import { resolvePinnedCodexCommand } from "./codex-command.js";
import { evalProviderTransportOptions } from "../../cli/eval-provider-runtime.js";
import { describe, expect, it } from "vitest";
import {
codexExecutableReadOnlyRoots,
codexNetworkReadOnlyRoots,
createIsolatedCodexAppServerArgs,
createSecuredCodexThreadParams,
createSkilllessCodexThreadConfig,
} from "./codex-security-config.js";
describe("Codex security configuration", () => {
it("uses the same actual pinned executable for native defaults and direct evals without a global Codex PATH", () => {
const command = resolvePinnedCodexCommand();
expect(evalProviderTransportOptions("codex").codexCommand).toBe(command);
expect(execFileSync(command, ["--version"], { env: { PATH: dirname(process.execPath) },
encoding: "utf8", timeout: 30_000, maxBuffer: 16 * 1024 }).trim()).toBe("codex-cli 0.160.0");
});
it("resolves usable installed Codex commands and an absent-package PATH fallback while rejecting malformed or escaped dependencies", () => {
// Vitest adds its dependency directories to global module lookup paths.
// Use the existing Node/tsx boundary so an absent fixture dependency cannot
// be supplied by the test runner's installed bridge instead.
const source = `
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { createRequire } from "node:module";
import { chmodSync, mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { delimiter, dirname, isAbsolute, join } from "node:path";
import { resolveCodexCommand, resolvePinnedCodexCommand } from ${JSON.stringify(new URL("./codex-command.ts", import.meta.url).href)};
import { codexExecutableReadOnlyRoots, createIsolatedCodexAppServerArgs } from ${JSON.stringify(new URL("./codex-security-config.ts", import.meta.url).href)};
const root = realpathSync(mkdtempSync(join(tmpdir(), "paperclip-pinned-codex-test-")));
const pathOnlyRoot = realpathSync(mkdtempSync(join(tmpdir(), "paperclip-path-codex-test-")));
const pathDirectory = join(pathOnlyRoot, "bin");
const pathCommand = join(pathDirectory, "codex");
const commandEnvironment = { PATH: pathDirectory };
const issuer = join(root, "node_modules/@paperclipai/server/dist/vendor/paperclip-runner/drivers/codex/codex-command.js");
const adapter = join(root, "node_modules/@paperclipai/adapter-codex-local");
const bridge = join(adapter, "node_modules/@agentclientprotocol/codex-acp");
const codex = join(bridge, "node_modules/@openai/codex");
const executable = join(codex, "bin/codex.js");
const platformName = "@openai/codex-" + process.platform + "-" + process.arch;
const platform = join(root, "node_modules", platformName), vendor = join(platform, "vendor");
const metadata = { name: "@openai/codex", version: "0.160.0", bin: { codex: "bin/codex.js" },
optionalDependencies: { [platformName]: "npm:@openai/codex@0.160.0-" + process.platform + "-" + process.arch } };
try {
mkdirSync(pathDirectory);
writeFileSync(pathCommand, "#!" + process.execPath + "\\nif (process.argv.slice(2).join(' ') !== '--version') process.exit(9); console.log('codex-cli 0.141.0');\\n", { mode: 0o755 });
const pathOnlyIssuer = join(pathOnlyRoot, "standalone-command.js");
assert.equal(resolveCodexCommand(pathOnlyIssuer, commandEnvironment), pathCommand, "Only absent dependency graphs use the selected PATH command");
assert.equal(execFileSync(resolveCodexCommand(pathOnlyIssuer, commandEnvironment), ["--version"], {
env: commandEnvironment, encoding: "utf8", timeout: 5_000 }).trim(), "codex-cli 0.141.0");
const relativeCommand = resolveCodexCommand(pathOnlyIssuer, { PATH: "./bin" }, pathOnlyRoot);
assert.equal(relativeCommand, pathCommand, "Relative PATH entries use the provider working directory");
assert.ok(isAbsolute(relativeCommand), "Provider launch receives an absolute executable");
assert.equal(execFileSync(relativeCommand, ["--version"], {
cwd: root, env: { PATH: "/missing-codex-command" }, encoding: "utf8", timeout: 5_000 }).trim(), "codex-cli 0.141.0");
const cwdCommand = join(pathOnlyRoot, "codex");
writeFileSync(cwdCommand, "#!" + process.execPath + "\\n", { mode: 0o755 });
const secondDirectory = join(pathOnlyRoot, "second-bin");
mkdirSync(secondDirectory);
const secondCommand = join(secondDirectory, "codex");
writeFileSync(secondCommand, "#!" + process.execPath + "\\n", { mode: 0o755 });
for (const path of [".", "", "./missing" + delimiter, delimiter + "./bin"]) {
assert.equal(resolveCodexCommand(pathOnlyIssuer, { PATH: path }, pathOnlyRoot), cwdCommand,
"Dot and empty PATH entries retain their ordered current-directory meaning");
}
for (const [path, expected] of [
["./bin" + delimiter + ".", pathCommand],
["./bin" + delimiter + "./second-bin", pathCommand],
["./second-bin" + delimiter + "./bin", secondCommand],
["./missing" + delimiter + "./bin", pathCommand],
]) assert.equal(resolveCodexCommand(pathOnlyIssuer, { PATH: path }, pathOnlyRoot), expected,
"Relative PATH resolution preserves search ordering and skips missing entries");
for (const environment of [{}, { PATH: "/missing-codex-command" }, { PATH: "./missing" }]) {
assert.throws(() => resolveCodexCommand(pathOnlyIssuer, environment, pathOnlyRoot),
/runtime unavailable.*No installed Codex dependency or executable on PATH/,
"An absent PATH must not implicitly authorize the provider working directory");
}
assert.throws(() => resolveCodexCommand(pathOnlyIssuer, { PATH: "./bin" }, root), /runtime unavailable/,
"Relative PATH must not use a different provider working directory");
const previousCwd = process.cwd();
try {
process.chdir(pathOnlyRoot);
assert.equal(resolveCodexCommand(pathOnlyIssuer, { PATH: "./bin" }), pathCommand,
"Omitted working directory follows the current process directory");
} finally { process.chdir(previousCwd); }
const directCodex = join(pathOnlyRoot, "node_modules/@openai/codex");
const directCommand = join(directCodex, "bin/codex.js");
mkdirSync(dirname(directCommand), { recursive: true });
writeFileSync(join(directCodex, "package.json"), JSON.stringify({ ...metadata, version: "0.155.0" }));
writeFileSync(directCommand, "#!" + process.execPath + "\\n", { mode: 0o755 });
assert.equal(resolveCodexCommand(pathOnlyIssuer, commandEnvironment), directCommand, "A directly installed older wrapper takes precedence over PATH");
writeFileSync(join(directCodex, "package.json"), JSON.stringify({ ...metadata, name: "unowned-wrapper" }));
assert.throws(() => resolveCodexCommand(pathOnlyIssuer, commandEnvironment), /unexpected package identity/);
writeFileSync(join(directCodex, "package.json"), "{");
assert.throws(() => resolveCodexCommand(pathOnlyIssuer, commandEnvironment), /runtime unavailable/);
mkdirSync(dirname(issuer), { recursive: true });
mkdirSync(join(codex, "bin"), { recursive: true });
writeFileSync(join(adapter, "package.json"), JSON.stringify({ name: "@paperclipai/adapter-codex-local", exports: { "./server": "./server.js" } }));
writeFileSync(join(adapter, "server.js"), "");
writeFileSync(join(bridge, "package.json"), JSON.stringify({ name: "@agentclientprotocol/codex-acp", version: "1.6.2" }));
writeFileSync(join(codex, "package.json"), JSON.stringify(metadata));
mkdirSync(vendor, { recursive: true });
const platformMetadata = { name: "@openai/codex", version: "0.160.0-" + process.platform + "-" + process.arch,
os: [process.platform], cpu: [process.arch] };
writeFileSync(join(platform, "package.json"), JSON.stringify(platformMetadata));
writeFileSync(executable, "#!" + process.execPath + "\\nif (process.argv.slice(2).join(' ') !== '--version') process.exit(9); console.log('codex-cli 0.160.0');\\n", { mode: 0o755 });
assert.equal(resolveCodexCommand(issuer, commandEnvironment), executable, "The selected installed bridge closure takes precedence over PATH");
assert.equal(resolvePinnedCodexCommand(issuer, commandEnvironment), executable, "The published compatibility alias keeps ordinary command semantics");
const roots = codexExecutableReadOnlyRoots({ PATH: "/missing-codex-command" }, executable);
assert.ok(roots.includes(vendor), "The npm-hoisted native vendor directory remains readable");
assert.ok(!roots.includes(platform) && !roots.includes(root), "Hoisting must not expose npm ancestry");
assert.equal(execFileSync(resolveCodexCommand(issuer, commandEnvironment), ["--version"], { env: { PATH: "/missing-codex-command" },
encoding: "utf8", timeout: 5_000 }).trim(), "codex-cli 0.160.0");
for (const bin of ["../../escaped-codex", "/usr/bin/codex", {}, ""]) {
writeFileSync(join(codex, "package.json"), JSON.stringify({ ...metadata, bin }));
assert.throws(() => resolveCodexCommand(issuer, commandEnvironment), /contained executable|escapes its package/);
}
for (const version of ["0.159.0", "0.161.0"]) {
writeFileSync(join(codex, "package.json"), JSON.stringify({ ...metadata, version }));
assert.equal(resolveCodexCommand(issuer, commandEnvironment), executable, "Independently usable older and newer installed wrappers remain selectable");
}
writeFileSync(join(codex, "package.json"), JSON.stringify({ ...metadata, name: "unowned-wrapper" }));
assert.throws(() => resolveCodexCommand(issuer, commandEnvironment), /unexpected package identity/);
writeFileSync(join(codex, "package.json"), "{");
assert.throws(() => resolveCodexCommand(issuer, commandEnvironment), /runtime unavailable/);
writeFileSync(join(codex, "package.json"), JSON.stringify(metadata));
chmodSync(executable, 0o600);
assert.throws(() => resolveCodexCommand(issuer, commandEnvironment), /runtime unavailable/);
rmSync(executable);
writeFileSync(join(root, "external-codex"), "external executable", { mode: 0o755 });
symlinkSync(join(root, "external-codex"), executable);
assert.throws(() => resolveCodexCommand(issuer, commandEnvironment), /escapes its package/);
rmSync(executable);
assert.throws(() => resolveCodexCommand(issuer, commandEnvironment), /runtime unavailable/);
rmSync(codex, { recursive: true });
// Use the existing fresh-process boundary: Node caches earlier module
// paths, whereas a genuinely absent installed graph must use PATH.
const resolverModule = ${JSON.stringify(new URL("./codex-command.ts", import.meta.url).href)};
const freshAbsence = [
'import assert from "node:assert/strict";',
'import { resolveCodexCommand } from ' + JSON.stringify(resolverModule) + ';',
'assert.equal(resolveCodexCommand(' + JSON.stringify(issuer) + ',' + JSON.stringify(commandEnvironment) + '),' + JSON.stringify(pathCommand) + ');',
'assert.throws(() => resolveCodexCommand(' + JSON.stringify(issuer) + ', { PATH: "/missing-codex-command" }), /runtime unavailable.*No installed Codex dependency or executable on PATH/);',
].join("\\n");
execFileSync(process.execPath, ['--no-global-search-paths', '--import', createRequire(resolverModule).resolve('tsx'),
'--input-type=module', '--eval', freshAbsence], { env: commandEnvironment, encoding: 'utf8', timeout: 5_000 });
// npm installs the host alias declared by the published server while
// its bundled JS wrapper deliberately omits optionalDependencies.
const server = join(root, "node_modules/@paperclipai/server");
const serverManifest = join(server, "package.json");
const normalized = join(server, "node_modules/@openai/codex");
const normalizedManifest = join(normalized, "package.json");
const normalizedCommand = join(normalized, "bin/codex.js");
const publishedBridgeManifest = join(server, "node_modules/@agentclientprotocol/codex-acp/package.json");
const publishedBridgeMetadata = { name: "@agentclientprotocol/codex-acp", version: "1.6.2" };
const serverMetadata = { name: "@paperclipai/server", dependencies: { "@agentclientprotocol/codex-acp": "1.6.2" },
optionalDependencies: metadata.optionalDependencies };
const normalizedMetadata = { ...metadata, optionalDependencies: undefined };
mkdirSync(dirname(normalizedCommand), { recursive: true });
mkdirSync(dirname(publishedBridgeManifest), { recursive: true });
writeFileSync(publishedBridgeManifest, JSON.stringify(publishedBridgeMetadata));
writeFileSync(serverManifest, JSON.stringify(serverMetadata));
writeFileSync(normalizedManifest, JSON.stringify(normalizedMetadata));
writeFileSync(normalizedCommand, "#!" + process.execPath + "\\n", { mode: 0o755 });
const normalizedRoots = () => codexExecutableReadOnlyRoots({ PATH: "/missing-codex-command" }, normalizedCommand);
assert.ok(normalizedRoots().includes(vendor), "The normalized published wrapper must retain its selected native sandbox resources");
const sandbox = createIsolatedCodexAppServerArgs({ HOME: "/private-provider-home" }, normalizedRoots()).join("\\n");
assert.ok(sandbox.includes(JSON.stringify(vendor) + '=\"read\"'), "Native sandbox resources must reach the actual read-only permission configuration");
assert.ok(sandbox.includes('\"/private-provider-home\"=\"none\"'), "The provider home remains denied");
for (const ancestor of [platform, dirname(platform), join(root, "node_modules"), server, root]) {
assert.ok(!normalizedRoots().includes(ancestor), "Platform discovery must not grant enclosing npm or server roots");
}
const restoreMetadata = () => {
writeFileSync(serverManifest, JSON.stringify(serverMetadata));
writeFileSync(publishedBridgeManifest, JSON.stringify(publishedBridgeMetadata));
writeFileSync(normalizedManifest, JSON.stringify(normalizedMetadata));
writeFileSync(join(platform, "package.json"), JSON.stringify(platformMetadata));
};
// Resource lookup protects package ownership and paths. Version
// qualification belongs to ACPX, not ordinary Codex shell access.
for (const version of ["0.159.0", "0.161.0"]) {
for (const [path, changed] of [
[serverManifest, { ...serverMetadata, optionalDependencies: { [platformName]: "npm:@openai/codex@" + version + "-" + process.platform + "-" + process.arch } }],
[serverManifest, { ...serverMetadata, dependencies: { "@agentclientprotocol/codex-acp": version } }],
[publishedBridgeManifest, { ...publishedBridgeMetadata, version }],
[normalizedManifest, { ...normalizedMetadata, version }],
[join(platform, "package.json"), { ...platformMetadata, version: version + "-" + process.platform + "-" + process.arch }],
]) {
writeFileSync(path, JSON.stringify(changed));
assert.ok(normalizedRoots().includes(vendor), "Older or newer usable metadata must retain native resources independently of other version numbers");
for (const ancestor of [platform, dirname(platform), server, root]) assert.ok(!normalizedRoots().includes(ancestor));
restoreMetadata();
}
}
for (const [path, changed] of [
[serverManifest, { ...serverMetadata, optionalDependencies: {} }],
[serverManifest, { ...serverMetadata, name: "unowned-server" }],
[serverManifest, { ...serverMetadata, optionalDependencies: { [platformName]: "npm:unowned/codex@0.160.0-" + process.platform + "-" + process.arch } }],
[serverManifest, { ...serverMetadata, optionalDependencies: { [platformName]: "file:/unowned/native" } }],
[serverManifest, { ...serverMetadata, optionalDependencies: { [platformName]: "npm:@openai/codex@0.160.0-unsupported-os-" + process.arch } }],
[serverManifest, { ...serverMetadata, dependencies: {} }],
[serverManifest, { ...serverMetadata, dependencies: { "@agentclientprotocol/codex-acp": "" } }],
[publishedBridgeManifest, { ...publishedBridgeMetadata, name: "unqualified-bridge" }],
[normalizedManifest, { ...normalizedMetadata, name: "unowned-wrapper" }],
[normalizedManifest, { ...normalizedMetadata, optionalDependencies: {} }],
[join(platform, "package.json"), { ...platformMetadata, name: "unqualified-native" }],
[join(platform, "package.json"), { ...platformMetadata, os: ["unsupported-os"] }],
[join(platform, "package.json"), { ...platformMetadata, cpu: ["unsupported-cpu"] }],
]) {
writeFileSync(path, JSON.stringify(changed));
assert.ok(!normalizedRoots().includes(vendor), "Unqualified metadata must not grant native resources");
restoreMetadata();
}
const outsideVendor = join(root, "outside-vendor");
mkdirSync(outsideVendor);
rmSync(vendor, { recursive: true });
symlinkSync(outsideVendor, vendor);
assert.ok(!normalizedRoots().includes(outsideVendor), "A vendor symlink must not escape the selected platform package");
rmSync(vendor); mkdirSync(vendor);
const outsidePlatform = join(root, "outside-platform");
mkdirSync(join(outsidePlatform, "vendor"), { recursive: true });
writeFileSync(join(outsidePlatform, "package.json"), JSON.stringify(platformMetadata));
rmSync(platform, { recursive: true }); symlinkSync(outsidePlatform, platform);
assert.ok(!normalizedRoots().includes(join(outsidePlatform, "vendor")), "An alias symlink must not grant a foreign package");
rmSync(platform); mkdirSync(vendor, { recursive: true });
writeFileSync(join(platform, "package.json"), JSON.stringify(platformMetadata));
} finally { rmSync(root, { recursive: true, force: true }); rmSync(pathOnlyRoot, { recursive: true, force: true }); }
process.stdout.write("PINNED_CODEX_ISOLATION_VERIFIED");
`;
expect(execFileSync(process.execPath, [
"--no-global-search-paths", "--import", createRequire(import.meta.url).resolve("tsx"),
"--input-type=module", "--eval", source,
], { env: { ...process.env, NODE_PATH: "", NODE_OPTIONS: "" }, encoding: "utf8", timeout: 10_000,
maxBuffer: 32 * 1024 })).toBe("PINNED_CODEX_ISOLATION_VERIFIED");
});
it("uses the execution account's HOME without credential deny roots in preconfigured mode", () => {
const source = { HOME: "/users/executor", CODEX_HOME: "/users/executor/.codex", PAPERCLIP_RUNNER_HOME_MODE: "preconfigured" };
const args = createIsolatedCodexAppServerArgs(source).join("\n");
expect(args).not.toContain('"/users/executor"="none"');
expect(args).not.toContain('"/users/executor/.codex"="none"');
expect(args).toContain('HOME="/users/executor"');
expect(args).toContain('"/users/executor"="write"');
expect(createSecuredCodexThreadParams("/workspace", "default", true, true, source).config).toEqual({
"skills.include_instructions": true, include_collaboration_mode_instructions: true,
});
});
it("allows only the registered private instruction directory while keeping shared context read-only", () => {
const args = createIsolatedCodexAppServerArgs({ HOME: "/host/home" }, ["/runtime/immutable-context"], "/runtime/instruction-edits/run-1").join("\n");
expect(args).toContain('"/runtime/instruction-edits/run-1"="write"');
expect(args).toContain('"/runtime/immutable-context"="read"');
expect(args).not.toContain('"/runtime"="write"');
expect(args).toContain('"/host/home"="none"');
expect(createIsolatedCodexAppServerArgs({ PAPERCLIP_INSTRUCTION_ROOT: "/host/home" }).join("\n"))
.not.toContain('"/host/home"="write"');
});
it("exposes AGENT_HOME only when it matches the controller-registered writable directory", () => {
const root = "/agent-files/run-1";
const registered = createIsolatedCodexAppServerArgs({ AGENT_HOME: root, HOME: "/provider" }, [], root).join("\n");
expect(registered).toContain('AGENT_HOME="/agent-files/run-1"');
expect(registered).toContain('"/provider"="none"');
expect(createIsolatedCodexAppServerArgs({ AGENT_HOME: "/arbitrary" }, [], root).join("\n")).not.toContain("AGENT_HOME");
expect(createIsolatedCodexAppServerArgs({ AGENT_HOME: root }).join("\n")).not.toContain("AGENT_HOME");
});
it("makes the installed npm Codex native sandbox executable readable without exposing its parent workspace", () => {
const command = evalProviderTransportOptions("codex").codexCommand!;
const manifest = createRequire(command).resolve(`@openai/codex-${process.platform}-${process.arch}/package.json`);
const vendor = resolve(dirname(manifest), "vendor");
const roots = codexExecutableReadOnlyRoots({ HOME: "/private-provider-home", PATH: "/usr/bin" }, command);
expect(roots).toContain(vendor);
expect(roots).toContain(process.execPath);
expect(roots).not.toContain(dirname(manifest));
expect(roots).not.toContain("/private-provider-home");
const args = createIsolatedCodexAppServerArgs({ HOME: "/private-provider-home" }, roots).join("\n");
expect(args).toContain(`${JSON.stringify(vendor)}="read"`);
expect(args).toContain('"/private-provider-home"="none"');
});
it("preserves target DNS symlink resources without opening all of /run", () => {
const source = { PAPERCLIP_RUNNER_NETWORK_ACCESS: "enabled", PAPERCLIP_RUNNER_NETWORK_ROOTS: '["/run/systemd/resolve/stub-resolv.conf","/etc/ssl/certs"]' };
const args = createIsolatedCodexAppServerArgs(source).join("\n");
expect(args).toContain('"/run/systemd/resolve/stub-resolv.conf"="read"');
expect(args).not.toContain('"/run"="read"');
expect(codexNetworkReadOnlyRoots({ ...source, PAPERCLIP_RUNNER_NETWORK_ACCESS: "disabled" })).toEqual([]);
});
it("requires the controller's network decision even when GitHub credentials exist", () => {
for (const GH_TOKEN of [undefined, "managed-token"]) {
const args = createIsolatedCodexAppServerArgs({ GH_TOKEN }).join("\n");
expect(args).toContain("network.enabled=false");
expect(args).not.toContain("network.enabled=true");
}
});
it("honors an explicit network restriction independently of GitHub", () => {
for (const GH_TOKEN of [undefined, "managed-token"]) {
const args = createIsolatedCodexAppServerArgs({ GH_TOKEN, PAPERCLIP_RUNNER_NETWORK_ACCESS: "disabled" }).join("\n");
expect(args).toContain("network.enabled=false");
expect(args).not.toContain("network.enabled=true");
}
});
it("restores host Git resources without exposing the provider home", () => {
const args = createIsolatedCodexAppServerArgs({
HOME: "/provider", CODEX_HOME: "/provider", PATH: "/usr/bin:/bin", PAPERCLIP_GITHUB_AUTH_MODE: "host",
OPENAI_API_KEY: "must-not-cross", DATABASE_URL: "must-not-cross", PAPERCLIP_API_KEY: "must-not-cross",
PAPERCLIP_GITHUB_HOST_HOME: "/legacy", GH_CONFIG_DIR: "/legacy/.config/gh",
SSH_AUTH_SOCK: "/agent/socket", PAPERCLIP_GIT_METADATA_ROOTS: '["/repo/.git","/repo/.git"]',
}).join("\n");
const allowlist = JSON.parse(args.split("\n").find((arg) => arg.startsWith("shell_environment_policy.include_only="))!.split("=", 2)[1]!);
expect(allowlist).toEqual(["GH_CONFIG_DIR", "HOME", "PAPERCLIP_GITHUB_AUTH_MODE", "PAPERCLIP_GITHUB_HOST_HOME", "PAPERCLIP_GIT_METADATA_ROOTS", "PATH", "SSH_AUTH_SOCK"]);
expect(args).not.toContain("must-not-cross");
expect(args).not.toContain("OPENAI_API_KEY");
expect(args).not.toContain("DATABASE_URL");
expect(args).not.toContain("PAPERCLIP_API_KEY");
expect(args).toContain('HOME="/legacy"');
expect(args).toContain('"/legacy/.gitconfig"="read"');
expect(args).toContain('"/legacy/.ssh"="read"');
expect(args).toContain('"/agent/socket"="read"');
expect(args).toContain('"/repo/.git"="write"');
expect(args).toContain('"/repo/.git"="read"');
expect(args).toContain('"/provider"="none"');
expect(args).not.toContain('"/legacy"="read"');
expect(args.match(/"\/legacy\/.config\/gh"="read"/g)).toHaveLength(2);
});
it("disables host extensions and makes collaboration instructions explicit", () => {
expect(createSkilllessCodexThreadConfig("/workspace", {}, false)).toEqual({
"skills.include_instructions": false,
include_apps_instructions: false,
include_collaboration_mode_instructions: false,
"features.apps": false,
"features.plugins": false,
"features.multi_agent": false,
"features.memories": false,
"features.image_generation": false,
});
});
it("keeps automatic execution inside the workspace without host credentials and with normal network access", () => {
const args = createIsolatedCodexAppServerArgs(
{
HOME: "/host/home",
CODEX_HOME: "/host/codex",
PATH: "/safe/bin",
PAPERCLIP_RUNNER_NETWORK_ACCESS: "enabled",
LANG: "C.UTF-8",
OPENAI_API_KEY: "must-not-cross",
},
["/isolated/codex-home/skills", "/runner/context"],
);
const serialized = args.join("\n");
expect(serialized).toContain('":root"="none"');
expect(serialized).toContain('":minimal"="read"');
expect(serialized).toContain('":tmpdir"="none"');
expect(serialized).toContain('"/host/home"="none"');
expect(serialized).toContain('"/host/codex"="none"');
expect(serialized).toContain('"/isolated/codex-home/skills"="read"');
expect(serialized).not.toContain('"/isolated/codex-home"="read"');
expect(serialized).toContain('"/runner/context"="read"');
expect(serialized).toContain('":workspace_roots"={"."="write"}');
expect(serialized).toContain('":workspace_roots"={"."="read"}');
expect(serialized).toContain("network.enabled=true");
expect(serialized).toContain('shell_environment_policy.include_only=["LANG","PAPERCLIP_RUNNER_NETWORK_ACCESS","PATH"]');
expect(serialized).toContain('PATH="/safe/bin"');
expect(serialized).toContain('LANG="C.UTF-8"');
expect(serialized).not.toContain("OPENAI_API_KEY");
expect(serialized).not.toContain("must-not-cross");
});
it("inherits only projected GitHub credentials without serializing their values", () => {
const args = createIsolatedCodexAppServerArgs({
PATH: "/safe/bin",
PAPERCLIP_RUNNER_NETWORK_ACCESS: "enabled",
GH_TOKEN: "must-remain-in-process-environment",
GITHUB_TOKEN: "must-remain-in-process-environment",
PAPERCLIP_GIT_TOKEN: "must-remain-in-process-environment",
GIT_TERMINAL_PROMPT: "0",
GIT_CONFIG_COUNT: "1",
GIT_CONFIG_KEY_0: "credential.https://github.com.helper",
GIT_CONFIG_VALUE_0: "!trusted-helper",
OPENAI_API_KEY: "must-not-cross",
});
const serialized = args.join("\n");
expect(serialized).toContain("network.enabled=true");
expect(serialized).toContain('shell_environment_policy.inherit="all"');
expect(serialized).toContain(
"shell_environment_policy.ignore_default_excludes=true",
);
expect(serialized).toContain("shell_environment_policy.include_only=");
expect(serialized).toContain('"GH_TOKEN"');
expect(serialized).toContain('"GIT_CONFIG_KEY_0"');
expect(serialized).toContain('"GIT_CONFIG_VALUE_0"');
expect(serialized).not.toContain("must-remain-in-process-environment");
expect(serialized).not.toContain("must-not-cross");
expect(serialized).not.toContain("!trusted-helper");
});
it("isolates managed launcher profiles and never serializes broker capabilities or host API credentials", () => {
const serialized = createIsolatedCodexAppServerArgs({
HOME: "/isolated/provider", CODEX_HOME: "/isolated/provider",
PATH: "/runtime/run-B:/safe/bin",
PAPERCLIP_GITHUB_LAUNCHER_DIR: "/runtime/run-B",
PAPERCLIP_GITHUB_BROKER_TOKEN: "private-run-capability",
PAPERCLIP_GITHUB_BRIDGE_TOKEN: "private-bridge-capability",
PAPERCLIP_API_KEY: "forbidden-agent-token",
GH_CONFIG_DIR: "/runtime/run-B/gh-config",
}).join("\n");
expect(serialized).toContain('"/isolated/provider"="none"');
expect(serialized).toContain('"/runtime/run-B"="read"');
expect(serialized).toContain('"/runtime/run-B/gh-config"="write"');
expect(serialized).toContain('HOME="/runtime/run-B"');
expect(serialized).toContain('ZDOTDIR="/runtime/run-B"');
expect(serialized).toContain('BASH_ENV="/runtime/run-B/.bashrc"');
expect(serialized).toContain('"PAPERCLIP_GITHUB_BRIDGE_TOKEN"');
expect(serialized).not.toContain("PAPERCLIP_API_KEY");
expect(serialized).not.toContain("private-run-capability");
expect(serialized).not.toContain("private-bridge-capability");
expect(serialized).not.toContain("forbidden-agent-token");
});
it("uses a read-only permission profile for plan mode", () => {
expect(createSecuredCodexThreadParams("/workspace", "plan")).toMatchObject({
cwd: "/workspace",
permissions: "paperclip-runner-workspace-read-only",
runtimeWorkspaceRoots: ["/workspace"],
config: {
"skills.include_instructions": false,
include_collaboration_mode_instructions: true,
},
});
});
it("uses the outer sandbox for default-mode commands only when the controller authorizes it", () => {
const source = { PAPERCLIP_RUNNER_EXTERNAL_SANDBOX: "1", PAPERCLIP_RUNNER_NETWORK_ACCESS: "enabled" };
const externalArgs = createIsolatedCodexAppServerArgs(source);
const serializedExternalArgs = externalArgs.join("\n");
expect(externalArgs).toContain(
"--dangerously-bypass-approvals-and-sandbox",
);
expect(serializedExternalArgs).toContain(
'default_permissions="paperclip-runner-external-sandbox"',
);
expect(serializedExternalArgs).toContain(
'permissions.paperclip-runner-external-sandbox.filesystem={":root"="write"}',
);
expect(serializedExternalArgs).toContain(
"permissions.paperclip-runner-external-sandbox.network.enabled=true",
);
expect(
createSecuredCodexThreadParams(
"/workspace",
"default",
true,
false,
source,
),
).toMatchObject({
permissions: "paperclip-runner-external-sandbox",
});
expect(
createSecuredCodexThreadParams(
"/workspace",
"plan",
true,
false,
source,
),
).toMatchObject({
permissions: "paperclip-runner-workspace-read-only",
});
expect(createIsolatedCodexAppServerArgs({})).not.toContain(
"--dangerously-bypass-approvals-and-sandbox",
);
});
});