@jbkjr/relay 0.0.0-stage → 1.2.2
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 +393 -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 +1539 -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 +345 -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 +641 -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 +345 -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 +635 -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 +1518 -0
- package/docs/pinned-codex.md +333 -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,501 @@
|
|
|
1
|
+
import dgram from "node:dgram";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import net from "node:net";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { within } from "./paths.js";
|
|
7
|
+
import { STATIC_DENIED_TCP } from "./ports.js";
|
|
8
|
+
import { localAddressSpellings } from "./probe.js";
|
|
9
|
+
import { boundaryPaths, dirIdentity } from "./seatbelt.js";
|
|
10
|
+
import { neverPathIdentity, neverPolicyHash } from "../core/never-policy.js";
|
|
11
|
+
/** How long a connect or a send the sandbox should refuse at once may hang before it counts as not refused. */
|
|
12
|
+
const STEP_TIMEOUT_MS = 1000;
|
|
13
|
+
/** Never routed (TEST-NET-1): the "any host" form of the UDP deny, without sending anything anywhere real. */
|
|
14
|
+
const UNROUTABLE = "192.0.2.1";
|
|
15
|
+
/** macOS keeps 104 bytes for a Unix socket's path, its terminating NUL included. */
|
|
16
|
+
const MAX_SOCKET_PATH = 103;
|
|
17
|
+
/** Where filler decoy values come from: the dynamic range, far from every port the static denylist names. */
|
|
18
|
+
const DECOY_FIRST = 49152;
|
|
19
|
+
const DECOY_LAST = 65535;
|
|
20
|
+
const sandboxDenied = (g) => g === "EPERM";
|
|
21
|
+
/** Retrying positive listens retain their complete diagnostic JSON in ProbeResult.got. */
|
|
22
|
+
const port0Listened = (budget) => (got) => {
|
|
23
|
+
try {
|
|
24
|
+
const result = JSON.parse(got);
|
|
25
|
+
return result.outcome === "allowed" && result.attemptCount >= 1 && result.attemptCount <= budget.maxAttempts
|
|
26
|
+
&& result.attempts.length === result.attemptCount && result.attempts.at(-1)?.errorCode === null;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Mirror the TCP listener denies in stage2NetworkRules and neverSeatbeltRules, including their
|
|
34
|
+
* exact-listen and own-executor/block exceptions. Use the completed decoy values, not real ports:
|
|
35
|
+
* the reserved range is scattered by the decoy mapping, while kernel-picked floor ports may bunch.
|
|
36
|
+
*/
|
|
37
|
+
export function stage2Port0RetryBudget(network, floor) {
|
|
38
|
+
const denied = [...new Set([
|
|
39
|
+
...network.deniedTcp.filter(port => !network.listenPorts?.includes(port)),
|
|
40
|
+
...network.relayRange.filter(port => port !== network.execPort),
|
|
41
|
+
...(floor?.ports.filter(rule => rule.transport !== "udp").map(rule => rule.port) ?? []),
|
|
42
|
+
...(floor?.authorityPorts.filter(port => port !== network.execPort && !network.bindPorts.includes(port)) ?? []),
|
|
43
|
+
])].filter(port => port >= DECOY_FIRST && port <= DECOY_LAST).sort((a, b) => a - b);
|
|
44
|
+
let run = 0, longestDeniedRun = 0, previous = -1;
|
|
45
|
+
for (const port of denied) {
|
|
46
|
+
run = port === previous + 1 ? run + 1 : 1;
|
|
47
|
+
longestDeniedRun = Math.max(longestDeniedRun, run);
|
|
48
|
+
previous = port;
|
|
49
|
+
}
|
|
50
|
+
// A sequential allocator may wrap from the top of the dynamic range back to its bottom.
|
|
51
|
+
if (denied[0] === DECOY_FIRST && denied.at(-1) === DECOY_LAST && denied.length < DECOY_LAST - DECOY_FIRST + 1) {
|
|
52
|
+
let head = 1, tail = 1;
|
|
53
|
+
while (denied[head] === DECOY_FIRST + head)
|
|
54
|
+
head++;
|
|
55
|
+
while (denied[denied.length - 1 - tail] === DECOY_LAST - tail)
|
|
56
|
+
tail++;
|
|
57
|
+
longestDeniedRun = Math.max(longestDeniedRun, head + tail);
|
|
58
|
+
}
|
|
59
|
+
// L denied ports take L failures plus one successful listen; allow one more attempt as margin.
|
|
60
|
+
// Other processes may advance the allocator too: success is still measured, never presumed.
|
|
61
|
+
return { longestDeniedRun, maxAttempts: Math.max(5, longestDeniedRun + 2) };
|
|
62
|
+
}
|
|
63
|
+
/** Expectations from the trusted, compiled turn policy; the canary reports outcomes only. */
|
|
64
|
+
export function stage2ProbeExpectations(network) {
|
|
65
|
+
const blockListen = network.bindPorts.length ? "allow" : "deny";
|
|
66
|
+
return {
|
|
67
|
+
egress: network.egress === "none" ? "deny" : "allow",
|
|
68
|
+
outsideListen: network.broadBind === true ? "allow" : "deny",
|
|
69
|
+
blockListen,
|
|
70
|
+
connectBlock: blockListen === "allow" && network.egress !== "none",
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The same network with decoy ports in place of the real denied, range and block ports, for the
|
|
75
|
+
* probe's wrap. Each list keeps its length, so the profile has the same rules and only its `-D`
|
|
76
|
+
* values differ (the rules hash is the real turn's; the fingerprint is not). Refuses a decoy that is
|
|
77
|
+
* one of the real ports or of the static denylist, or a list of another length.
|
|
78
|
+
*/
|
|
79
|
+
export function decoyNetwork(real, decoys) {
|
|
80
|
+
const lists = ["deniedTcp", "deniedUdp", "relayRange", "bindPorts"];
|
|
81
|
+
for (const k of lists) {
|
|
82
|
+
if (decoys[k].length !== real[k].length)
|
|
83
|
+
throw new Error(`the decoy ${k} has ${decoys[k].length} ports where the turn's has ${real[k].length}, so it would not be under the same rules`);
|
|
84
|
+
}
|
|
85
|
+
if ((decoys.listenPorts?.length ?? 0) !== (real.listenPorts?.length ?? 0))
|
|
86
|
+
throw new Error("the decoy listenPorts must keep the turn's list length");
|
|
87
|
+
if ((decoys.connectPorts?.length ?? 0) !== (real.connectPorts?.length ?? 0))
|
|
88
|
+
throw new Error("the decoy connectPorts must keep the turn's list length");
|
|
89
|
+
if ((real.mediatedPort === undefined) !== (decoys.mediatedPort === undefined))
|
|
90
|
+
throw new Error("the decoy mediated port must match the turn shape");
|
|
91
|
+
const all = [...lists.flatMap((k) => decoys[k]), decoys.execPort, ...(decoys.listenPorts ?? []), ...(decoys.connectPorts ?? []), ...(decoys.mediatedPort ? [decoys.mediatedPort] : [])];
|
|
92
|
+
// Never a real port: a decoy that were one would aim a probe at it.
|
|
93
|
+
const realPorts = new Set([...lists.flatMap((k) => real[k]), real.execPort, ...(real.listenPorts ?? []), ...(real.connectPorts ?? []), ...(real.mediatedPort ? [real.mediatedPort] : []), ...STATIC_DENIED_TCP]);
|
|
94
|
+
const clash = all.find((p) => realPorts.has(p));
|
|
95
|
+
if (clash !== undefined)
|
|
96
|
+
throw new Error(`decoy port ${clash} is one of the turn's real ports`);
|
|
97
|
+
const bad = all.find((p) => !Number.isInteger(p) || p < 1024 || p > 65535);
|
|
98
|
+
if (bad !== undefined)
|
|
99
|
+
throw new Error(`decoy port ${bad} is not a port in the high range`);
|
|
100
|
+
return { ...real, deniedTcp: [...decoys.deniedTcp], deniedUdp: [...decoys.deniedUdp], relayRange: [...decoys.relayRange], bindPorts: [...decoys.bindPorts], execPort: decoys.execPort,
|
|
101
|
+
...(real.mediatedPort ? { mediatedPort: decoys.mediatedPort } : {}),
|
|
102
|
+
...(real.listenPorts ? { listenPorts: [...(decoys.listenPorts ?? [])] } : {}),
|
|
103
|
+
...(real.connectPorts ? { connectPorts: [...(decoys.connectPorts ?? [])] } : {}) };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Every address a connect could reach this Mac at, all of which a denied port's rules must cover
|
|
107
|
+
* (§5.2): probe.ts localAddressSpellings, that is the loopback and unspecified addresses, each
|
|
108
|
+
* interface address `os.networkInterfaces()` reports (lo0 aliases included; link-local ones with
|
|
109
|
+
* their interface, without which they cannot be connected to), and the IPv4-mapped (`::ffff:a.b.c.d`,
|
|
110
|
+
* `0:0:0:0:0:ffff:0:0`) and IPv4-compatible (`::a.b.c.d`) spellings of each IPv4 one, which
|
|
111
|
+
* Seatbelt's "localhost" does not cover (review R1).
|
|
112
|
+
*/
|
|
113
|
+
export function localDestinations(interfaces = os.networkInterfaces()) {
|
|
114
|
+
return localAddressSpellings(interfaces);
|
|
115
|
+
}
|
|
116
|
+
function listenOn(server, port, host) {
|
|
117
|
+
return new Promise((resolve, reject) => {
|
|
118
|
+
server.once("error", reject);
|
|
119
|
+
server.listen({ port, host, exclusive: true }, () => (server.off("error", reject), resolve()));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
const closeServer = (s) => new Promise((r) => s.close(() => r()));
|
|
123
|
+
/**
|
|
124
|
+
* A decoy of relay's own on a fresh port, normally at 127.0.0.1 and ::1, counting connections,
|
|
125
|
+
* each closed at once. Approved-connect probes use a wildcard IPv4 listener to measure the
|
|
126
|
+
* disclosed LAN/tailnet reach at that port as well; no application protocol is served.
|
|
127
|
+
*/
|
|
128
|
+
async function decoyListener(p, avoid, ipv4Host = "127.0.0.1") {
|
|
129
|
+
let accepted = 0;
|
|
130
|
+
const count = (c) => (accepted++, c.destroy());
|
|
131
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
132
|
+
const v4 = net.createServer(count);
|
|
133
|
+
await listenOn(v4, 0, ipv4Host);
|
|
134
|
+
p.servers.push(v4);
|
|
135
|
+
const port = v4.address().port;
|
|
136
|
+
if (port < 1024 || avoid.has(port))
|
|
137
|
+
continue;
|
|
138
|
+
const v6 = net.createServer(count);
|
|
139
|
+
let hasV6 = true;
|
|
140
|
+
try {
|
|
141
|
+
await listenOn(v6, port, "::1");
|
|
142
|
+
p.servers.push(v6);
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
const code = err.code;
|
|
146
|
+
if (code === "EADDRINUSE")
|
|
147
|
+
continue;
|
|
148
|
+
// No IPv6 loopback on this machine: the IPv4 decoy is the target, and ::1 leads nowhere anyway.
|
|
149
|
+
if (code !== "EADDRNOTAVAIL")
|
|
150
|
+
throw err;
|
|
151
|
+
hasV6 = false;
|
|
152
|
+
}
|
|
153
|
+
avoid.add(port);
|
|
154
|
+
return { port, accepted: () => accepted, v6: hasV6 };
|
|
155
|
+
}
|
|
156
|
+
throw new Error("relay could not find a free port for a decoy listener");
|
|
157
|
+
}
|
|
158
|
+
/** A TCP port free on both loopback addresses just now (listened on and closed), not in `avoid`. */
|
|
159
|
+
async function freeTcpPort(avoid) {
|
|
160
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
161
|
+
const v4 = net.createServer();
|
|
162
|
+
await listenOn(v4, 0, "127.0.0.1");
|
|
163
|
+
const port = v4.address().port;
|
|
164
|
+
let free = port >= 1024 && !avoid.has(port);
|
|
165
|
+
if (free) {
|
|
166
|
+
const v6 = net.createServer();
|
|
167
|
+
try {
|
|
168
|
+
await listenOn(v6, port, "::1");
|
|
169
|
+
await closeServer(v6);
|
|
170
|
+
}
|
|
171
|
+
catch (err) {
|
|
172
|
+
if (err.code !== "EADDRNOTAVAIL")
|
|
173
|
+
free = false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
await closeServer(v4);
|
|
177
|
+
if (free) {
|
|
178
|
+
avoid.add(port);
|
|
179
|
+
return port;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
throw new Error("relay could not find a free TCP port for its stage-2 probes");
|
|
183
|
+
}
|
|
184
|
+
/** A UDP port free on 127.0.0.1 just now, not in `avoid`. */
|
|
185
|
+
async function freeUdpPort(avoid) {
|
|
186
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
187
|
+
const port = await new Promise((resolve, reject) => {
|
|
188
|
+
const s = dgram.createSocket("udp4");
|
|
189
|
+
s.once("error", reject);
|
|
190
|
+
s.bind(0, "127.0.0.1", () => {
|
|
191
|
+
const got = s.address().port;
|
|
192
|
+
s.close(() => resolve(got));
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
if (port >= 1024 && !avoid.has(port)) {
|
|
196
|
+
avoid.add(port);
|
|
197
|
+
return port;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
throw new Error("relay could not find a free UDP port for its stage-2 probes");
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* A scratch directory of relay's own laid out like a read turn's cwd as far as the profile can tell:
|
|
204
|
+
* the same git layout (no repository, a `.git` directory, or a `.git` file to a git directory with
|
|
205
|
+
* or without a shared one; the cwd at the repository's top or below it) and the same pin and git
|
|
206
|
+
* environment, so that the profile has the same rules and only its `-D` values differ. probeBoundary
|
|
207
|
+
* checks the rules hash, which refuses the launch for a layout this does not reproduce.
|
|
208
|
+
*/
|
|
209
|
+
export function decoyCwd(policy, root) {
|
|
210
|
+
const real = boundaryPaths(policy);
|
|
211
|
+
const top = path.join(root, "r");
|
|
212
|
+
fs.mkdirSync(top, { recursive: true });
|
|
213
|
+
const pointer = fs.lstatSync(real.gitPointer, { throwIfNoEntry: false });
|
|
214
|
+
let gitDir;
|
|
215
|
+
if (pointer?.isDirectory())
|
|
216
|
+
fs.mkdirSync(path.join(top, ".git"));
|
|
217
|
+
else if (pointer) {
|
|
218
|
+
// A .git file: to a git directory (with a shared one when the real has its own), or to nothing.
|
|
219
|
+
if (real.gitDir) {
|
|
220
|
+
gitDir = path.join(root, "g");
|
|
221
|
+
fs.mkdirSync(gitDir);
|
|
222
|
+
if (real.commonDir && real.commonDir !== real.gitDir) {
|
|
223
|
+
fs.mkdirSync(path.join(root, "k"));
|
|
224
|
+
fs.writeFileSync(path.join(gitDir, "commondir"), "../k\n");
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
fs.writeFileSync(path.join(top, ".git"), `gitdir: ${gitDir ?? path.join(root, "absent")}\n`);
|
|
228
|
+
}
|
|
229
|
+
// Below the repository's top when the real cwd is (its own .git then a nested pointer the profile denies).
|
|
230
|
+
const below = pointer !== undefined && path.dirname(real.gitPointer) !== real.worktree;
|
|
231
|
+
const worktree = below ? path.join(top, "s") : top;
|
|
232
|
+
if (below)
|
|
233
|
+
fs.mkdirSync(worktree);
|
|
234
|
+
return {
|
|
235
|
+
worktree,
|
|
236
|
+
worktreePin: policy.worktreePin ? dirIdentity(worktree) : undefined,
|
|
237
|
+
gitEnv: policy.gitEnv ? { GIT_DIR: gitDir ?? path.join(top, ".git"), GIT_WORK_TREE: top } : undefined,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Plans the stage-2 network checks: denied-port decoys at every local address, the positive
|
|
242
|
+
* control, the relay range, UDP denies, the TCP bind block, and AF_UNIX by turn kind (a decoy
|
|
243
|
+
* socket in a read turn's cwd). The decoys are relay's own listeners; the plan runs under the same
|
|
244
|
+
* rule text with decoy `-D` values (`decoyNetwork`), never against the real denied ports.
|
|
245
|
+
*/
|
|
246
|
+
export async function planStage2Network(p, policy, ctx) {
|
|
247
|
+
const real = policy.network;
|
|
248
|
+
// These inputs are compiled by executorPolicy/startStage2 from the consumed overlay and saved
|
|
249
|
+
// baseline grant. No canary result, child environment or requested capability decides an expectation.
|
|
250
|
+
const expected = stage2ProbeExpectations(real);
|
|
251
|
+
const broadBind = expected.outsideListen === "allow";
|
|
252
|
+
const relayOwnsWorktree = policy.worktreePin !== undefined;
|
|
253
|
+
// Review C4: Unix sockets in the worktree only for write turns in a worktree relay made and pinned.
|
|
254
|
+
// Any other turn (a read turn, whose cwd may be the person's own checkout or home) gets TMPDIR only.
|
|
255
|
+
if (real.worktreeSockets && !(policy.worktreeWritable && relayOwnsWorktree)) {
|
|
256
|
+
throw new Error("a stage-2 turn may have Unix sockets in its worktree only when it writes a worktree relay created and pinned (review C4)");
|
|
257
|
+
}
|
|
258
|
+
for (const k of ["deniedTcp", "deniedUdp"]) {
|
|
259
|
+
if (!real[k].length)
|
|
260
|
+
throw new Error(`the stage-2 network has no ${k}, so relay cannot probe it`);
|
|
261
|
+
}
|
|
262
|
+
// --- AF_UNIX by turn kind: write turns under the turn's own profile, read turns under the decoy's.
|
|
263
|
+
let cwd;
|
|
264
|
+
let cwdSocket;
|
|
265
|
+
// Short names: the canary runs in the worktree, so the name alone reaches it however deep it is.
|
|
266
|
+
const sockName = (kind) => `r${kind}${ctx.tag.slice(13, 19)}.sock`;
|
|
267
|
+
if (within(ctx.worktree, ctx.tmpDir)) {
|
|
268
|
+
for (const n of ["stage2-unix-worktree", "stage2-unix-cwd-decoy"])
|
|
269
|
+
p.skip(n, real.worktreeSockets ? "allow" : "deny", "the worktree lies inside the turn's temp dir, where Unix sockets are the turn's own");
|
|
270
|
+
}
|
|
271
|
+
else if (real.worktreeSockets) {
|
|
272
|
+
// A write turn in relay's worktree: a socket there is listened on and connected to.
|
|
273
|
+
p.allow("stage2-unix-worktree", { kind: "listen-unix", path: sockName("w"), connect: true });
|
|
274
|
+
p.created.push(path.join(ctx.worktree, sockName("w")));
|
|
275
|
+
}
|
|
276
|
+
else if (policy.worktreeWritable) {
|
|
277
|
+
// A write turn in a worktree relay did not make: it writes there, but the listening itself must be refused.
|
|
278
|
+
p.deny("stage2-unix-worktree", { kind: "listen-unix", path: sockName("w"), connect: false });
|
|
279
|
+
p.created.push(path.join(ctx.worktree, sockName("w")));
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
// A read turn cannot create a socket in its worktree at all (the file rules refuse it: write-worktree,
|
|
283
|
+
// and the generator tests for the person's checkout). What C4 is about is reaching one that is
|
|
284
|
+
// there, and relay makes nothing in the cwd, which may be the person's own checkout: a socket of
|
|
285
|
+
// relay's own listens in a decoy cwd (decoyCwd), which the decoy canary runs with as its worktree.
|
|
286
|
+
p.skip("stage2-unix-worktree", "deny", "a read turn cannot create files in its worktree; relay's decoy socket in a decoy cwd (stage2-unix-cwd-decoy) is the socket check");
|
|
287
|
+
cwd = decoyCwd(policy, path.join(ctx.scratch, "c"));
|
|
288
|
+
const at = path.join(cwd.worktree, sockName("d"));
|
|
289
|
+
if (Buffer.byteLength(at) > MAX_SOCKET_PATH) {
|
|
290
|
+
p.skip("stage2-unix-cwd-decoy", "deny", "relay's scratch dir is too deep for a Unix socket path");
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
const decoy = net.createServer((c) => c.destroy());
|
|
294
|
+
await new Promise((resolve, reject) => {
|
|
295
|
+
decoy.once("error", reject);
|
|
296
|
+
decoy.listen(at, () => (decoy.off("error", reject), resolve()));
|
|
297
|
+
});
|
|
298
|
+
p.servers.push(decoy);
|
|
299
|
+
cwdSocket = at;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// --- TCP and UDP, under the same rules with decoy ports.
|
|
303
|
+
const avoid = new Set([...real.deniedTcp, ...real.deniedUdp, ...real.relayRange, ...real.bindPorts, real.execPort, ...(real.listenPorts ?? []), ...(real.connectPorts ?? []),
|
|
304
|
+
...(policy.neverPolicy?.ports.map(p => p.port) ?? []), ...(policy.neverPolicy?.authorityPorts ?? []), ...STATIC_DENIED_TCP]);
|
|
305
|
+
const denied = await decoyListener(p, avoid);
|
|
306
|
+
const ranged = await decoyListener(p, avoid);
|
|
307
|
+
const open = await decoyListener(p, avoid);
|
|
308
|
+
const mediated = real.mediatedPort ? await decoyListener(p, avoid) : undefined;
|
|
309
|
+
const exec = await freeTcpPort(avoid);
|
|
310
|
+
const block = await freeTcpPort(avoid);
|
|
311
|
+
const outside = await freeTcpPort(avoid);
|
|
312
|
+
const udpDenied = await freeUdpPort(avoid);
|
|
313
|
+
const udpOpen = await freeUdpPort(avoid);
|
|
314
|
+
// Fillers: further decoy values that keep each list's length (so the rules are the turn's). They are
|
|
315
|
+
// only ever denied, or allowed to be bound, in the decoy profile; no probe is aimed at them.
|
|
316
|
+
const filler = () => {
|
|
317
|
+
for (;;) {
|
|
318
|
+
const port = DECOY_FIRST + Math.floor(Math.random() * (DECOY_LAST - DECOY_FIRST + 1));
|
|
319
|
+
if (!avoid.has(port))
|
|
320
|
+
return avoid.add(port), port;
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
// Preserve every overlap (snapshot/listen/never/control/block), including final-floor exceptions
|
|
324
|
+
// for this executor and its own block. All probes use high decoys, never the real service ports.
|
|
325
|
+
const ports = new Map([[real.execPort, exec]]);
|
|
326
|
+
// An approved localhost:P rule also reaches this Mac's LAN/tailnet IPv4 addresses.
|
|
327
|
+
// Our wildcard IPv4 listener gives each allow a positive control and makes 127.0.0.2
|
|
328
|
+
// a meaningful negative control. IPv6 has its own owned listener when available.
|
|
329
|
+
const connects = [];
|
|
330
|
+
for (const port of real.connectPorts ?? []) {
|
|
331
|
+
const listener = await decoyListener(p, avoid, "0.0.0.0");
|
|
332
|
+
connects.push({ port, listener });
|
|
333
|
+
ports.set(port, listener.port);
|
|
334
|
+
}
|
|
335
|
+
// A never TCP port gets an owned listener too: refusal must come from the final floor.
|
|
336
|
+
const neverListeners = new Map();
|
|
337
|
+
if (connects.length)
|
|
338
|
+
for (const rule of policy.neverPolicy?.ports ?? []) {
|
|
339
|
+
if (rule.transport === "udp" || ports.has(rule.port))
|
|
340
|
+
continue;
|
|
341
|
+
const listener = await decoyListener(p, avoid);
|
|
342
|
+
ports.set(rule.port, listener.port);
|
|
343
|
+
neverListeners.set(rule.port, listener);
|
|
344
|
+
}
|
|
345
|
+
const mapped = (port, preferred) => {
|
|
346
|
+
if (!ports.has(port))
|
|
347
|
+
ports.set(port, preferred ?? filler());
|
|
348
|
+
return ports.get(port);
|
|
349
|
+
};
|
|
350
|
+
// The relay range: the executor's decoy where the turn's executor port is (§5.1: it falls under the
|
|
351
|
+
// range deny), and the ranged decoy at the first other place.
|
|
352
|
+
const execAt = real.relayRange.indexOf(real.execPort);
|
|
353
|
+
if (real.mediatedPort && mediated)
|
|
354
|
+
ports.set(real.mediatedPort, mediated.port);
|
|
355
|
+
const rangeAt = real.relayRange.findIndex((port, i) => i !== execAt && port !== real.mediatedPort);
|
|
356
|
+
// With no bindPorts, choose another free member of the reserved range as the job-block stand-in:
|
|
357
|
+
// this proves the same final rule that denies the actual reserved block in the turn profile.
|
|
358
|
+
const blockAt = real.bindPorts.length ? -1 : real.relayRange.findIndex((port, i) => i !== execAt && i !== rangeAt && port !== real.mediatedPort);
|
|
359
|
+
if (!real.bindPorts.length && blockAt < 0)
|
|
360
|
+
throw new Error("the stage-2 network has no reserved port with which to probe its closed job block");
|
|
361
|
+
const relayRange = real.relayRange.map((port, i) => mapped(port, i === execAt ? exec : i === rangeAt ? ranged.port : i === blockAt ? block : undefined));
|
|
362
|
+
const deniedTcp = real.deniedTcp.map((port, i) => mapped(port, i === 0 ? denied.port : undefined));
|
|
363
|
+
const deniedUdp = real.deniedUdp.map((port, i) => mapped(port, i === 0 ? udpDenied : undefined));
|
|
364
|
+
const bindPorts = real.bindPorts.map((port, i) => mapped(port, i === 0 ? block : undefined));
|
|
365
|
+
const connectPorts = connects.map(c => mapped(c.port));
|
|
366
|
+
const listenPorts = [];
|
|
367
|
+
for (const port of real.listenPorts ?? [])
|
|
368
|
+
listenPorts.push(mapped(port, ports.has(port) ? undefined : await freeTcpPort(avoid)));
|
|
369
|
+
const floor = policy.neverPolicy;
|
|
370
|
+
for (const port of [...(floor?.ports.map(p => p.port) ?? []), ...(floor?.authorityPorts ?? [])]) {
|
|
371
|
+
if (!ports.has(port))
|
|
372
|
+
mapped(port, await freeTcpPort(avoid));
|
|
373
|
+
}
|
|
374
|
+
let neverPolicy = floor;
|
|
375
|
+
if (floor) {
|
|
376
|
+
const { hash: _hash, ...body } = floor;
|
|
377
|
+
const decoyFloor = { ...body, ports: floor.ports.map(rule => ({ ...rule, port: mapped(rule.port) })), authorityPorts: floor.authorityPorts.map(port => mapped(port)) };
|
|
378
|
+
neverPolicy = { ...decoyFloor, hash: neverPolicyHash(decoyFloor) };
|
|
379
|
+
}
|
|
380
|
+
// File grants are exercised only on scratch copies; never open or write the approved real file.
|
|
381
|
+
const overlayFiles = policy.overlayFiles?.map((root, i) => {
|
|
382
|
+
const at = path.join(ctx.scratch, `overlay-file-${i}`);
|
|
383
|
+
if (root.existed)
|
|
384
|
+
fs.writeFileSync(at, "relay probe\n");
|
|
385
|
+
return { ...root, root: at, pins: neverPathIdentity(at) };
|
|
386
|
+
});
|
|
387
|
+
const decoyPolicy = { ...policy, ...cwd, overlayFiles, neverPolicy, network: decoyNetwork(real, {
|
|
388
|
+
deniedTcp,
|
|
389
|
+
deniedUdp,
|
|
390
|
+
relayRange,
|
|
391
|
+
bindPorts,
|
|
392
|
+
execPort: exec,
|
|
393
|
+
...(mediated ? { mediatedPort: mediated.port } : {}),
|
|
394
|
+
...(real.listenPorts ? { listenPorts } : {}),
|
|
395
|
+
...(real.connectPorts ? { connectPorts } : {}),
|
|
396
|
+
}) };
|
|
397
|
+
const d = p.decoy(decoyPolicy);
|
|
398
|
+
const port0Budget = stage2Port0RetryBudget(decoyPolicy.network, decoyPolicy.neverPolicy);
|
|
399
|
+
overlayFiles?.forEach((root, i) => d.allow(`stage2-write-overlay-file-${i}`, { kind: "write", path: root.root }));
|
|
400
|
+
for (const [i, root] of (policy.overlayRoots ?? []).entries()) {
|
|
401
|
+
if (root.scope !== "tree")
|
|
402
|
+
continue;
|
|
403
|
+
// An owned disposable directory within the approved tree: no user file is touched, even if
|
|
404
|
+
// the profile is broken. The surrounding write-outside and all hard denies remain unchanged.
|
|
405
|
+
const dir = fs.mkdtempSync(path.join(root.root, `${ctx.tag}-`));
|
|
406
|
+
p.closers.push(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
407
|
+
p.allow(`stage2-write-overlay-tree-${i}`, { kind: "write", path: path.join(dir, "written") });
|
|
408
|
+
}
|
|
409
|
+
if (cwdSocket)
|
|
410
|
+
d.deny("stage2-unix-cwd-decoy", { kind: "unix", path: cwdSocket });
|
|
411
|
+
const tcp = (host, port) => ({ kind: "tcp", host, port, timeoutMs: STEP_TIMEOUT_MS });
|
|
412
|
+
const udp = (host, port) => ({ kind: "udp", host, port, timeoutMs: STEP_TIMEOUT_MS });
|
|
413
|
+
// Refused by the sandbox, and the decoy saw no connection at all.
|
|
414
|
+
const neverReached = (l) => (g) => sandboxDenied(g) && l.accepted() === 0;
|
|
415
|
+
// A denied port, at every address of this Mac, IPv4-mapped and -compatible spellings included.
|
|
416
|
+
const destinations = localDestinations();
|
|
417
|
+
for (const host of destinations)
|
|
418
|
+
d.deny(`stage2-tcp-denied@${host}`, tcp(host, denied.port), neverReached(denied));
|
|
419
|
+
if (mediated) {
|
|
420
|
+
d.allow("stage2-mediated-proxy", tcp("127.0.0.1", mediated.port));
|
|
421
|
+
d.deny("stage2-mediated-proxy-ipv6", tcp("::1", mediated.port));
|
|
422
|
+
d.deny("stage2-mediated-proxy-mapped", tcp("::ffff:127.0.0.1", mediated.port));
|
|
423
|
+
}
|
|
424
|
+
connects.forEach(({ listener }, i) => {
|
|
425
|
+
const hosts = [...new Set(["127.0.0.1", ...Object.values(os.networkInterfaces()).flatMap(as => as ?? [])
|
|
426
|
+
.filter(a => a.family === "IPv4" && !a.address.startsWith("127.")).map(a => a.address)])];
|
|
427
|
+
for (const host of hosts)
|
|
428
|
+
d.allow(`stage2-connect-overlay-${i}@${host}`, tcp(host, listener.port));
|
|
429
|
+
if (expected.egress === "deny") {
|
|
430
|
+
for (const host of ["127.0.0.2", ...(listener.v6 ? ["::1"] : []), "::ffff:127.0.0.1"])
|
|
431
|
+
d.deny(`stage2-connect-overlay-${i}-denied@${host}`, tcp(host, listener.port));
|
|
432
|
+
d.deny(`stage2-connect-overlay-${i}-other-port`, tcp("127.0.0.1", open.port));
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
// Raw egress's positive control, or a deny for overlay-only turns without raw authority.
|
|
436
|
+
// Include IPv6 and mapped IPv4 (the IPv6 denies name their ports, not all of IPv6).
|
|
437
|
+
const egress = d[expected.egress];
|
|
438
|
+
egress("stage2-tcp-open-port", tcp("127.0.0.1", open.port));
|
|
439
|
+
if (open.v6)
|
|
440
|
+
egress("stage2-tcp-open-port@::1", tcp("::1", open.port));
|
|
441
|
+
egress("stage2-tcp-open-port@::ffff:127.0.0.1", tcp("::ffff:127.0.0.1", open.port));
|
|
442
|
+
// relay's reserved range: another turn's executor or block (at every address, as a denied port),
|
|
443
|
+
// and this turn's own executor.
|
|
444
|
+
if (rangeAt >= 0) {
|
|
445
|
+
d.deny("stage2-tcp-relay-range", tcp("127.0.0.1", ranged.port), neverReached(ranged));
|
|
446
|
+
for (const host of destinations.filter((h) => h !== "127.0.0.1"))
|
|
447
|
+
d.deny(`stage2-tcp-relay-range@${host}`, tcp(host, ranged.port), neverReached(ranged));
|
|
448
|
+
}
|
|
449
|
+
d.deny("stage2-tcp-exec-port", tcp("127.0.0.1", exec));
|
|
450
|
+
// The executor always binds. The block binds only with raw/person authority or an explicit
|
|
451
|
+
// listen/port-0 overlay; exercise both loopback and wildcard because Seatbelt's localhost local
|
|
452
|
+
// rule matches both. Outbound self-connect still requires raw authority.
|
|
453
|
+
d[expected.blockListen]("stage2-listen-block", { kind: "listen-port", host: "127.0.0.1", port: block, connect: expected.connectBlock });
|
|
454
|
+
d[expected.blockListen]("stage2-listen-block-any", { kind: "listen-port", host: "0.0.0.0", port: block, connect: false });
|
|
455
|
+
d.allow("stage2-listen-exec-port", { kind: "listen-port", host: "127.0.0.1", port: exec, connect: false });
|
|
456
|
+
const bind = d[expected.outsideListen];
|
|
457
|
+
bind("stage2-listen-outside-block", { kind: "listen-port", host: "127.0.0.1", port: outside, connect: false });
|
|
458
|
+
const port0 = (host) => ({ kind: "listen-port", host, port: 0, connect: false, ...(broadBind ? { retryOnEperm: port0Budget } : {}) });
|
|
459
|
+
// A kernel-selected port can collide with this profile's denied decoys. Only the positive
|
|
460
|
+
// port-0 checks retry EPERM; a negative control must still observe its very first refusal.
|
|
461
|
+
bind("stage2-listen-port-0", port0("127.0.0.1"), broadBind ? port0Listened(port0Budget) : undefined);
|
|
462
|
+
// Wildcard binding is part of port-0's disclosed scope, not loopback-only authority.
|
|
463
|
+
if (broadBind)
|
|
464
|
+
d.allow("stage2-listen-port-0-any", port0("0.0.0.0"), port0Listened(port0Budget));
|
|
465
|
+
listenPorts.forEach((port, i) => d.allow(`stage2-listen-overlay-${i}`, { kind: "listen-port", host: "127.0.0.1", port, connect: false }));
|
|
466
|
+
const refuseListen = (name, port) => {
|
|
467
|
+
d.deny(name, { kind: "listen-port", host: "127.0.0.1", port, connect: false });
|
|
468
|
+
d.deny(`${name}@0.0.0.0`, { kind: "listen-port", host: "0.0.0.0", port, connect: false });
|
|
469
|
+
};
|
|
470
|
+
// Snapshot/static, other assigned blocks and final never/control-plane listener floors still win.
|
|
471
|
+
// Exact listen exceptions intentionally override the earlier snapshot deny for that port only.
|
|
472
|
+
if (broadBind) {
|
|
473
|
+
deniedTcp.forEach((port, i) => { if (!listenPorts.includes(port))
|
|
474
|
+
refuseListen(`stage2-listen-denied-${i}`, port); });
|
|
475
|
+
relayRange.forEach((port, i) => { if (port !== exec)
|
|
476
|
+
refuseListen(`stage2-listen-relay-range-${i}`, port); });
|
|
477
|
+
}
|
|
478
|
+
neverPolicy?.ports.forEach((rule, i) => {
|
|
479
|
+
if (rule.transport !== "udp") {
|
|
480
|
+
refuseListen(`stage2-listen-never-${i}`, rule.port);
|
|
481
|
+
const listener = neverListeners.get(floor.ports[i].port);
|
|
482
|
+
d.deny(`stage2-tcp-never-${i}`, tcp("127.0.0.1", rule.port), listener ? neverReached(listener) : undefined);
|
|
483
|
+
}
|
|
484
|
+
if (rule.transport !== "tcp") {
|
|
485
|
+
d.deny(`stage2-udp-bind-never-${i}`, { kind: "udp-bind", host: "127.0.0.1", port: rule.port });
|
|
486
|
+
d.deny(`stage2-udp-never-${i}`, udp("127.0.0.1", rule.port));
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
neverPolicy?.authorityPorts.forEach((port, i) => {
|
|
490
|
+
if (port !== exec && !bindPorts.includes(port))
|
|
491
|
+
refuseListen(`stage2-listen-control-${i}`, port);
|
|
492
|
+
if (!bindPorts.includes(port) && port !== mediated?.port)
|
|
493
|
+
d.deny(`stage2-tcp-control-${i}`, tcp("127.0.0.1", port));
|
|
494
|
+
});
|
|
495
|
+
egress("stage2-udp-bind", { kind: "udp-bind", host: "127.0.0.1", port: 0 });
|
|
496
|
+
// UDP to a denied port (1900 and 5351 in the real turn) at any host: loopback both ways and a
|
|
497
|
+
// never-routed address; another port is sent to (the positive control).
|
|
498
|
+
for (const host of ["127.0.0.1", "::1", UNROUTABLE])
|
|
499
|
+
d.deny(`stage2-udp-denied@${host}`, udp(host, deniedUdp[0]));
|
|
500
|
+
egress("stage2-udp-open-port", udp("127.0.0.1", udpOpen));
|
|
501
|
+
}
|