@jbkjr/relay 0.0.0-stage → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/LICENSES/Apache-2.0.txt +201 -0
- package/LICENSES/README.md +19 -0
- package/LICENSES/codex-NOTICE.txt +6 -0
- package/README.md +386 -2
- package/SECURITY.md +145 -0
- package/adapters/briefs/claude.md +43 -0
- package/adapters/briefs/codex.md +56 -0
- package/adapters/briefs/models.json +33 -0
- package/adapters/build.mjs +8 -0
- package/adapters/claude/.claude-plugin/marketplace.json +13 -0
- package/adapters/claude/relay/.claude-plugin/plugin.json +5 -0
- package/adapters/claude/relay/agents/codex.md +21 -0
- package/adapters/claude/relay/agents/operator.md +21 -0
- package/adapters/claude/relay/skills/ask/SKILL.md +20 -0
- package/adapters/claude/relay/skills/chat/SKILL.md +13 -0
- package/adapters/claude/relay/skills/delegate/SKILL.md +21 -0
- package/adapters/claude/relay/skills/discuss/SKILL.md +19 -0
- package/adapters/claude/relay/skills/review/SKILL.md +21 -0
- package/adapters/claude/relay/skills/tree/SKILL.md +10 -0
- package/adapters/claude/relay/skills/write-for-codex/SKILL.md +78 -0
- package/adapters/codex/.agents/plugins/marketplace.json +20 -0
- package/adapters/codex/AGENTS.snippet.md +5 -0
- package/adapters/codex/relay/.codex-plugin/plugin.json +6 -0
- package/adapters/codex/relay/skills/relay-ask/SKILL.md +20 -0
- package/adapters/codex/relay/skills/relay-chat/SKILL.md +13 -0
- package/adapters/codex/relay/skills/relay-delegate/SKILL.md +21 -0
- package/adapters/codex/relay/skills/relay-discuss/SKILL.md +19 -0
- package/adapters/codex/relay/skills/relay-review/SKILL.md +21 -0
- package/adapters/codex/relay/skills/relay-tree/SKILL.md +10 -0
- package/adapters/codex/relay/skills/relay-write-for-claude/SKILL.md +66 -0
- package/adapters/generate.mjs +124 -0
- package/bin/relay +59 -0
- package/codex-pin.json +12 -0
- package/dist/backends/acp.js +1424 -0
- package/dist/backends/claude.js +890 -0
- package/dist/backends/cli-version.js +112 -0
- package/dist/backends/codex-app.js +1525 -0
- package/dist/backends/codex-daybreak.js +75 -0
- package/dist/backends/codex-env.js +656 -0
- package/dist/backends/codex-network.js +304 -0
- package/dist/backends/codex-pin.js +320 -0
- package/dist/backends/codex-shell-policy.js +182 -0
- package/dist/backends/codex-stage2.js +998 -0
- package/dist/backends/codex-subagents.js +47 -0
- package/dist/backends/codex.js +637 -0
- package/dist/backends/copilot.js +246 -0
- package/dist/backends/env.js +147 -0
- package/dist/backends/gemini.js +607 -0
- package/dist/backends/grok.js +337 -0
- package/dist/backends/index.js +8 -0
- package/dist/backends/native-temp.js +187 -0
- package/dist/backends/never-admission.js +90 -0
- package/dist/backends/run.js +67 -0
- package/dist/backends/stream.js +230 -0
- package/dist/backends/tool-discovery.js +15 -0
- package/dist/backends/types.js +264 -0
- package/dist/build-info.json +5 -0
- package/dist/cli.js +344 -0
- package/dist/confine/bridge.js +113 -0
- package/dist/confine/broker-core.js +76 -0
- package/dist/confine/broker-http.js +650 -0
- package/dist/confine/broker-names.js +9 -0
- package/dist/confine/broker.js +258 -0
- package/dist/confine/canary.js +380 -0
- package/dist/confine/diagnostics.js +122 -0
- package/dist/confine/gemini-tools-main.js +8 -0
- package/dist/confine/gemini-tools.js +181 -0
- package/dist/confine/hardlinks.js +58 -0
- package/dist/confine/harness-config.js +16 -0
- package/dist/confine/index.js +117 -0
- package/dist/confine/leases.js +217 -0
- package/dist/confine/paths.js +6 -0
- package/dist/confine/peer.js +115 -0
- package/dist/confine/port-policy.js +8 -0
- package/dist/confine/ports.js +365 -0
- package/dist/confine/private-root.js +208 -0
- package/dist/confine/probe-stage2.js +501 -0
- package/dist/confine/probe.js +1109 -0
- package/dist/confine/proxy.js +783 -0
- package/dist/confine/sbcheck.js +208 -0
- package/dist/confine/seatbelt-never.js +41 -0
- package/dist/confine/seatbelt-stage2.js +139 -0
- package/dist/confine/seatbelt.js +1129 -0
- package/dist/confine/sockets.js +36 -0
- package/dist/confine/turn-probe.js +19 -0
- package/dist/confine/turn.js +86 -0
- package/dist/core/adoption.js +124 -0
- package/dist/core/api-keys.js +423 -0
- package/dist/core/approvals.js +740 -0
- package/dist/core/build-identity.js +248 -0
- package/dist/core/caller.js +749 -0
- package/dist/core/calls.js +102 -0
- package/dist/core/claude-settings.js +1045 -0
- package/dist/core/config.js +453 -0
- package/dist/core/context.js +186 -0
- package/dist/core/db.js +753 -0
- package/dist/core/dispatch.js +110 -0
- package/dist/core/env-broker.js +505 -0
- package/dist/core/env-policy.js +182 -0
- package/dist/core/escalation-authority.js +76 -0
- package/dist/core/escalation-contract.js +13 -0
- package/dist/core/escalation-person-path.js +131 -0
- package/dist/core/escalation-policy.js +79 -0
- package/dist/core/escalation-runtime.js +135 -0
- package/dist/core/escalation-state.js +42 -0
- package/dist/core/escalations.js +191 -0
- package/dist/core/events.js +235 -0
- package/dist/core/executables.js +147 -0
- package/dist/core/extensions.js +645 -0
- package/dist/core/families.js +204 -0
- package/dist/core/git.js +657 -0
- package/dist/core/identity.js +113 -0
- package/dist/core/liveness.js +68 -0
- package/dist/core/mediated-network-provider.js +81 -0
- package/dist/core/never-checkpoint.js +76 -0
- package/dist/core/never-coverage.js +97 -0
- package/dist/core/never-network.js +49 -0
- package/dist/core/never-policy.js +302 -0
- package/dist/core/never-proxy-policy.js +109 -0
- package/dist/core/never-schema.js +61 -0
- package/dist/core/never-tools.js +56 -0
- package/dist/core/notices.js +48 -0
- package/dist/core/ops.js +959 -0
- package/dist/core/outcome.js +224 -0
- package/dist/core/params.js +32 -0
- package/dist/core/presence.js +174 -0
- package/dist/core/procs.js +546 -0
- package/dist/core/prompts.js +363 -0
- package/dist/core/protected-config.js +238 -0
- package/dist/core/raw-network.js +192 -0
- package/dist/core/redact.js +152 -0
- package/dist/core/refusal.js +33 -0
- package/dist/core/relay-command.js +310 -0
- package/dist/core/reported.js +3 -0
- package/dist/core/restart-overlay.js +651 -0
- package/dist/core/retention.js +1135 -0
- package/dist/core/secrets.js +329 -0
- package/dist/core/session-token.js +175 -0
- package/dist/core/session.js +744 -0
- package/dist/core/shell.js +2 -0
- package/dist/core/shipped-pin.js +47 -0
- package/dist/core/shown.js +42 -0
- package/dist/core/slots.js +67 -0
- package/dist/core/stage2-executors.js +453 -0
- package/dist/core/stage2-switch.js +5 -0
- package/dist/core/steering.js +149 -0
- package/dist/core/storage.js +358 -0
- package/dist/core/toml.js +175 -0
- package/dist/core/views.js +324 -0
- package/dist/core/wait.js +114 -0
- package/dist/core/wake.js +207 -0
- package/dist/core/watch.js +247 -0
- package/dist/core/worktree.js +213 -0
- package/dist/hosts/claude-desktop.js +274 -0
- package/dist/hosts/claude.js +249 -0
- package/dist/hosts/codex.js +329 -0
- package/dist/hosts/desktop-shared.js +20 -0
- package/dist/hosts/index.js +11 -0
- package/dist/hosts/types.js +4 -0
- package/dist/install/basics.js +18 -0
- package/dist/install/claude-init.js +133 -0
- package/dist/install/common.js +655 -0
- package/dist/install/doctor.js +629 -0
- package/dist/install/qualify-codex.js +205 -0
- package/dist/install.js +524 -0
- package/dist/mcp/elicit.js +209 -0
- package/dist/mcp/gateway.js +218 -0
- package/dist/mcp/host.js +253 -0
- package/dist/mcp/server.js +1124 -0
- package/dist/mcp/stdio.js +37 -0
- package/dist/modes/discussion.js +393 -0
- package/dist/modes/review.js +722 -0
- package/dist/native/relay-sbcheck +0 -0
- package/dist/targets/briefs.js +86 -0
- package/dist/targets/claude.js +94 -0
- package/dist/targets/codex.js +66 -0
- package/dist/targets/copilot.js +93 -0
- package/dist/targets/gemini.js +119 -0
- package/dist/targets/grok.js +126 -0
- package/dist/targets/index.js +192 -0
- package/dist/targets/types.js +18 -0
- package/dist/targets/verified.js +35 -0
- package/dist/worker.js +113 -0
- package/docs/UPGRADING.md +1502 -0
- package/docs/pinned-codex.md +321 -0
- package/experiments/stage2-redteam/lib/oracles.mjs +214 -0
- package/experiments/stage2-redteam/lib/report.mjs +35 -0
- package/npm-shrinkwrap.json +1632 -0
- package/package.json +82 -4
- package/scripts/codex-qualify/codex-pin-store.mjs +353 -0
- package/scripts/codex-qualify/codex-qualifier-process.mjs +265 -0
- package/scripts/codex-qualify/codex-qualifier.example.json +5 -0
- package/scripts/codex-qualify/codex-qualifier.mjs +281 -0
- package/scripts/codex-qualify/codex-shipped-pin.mjs +158 -0
- package/scripts/codex-qualify/codex-stage2-pin.mjs +48 -0
- package/scripts/codex-qualify/codex-stage2.mjs +2381 -0
- package/scripts/codex-qualify/install-codex-qualifier.mjs +98 -0
- package/scripts/codex-qualify/main-module.mjs +14 -0
- package/scripts/codex-qualify/relay-codex-qualifier.plist +37 -0
- package/scripts/codex-qualify/scratch-env.mjs +31 -0
- package/scripts/codex-qualify/session.mjs +96 -0
- package/scripts/codex-qualify/stage2-escalation-cases.mjs +125 -0
- package/test/codex-permissions.test.ts +875 -0
- package/test/fixtures/boundary-probes.mjs +17 -0
- package/test/fixtures/denial-stream-host.mjs +7 -0
- package/test/fixtures/families.json +246 -0
- package/test/fixtures/install-snapshots/claude-desktop-foreign.txt +158 -0
- package/test/fixtures/install-snapshots/claude-desktop-fresh.txt +101 -0
- package/test/fixtures/install-snapshots/claude-desktop-installed.txt +161 -0
- package/test/fixtures/install-snapshots/claude-foreign.txt +133 -0
- package/test/fixtures/install-snapshots/claude-fresh.txt +77 -0
- package/test/fixtures/install-snapshots/claude-installed.txt +133 -0
- package/test/fixtures/install-snapshots/codex-foreign.txt +159 -0
- package/test/fixtures/install-snapshots/codex-fresh.txt +107 -0
- package/test/fixtures/install-snapshots/codex-installed.txt +161 -0
- package/test/fixtures/link-parent.mjs +6 -0
- package/test/fixtures/mediated-command.cjs +72 -0
- package/test/fixtures/native-skill-isolation.json +280 -0
- package/test/fixtures/pinned-codex-app-server.mjs +25 -0
- package/test/fixtures/qualified-ext-adapters.mjs +36 -0
- package/test/fixtures/refusals/README.md +52 -0
- package/test/fixtures/refusals/claude-source-result.json +7 -0
- package/test/fixtures/refusals/openai-app-capacity.json +24 -0
- package/test/fixtures/refusals/openai-app-cyberPolicy.json +24 -0
- package/test/fixtures/refusals/openai-gpt-6-astra-max.json +12 -0
- package/test/fixtures/refusals/openai-gpt-6.1-sol-xhigh.json +12 -0
- package/test/fixtures/refusals/openai-review-capacity.json +24 -0
- package/test/fixtures/relay-0.2.0.sql +57 -0
- package/test/fixtures/relay-0.9.1.gen.mjs +212 -0
- package/test/fixtures/relay-0.9.1.sql +340 -0
- package/test/fixtures/slow-boundary-probes.mjs +37 -0
- package/test/fixtures/stable-surfaces/config.json +1122 -0
- package/test/fixtures/stable-surfaces/escalation.json +128 -0
- package/test/fixtures/stable-surfaces/events.json +37 -0
- package/test/fixtures/stable-surfaces/gemini-native-resume.json +4 -0
- package/test/fixtures/stable-surfaces/inbox.jsonl +2 -0
- package/test/fixtures/stable-surfaces/notice.txt +1 -0
- package/test/fixtures/stable-surfaces/refusal-item.json +28 -0
- package/test/fixtures/stable-surfaces/refusal-review.json +23 -0
- package/test/fixtures/stable-surfaces/registry.json +49 -0
- package/test/fixtures/stable-surfaces/tools-desktop.json +575 -0
- package/test/fixtures/stable-surfaces/tools.json +716 -0
- package/test/fixtures/stable-surfaces/usage.txt +46 -0
- package/test/fixtures/stage2-acp-native.mjs +138 -0
- package/test/fixtures/stage2-app-server.mjs +23 -0
- package/test/fixtures/stage2-catalog.json +91 -0
- package/test/fixtures/stage2-hostile-web.mjs +35 -0
- package/test/fixtures/stage2-native-network.mjs +62 -0
- package/test/fixtures/stage2-worker.ts +87 -0
- package/test/fixtures/status-views/expected.json +7825 -0
- package/test/fixtures/status-views/registries.json +1 -0
- package/test/fixtures/steering.json +209 -0
- package/test/helpers.ts +740 -0
- package/test/host-env.ts +7 -0
- package/test/keychain-decoy.ts +197 -0
- package/test/live-scope.ts +44 -0
- package/test/mediated-network.test.ts +192 -0
- package/test/mediated-proxy.test.ts +264 -0
- package/test/native-temp-alias.ts +10 -0
- package/test/never-policy.test.ts +629 -0
- package/test/never-tools.test.ts +136 -0
- package/test/process-home.ts +18 -0
- package/test/proxy.test.ts +531 -0
- package/test/restart-overlay-live.test.ts +85 -0
- package/test/restart-overlay-profile.test.ts +66 -0
- package/test/restart-overlay.test.ts +416 -0
- package/test/stable-surfaces.test.ts +249 -0
- package/test/stage2-escalation-live.test.ts +544 -0
- package/test/stage2-escalation-qualification.test.ts +170 -0
- package/test/stage2-escalation.test.ts +747 -0
- package/test/stage2-executor.test.ts +849 -0
- package/test/stage2-grant.test.ts +603 -0
- package/test/stage2-native-live.test.ts +647 -0
- package/test/stage2-network-decisions.test.ts +70 -0
- package/test/stage2-probe.test.ts +845 -0
- package/test/stage2-standing-live.test.ts +203 -0
- package/test/stubs/broker-host.mjs +59 -0
- package/test/stubs/broker-peer.mjs +20 -0
- package/test/stubs/confined-harness.mjs +91 -0
- package/test/stubs/ext-broken-server.mjs +2 -0
- package/test/stubs/ext-odd-server.mjs +21 -0
- package/test/stubs/ext-server.mjs +31 -0
- package/test/stubs/fake-host-cli.mjs +218 -0
- package/test/stubs/procargs.mjs +30 -0
- package/test/stubs/stub-acp +4 -0
- package/test/stubs/stub-agent.mjs +322 -0
- package/test/stubs/stub-claude +4 -0
- package/test/stubs/stub-codex +4 -0
- package/test/stubs/targets/acp.mjs +307 -0
- package/test/stubs/targets/claude.mjs +149 -0
- package/test/stubs/targets/codex.mjs +479 -0
- package/test/stubs/targets/gemini.mjs +604 -0
- package/test/temp-root.ts +11 -0
- package/test/wp4-adapters.test.ts +353 -0
- package/test/wp4-interlock.test.ts +136 -0
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
import "./helpers.ts";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import net from "node:net";
|
|
5
|
+
import http from "node:http";
|
|
6
|
+
import dgram from "node:dgram";
|
|
7
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
8
|
+
import os from "node:os";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { test } from "node:test";
|
|
11
|
+
import { hostileWebCalls, assertHostileWebEvidence, codeCall, modelStubRequestKind } from "./fixtures/stage2-hostile-web.mjs";
|
|
12
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
13
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
14
|
+
import { openHttpBroker, BROKER_TOKEN_ENV } from "../dist/confine/broker-http.js";
|
|
15
|
+
import { withDefaults } from "../dist/core/config.js";
|
|
16
|
+
import { closeDbForTests, createRun, insertNode, insertTurn, setParam, getTurn, getNode, reap, updateTurn } from "../dist/core/db.js";
|
|
17
|
+
import { newInstance } from "../dist/core/caller.js";
|
|
18
|
+
import { approve, cancel } from "../dist/core/ops.js";
|
|
19
|
+
import { pendingRequests, requestView } from "../dist/core/approvals.js";
|
|
20
|
+
import { registerEscalationRuntime } from "../dist/core/escalation-contract.js";
|
|
21
|
+
import { procStart, sameProcess } from "../dist/core/procs.js";
|
|
22
|
+
import { getOverlay, consumeOverlay, overlayInterlock } from "../dist/core/restart-overlay.js";
|
|
23
|
+
import { escalationAccounting } from "../dist/core/escalations.js";
|
|
24
|
+
import { rawNetworkKey } from "../dist/core/raw-network.js";
|
|
25
|
+
import { openRootEnvironment, workerEnvironment } from "../dist/core/env-broker.js";
|
|
26
|
+
import { setCodexHomeConfigForTests } from "../dist/backends/codex-pin.js";
|
|
27
|
+
import { busyPorts } from "../dist/confine/ports.js";
|
|
28
|
+
import { nodeDir } from "../dist/core/config.js";
|
|
29
|
+
import { executorRecords, sweepOrphanedExecutors } from "../dist/core/stage2-executors.js";
|
|
30
|
+
import { withTurnSecrets, redactor } from "../dist/core/secrets.js";
|
|
31
|
+
import { codexBackend } from "../dist/backends/codex.js";
|
|
32
|
+
import { setLeaseOptionsForTests } from "../dist/backends/codex-env.js";
|
|
33
|
+
import { checkpointNever } from "../dist/core/never-checkpoint.js";
|
|
34
|
+
import { targetDef } from "../dist/targets/index.js";
|
|
35
|
+
import { portHeld } from "../dist/confine/leases.js";
|
|
36
|
+
import type { TurnHandle, TurnSpec } from "../dist/backends/types.js";
|
|
37
|
+
import { sendGroupSignal } from "../dist/core/procs.js";
|
|
38
|
+
|
|
39
|
+
// Host-only transport qualification. Actual HTTP sockets and the production kernel peer check;
|
|
40
|
+
// a fake overlay lifecycle deliberately does not claim production executor/profile qualification.
|
|
41
|
+
test("WP3 live HTTP: non-delegating child escalates with actual peer authentication and receipt delivery", {
|
|
42
|
+
skip: process.env.RELAY_TEST_ESCALATION_HTTP !== "1" ? "host only: RELAY_TEST_ESCALATION_HTTP=1" : false,
|
|
43
|
+
timeout: 20000,
|
|
44
|
+
}, async () => {
|
|
45
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), "relay-escalation-http-"));
|
|
46
|
+
process.env.RELAY_HOME = path.join(home, "registry"); process.env.RELAY_CONFIG = path.join(home, "config.json");
|
|
47
|
+
const cfg = withDefaults({ desktop_notifications: false, approval_timeout_s: 5, ask_user: "never", targets: { codex: { stage2: true } } });
|
|
48
|
+
fs.writeFileSync(process.env.RELAY_CONFIG, JSON.stringify(cfg));
|
|
49
|
+
const root = newInstance({ kind: "root" }, home); const run = createRun(home, cfg, root.token); root.runId = run;
|
|
50
|
+
const node = insertNode({ id: "j_00000001", run_id: run, kind: "job", target: "codex", mode: "write", depth: 1, task: "run a server test", cwd: home, status: "running" });
|
|
51
|
+
const turn = insertTurn(node.id, "test", "", "write", "running");
|
|
52
|
+
const delivered: boolean[] = []; let requests = 0;
|
|
53
|
+
const unregister = registerEscalationRuntime(node.id, turn, {
|
|
54
|
+
snapshot: () => ({ native_thread: "fake-thread", native_turn: "fake-turn", base_policy_hash: "fake-policy", launch_plan: "fake-overlay", overlay_budget_s: 3600 }),
|
|
55
|
+
qualify: () => null,
|
|
56
|
+
request: async () => { requests++; return { state: "restart_pending" }; },
|
|
57
|
+
receipt: async (_approval, ok) => { delivered.push(ok); },
|
|
58
|
+
});
|
|
59
|
+
const client = new Client({ name: "wp3-live-fake-model", version: "1" });
|
|
60
|
+
let broker: Awaited<ReturnType<typeof openHttpBroker>> | undefined;
|
|
61
|
+
try {
|
|
62
|
+
// OS-selected owned high port; never contact any real service or credential store.
|
|
63
|
+
const candidate = net.createServer(); await new Promise<void>(resolve => candidate.listen(0, "127.0.0.1", resolve));
|
|
64
|
+
const port = (candidate.address() as net.AddressInfo).port;
|
|
65
|
+
await new Promise<void>((resolve, reject) => candidate.close(err => err ? reject(err) : resolve()));
|
|
66
|
+
assert.ok(port > 1024 && port !== 5900);
|
|
67
|
+
broker = await withTurnSecrets(() => openHttpBroker({ nodeId: node.id, turnId: turn, mayDelegate: false, mayAsk: true }, port, { onRefusal() {} }));
|
|
68
|
+
const start = procStart(process.pid); assert.ok(start);
|
|
69
|
+
broker.bindAppServer({ pid: process.pid, start }); broker.arm();
|
|
70
|
+
await client.connect(new StreamableHTTPClientTransport(new URL(broker.url), { requestInit: { headers: { Authorization: `Bearer ${broker.env[BROKER_TOKEN_ENV]}` } } }));
|
|
71
|
+
assert.deepEqual((await client.listTools()).tools.map(t => t.name), ["escalate"]);
|
|
72
|
+
const answer = client.callTool({ name: "escalate", arguments: { capabilities: [{ kind: "port0" }], reason: "authorized server test", continuation: { argv: ["node", "./server-test.js"], cwd: home } } });
|
|
73
|
+
let request;
|
|
74
|
+
for (let n = 0; n < 500 && !request; n++) { request = pendingRequests()[0]; if (!request) await new Promise(r => setTimeout(r, 10)); }
|
|
75
|
+
assert.ok(request); assert.equal(approve(root, { request_id: request.id, decision: "allow" }).decision, "allowed");
|
|
76
|
+
const result = JSON.parse((await answer).content[0].text);
|
|
77
|
+
assert.equal(result.state, "restart_pending"); assert.match(result.effect, /0\.0\.0\.0.*on the LAN/);
|
|
78
|
+
for (let n = 0; n < 100 && !delivered.length; n++) await new Promise(r => setTimeout(r, 10));
|
|
79
|
+
assert.deepEqual(delivered, [true]); assert.equal(requests, 1);
|
|
80
|
+
} finally {
|
|
81
|
+
await client.close(); await broker?.close(); unregister(); closeDbForTests(); fs.rmSync(home, { recursive: true, force: true });
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Actual Codex + production worker, broker, lifecycle, executor and profile. The only model
|
|
86
|
+
// is the owned high-port Responses stub below. No real model API or credential store is used.
|
|
87
|
+
/** The kernel's first ephemeral port (net.inet.ip.portrange.first; 49152 by default). Read only by the macOS listen case. */
|
|
88
|
+
const ephemeralFirst = () => Number(execFileSync("/usr/sbin/sysctl", ["-n", "net.inet.ip.portrange.first"], { encoding: "utf8" }).trim()) || 49152;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The listen case's port: free on 0.0.0.0 now, and below both the ephemeral range and relay's
|
|
92
|
+
* reserved range (47200), so no outgoing connection can be given it in the ~25 s before the
|
|
93
|
+
* overlay's command binds it. A port taken from the ephemeral range lost that race on a laptop
|
|
94
|
+
* (item 27, EADDRINUSE, Nov 3). The case still fails when the listen itself is denied: the
|
|
95
|
+
* command then prints no OVERLAY_PORT0_OK line for this port.
|
|
96
|
+
*/
|
|
97
|
+
async function stableListenPort(): Promise<number> {
|
|
98
|
+
const top = Math.min(ephemeralFirst(), 47200);
|
|
99
|
+
for (let i = 0; i < 200; i++) {
|
|
100
|
+
const p = 20000 + Math.floor(Math.random() * (top - 20000));
|
|
101
|
+
const free = await new Promise<boolean>((ok) => { const s = net.createServer(); s.once("error", () => ok(false)); s.listen(p, "0.0.0.0", () => s.close(() => ok(true))); });
|
|
102
|
+
if (free) return p;
|
|
103
|
+
}
|
|
104
|
+
throw new Error(`no free listen port in 20000-${top - 1}`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
for (const mode of ["port0", "listen", "connect", "write", "cancel", "port0-code", "cancel-code", "identity-mismatch", "readback-mismatch", "hostile-direct", "hostile-code", "mediated", "mediated-crash", "mediated-public"]) test(`escalation live worker: real approve, delivered receipt, teardown, restart, consume and ${mode} result`, {
|
|
108
|
+
skip: process.platform !== "darwin" ? "macOS only" : !mode.startsWith("mediated")
|
|
109
|
+
? process.env.RELAY_TEST_STAGE2_LIVE !== "1" && "host only: RELAY_TEST_STAGE2_LIVE=1"
|
|
110
|
+
: mode === "mediated-public" ? process.env.RELAY_TEST_PUBLIC_NET !== "1" && "opt in: RELAY_TEST_PUBLIC_NET=1"
|
|
111
|
+
: process.env.RELAY_TEST_MEDIATED_LIVE !== "1" && "host only: RELAY_TEST_MEDIATED_LIVE=1",
|
|
112
|
+
timeout: mode === "mediated-crash" ? 240_000 : 150_000,
|
|
113
|
+
}, async t => {
|
|
114
|
+
const bin = process.env.RELAY_TEST_CODEX ?? "/opt/homebrew/bin/codex";
|
|
115
|
+
assert.match(execFileSync(bin, ["--version"], { encoding: "utf8" }), /codex-cli/);
|
|
116
|
+
const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "esc-live-")));
|
|
117
|
+
const cwd = path.join(home, "work"), appHome = path.join(home, "app");
|
|
118
|
+
fs.mkdirSync(cwd); fs.mkdirSync(appHome);
|
|
119
|
+
process.env.RELAY_HOME = path.join(home, "registry"); process.env.RELAY_CONFIG = path.join(home, "config.json");
|
|
120
|
+
const usage = { input_tokens: 0, input_tokens_details: null, output_tokens: 0, output_tokens_details: null, total_tokens: 0 };
|
|
121
|
+
const mediated = mode.startsWith("mediated");
|
|
122
|
+
const surface = mode.startsWith("hostile-") ? mode.slice("hostile-".length) : "";
|
|
123
|
+
const codeMode = mode.endsWith("-code");
|
|
124
|
+
const mismatch = mode.endsWith("-mismatch");
|
|
125
|
+
const outside = path.join(home, "outside"); fs.mkdirSync(outside);
|
|
126
|
+
const forbiddenFile = path.join(home, "never-file"); fs.writeFileSync(forbiddenFile, "owned decoy");
|
|
127
|
+
let capability: Record<string, unknown> = mediated ? { kind: "network", mode: "mediated" } : { kind: "port0" };
|
|
128
|
+
let listenPort = 0;
|
|
129
|
+
const connectServers: net.Server[] = [];
|
|
130
|
+
const connectScript = path.join(cwd, "connect-command.cjs");
|
|
131
|
+
let connectNeverPort = 0;
|
|
132
|
+
let bindOutput = "", destinationAsked = false;
|
|
133
|
+
let ownedPort = 0, udpSeen = 0;
|
|
134
|
+
const udpSink = dgram.createSocket("udp4");
|
|
135
|
+
udpSink.on("message", () => udpSeen++);
|
|
136
|
+
const upstreamSockets = new Set<net.Socket>();
|
|
137
|
+
const script = path.join(cwd, "mediated-command.cjs"), marker = path.join(cwd, "mediated-held");
|
|
138
|
+
fs.copyFileSync(path.join(import.meta.dirname, "fixtures/mediated-command.cjs"), script);
|
|
139
|
+
let origin: number | undefined, overlayBinding: { run: string; id: string } | undefined;
|
|
140
|
+
let handoffEvidence: { delivered: boolean | undefined; clean: boolean | undefined; sourceExecutors: number; sourceStatus: string | undefined } | undefined;
|
|
141
|
+
const pendingResponses = new Set<http.ServerResponse>();
|
|
142
|
+
const hostileBodies: any[] = [], hostileOutputs = new Map<string, string>();
|
|
143
|
+
let unexpectedRequests = 0, responsesWebSocketUpgrades = 0, attackerOutput = "", ownedRun: string | undefined;
|
|
144
|
+
const model = http.createServer((req, res) => {
|
|
145
|
+
if (req.url === "/mediated-upstream" || req.url === "/mediated-hold") {
|
|
146
|
+
upstreamSockets.add(req.socket); req.socket.once("close", () => upstreamSockets.delete(req.socket));
|
|
147
|
+
if (req.url === "/mediated-upstream") res.end("MEDIATED_UPSTREAM_OK");
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
let raw = ""; req.on("data", d => raw += d);
|
|
151
|
+
req.on("end", () => {
|
|
152
|
+
const kind = modelStubRequestKind(req);
|
|
153
|
+
if (kind === "websocket") { responsesWebSocketUpgrades++; res.writeHead(404); res.end(); return; }
|
|
154
|
+
if (kind === "unexpected") { unexpectedRequests++; res.writeHead(404); res.end(); return; }
|
|
155
|
+
const body = JSON.parse(raw), input = body.input ?? [];
|
|
156
|
+
const text = JSON.stringify(input);
|
|
157
|
+
const destination = text.includes("One-turn overlay");
|
|
158
|
+
const outputs = input.filter((i: any) => /(?:_call_output|tool_search_output)$/.test(i.type ?? ""));
|
|
159
|
+
const out = (id: string) => outputs.find((i: any) => i.call_id === id);
|
|
160
|
+
let call: any, id: string;
|
|
161
|
+
if (text.includes("CROSS_JOB_ATTACKER")) {
|
|
162
|
+
id = "cross-job";
|
|
163
|
+
const got = out(id);
|
|
164
|
+
if (got) attackerOutput = typeof got.output === "string" ? got.output : JSON.stringify(got.output);
|
|
165
|
+
call = got ? { type: "message", role: "assistant", content: [{ type: "output_text", text: "attacker complete" }] }
|
|
166
|
+
: { type: "function_call", name: "exec_command", arguments: JSON.stringify({ login: false,
|
|
167
|
+
cmd: `${JSON.stringify(process.execPath)} -e 'const n=require("node:net");const s=n.connect(${Number(fs.readFileSync(marker, "utf8"))},"127.0.0.1");s.setTimeout(3000,()=>{s.destroy();console.log("CROSS_JOB_TIMEOUT")});s.once("connect",()=>{console.log("CROSS_JOB_CONNECTED");s.destroy()});s.once("error",e=>console.log("CROSS_JOB_RESULT:"+e.code))'` }) };
|
|
168
|
+
} else if (destination) {
|
|
169
|
+
hostileBodies.push(body);
|
|
170
|
+
destinationAsked = true;
|
|
171
|
+
// Take evidence before the model issues its first replacement command,
|
|
172
|
+
// rather than rely on catching a potentially short turn in a poll.
|
|
173
|
+
const overlay = overlayBinding && getOverlay(overlayBinding.run, overlayBinding.id);
|
|
174
|
+
handoffEvidence ??= { delivered: overlay?.receipt?.delivered, clean: overlay?.cleanup?.clean,
|
|
175
|
+
sourceExecutors: executorRecords().records.filter(r => r.record.turnId === origin).length,
|
|
176
|
+
sourceStatus: origin === undefined ? undefined : getTurn(origin)?.status };
|
|
177
|
+
if (mismatch) {
|
|
178
|
+
// Let the real restart reach its live readback, but issue no continuation commands.
|
|
179
|
+
pendingResponses.add(res); res.once("close", () => pendingResponses.delete(res)); return;
|
|
180
|
+
}
|
|
181
|
+
const got = out("esc-bind");
|
|
182
|
+
if (got) bindOutput = typeof got.output === "string" ? got.output : JSON.stringify(got.output);
|
|
183
|
+
if (got && (mode === "mediated-crash" || mode === "cancel-code")) {
|
|
184
|
+
pendingResponses.add(res); res.once("close", () => pendingResponses.delete(res)); return;
|
|
185
|
+
}
|
|
186
|
+
id = "esc-bind";
|
|
187
|
+
call = got ? { type: "message", role: "assistant", content: [{ type: "output_text", text: "overlay complete" }] }
|
|
188
|
+
: { type: "function_call", name: "exec_command", arguments: JSON.stringify({ login: false,
|
|
189
|
+
cmd: mediated ? `${JSON.stringify(process.execPath)} ${JSON.stringify(script)} ${ownedPort} ${mode === "mediated-public" ? "public" : "check"} ${JSON.stringify(marker)}`
|
|
190
|
+
: mode === "connect" ? `${JSON.stringify(process.execPath)} ${JSON.stringify(connectScript)}`
|
|
191
|
+
: mode === "write" ? `${JSON.stringify(process.execPath)} -e 'const fs=require("node:fs");fs.writeFileSync(${JSON.stringify(path.join(outside, "written"))},"overlay write");try{fs.writeFileSync(${JSON.stringify(forbiddenFile)},"bad");throw Error("never write succeeded")}catch(e){if(e.code!=="EPERM"&&e.code!=="EACCES")throw e}console.log("OVERLAY_WRITE_OK")'`
|
|
192
|
+
: `${JSON.stringify(process.execPath)} -e 'const s=require("node:net").createServer();s.listen(${mode === "listen" ? listenPort : 0},"0.0.0.0",()=>{console.log("OVERLAY_PORT0_OK:"+s.address().port);${mode === "cancel-code" ? "" : "s.close()"}})'`,
|
|
193
|
+
...(mode === "cancel-code" ? { yield_time_ms: 1000 } : {}),
|
|
194
|
+
}) };
|
|
195
|
+
if (!got && codeMode) call = codeCall(`const r = await tools.exec_command(${call.arguments}); text(typeof r === "string" ? r : JSON.stringify(r));`);
|
|
196
|
+
if (surface) {
|
|
197
|
+
const attempts = hostileWebCalls(surface, `http://forbidden.test:${ownedPort}/owned-never-page`);
|
|
198
|
+
for (let k = 0; k < attempts.length; k++) {
|
|
199
|
+
const got = out(`hostile-${k}`);
|
|
200
|
+
if (got) hostileOutputs.set(`hostile-${k}`, typeof got.output === "string" ? got.output : JSON.stringify(got.output));
|
|
201
|
+
}
|
|
202
|
+
const next = attempts.findIndex((_, k) => !hostileOutputs.has(`hostile-${k}`));
|
|
203
|
+
if (next >= 0) { id = `hostile-${next}`; call = attempts[next]; }
|
|
204
|
+
}
|
|
205
|
+
} else if (out("esc-request")) {
|
|
206
|
+
// Keep the old model turn open until the worker interrupts it after receipt delivery.
|
|
207
|
+
pendingResponses.add(res); res.once("close", () => pendingResponses.delete(res)); return;
|
|
208
|
+
} else if (out("esc-search")) {
|
|
209
|
+
id = "esc-request";
|
|
210
|
+
call = { type: "function_call", namespace: "mcp__relay_tools", name: "escalate", arguments: JSON.stringify({ capabilities: [capability], reason: "authorized owned network/server test",
|
|
211
|
+
continuation: { work: "Run the owned port-0 server test in the replacement turn", cwd } }) };
|
|
212
|
+
if (codeMode) call = codeCall(`const r = await tools.mcp__relay_tools__escalate(${call.arguments}); text(typeof r === "string" ? r : JSON.stringify(r));`);
|
|
213
|
+
} else {
|
|
214
|
+
id = "esc-search"; call = { type: "tool_search_call", execution: "client", arguments: { query: "relay escalate", limit: 10 } };
|
|
215
|
+
}
|
|
216
|
+
const events = [{ type: "response.created", response: { id: `r-${id}` } },
|
|
217
|
+
{ type: "response.output_item.done", item: { ...call, ...(call.type === "message" ? {} : { call_id: id }), id: `i-${id}` } },
|
|
218
|
+
{ type: "response.completed", response: { id: `r-${id}`, usage } }];
|
|
219
|
+
res.writeHead(200, { "content-type": "text/event-stream" });
|
|
220
|
+
res.end(events.map(e => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`).join(""));
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
let authority: Awaited<ReturnType<typeof openRootEnvironment>> | undefined;
|
|
224
|
+
let worker: ReturnType<typeof spawn> | undefined, exited: Promise<void> | undefined;
|
|
225
|
+
let workerDone = false, workerExit: number | null = null, diagnostics = "";
|
|
226
|
+
const stopWorker = () => { worker?.kill("SIGTERM"); };
|
|
227
|
+
t.signal.addEventListener("abort", stopWorker, { once: true });
|
|
228
|
+
try {
|
|
229
|
+
await new Promise<void>((ok, fail) => { model.once("error", fail); model.listen(0, "127.0.0.1", ok); });
|
|
230
|
+
const port = (model.address() as net.AddressInfo).port;
|
|
231
|
+
ownedPort = port;
|
|
232
|
+
if (mode === "listen") {
|
|
233
|
+
listenPort = await stableListenPort();
|
|
234
|
+
assert.ok(listenPort > 10000 && listenPort < ephemeralFirst() && listenPort < 47200);
|
|
235
|
+
capability = { kind: "localhost", port: listenPort, operation: "listen", api_effects: "owned server decoy" };
|
|
236
|
+
} else if (mode === "connect") {
|
|
237
|
+
const own = async (host: string) => {
|
|
238
|
+
const server = net.createServer(c => c.destroy()); connectServers.push(server);
|
|
239
|
+
await new Promise<void>((ok, fail) => { server.once("error", fail); server.listen(0, host, ok); });
|
|
240
|
+
const port = (server.address() as net.AddressInfo).port;
|
|
241
|
+
assert.ok(port > 10000);
|
|
242
|
+
return port;
|
|
243
|
+
};
|
|
244
|
+
listenPort = await own("0.0.0.0");
|
|
245
|
+
const otherPort = await own("127.0.0.1");
|
|
246
|
+
connectNeverPort = await own("127.0.0.1");
|
|
247
|
+
capability = { kind: "localhost", port: listenPort, operation: "connect", host: "127.0.0.1", api_effects: "Connect only to the owned test listener; no service API mutations" };
|
|
248
|
+
const hosts = [...new Set(["127.0.0.1", ...Object.values(os.networkInterfaces()).flatMap(as => as ?? [])
|
|
249
|
+
.filter(a => a.family === "IPv4" && !a.address.startsWith("127.")).map(a => a.address)])];
|
|
250
|
+
fs.writeFileSync(connectScript, `const net=require("node:net"),assert=require("node:assert/strict");
|
|
251
|
+
const check=(host,port)=>new Promise((resolve,reject)=>{const c=net.connect({host,port});const t=setTimeout(()=>{c.destroy();reject(Error("connect timed out:"+host+":"+port))},2000);const end=value=>{clearTimeout(t);c.destroy();resolve(value)};c.once("connect",()=>end("allowed"));c.once("error",e=>end(e.code))});
|
|
252
|
+
(async()=>{for(const host of ${JSON.stringify(hosts)})assert.equal(await check(host,${listenPort}),"allowed",host);
|
|
253
|
+
for(const [host,port] of ${JSON.stringify([["127.0.0.2", listenPort], ["::1", listenPort], ["::ffff:127.0.0.1", listenPort], ["127.0.0.1", otherPort], ["127.0.0.1", connectNeverPort]])})assert.equal(await check(host,port),"EPERM",host+":"+port);
|
|
254
|
+
console.log("OVERLAY_CONNECT_OK:"+${JSON.stringify(hosts)}.join(","))})().catch(e=>{console.error(e);process.exitCode=1});\n`);
|
|
255
|
+
} else if (mode === "write") capability = { kind: "write_root", root: outside };
|
|
256
|
+
if (mediated) await new Promise<void>((ok, fail) => { udpSink.once("error", fail); udpSink.bind(port, "127.0.0.1", ok); });
|
|
257
|
+
assert.ok(port > 10000);
|
|
258
|
+
fs.copyFileSync(path.join(import.meta.dirname, "fixtures/stage2-catalog.json"), path.join(home, "catalog.json"));
|
|
259
|
+
if (surface === "direct") {
|
|
260
|
+
const catalog = JSON.parse(fs.readFileSync(path.join(home, "catalog.json"), "utf8"));
|
|
261
|
+
catalog.models[0].tool_mode = "direct";
|
|
262
|
+
fs.writeFileSync(path.join(home, "catalog.json"), JSON.stringify(catalog));
|
|
263
|
+
}
|
|
264
|
+
fs.writeFileSync(path.join(appHome, "config.toml"), `cli_auth_credentials_store="file"\nmodel="mock-model"\nopenai_base_url="http://127.0.0.1:${port}/v1"\nmodel_catalog_json=${JSON.stringify(path.join(home, "catalog.json"))}\n`);
|
|
265
|
+
fs.writeFileSync(path.join(appHome, "auth.json"), JSON.stringify({ OPENAI_API_KEY: "sk-owned-model-stub" }));
|
|
266
|
+
setCodexHomeConfigForTests(h => h === appHome ? fs.readFileSync(path.join(appHome, "config.toml"), "utf8") : undefined);
|
|
267
|
+
let range;
|
|
268
|
+
for (let n = 0; n < 100; n++) {
|
|
269
|
+
const first = 50000 + 16 * Math.floor(Math.random() * 600);
|
|
270
|
+
if (!busyPorts(Array.from({ length: 16 }, (_, k) => first + k)).size) { range = { first, count: 16 }; break; }
|
|
271
|
+
}
|
|
272
|
+
assert.ok(range, "owned high-port executor range");
|
|
273
|
+
const cfg = withDefaults({ desktop_notifications: false, ask_user: "never", approval_timeout_s: 60, allow_temp_home: true,
|
|
274
|
+
child_auth: "any", codex_endpoint_override: "accept", cancel_grace_ms: 1000,
|
|
275
|
+
never: { paths: [{ path: forbiddenFile }], ...(surface ? { hosts: ["forbidden.test"] } : {}), ...(mediated ? { ports: [{ port: 49444, destinations: "any" }] } : mode === "connect" ? { ports: [{ port: connectNeverPort, destinations: "this-machine" }] } : {}) },
|
|
276
|
+
targets: { codex: { bin, stage2: true, home: appHome, pinned: false } } });
|
|
277
|
+
fs.writeFileSync(process.env.RELAY_CONFIG, JSON.stringify(cfg));
|
|
278
|
+
const root = newInstance({ kind: "root" }, cwd), run = createRun(cwd, cfg, root.token); root.runId = run;
|
|
279
|
+
ownedRun = run;
|
|
280
|
+
authority = await openRootEnvironment(root);
|
|
281
|
+
const grant = mediated || surface || mode === "connect" ? undefined : { network: true, raw_network: true };
|
|
282
|
+
const node = insertNode({ id: "j_00000002", run_id: run, kind: "job", target: "codex", mode: "write", depth: 1, task: "owned server test", cwd, status: "running", params_json: JSON.stringify({ grant }) });
|
|
283
|
+
if (grant) setParam(node.id, "raw_network_consent", { grant: rawNetworkKey(grant), by: "test fixture person", at: Date.now() });
|
|
284
|
+
fs.mkdirSync(nodeDir(run, node.id), { recursive: true });
|
|
285
|
+
origin = insertTurn(node.id, "test", "Ask relay for a port0 overlay for the owned server test.", "write");
|
|
286
|
+
// Run the actual detached worker entry point: it pins its own pid/start, claims
|
|
287
|
+
// environment/redaction through the real root broker and drains both turns.
|
|
288
|
+
// Only the synthetic model config and owned port range use code-only fixtures.
|
|
289
|
+
const bootstrap = `
|
|
290
|
+
import { runWorker } from ${JSON.stringify(new URL("../dist/worker.js", import.meta.url).href)};
|
|
291
|
+
import { setLeaseOptionsForTests } from ${JSON.stringify(new URL("../dist/backends/codex-env.js", import.meta.url).href)};
|
|
292
|
+
import { setCodexHomeConfigForTests } from ${JSON.stringify(new URL("../dist/backends/codex-pin.js", import.meta.url).href)};
|
|
293
|
+
setLeaseOptionsForTests(${JSON.stringify({ range })});
|
|
294
|
+
setCodexHomeConfigForTests(h => h === ${JSON.stringify(appHome)} ? ${JSON.stringify(fs.readFileSync(path.join(appHome, "config.toml"), "utf8"))} : undefined);
|
|
295
|
+
${mediated && mode !== "mediated-public" ? `
|
|
296
|
+
const { createMediatedNetworkProvider, mediatedLookup, setMediatedNetworkProviderForTests } = await import(${JSON.stringify(new URL("../dist/core/mediated-network-provider.js", import.meta.url).href)});
|
|
297
|
+
const { openProxy } = await import(${JSON.stringify(new URL("../dist/confine/proxy.js", import.meta.url).href)});
|
|
298
|
+
const { default: net } = await import("node:net");
|
|
299
|
+
const connect = net.connect;
|
|
300
|
+
// Final numeric dial only: retain production publicPolicy and address screening first.
|
|
301
|
+
net.connect = function(options, ...args) {
|
|
302
|
+
return connect.call(this, options?.host === "8.8.8.8" && options.port === ${port} ? { ...options, host: "127.0.0.1" } : options, ...args);
|
|
303
|
+
};
|
|
304
|
+
setMediatedNetworkProviderForTests(createMediatedNetworkProvider({
|
|
305
|
+
lookup: async (host, signal) => host === "owned-upstream.test" ? { addresses: ["8.8.8.8"], cnames: [] } : mediatedLookup(host, signal),
|
|
306
|
+
open: async options => openProxy({ ...options, testing: { upstreamPort: ${port} } })
|
|
307
|
+
}));
|
|
308
|
+
` : ""}
|
|
309
|
+
${mismatch ? `
|
|
310
|
+
const { setStage2DepsForTests } = await import(${JSON.stringify(new URL("../dist/backends/codex-env.js", import.meta.url).href)});
|
|
311
|
+
const { stage2TurnReadback } = await import(${JSON.stringify(new URL("../dist/backends/codex-stage2.js", import.meta.url).href)});
|
|
312
|
+
const { procStart } = await import(${JSON.stringify(new URL("../dist/core/procs.js", import.meta.url).href)});
|
|
313
|
+
let apps = 0, reads = 0;
|
|
314
|
+
setStage2DepsForTests({
|
|
315
|
+
appStart: (pid, options) => {
|
|
316
|
+
const start = procStart(pid, options);
|
|
317
|
+
return ++apps === 2 && ${mode === "identity-mismatch"} ? start + "-mismatched-start" : start;
|
|
318
|
+
},
|
|
319
|
+
turnReadback: (rpc, ...args) => {
|
|
320
|
+
if (++reads !== 2 || ${mode !== "readback-mismatch"}) return stage2TurnReadback(rpc, ...args);
|
|
321
|
+
// Real live reply, with only its environment identity contradicted at the existing seam.
|
|
322
|
+
return stage2TurnReadback({ request: async (method, params) => {
|
|
323
|
+
const result = await rpc.request(method, params);
|
|
324
|
+
return method === "thread/read" ? { ...result, thread: { ...result.thread, environments: [] } } : result;
|
|
325
|
+
} }, ...args);
|
|
326
|
+
}
|
|
327
|
+
});` : ""}
|
|
328
|
+
await runWorker(${JSON.stringify(node.id)});
|
|
329
|
+
`;
|
|
330
|
+
worker = spawn(process.execPath, ["--input-type=module", "-e", bootstrap], { detached: true,
|
|
331
|
+
env: { ...workerEnvironment(run), RELAY_HOME: process.env.RELAY_HOME, RELAY_CONFIG: process.env.RELAY_CONFIG }, stdio: ["ignore", "pipe", "pipe"] });
|
|
332
|
+
const cut = redactor();
|
|
333
|
+
for (const stream of [worker.stdout!, worker.stderr!]) stream.on("data", d => { diagnostics = (diagnostics + cut(String(d))).slice(-16000); });
|
|
334
|
+
exited = new Promise<void>(resolve => {
|
|
335
|
+
worker!.once("error", e => { diagnostics += String(e); });
|
|
336
|
+
worker!.once("close", code => { workerDone = true; workerExit = code; resolve(); });
|
|
337
|
+
});
|
|
338
|
+
let request;
|
|
339
|
+
for (const deadline = Date.now() + 60_000; Date.now() < deadline && !request && !workerDone && !t.signal.aborted;) {
|
|
340
|
+
request = pendingRequests().find(r => r.node_id === node.id && r.kind === "escalation");
|
|
341
|
+
if (!request) await new Promise(ok => setTimeout(ok, 20));
|
|
342
|
+
}
|
|
343
|
+
assert.ok(request, JSON.stringify({ node: getNode(node.id), turn: getTurn(origin), diagnostics }));
|
|
344
|
+
assert.equal(getNode(node.id)?.worker_pid, worker.pid, "production worker registered its real identity");
|
|
345
|
+
assert.equal(getNode(node.id)?.worker_start, procStart(worker.pid!));
|
|
346
|
+
const overlayId = JSON.parse(request.detail_json!).overlay.id;
|
|
347
|
+
if (mode === "connect") {
|
|
348
|
+
const detail = JSON.parse(request.detail_json!);
|
|
349
|
+
assert.match(JSON.stringify(detail), /LAN.*tailnet/);
|
|
350
|
+
assert.match(detail.effect, /LAN.*tailnet/);
|
|
351
|
+
assert.equal(requestView(request).effect, detail.effect, "approver view includes the stored connect disclosure");
|
|
352
|
+
}
|
|
353
|
+
overlayBinding = { run, id: overlayId };
|
|
354
|
+
if (mode === "cancel") {
|
|
355
|
+
const result = await cancel(root, { id: node.id });
|
|
356
|
+
assert.ok(!result.error && !result.refused, JSON.stringify(result));
|
|
357
|
+
while (!workerDone && !t.signal.aborted) await new Promise(ok => setTimeout(ok, 10));
|
|
358
|
+
await exited;
|
|
359
|
+
assert.equal(getNode(node.id)?.status, "cancelled");
|
|
360
|
+
assert.equal(destinationAsked, false);
|
|
361
|
+
assert.equal(getOverlay(run, overlayId).destinationTurn, undefined);
|
|
362
|
+
assert.throws(() => consumeOverlay(run, overlayId, origin!), /destination|ready|cancel/);
|
|
363
|
+
assert.equal(approve(root, { request_id: request.id, decision: "allow" }).decision === "allowed", false);
|
|
364
|
+
assert.deepEqual(executorRecords().records, [], "cancel stopped the source executor");
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
assert.equal(approve(root, { request_id: request.id, decision: "allow" }).decision, "allowed");
|
|
368
|
+
if (mode === "cancel-code") {
|
|
369
|
+
for (const end = Date.now() + 60_000; Date.now() < end && !/OVERLAY_PORT0_OK:/.test(bindOutput) && !workerDone && !t.signal.aborted; )
|
|
370
|
+
await new Promise(ok => setTimeout(ok, 20));
|
|
371
|
+
assert.match(bindOutput, /OVERLAY_PORT0_OK:\d+/, JSON.stringify({ bindOutput, diagnostics }));
|
|
372
|
+
const live = getOverlay(run, overlayId);
|
|
373
|
+
assert.equal(live.receipt?.delivered, true);
|
|
374
|
+
assert.ok(live.destinationTurn && live.launchedAt !== undefined, "cancel targets the running code-mode replacement");
|
|
375
|
+
const result = await cancel(root, { id: node.id });
|
|
376
|
+
assert.ok(!result.error && !result.refused, JSON.stringify(result));
|
|
377
|
+
while (!workerDone && !t.signal.aborted) await new Promise(ok => setTimeout(ok, 10));
|
|
378
|
+
await exited;
|
|
379
|
+
assert.equal(getNode(node.id)?.status, "cancelled");
|
|
380
|
+
assert.equal(getTurn(live.destinationTurn!)?.status, "cancelled");
|
|
381
|
+
assert.deepEqual(executorRecords().records, [], "cancel stopped the code-mode replacement executor");
|
|
382
|
+
assert.throws(() => consumeOverlay(run, overlayId, live.destinationTurn!), /ready|consumed/);
|
|
383
|
+
await new Promise<void>((ok, fail) => {
|
|
384
|
+
const c = net.connect(Number(/OVERLAY_PORT0_OK:(\d+)/.exec(bindOutput)![1]), "127.0.0.1");
|
|
385
|
+
c.setTimeout(3000, () => { c.destroy(); fail(new Error("cancelled listener check timed out")); });
|
|
386
|
+
c.once("connect", () => { c.destroy(); fail(new Error("code-mode listener survived cancellation")); });
|
|
387
|
+
c.once("error", e => e.code === "ECONNREFUSED" ? ok() : fail(e));
|
|
388
|
+
});
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (mode === "mediated-crash") {
|
|
392
|
+
// Keep the replacement model turn open after its successful mediated command. The child
|
|
393
|
+
// has left a real CONNECT tunnel to our held upstream, not a synthetic socket record.
|
|
394
|
+
const until = async (check: () => boolean, what: string) => {
|
|
395
|
+
for (const end = Date.now() + 60_000; Date.now() < end && !t.signal.aborted; ) {
|
|
396
|
+
if (check()) return;
|
|
397
|
+
assert.equal(workerDone, false, JSON.stringify({ diagnostics, node: getNode(node.id) }));
|
|
398
|
+
await new Promise(ok => setTimeout(ok, 20));
|
|
399
|
+
}
|
|
400
|
+
assert.fail(`timed out waiting for ${what}`);
|
|
401
|
+
};
|
|
402
|
+
await until(() => /MEDIATED_RESULT:/.test(bindOutput) && upstreamSockets.size > 0, "live mediated tunnel");
|
|
403
|
+
const proxyPort = Number(fs.readFileSync(marker, "utf8"));
|
|
404
|
+
assert.ok(portHeld(proxyPort), "live victim proxy has a held lease");
|
|
405
|
+
const victimRecord = executorRecords().records.find(r => r.record.nodeId === node.id);
|
|
406
|
+
assert.ok(victimRecord, "live victim executor recorded before SIGKILL");
|
|
407
|
+
const app = getNode(node.id)!;
|
|
408
|
+
assert.ok(app.agent_pgid && app.agent_start, "live app identity persisted before crash");
|
|
409
|
+
|
|
410
|
+
// Unsandboxed host control: this proxy really serves requests while the attacker probes.
|
|
411
|
+
const control = () => new Promise<string>((ok, fail) => {
|
|
412
|
+
const req = http.get({ hostname: "127.0.0.1", port: proxyPort,
|
|
413
|
+
path: "http://owned-upstream.test/mediated-upstream", headers: { Host: "owned-upstream.test" } }, res => {
|
|
414
|
+
let body = ""; res.on("data", b => body += b); res.once("end", () => ok(body));
|
|
415
|
+
});
|
|
416
|
+
req.setTimeout(5000, () => req.destroy(new Error("live proxy control timed out"))); req.once("error", fail);
|
|
417
|
+
});
|
|
418
|
+
assert.equal(await control(), "MEDIATED_UPSTREAM_OK");
|
|
419
|
+
const attackerCwd = path.join(home, "attacker"); fs.mkdirSync(attackerCwd);
|
|
420
|
+
const attacker = insertNode({ id: "j_attacker", run_id: run, kind: "job", target: "codex", mode: "write", depth: 1,
|
|
421
|
+
task: "CROSS_JOB_ATTACKER", cwd: attackerCwd, status: "running", params_json: JSON.stringify({ grant: { network: true } }) });
|
|
422
|
+
const attackTurn = insertTurn(attacker.id, "test", "CROSS_JOB_ATTACKER", "write", "running");
|
|
423
|
+
const spec: TurnSpec = { target: targetDef("codex"), config: cfg, cwd: attackerCwd, access: "write", grant: { network: true },
|
|
424
|
+
prompt: "CROSS_JOB_ATTACKER", dir: home, logPath: path.join(home, "attacker.log"), relay: null,
|
|
425
|
+
markers: { runId: run, nodeId: attacker.id, depth: 1 }, turnId: attackTurn, neverPolicy: checkpointNever(run, "launch"),
|
|
426
|
+
environment: { PATH: "/usr/bin:/bin:/usr/sbin:/sbin", HOME: os.homedir(), CODEX_HOME: appHome, SHELL: "/bin/zsh" },
|
|
427
|
+
signal: t.signal, onApproval: async () => { throw new Error("attacker must not request approval"); } };
|
|
428
|
+
let handle: TurnHandle | undefined, finished = false;
|
|
429
|
+
setLeaseOptionsForTests({ range });
|
|
430
|
+
try {
|
|
431
|
+
await withTurnSecrets(async () => {
|
|
432
|
+
handle = await codexBackend.start(spec);
|
|
433
|
+
const result = await handle.done; finished = true;
|
|
434
|
+
assert.equal(result.status, "ok", JSON.stringify({ result, attackerOutput }));
|
|
435
|
+
});
|
|
436
|
+
} finally {
|
|
437
|
+
if (handle) { if (!finished) await handle.cancel(); await handle.done; }
|
|
438
|
+
updateTurn(attackTurn, { status: finished ? "done" : "failed" });
|
|
439
|
+
setLeaseOptionsForTests(null);
|
|
440
|
+
}
|
|
441
|
+
assert.match(attackerOutput, /CROSS_JOB_RESULT:EPERM/);
|
|
442
|
+
assert.doesNotMatch(attackerOutput, /CROSS_JOB_CONNECTED|CROSS_JOB_TIMEOUT/);
|
|
443
|
+
assert.equal(await control(), "MEDIATED_UPSTREAM_OK", "victim was live on both sides of the confined attack");
|
|
444
|
+
assert.ok(upstreamSockets.size > 0, "victim tunnel is still live at worker SIGKILL");
|
|
445
|
+
worker.kill("SIGKILL"); // the worker only; production reap must stop its app and executor
|
|
446
|
+
await exited;
|
|
447
|
+
for (let end = Date.now() + 5000; upstreamSockets.size && Date.now() < end;) await new Promise(ok => setTimeout(ok, 20));
|
|
448
|
+
assert.equal(upstreamSockets.size, 0, "SIGKILL closed the mediated tunnel");
|
|
449
|
+
assert.ok(portHeld(proxyPort), "dead worker cannot release a proxy lease before verified reap");
|
|
450
|
+
reap(run);
|
|
451
|
+
assert.equal(sameProcess(app.agent_pgid!, app.agent_start!, { fresh: true }), false, "reap stopped the pinned app server");
|
|
452
|
+
assert.equal(sameProcess(victimRecord.record.pid, victimRecord.record.start, { fresh: true }), false, "reap stopped the executor");
|
|
453
|
+
assert.ok(executorRecords().records.some(r => r.file === victimRecord.file && /quarantine|untraceable/.test(r.record.tainted ?? "")), "unknown app cleanup keeps the executor record quarantined");
|
|
454
|
+
assert.ok(portHeld(proxyPort), "proxy lease remains quarantined after reap");
|
|
455
|
+
assert.match(overlayInterlock("local") ?? "", /reservation|executor|tainted/);
|
|
456
|
+
assert.throws(() => consumeOverlay(run, overlayId, getOverlay(run, overlayId).destinationTurn!), /ready|consumed/);
|
|
457
|
+
await assert.rejects(control(), /ECONNREFUSED/, "crashed proxy no longer serves requests");
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
// Let the worker select, consume and launch the replacement itself.
|
|
461
|
+
while (!workerDone && !t.signal.aborted) {
|
|
462
|
+
await new Promise(ok => setTimeout(ok, 10));
|
|
463
|
+
}
|
|
464
|
+
await exited;
|
|
465
|
+
assert.equal(workerExit, 0, diagnostics);
|
|
466
|
+
if (mismatch) {
|
|
467
|
+
const overlay = getOverlay(run, overlayId);
|
|
468
|
+
assert.equal(overlay.receipt?.delivered, true, "restart had a delivered receipt");
|
|
469
|
+
assert.ok(overlay.consumedAt !== undefined && overlay.destinationTurn, "replacement was consumed exactly once");
|
|
470
|
+
assert.equal(getTurn(overlay.destinationTurn!)?.status, "failed");
|
|
471
|
+
assert.equal(getNode(node.id)?.status, "failed");
|
|
472
|
+
assert.match(getNode(node.id)?.error ?? "", mode === "identity-mismatch" ? /unknown, dead or reused app server/ : /thread.*environment|thread.*relay/);
|
|
473
|
+
assert.equal(bindOutput, "", "no continuation command was issued");
|
|
474
|
+
assert.deepEqual(executorRecords().records, [], "failed replacement cleaned its real executor");
|
|
475
|
+
assert.throws(() => consumeOverlay(run, overlayId, overlay.destinationTurn!), /ready|consumed/);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
assert.equal(getNode(node.id)?.status, "done", JSON.stringify({ node: getNode(node.id), diagnostics }));
|
|
479
|
+
assert.ok(handoffEvidence, "replacement reached the model");
|
|
480
|
+
assert.equal(handoffEvidence.delivered, true);
|
|
481
|
+
assert.equal(handoffEvidence.clean, true);
|
|
482
|
+
assert.equal(handoffEvidence.sourceExecutors, 0, "old executor is gone before the replacement executes");
|
|
483
|
+
assert.match(handoffEvidence.sourceStatus!, /^(done|failed|cancelled)$/, "source turn is terminal before replacement execution");
|
|
484
|
+
assert.ok(destinationAsked);
|
|
485
|
+
if (surface) {
|
|
486
|
+
assertHostileWebEvidence(surface, hostileBodies, [...hostileOutputs.values()]);
|
|
487
|
+
t.diagnostic(`model stub observed ${responsesWebSocketUpgrades} exact Responses websocket upgrade attempt(s)`);
|
|
488
|
+
assert.equal(unexpectedRequests, 0, "hostile overlay made no non-model request, including for the decoy page, to the model stub");
|
|
489
|
+
}
|
|
490
|
+
assert.deepEqual(executorRecords().records, [], "replacement executor is also gone");
|
|
491
|
+
const nextId = getOverlay(run, overlayId).destinationTurn; assert.ok(nextId);
|
|
492
|
+
if (mediated) {
|
|
493
|
+
assert.match(bindOutput, mode === "mediated-public" ? /MEDIATED_PUBLIC_OK:/ : /MEDIATED_RESULT:/, bindOutput);
|
|
494
|
+
const proxyPort = mode === "mediated-public" ? Number(/MEDIATED_PUBLIC_OK:(\d+)/.exec(bindOutput)![1]) : Number(fs.readFileSync(marker, "utf8"));
|
|
495
|
+
assert.ok(proxyPort > 1024);
|
|
496
|
+
await new Promise<void>((ok, fail) => {
|
|
497
|
+
const c = net.connect(proxyPort, "127.0.0.1");
|
|
498
|
+
c.once("connect", () => { c.destroy(); fail(new Error("proxy still accepts after teardown")); });
|
|
499
|
+
c.once("error", e => e.code === "ECONNREFUSED" ? ok() : fail(e));
|
|
500
|
+
});
|
|
501
|
+
assert.equal(upstreamSockets.size, 0, "no live owned upstream tunnels remain");
|
|
502
|
+
assert.equal(udpSeen, 0, "owned DNS sink received no direct datagram");
|
|
503
|
+
} else if (mode === "write") {
|
|
504
|
+
assert.match(bindOutput, /OVERLAY_WRITE_OK/, bindOutput);
|
|
505
|
+
assert.equal(fs.readFileSync(path.join(outside, "written"), "utf8"), "overlay write");
|
|
506
|
+
assert.equal(fs.readFileSync(forbiddenFile, "utf8"), "owned decoy");
|
|
507
|
+
} else if (mode === "connect") {
|
|
508
|
+
assert.match(bindOutput, /OVERLAY_CONNECT_OK:127\.0\.0\.1/, bindOutput);
|
|
509
|
+
} else {
|
|
510
|
+
assert.match(bindOutput, /OVERLAY_PORT0_OK:/, bindOutput);
|
|
511
|
+
if (mode === "listen") assert.match(bindOutput, new RegExp(`OVERLAY_PORT0_OK:${listenPort}\\b`));
|
|
512
|
+
}
|
|
513
|
+
assert.equal(getTurn(nextId)?.status, "done");
|
|
514
|
+
assert.throws(() => consumeOverlay(run, overlayId, nextId), /ready|consumed/);
|
|
515
|
+
const outcome = escalationAccounting(node.id)[0];
|
|
516
|
+
assert.equal(outcome.destination_turn, nextId); assert.equal(outcome.launched, true);
|
|
517
|
+
assert.equal(outcome.outcome, "completed"); assert.equal(outcome.cleanup, "verified");
|
|
518
|
+
if (mediated) {
|
|
519
|
+
assert.match(outcome.effective_scope, /Public hosts.*Tools must honour proxy settings/);
|
|
520
|
+
} else if (mode === "port0" || mode === "port0-code" || surface) { assert.match(outcome.effective_scope, /port0/); assert.match(outcome.lan_disclosure, /0\.0\.0\.0.*LAN/); }
|
|
521
|
+
else if (mode === "connect") { assert.match(outcome.effective_scope, /localhost/); assert.match(outcome.connect_disclosure, /LAN.*tailnet/); }
|
|
522
|
+
else assert.match(outcome.effective_scope, mode === "listen" ? /localhost/ : /write/);
|
|
523
|
+
} finally {
|
|
524
|
+
t.signal.removeEventListener("abort", stopWorker);
|
|
525
|
+
if (worker && !workerDone) {
|
|
526
|
+
stopWorker();
|
|
527
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
528
|
+
try { await Promise.race([exited, new Promise<void>(resolve => { timer = setTimeout(resolve, 20000); })]); }
|
|
529
|
+
finally { clearTimeout(timer); }
|
|
530
|
+
if (!workerDone) { sendGroupSignal(worker.pid!, "SIGKILL"); await exited; }
|
|
531
|
+
}
|
|
532
|
+
// A worker can also die before the intended crash checkpoint. Reap its app first on every
|
|
533
|
+
// exit path, rather than sweeping just the executor and leaving an orphaned app server.
|
|
534
|
+
if (ownedRun) reap(ownedRun);
|
|
535
|
+
sweepOrphanedExecutors();
|
|
536
|
+
await authority?.close();
|
|
537
|
+
if (mediated && ownedPort) udpSink.close();
|
|
538
|
+
for (const socket of upstreamSockets) socket.destroy();
|
|
539
|
+
await Promise.all(connectServers.map(server => new Promise<void>(ok => server.close(() => ok()))));
|
|
540
|
+
for (const res of pendingResponses) res.destroy(); model.closeAllConnections();
|
|
541
|
+
await new Promise<void>(ok => model.close(() => ok()));
|
|
542
|
+
setCodexHomeConfigForTests(null); closeDbForTests(); fs.rmSync(home, { recursive: true, force: true });
|
|
543
|
+
}
|
|
544
|
+
});
|