@acepaste/ingress 0.9.25 → 0.9.26
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/dist/src/agent-hooks.d.ts +31 -0
- package/dist/src/agent-hooks.js +99 -3
- package/dist/src/broker.d.ts +12 -0
- package/dist/src/broker.js +10 -0
- package/dist/src/cli.js +120 -4
- package/dist/src/egress-proxy.d.ts +182 -0
- package/dist/src/egress-proxy.js +567 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +2 -0
- package/dist/src/types.d.ts +1 -1
- package/dist/src/types.js +1 -1
- package/package.json +1 -1
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import { type BurstPolicy } from "./safeguards.js";
|
|
25
25
|
import { type Approver } from "./broker.js";
|
|
26
|
+
import { type ProxyConnection } from "./egress-proxy.js";
|
|
26
27
|
import { Blocklist } from "./egress.js";
|
|
27
28
|
import type { ContentKind, IngressResult, SourceInfo } from "./types.js";
|
|
28
29
|
/** What a hook needs from Ingress: the engine in this process, or the local service over HTTP. */
|
|
@@ -59,6 +60,16 @@ export interface HookOptions {
|
|
|
59
60
|
agent?: string;
|
|
60
61
|
from?: string;
|
|
61
62
|
} | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* The egress proxy is on (INGRESS_EGRESS_PROXY=on): what it logged for a session since a shell command started.
|
|
65
|
+
* A command whose programs connected somewhere has outside content in its output, whatever the command looked like.
|
|
66
|
+
*/
|
|
67
|
+
proxyConnections?: (session: string, toolUseId: string | undefined) => ProxyConnection[];
|
|
68
|
+
/** the egress proxy is on: its port, and the file the harness reads shell variables from (CLAUDE_ENV_FILE at session start) */
|
|
69
|
+
egressProxy?: {
|
|
70
|
+
port: number;
|
|
71
|
+
envFile?: string;
|
|
72
|
+
};
|
|
62
73
|
}
|
|
63
74
|
type Json = Record<string, unknown>;
|
|
64
75
|
export declare function withheld(source: string, why: string): string;
|
|
@@ -126,6 +137,26 @@ export declare function preToolUse(event: Json, o?: EgressHookOptions): Json | u
|
|
|
126
137
|
export declare function preToolUseAnswered(event: Json, approver: Approver, o?: EgressHookOptions & {
|
|
127
138
|
answerWithinMs?: number;
|
|
128
139
|
}): Promise<Json | undefined>;
|
|
140
|
+
/**
|
|
141
|
+
* With the egress proxy on, what the pre-tool-use hook does for a shell command after the egress decision
|
|
142
|
+
* (`reply` is that decision's reply; undefined means the gate had no objection, or a person at the machine
|
|
143
|
+
* approved it through the approver):
|
|
144
|
+
* - records when the command started, so the hook that runs after it can ask the proxy what connected meanwhile
|
|
145
|
+
* - records the hosts the command names, when the gate had no objection: what the command starts may connect
|
|
146
|
+
* to them. A command that was refused or put to the harness's prompt grants nothing: a prompt can be
|
|
147
|
+
* declined, and a refused command must not open a door for the next one.
|
|
148
|
+
* - asks, when the command visibly steps around the proxy settings, or the proxy is not running and could
|
|
149
|
+
* not be started (`ensure`)
|
|
150
|
+
* It never rewrites the command. The session's commands were pointed at the proxy when the session started.
|
|
151
|
+
*/
|
|
152
|
+
export declare function egressProxyBefore(event: Json, reply: Json | undefined, o: {
|
|
153
|
+
dir?: string;
|
|
154
|
+
now?: () => number;
|
|
155
|
+
ensure: () => Promise<{
|
|
156
|
+
ok: boolean;
|
|
157
|
+
why?: string;
|
|
158
|
+
}>;
|
|
159
|
+
}): Promise<Json | undefined>;
|
|
129
160
|
/** A gate backed by the Ingress HTTP service, so its receipts and audit trail record what hooks gated. */
|
|
130
161
|
export declare function remoteGate(base: string, key: string, timeoutMs?: number): HookGate;
|
|
131
162
|
export {};
|
package/dist/src/agent-hooks.js
CHANGED
|
@@ -26,7 +26,8 @@ import { execFileSync } from "node:child_process";
|
|
|
26
26
|
import { join } from "node:path";
|
|
27
27
|
import { stateDir } from "./changes.js";
|
|
28
28
|
import { stopReason, canaryNotice } from "./safeguards.js";
|
|
29
|
-
import { ActionBroker } from "./broker.js";
|
|
29
|
+
import { ActionBroker, destinationsIn } from "./broker.js";
|
|
30
|
+
import { noteCommandStart, noteVisibleHosts, proxyEnv, proxySecret, sessionToken } from "./egress-proxy.js";
|
|
30
31
|
import { Blocklist } from "./egress.js";
|
|
31
32
|
import { scanCharacters } from "./character.js";
|
|
32
33
|
import { shellOutputSource, shellWrites, shellUploads } from "./shell-commands.js";
|
|
@@ -88,7 +89,21 @@ async function postToolUseInner(event, gate, o, seen) {
|
|
|
88
89
|
const isMcp = name.startsWith("mcp__") && !gated.some((s) => name.startsWith(`mcp__${s}__`));
|
|
89
90
|
const emit = (out) => ({ hookSpecificOutput: { hookEventName: "PostToolUse", ...out } });
|
|
90
91
|
const cwd = typeof event.cwd === "string" ? event.cwd : undefined;
|
|
91
|
-
|
|
92
|
+
// What the egress proxy saw this command's programs do. A program that connected somewhere brought outside
|
|
93
|
+
// content back, whether or not the command looked like a network command.
|
|
94
|
+
let viaProxy = [];
|
|
95
|
+
if (name === "Bash" && obj && o.proxyConnections) {
|
|
96
|
+
try {
|
|
97
|
+
viaProxy = o.proxyConnections(String(event.session_id ?? ""), typeof event.tool_use_id === "string" ? event.tool_use_id : undefined);
|
|
98
|
+
}
|
|
99
|
+
catch { /* the proxy's log is extra knowledge */ }
|
|
100
|
+
}
|
|
101
|
+
const carried = [...new Set(viaProxy.filter((c) => c.decision === "allow").map((c) => c.host))];
|
|
102
|
+
const proxyNote = proxyNotes(viaProxy);
|
|
103
|
+
const bashSource = name !== "Bash" ? undefined
|
|
104
|
+
: shellOutputSource(String(input.command ?? ""), { cwd, ownPort: o.ownPort })
|
|
105
|
+
?? (carried.length ? `shell:a program that connected to ${carried.slice(0, 3).join(", ")}${carried.length > 3 ? ` and ${carried.length - 3} more` : ""}` : undefined);
|
|
106
|
+
const noted = (out) => (!proxyNote ? out : { hookSpecificOutput: { hookEventName: "PostToolUse", ...(out?.hookSpecificOutput ?? {}), additionalContext: proxyNote } });
|
|
92
107
|
try {
|
|
93
108
|
if (name === "WebFetch" && obj) {
|
|
94
109
|
const url = String(input.url ?? "");
|
|
@@ -123,11 +138,16 @@ async function postToolUseInner(event, gate, o, seen) {
|
|
|
123
138
|
// command output is terminal output: read as a terminal would show it, at open-channel trust
|
|
124
139
|
const text = out + (err ? `\n--- stderr ---\n${err}` : "");
|
|
125
140
|
seen.push({ ref: bashSource });
|
|
141
|
+
for (const h of carried)
|
|
142
|
+
seen.push({ ref: bashSource, host: h }); // where its programs really went joins what the session has contacted
|
|
126
143
|
const env = await refusal(gate, text, bashSource, { kind: "log", source: { channel: "shell command output", trust: 5 } });
|
|
127
144
|
if (!env)
|
|
128
145
|
seen.push({ ref: bashSource, text });
|
|
129
|
-
return env ? emit({ updatedToolOutput: { ...obj, stdout: env, stderr: "" } }) : undefined;
|
|
146
|
+
return noted(env ? emit({ updatedToolOutput: { ...obj, stdout: env, stderr: "" } }) : undefined);
|
|
130
147
|
}
|
|
148
|
+
// nothing came back through the proxy, but it refused something: the model is told why its command failed
|
|
149
|
+
if (name === "Bash" && proxyNote)
|
|
150
|
+
return noted(undefined);
|
|
131
151
|
if (name === "Read" && obj && isTaskOutput(String(input.file_path ?? ""))) {
|
|
132
152
|
const f = obj.file;
|
|
133
153
|
if (!f || typeof f.content !== "string")
|
|
@@ -253,6 +273,19 @@ async function postToolUseInner(event, gate, o, seen) {
|
|
|
253
273
|
}
|
|
254
274
|
return undefined;
|
|
255
275
|
}
|
|
276
|
+
/** What the model is told about connections the egress proxy refused or flagged while a command ran. */
|
|
277
|
+
function proxyNotes(conns) {
|
|
278
|
+
const refused = [...new Map(conns.filter((c) => c.decision === "refuse").map((c) => [`${c.host}:${c.port}`, c])).values()];
|
|
279
|
+
const flags = [...new Set(conns.map((c) => c.flag).filter((f) => typeof f === "string"))];
|
|
280
|
+
if (!refused.length && !flags.length)
|
|
281
|
+
return undefined;
|
|
282
|
+
const parts = [];
|
|
283
|
+
if (refused.length)
|
|
284
|
+
parts.push(`AcePaste Ingress's egress proxy refused ${refused.length === 1 ? "a connection" : `${refused.length} connections`} a program started by this command tried to make: ${refused.slice(0, 4).map((c) => `${c.host}:${c.port} (${c.reason})`).join("; ")}. That is a refusal, not a fault: do not unset the proxy settings or reach the destination another way. Tell the person, who can name the host in their own words or put it on the allowlist.`);
|
|
285
|
+
if (flags.length)
|
|
286
|
+
parts.push(`AcePaste Ingress's egress proxy flagged this command: ${flags.slice(0, 4).join("; ")}. Tell the person.`);
|
|
287
|
+
return parts.join("\n");
|
|
288
|
+
}
|
|
256
289
|
// ---------- UserPromptSubmit: the paste gate ----------
|
|
257
290
|
const PASTED = /<pasted_content id="([^"]*)">\n?([\s\S]*?)\n?<\/pasted_content id="\1">/g;
|
|
258
291
|
/** a person typing this, outside any pasted block, sends the prompt as it is */
|
|
@@ -407,6 +440,20 @@ export function sessionStart(event, o = {}) {
|
|
|
407
440
|
}
|
|
408
441
|
catch { /* without a marker the other checks still run */ }
|
|
409
442
|
}
|
|
443
|
+
// the egress proxy: this session's shell commands are pointed at it, with a token that says whose they are
|
|
444
|
+
if (session && o.egressProxy) {
|
|
445
|
+
try {
|
|
446
|
+
const vars = proxyEnv(o.egressProxy.port, sessionToken(proxySecret(dir), session));
|
|
447
|
+
if (o.egressProxy.envFile) {
|
|
448
|
+
appendFileSync(o.egressProxy.envFile, Object.entries(vars).map(([k, v]) => `export ${k}='${v}'`).join("\n") + "\n");
|
|
449
|
+
notes.push(`Network connections made by shell commands in this session go through AcePaste Ingress's egress proxy on this machine (127.0.0.1:${o.egressProxy.port}), which decides each one by its destination. A connection it refuses fails with "403" from the proxy and the reason. That is a refusal: do not unset the proxy variables or reach the destination another way. Tell the person.`);
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
notes.push("AcePaste Ingress's egress proxy is turned on, but this harness gave the session-start hook no environment file (CLAUDE_ENV_FILE), so this session's shell commands are not routed through it.");
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
catch { /* the other hooks still run */ }
|
|
456
|
+
}
|
|
410
457
|
return notes.length ? { hookSpecificOutput: { hookEventName: "SessionStart", additionalContext: notes.join("\n\n") } } : undefined;
|
|
411
458
|
}
|
|
412
459
|
// ---------- what the session has read, carried between hook calls ----------
|
|
@@ -516,6 +563,55 @@ export async function preToolUseAnswered(event, approver, o = {}) {
|
|
|
516
563
|
const how = answer === "denied" ? "A person at this machine was asked and declined" : answer === "timeout" ? "A person at this machine was asked and did not answer" : `It needs a person at this machine to confirm it, and the approval prompt (${approver.name}) could not be shown`;
|
|
517
564
|
return { hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "deny", permissionDecisionReason: `AcePaste Ingress: ${d.ask.reasons.join("; ")}. ${how}. This is refused; do not try another route. Tell the person what you wanted to do.` } };
|
|
518
565
|
}
|
|
566
|
+
/** visible ways a command steps around the proxy settings its session was given */
|
|
567
|
+
const PROXY_TAMPER = /(?:^|[\s;&|(])(?:unset\s+(?:-\w+\s+)*(?:https?_proxy|all_proxy|no_proxy|node_use_env_proxy)\b|(?:export\s+)?(?:https?_proxy|all_proxy|no_proxy|node_use_env_proxy)=|env\s+(?:-\w+\s+)*-u\s*(?:https?_proxy|all_proxy)\b)|\s--no-?proxy\b|\s--proxy(?:-server)?[=\s]|\bhttps?\.proxy=/i;
|
|
568
|
+
/**
|
|
569
|
+
* With the egress proxy on, what the pre-tool-use hook does for a shell command after the egress decision
|
|
570
|
+
* (`reply` is that decision's reply; undefined means the gate had no objection, or a person at the machine
|
|
571
|
+
* approved it through the approver):
|
|
572
|
+
* - records when the command started, so the hook that runs after it can ask the proxy what connected meanwhile
|
|
573
|
+
* - records the hosts the command names, when the gate had no objection: what the command starts may connect
|
|
574
|
+
* to them. A command that was refused or put to the harness's prompt grants nothing: a prompt can be
|
|
575
|
+
* declined, and a refused command must not open a door for the next one.
|
|
576
|
+
* - asks, when the command visibly steps around the proxy settings, or the proxy is not running and could
|
|
577
|
+
* not be started (`ensure`)
|
|
578
|
+
* It never rewrites the command. The session's commands were pointed at the proxy when the session started.
|
|
579
|
+
*/
|
|
580
|
+
export async function egressProxyBefore(event, reply, o) {
|
|
581
|
+
if (String(event.tool_name ?? "") !== "Bash")
|
|
582
|
+
return reply;
|
|
583
|
+
const session = String(event.session_id ?? "");
|
|
584
|
+
if (!session)
|
|
585
|
+
return reply;
|
|
586
|
+
const decided = (reply?.hookSpecificOutput?.permissionDecision ?? "");
|
|
587
|
+
if (decided === "deny")
|
|
588
|
+
return reply;
|
|
589
|
+
const dir = o.dir ?? stateDir(), now = o.now?.() ?? Date.now();
|
|
590
|
+
const command = String((event.tool_input ?? {}).command ?? "");
|
|
591
|
+
try {
|
|
592
|
+
noteCommandStart(dir, session, typeof event.tool_use_id === "string" ? event.tool_use_id : "", now);
|
|
593
|
+
}
|
|
594
|
+
catch { /* best effort */ }
|
|
595
|
+
if (reply !== undefined)
|
|
596
|
+
return reply; // already a question: it stands, and grants nothing
|
|
597
|
+
try {
|
|
598
|
+
noteVisibleHosts(dir, session, destinationsIn({ command }).hosts, now);
|
|
599
|
+
}
|
|
600
|
+
catch { /* best effort */ }
|
|
601
|
+
const ask = (why) => {
|
|
602
|
+
try {
|
|
603
|
+
appendFileSync(join(dir, "egress-log.jsonl"), JSON.stringify({ at: new Date(now).toISOString(), session, tool: "Bash", decision: "ask", reasons: [why] }) + "\n", { mode: 0o600 });
|
|
604
|
+
}
|
|
605
|
+
catch { /* the question stands */ }
|
|
606
|
+
return { hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "ask", permissionDecisionReason: `AcePaste Ingress: ${why}.` } };
|
|
607
|
+
};
|
|
608
|
+
if (PROXY_TAMPER.test(command))
|
|
609
|
+
return ask("this command changes or steps around the proxy settings that send its network calls through the egress proxy");
|
|
610
|
+
const up = await o.ensure();
|
|
611
|
+
if (!up.ok)
|
|
612
|
+
return ask(`the egress proxy is not running (${up.why ?? "unknown reason"}), so this command's network calls would fail or go unchecked`);
|
|
613
|
+
return undefined;
|
|
614
|
+
}
|
|
519
615
|
function decideEgress(event, o) {
|
|
520
616
|
const name = String(event.tool_name ?? "");
|
|
521
617
|
const action = asAction(name, (event.tool_input ?? {}));
|
package/dist/src/broker.d.ts
CHANGED
|
@@ -243,6 +243,18 @@ export declare class ActionBroker {
|
|
|
243
243
|
sources: string[];
|
|
244
244
|
quarantined: string[];
|
|
245
245
|
};
|
|
246
|
+
/**
|
|
247
|
+
* What this run knows about one destination, for something that sees a connection rather than an action
|
|
248
|
+
* (the egress proxy): is it on the allowlist, did the person name it, and did it first appear in outside
|
|
249
|
+
* content (and where). The same facts `classify` judges an action's destinations with.
|
|
250
|
+
*/
|
|
251
|
+
hostStanding(runId: string, host: string): {
|
|
252
|
+
allowed: boolean;
|
|
253
|
+
typed: boolean;
|
|
254
|
+
fromContent?: string;
|
|
255
|
+
tainted: boolean;
|
|
256
|
+
heardPerson: boolean;
|
|
257
|
+
};
|
|
246
258
|
/** decide without asking anyone (for previews and tests) */
|
|
247
259
|
classify(action: ProposedAction): {
|
|
248
260
|
decision: ActionDecision;
|
package/dist/src/broker.js
CHANGED
|
@@ -345,6 +345,16 @@ export class ActionBroker {
|
|
|
345
345
|
const r = this.run(runId);
|
|
346
346
|
return { tainted: r.sources.size > 0, sources: [...r.sources], quarantined: [...r.quarantined] };
|
|
347
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* What this run knows about one destination, for something that sees a connection rather than an action
|
|
350
|
+
* (the egress proxy): is it on the allowlist, did the person name it, and did it first appear in outside
|
|
351
|
+
* content (and where). The same facts `classify` judges an action's destinations with.
|
|
352
|
+
*/
|
|
353
|
+
hostStanding(runId, host) {
|
|
354
|
+
const r = this.run(runId);
|
|
355
|
+
const h = host.toLowerCase().replace(/^\[|\]$/g, "").replace(/\.$/, "");
|
|
356
|
+
return { allowed: hostAllowed(h, this.policy.allowHosts), typed: typedBy(r, h), fromContent: metIn(r, h), tainted: r.sources.size > 0, heardPerson: r.heardPerson };
|
|
357
|
+
}
|
|
348
358
|
/** decide without asking anyone (for previews and tests) */
|
|
349
359
|
classify(action) {
|
|
350
360
|
const r = this.run(action.runId);
|
package/dist/src/cli.js
CHANGED
|
@@ -21,7 +21,8 @@ import { trackAudit, confirmAuditArea, formatAuditChanges, AUDIT_NEEDS_CONFIRMAT
|
|
|
21
21
|
import { confirmServerTools, describeServerTools } from "./tooldefs.js";
|
|
22
22
|
import { BaselineStore } from "./changes.js";
|
|
23
23
|
import { engageStop, clearStop, stopReason } from "./safeguards.js";
|
|
24
|
-
import { postToolUse, preToolUse, preToolUseAnswered, userPromptSubmit, preCompact, postCompact, sessionStart, remoteGate } from "./agent-hooks.js";
|
|
24
|
+
import { postToolUse, preToolUse, preToolUseAnswered, egressProxyBefore, userPromptSubmit, preCompact, postCompact, sessionStart, remoteGate } from "./agent-hooks.js";
|
|
25
|
+
import { startEgressProxy, egressProxyOptionsFromEnv, egressProxyOn, ensureEgressProxy, proxyHealth, proxySecret, sessionToken, sessionOfToken, proxyEnv, commandStart, connectionsSince, sandboxUsable, runSandboxed } from "./egress-proxy.js";
|
|
25
26
|
import { Blocklist } from "./egress.js";
|
|
26
27
|
import { updateBlocklists, blocklistsRefreshedAt, startBlocklistRefresh } from "./blocklist-update.js";
|
|
27
28
|
import { ProvenanceLedger, describeMark, downloadOrigin } from "./provenance.js";
|
|
@@ -53,6 +54,10 @@ async function main() {
|
|
|
53
54
|
return toolsCommand(args.slice(1));
|
|
54
55
|
if (args[0] === "blocklist")
|
|
55
56
|
return blocklistCommand(args.slice(1));
|
|
57
|
+
if (args[0] === "egress-proxy")
|
|
58
|
+
return egressProxyCommand(args.slice(1));
|
|
59
|
+
if (args[0] === "run")
|
|
60
|
+
return runCommand(args.slice(1));
|
|
56
61
|
if (args[0] === "provenance")
|
|
57
62
|
return provenanceCommand(args.slice(1));
|
|
58
63
|
if (args[0] === "vision")
|
|
@@ -306,6 +311,100 @@ async function main() {
|
|
|
306
311
|
console.error(`\n[timings] ${Object.entries(result.timings).map(([k, v]) => `${k}=${v.toFixed(1)}ms`).join(" ")}`);
|
|
307
312
|
}
|
|
308
313
|
main().catch((e) => { console.error(e); process.exit(1); });
|
|
314
|
+
/**
|
|
315
|
+
* ingress egress-proxy run the egress proxy on 127.0.0.1 (see egress-proxy.ts and docs/EGRESS.md)
|
|
316
|
+
* ingress egress-proxy status is it running
|
|
317
|
+
* ingress egress-proxy env [name] the variables that point a shell at it, for a session called <name>
|
|
318
|
+
* ingress egress-proxy log [n] the last n connections it decided (host, port, bytes, decision; never content)
|
|
319
|
+
* Settings: INGRESS_EGRESS_PROXY_PORT (8788), INGRESS_EGRESS_PROXY_MODE=strict, INGRESS_EGRESS_ALLOW_HOSTS,
|
|
320
|
+
* INGRESS_EGRESS_PROXY_ALLOW_PRIVATE, INGRESS_EGRESS_PROXY_PORTS, INGRESS_EGRESS_PROXY_APPROVER=macos-presence.
|
|
321
|
+
*/
|
|
322
|
+
async function egressProxyCommand(a) {
|
|
323
|
+
const o = egressProxyOptionsFromEnv();
|
|
324
|
+
const port = o.port ?? 8788;
|
|
325
|
+
if (a[0] === "status") {
|
|
326
|
+
const s = await proxyHealth(port);
|
|
327
|
+
console.log(s === "ours" ? `running on 127.0.0.1:${port}` : s === "other" ? `port ${port} answers, but not as this installation's egress proxy` : `not running (port ${port})`);
|
|
328
|
+
if (s !== "ours")
|
|
329
|
+
process.exitCode = 1;
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (a[0] === "env") {
|
|
333
|
+
const name = a[1] ?? `shell-${process.ppid}`;
|
|
334
|
+
for (const [k, v] of Object.entries(proxyEnv(port, sessionToken(proxySecret(), name))))
|
|
335
|
+
console.log(`export ${k}='${v}'`);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (a[0] === "log") {
|
|
339
|
+
const n = Math.max(1, Math.min(1000, Number(a[1]) || 30));
|
|
340
|
+
let lines = [];
|
|
341
|
+
try {
|
|
342
|
+
lines = readFileSync(join(stateDir(), "egress-proxy", "connections.jsonl"), "utf8").trim().split("\n").slice(-n);
|
|
343
|
+
}
|
|
344
|
+
catch { /* nothing logged yet */ }
|
|
345
|
+
for (const l of lines) {
|
|
346
|
+
try {
|
|
347
|
+
const e = JSON.parse(l);
|
|
348
|
+
console.log([e.at, e.decision, `${e.host}:${e.port}`, e.closed ? `up ${e.up} down ${e.down} bytes` : e.reason, e.flag ? `FLAG ${e.flag}` : ""].filter(Boolean).join(" "));
|
|
349
|
+
}
|
|
350
|
+
catch { /* skip */ }
|
|
351
|
+
}
|
|
352
|
+
if (!lines.length)
|
|
353
|
+
console.log("no connections logged");
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
// a host known only from outside content: refused, unless a person at this Mac is asked (Touch ID or password)
|
|
357
|
+
if ((process.env.INGRESS_EGRESS_PROXY_APPROVER ?? "") === "macos-presence" && process.platform === "darwin" && existsSync(presenceHelperPath())) {
|
|
358
|
+
o.approver = presenceApprover(macosDialogApprover({ timeoutSec: 60 }), { biometryOnly: process.env.INGRESS_PRESENCE_BIOMETRY_ONLY === "1" });
|
|
359
|
+
o.policy = { ...o.policy, contentHosts: "ask" };
|
|
360
|
+
}
|
|
361
|
+
// started on demand by a hook: leave after twelve hours with no connection (a hook starts it again when needed)
|
|
362
|
+
if (a.includes("--idle-exit")) {
|
|
363
|
+
o.idleExitMs = 12 * 3600 * 1000;
|
|
364
|
+
o.onIdle = () => process.exit(0);
|
|
365
|
+
}
|
|
366
|
+
try {
|
|
367
|
+
const p = await startEgressProxy(o);
|
|
368
|
+
process.stderr.write(JSON.stringify({ event: "ingress.egress-proxy.listen", host: "127.0.0.1", port: p.port, mode: o.policy?.mode ?? "log", approver: o.approver?.name ?? "none" }) + "\n");
|
|
369
|
+
}
|
|
370
|
+
catch (e) {
|
|
371
|
+
console.error(`ingress egress-proxy: could not listen on 127.0.0.1:${port} (${e.message})`);
|
|
372
|
+
process.exit(1);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* ingress run [--loopback=allow] -- <command> [args...] run a command with its network confined to the egress proxy
|
|
377
|
+
* ingress run [--loopback=allow] -c '<shell script>' the same for a line of shell
|
|
378
|
+
* macOS only (sandbox-exec). The command and everything it starts can open network connections only to the
|
|
379
|
+
* proxy; with --loopback=allow, also to other ports on this machine (a test suite and its own server), except
|
|
380
|
+
* the name-service ports. Inside an agent session whose commands are already pointed at the proxy, the
|
|
381
|
+
* session's token is used, so the proxy judges the connections with what that session has read.
|
|
382
|
+
*/
|
|
383
|
+
async function runCommand(a) {
|
|
384
|
+
const loopback = a.includes("--loopback=allow") ? "allow" : "proxy-only";
|
|
385
|
+
const sep = a.indexOf("--"), c = a.indexOf("-c");
|
|
386
|
+
const argv = c >= 0 && (sep < 0 || c < sep) ? [process.env.SHELL || "/bin/sh", "-c", a[c + 1] ?? ""] : sep >= 0 ? a.slice(sep + 1) : [];
|
|
387
|
+
if (!argv.length || (c >= 0 && !a[c + 1])) {
|
|
388
|
+
console.error("usage: ingress run [--loopback=allow] -- <command> [args...] | ingress run [--loopback=allow] -c '<shell script>'");
|
|
389
|
+
process.exit(2);
|
|
390
|
+
}
|
|
391
|
+
const usable = sandboxUsable();
|
|
392
|
+
if (!usable.ok) {
|
|
393
|
+
console.error(`ingress run: ${usable.why}.`);
|
|
394
|
+
process.exit(126);
|
|
395
|
+
}
|
|
396
|
+
const port = egressProxyOptionsFromEnv().port ?? 8788;
|
|
397
|
+
const up = await ensureEgressProxy({ port });
|
|
398
|
+
if (!up.ok) {
|
|
399
|
+
console.error(`ingress run: the egress proxy is not running (${up.why}). Nothing was run.`);
|
|
400
|
+
process.exit(126);
|
|
401
|
+
}
|
|
402
|
+
const secret = proxySecret();
|
|
403
|
+
// the token this shell was given at session start, if it is one of ours; else a session of its own
|
|
404
|
+
const given = (process.env.INGRESS_EGRESS_PROXY_URL ?? "").match(/^http:\/\/[^:@/]*:([^@/]+)@/)?.[1];
|
|
405
|
+
const token = given && sessionOfToken(secret, given) ? given : sessionToken(secret, `run-${process.pid}-${Date.now().toString(36)}`);
|
|
406
|
+
process.exit(runSandboxed(argv, { proxyPort: port, token, loopback }));
|
|
407
|
+
}
|
|
309
408
|
/**
|
|
310
409
|
* ingress hook <event> reply to an agent harness hook: the event's JSON on stdin, the reply on stdout
|
|
311
410
|
* events: pre-tool-use, post-tool-use, user-prompt-submit, pre-compact, post-compact, session-start
|
|
@@ -368,13 +467,16 @@ async function hookCommand(event, stdin) {
|
|
|
368
467
|
// INGRESS_EGRESS_ALLOW_HOSTS: comma-separated hosts data may go to without a question (".example.com" covers subdomains)
|
|
369
468
|
case "pre-tool-use": {
|
|
370
469
|
const egress = { ownPort, allowHosts: (process.env.INGRESS_EGRESS_ALLOW_HOSTS ?? "").split(",").map((s) => s.trim()).filter(Boolean) };
|
|
470
|
+
// INGRESS_EGRESS_PROXY=on: after the decision, a shell command's start and the hosts it names are noted for
|
|
471
|
+
// the egress proxy, and the proxy is started if it is not running. The command is never rewritten.
|
|
472
|
+
const proxied = (reply) => (egressProxyOn() ? egressProxyBefore(input, reply, { ensure: () => ensureEgressProxy({ port: egressProxyOptionsFromEnv().port }) }) : Promise.resolve(reply));
|
|
371
473
|
// INGRESS_HOOK_APPROVER: who answers when the gate has a question. Unset: the harness's own prompt (which a
|
|
372
474
|
// mode that approves prompts automatically answers for the person). "macos-presence": a person at this Mac,
|
|
373
475
|
// through a system dialog showing the action and then Touch ID or the login password. "deny": nobody; every
|
|
374
476
|
// question is a refusal. Anything else is a mistake in the settings and is treated as "deny", not ignored.
|
|
375
477
|
const who = (process.env.INGRESS_HOOK_APPROVER ?? "").trim();
|
|
376
478
|
if (!who)
|
|
377
|
-
return out(preToolUse(input, egress));
|
|
479
|
+
return out(await proxied(preToolUse(input, egress)));
|
|
378
480
|
// INGRESS_HOOK_APPROVER_SECONDS: how long a person has to answer (default 120, 20 to 540). The hook's timeout
|
|
379
481
|
// in the harness settings must be longer than this, or the harness kills the hook and carries on without it.
|
|
380
482
|
const budget = Math.max(20, Math.min(540, Number(process.env.INGRESS_HOOK_APPROVER_SECONDS) || 120));
|
|
@@ -383,14 +485,21 @@ async function hookCommand(event, stdin) {
|
|
|
383
485
|
: process.platform !== "darwin" ? cannot("macos-presence needs macOS")
|
|
384
486
|
: !existsSync(presenceHelperPath()) ? cannot("the presence helper is not built (ingress presence build)")
|
|
385
487
|
: presenceApprover(macosDialogApprover({ timeoutSec: Math.round(budget * 0.6) }), { biometryOnly: process.env.INGRESS_PRESENCE_BIOMETRY_ONLY === "1" });
|
|
386
|
-
const reply = await preToolUseAnswered(input, approver, { ...egress, answerWithinMs: budget * 1000 });
|
|
488
|
+
const reply = await proxied(await preToolUseAnswered(input, approver, { ...egress, answerWithinMs: budget * 1000 }));
|
|
387
489
|
// The answer is written, then the process ends at once: a dialog nobody answered may still be on the
|
|
388
490
|
// screen, and waiting for it would hold the hook past the harness's timeout (which would count as no answer).
|
|
389
491
|
if (reply !== undefined)
|
|
390
492
|
await new Promise((done) => process.stdout.write(JSON.stringify(reply) + "\n", () => done()));
|
|
391
493
|
process.exit(0);
|
|
392
494
|
}
|
|
393
|
-
case "post-tool-use":
|
|
495
|
+
case "post-tool-use": {
|
|
496
|
+
// with the egress proxy on: what it carried or refused for this session since the command started
|
|
497
|
+
const proxyConnections = !egressProxyOn() ? undefined : (session, toolUseId) => {
|
|
498
|
+
const since = commandStart(stateDir(), session, toolUseId);
|
|
499
|
+
return since === undefined ? [] : connectionsSince(stateDir(), session, since - 250);
|
|
500
|
+
};
|
|
501
|
+
return out(await postToolUse(input, gate, { ownPort, ...(proxyConnections ? { proxyConnections } : {}) }));
|
|
502
|
+
}
|
|
394
503
|
case "user-prompt-submit": return out(await userPromptSubmit(input, gate));
|
|
395
504
|
case "pre-compact":
|
|
396
505
|
process.stdout.write(preCompact() + "\n");
|
|
@@ -401,6 +510,13 @@ async function hookCommand(event, stdin) {
|
|
|
401
510
|
case "session-start":
|
|
402
511
|
// stale blocklists are refreshed by a separate process; the reply does not wait for it
|
|
403
512
|
startBlocklistRefresh();
|
|
513
|
+
// INGRESS_EGRESS_PROXY=on: the proxy is started if need be, and the session's shell commands are pointed at
|
|
514
|
+
// it through the file the harness reads variables from (CLAUDE_ENV_FILE, which only this hook is given)
|
|
515
|
+
if (egressProxyOn()) {
|
|
516
|
+
const port = egressProxyOptionsFromEnv().port ?? 8788;
|
|
517
|
+
await ensureEgressProxy({ port });
|
|
518
|
+
return out(sessionStart(input, { marker: process.env.INGRESS_SESSION_MARKER !== "0", egressProxy: { port, envFile: process.env.CLAUDE_ENV_FILE || undefined } }));
|
|
519
|
+
}
|
|
404
520
|
return out(sessionStart(input, { marker: process.env.INGRESS_SESSION_MARKER !== "0" }));
|
|
405
521
|
default:
|
|
406
522
|
console.error("usage: ingress hook pre-tool-use|post-tool-use|user-prompt-submit|pre-compact|post-compact|session-start (event JSON on stdin)");
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The egress proxy: where programs an agent runs actually connect.
|
|
3
|
+
*
|
|
4
|
+
* The egress hook reads a command. It does not read the program the command starts: `python upload.py` sends
|
|
5
|
+
* whatever the script sends. Stopping that for a whole machine needs a network filter Apple licenses. This is
|
|
6
|
+
* the part that needs no licence: an HTTP forward proxy on 127.0.0.1 that the agent's shell commands are
|
|
7
|
+
* pointed at (HTTPS_PROXY and friends), which decides each connection by its destination.
|
|
8
|
+
*
|
|
9
|
+
* - CONNECT (everything over TLS) and plain HTTP. It sees a host name and a port, and counts bytes. It never
|
|
10
|
+
* reads what is sent: TLS is not opened.
|
|
11
|
+
* - Every client presents a token made for one agent session. No token, no service. The token says whose
|
|
12
|
+
* connection this is, so the decision can use what that session has read.
|
|
13
|
+
* - The decision reuses what the action broker knows (see ActionBroker.hostStanding): a blocklisted host or
|
|
14
|
+
* address is refused; an address on this machine or a private network is refused (the proxy must not be a
|
|
15
|
+
* way around the checks the gate's own fetches make); a host that came into the session only through
|
|
16
|
+
* outside content, and that no command named, is refused (or put to a person, when an approver is
|
|
17
|
+
* configured); a host on the allowlist, or one the person typed, passes. Anything else passes and is
|
|
18
|
+
* logged, or is refused in strict mode.
|
|
19
|
+
* - Every connection is logged in <state>/egress-proxy/connections.jsonl: time, session, host, port, bytes
|
|
20
|
+
* each way, decision. Never content.
|
|
21
|
+
*
|
|
22
|
+
* What it is not: a firewall. A program that ignores proxy settings connects directly unless it runs under the
|
|
23
|
+
* sandbox wrapper below (`ingress run`). A host that is allowed can pass data on to another. It covers what
|
|
24
|
+
* the agent starts, not the machine.
|
|
25
|
+
*/
|
|
26
|
+
import { type Server } from "node:http";
|
|
27
|
+
import { type Approver } from "./broker.js";
|
|
28
|
+
import { Blocklist } from "./egress.js";
|
|
29
|
+
export declare const EGRESS_PROXY_PORT = 8788;
|
|
30
|
+
/** how long a host named in a command the hook let through stays usable by what that command started */
|
|
31
|
+
export declare const VISIBLE_HOST_TTL_MS: number;
|
|
32
|
+
/** The secret tokens are made with. Created on first use; readable by this user only. */
|
|
33
|
+
export declare function proxySecret(dir?: string): string;
|
|
34
|
+
/** A token for one session: the session's name and a keyed digest of it. Safe to put in a URL. */
|
|
35
|
+
export declare function sessionToken(secret: string, session: string): string;
|
|
36
|
+
/** The session a token was made for, or undefined when it was not made with this secret. */
|
|
37
|
+
export declare function sessionOfToken(secret: string, token: string): string | undefined;
|
|
38
|
+
/** The variables that point a command's network calls at the proxy. Loopback is left alone. */
|
|
39
|
+
export declare function proxyEnv(port: number, token: string): Record<string, string>;
|
|
40
|
+
/** Hosts named in a command the egress hook let through: what that command starts may connect to them. */
|
|
41
|
+
export declare function noteVisibleHosts(dir: string, session: string, hosts: readonly string[], now?: number): void;
|
|
42
|
+
export declare function isVisibleHost(dir: string, session: string, host: string, now?: number): boolean;
|
|
43
|
+
/** When a shell command started, so the hook that runs after it can ask what connected meanwhile. */
|
|
44
|
+
export declare function noteCommandStart(dir: string, session: string, toolUseId: string, now?: number): void;
|
|
45
|
+
export declare function commandStart(dir: string, session: string, toolUseId: string | undefined): number | undefined;
|
|
46
|
+
export interface ProxyConnection {
|
|
47
|
+
at: string;
|
|
48
|
+
session: string;
|
|
49
|
+
host: string;
|
|
50
|
+
port: number;
|
|
51
|
+
decision: "allow" | "refuse";
|
|
52
|
+
/** why, in a few words a person can read */
|
|
53
|
+
reason: string;
|
|
54
|
+
code: string;
|
|
55
|
+
up?: number;
|
|
56
|
+
down?: number;
|
|
57
|
+
ms?: number;
|
|
58
|
+
flag?: string;
|
|
59
|
+
id: string;
|
|
60
|
+
closed?: boolean;
|
|
61
|
+
}
|
|
62
|
+
/** Connections a session opened (or was refused) since a moment. One entry per connection, with the bytes when it has closed. */
|
|
63
|
+
export declare function connectionsSince(dir: string, session: string, sinceMs: number): ProxyConnection[];
|
|
64
|
+
export interface ProxyPolicy {
|
|
65
|
+
/** "log": what no rule refuses passes and is logged. "strict": only allowlisted, typed and command-named hosts pass. */
|
|
66
|
+
mode: "log" | "strict";
|
|
67
|
+
/** hosts that pass without a question (a leading dot covers subdomains) */
|
|
68
|
+
allowHosts: string[];
|
|
69
|
+
/** hosts that may resolve to this machine or a private network */
|
|
70
|
+
allowPrivate: string[];
|
|
71
|
+
/** ports besides 80 and 443 */
|
|
72
|
+
ports: number[];
|
|
73
|
+
/** a host known only from outside content: refuse, or ask the approver */
|
|
74
|
+
contentHosts: "refuse" | "ask";
|
|
75
|
+
/** bytes sent on one connection, after the session has read outside content, that get the connection flagged */
|
|
76
|
+
uploadFlagBytes: number;
|
|
77
|
+
}
|
|
78
|
+
export declare const DEFAULT_PROXY_POLICY: ProxyPolicy;
|
|
79
|
+
export interface ConnectionDecision {
|
|
80
|
+
decision: "allow" | "refuse" | "ask";
|
|
81
|
+
code: string;
|
|
82
|
+
reason: string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Decide one connection. Pure: everything it needs is handed in.
|
|
86
|
+
* `addresses` are what the name resolved to (or the literal address); the caller connects to one of exactly those.
|
|
87
|
+
* Without `addresses` only the rules about the name are applied. The proxy asks that way first: a name it is
|
|
88
|
+
* going to refuse is never looked up, because a lookup of `<anything>.their-domain` is itself a message to
|
|
89
|
+
* whoever runs that domain.
|
|
90
|
+
*/
|
|
91
|
+
export declare function decideConnection(c: {
|
|
92
|
+
host: string;
|
|
93
|
+
port: number;
|
|
94
|
+
addresses?: readonly string[];
|
|
95
|
+
}, ctx: {
|
|
96
|
+
policy: ProxyPolicy;
|
|
97
|
+
blocklist: Blocklist;
|
|
98
|
+
standing: {
|
|
99
|
+
allowed: boolean;
|
|
100
|
+
typed: boolean;
|
|
101
|
+
fromContent?: string;
|
|
102
|
+
tainted: boolean;
|
|
103
|
+
};
|
|
104
|
+
visible: boolean;
|
|
105
|
+
stopped?: string;
|
|
106
|
+
}): ConnectionDecision;
|
|
107
|
+
export interface EgressProxyOptions {
|
|
108
|
+
dir?: string;
|
|
109
|
+
/** 0 picks a free port (tests) */
|
|
110
|
+
port?: number;
|
|
111
|
+
policy?: Partial<ProxyPolicy>;
|
|
112
|
+
blocklist?: Blocklist;
|
|
113
|
+
/** who answers when a host known only from content is put to a person */
|
|
114
|
+
approver?: Approver;
|
|
115
|
+
/** how long a person has to answer */
|
|
116
|
+
approvalMs?: number;
|
|
117
|
+
resolve?: (host: string) => Promise<string[]>;
|
|
118
|
+
now?: () => number;
|
|
119
|
+
/** leave when nothing has connected for this long (a proxy a hook started on demand) */
|
|
120
|
+
idleExitMs?: number;
|
|
121
|
+
onIdle?: () => void;
|
|
122
|
+
}
|
|
123
|
+
export interface RunningEgressProxy {
|
|
124
|
+
port: number;
|
|
125
|
+
server: Server;
|
|
126
|
+
close(): Promise<void>;
|
|
127
|
+
}
|
|
128
|
+
/** Start the proxy on 127.0.0.1. Resolves once it is listening. */
|
|
129
|
+
export declare function startEgressProxy(o?: EgressProxyOptions): Promise<RunningEgressProxy>;
|
|
130
|
+
/** Options from the environment, shared by `ingress egress-proxy` and the HTTP service. */
|
|
131
|
+
export declare function egressProxyOptionsFromEnv(env?: Record<string, string | undefined>): EgressProxyOptions;
|
|
132
|
+
export declare const egressProxyOn: (env?: Record<string, string | undefined>) => boolean;
|
|
133
|
+
/** Is our proxy (one that holds this state folder's secret) listening on the port? */
|
|
134
|
+
export declare function proxyHealth(port: number, dir?: string, timeoutMs?: number): Promise<"ours" | "other" | "down">;
|
|
135
|
+
/**
|
|
136
|
+
* Make sure the proxy is listening, starting `ingress egress-proxy` as a detached process when it is not.
|
|
137
|
+
* Never throws. "other": something else has the port, and nothing was started.
|
|
138
|
+
*/
|
|
139
|
+
export declare function ensureEgressProxy(o?: {
|
|
140
|
+
port?: number;
|
|
141
|
+
dir?: string;
|
|
142
|
+
cli?: string;
|
|
143
|
+
waitMs?: number;
|
|
144
|
+
spawnImpl?: (command: string, args: string[]) => void;
|
|
145
|
+
}): Promise<{
|
|
146
|
+
ok: boolean;
|
|
147
|
+
why?: string;
|
|
148
|
+
}>;
|
|
149
|
+
/**
|
|
150
|
+
* A macOS sandbox profile (the language `sandbox-exec` reads): everything is allowed except the process's own
|
|
151
|
+
* outbound network, which may only reach the proxy.
|
|
152
|
+
* loopback "proxy-only" nothing but the proxy's port
|
|
153
|
+
* loopback "allow" other ports on this machine too (a test suite talking to its own server), except
|
|
154
|
+
* the name-service ports: a resolver listening on this machine would carry data out
|
|
155
|
+
* Name lookups through the system (which is how nearly every program resolves a name) go over a local socket
|
|
156
|
+
* to a system service; "deny network-outbound" covers that socket, so they fail. A program using the proxy
|
|
157
|
+
* does not need them: the proxy resolves. The files that would let a program speak for another session, or
|
|
158
|
+
* use the Ingress service as a relay, cannot be read.
|
|
159
|
+
*/
|
|
160
|
+
export declare function sandboxProfile(o: {
|
|
161
|
+
proxyPort: number;
|
|
162
|
+
loopback?: "proxy-only" | "allow";
|
|
163
|
+
denyRead?: string[];
|
|
164
|
+
}): string;
|
|
165
|
+
/** Files a sandboxed program must not read: the token secret, and the keys of the local Ingress service. */
|
|
166
|
+
export declare function sandboxSecrets(dir?: string): string[];
|
|
167
|
+
/** Can this process start a sandbox? Not on other systems, and not from inside another sandbox. */
|
|
168
|
+
export declare function sandboxUsable(): {
|
|
169
|
+
ok: boolean;
|
|
170
|
+
why?: string;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Run a command with its network confined to the proxy. Returns the command's exit status.
|
|
174
|
+
* The command inherits this process's terminal. Its children are confined too: a sandbox cannot be left.
|
|
175
|
+
*/
|
|
176
|
+
export declare function runSandboxed(argv: string[], o: {
|
|
177
|
+
proxyPort: number;
|
|
178
|
+
token: string;
|
|
179
|
+
dir?: string;
|
|
180
|
+
loopback?: "proxy-only" | "allow";
|
|
181
|
+
env?: Record<string, string | undefined>;
|
|
182
|
+
}): number;
|
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The egress proxy: where programs an agent runs actually connect.
|
|
3
|
+
*
|
|
4
|
+
* The egress hook reads a command. It does not read the program the command starts: `python upload.py` sends
|
|
5
|
+
* whatever the script sends. Stopping that for a whole machine needs a network filter Apple licenses. This is
|
|
6
|
+
* the part that needs no licence: an HTTP forward proxy on 127.0.0.1 that the agent's shell commands are
|
|
7
|
+
* pointed at (HTTPS_PROXY and friends), which decides each connection by its destination.
|
|
8
|
+
*
|
|
9
|
+
* - CONNECT (everything over TLS) and plain HTTP. It sees a host name and a port, and counts bytes. It never
|
|
10
|
+
* reads what is sent: TLS is not opened.
|
|
11
|
+
* - Every client presents a token made for one agent session. No token, no service. The token says whose
|
|
12
|
+
* connection this is, so the decision can use what that session has read.
|
|
13
|
+
* - The decision reuses what the action broker knows (see ActionBroker.hostStanding): a blocklisted host or
|
|
14
|
+
* address is refused; an address on this machine or a private network is refused (the proxy must not be a
|
|
15
|
+
* way around the checks the gate's own fetches make); a host that came into the session only through
|
|
16
|
+
* outside content, and that no command named, is refused (or put to a person, when an approver is
|
|
17
|
+
* configured); a host on the allowlist, or one the person typed, passes. Anything else passes and is
|
|
18
|
+
* logged, or is refused in strict mode.
|
|
19
|
+
* - Every connection is logged in <state>/egress-proxy/connections.jsonl: time, session, host, port, bytes
|
|
20
|
+
* each way, decision. Never content.
|
|
21
|
+
*
|
|
22
|
+
* What it is not: a firewall. A program that ignores proxy settings connects directly unless it runs under the
|
|
23
|
+
* sandbox wrapper below (`ingress run`). A host that is allowed can pass data on to another. It covers what
|
|
24
|
+
* the agent starts, not the machine.
|
|
25
|
+
*/
|
|
26
|
+
import { createServer, request as httpRequest } from "node:http";
|
|
27
|
+
import { connect as netConnect, isIP } from "node:net";
|
|
28
|
+
import { lookup } from "node:dns/promises";
|
|
29
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
30
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, realpathSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
31
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
32
|
+
import { homedir } from "node:os";
|
|
33
|
+
import { join } from "node:path";
|
|
34
|
+
import { stateDir } from "./changes.js";
|
|
35
|
+
import { stopReason } from "./safeguards.js";
|
|
36
|
+
import { ActionBroker } from "./broker.js";
|
|
37
|
+
import { Blocklist } from "./egress.js";
|
|
38
|
+
import { isPrivateIP } from "./netpolicy.js";
|
|
39
|
+
export const EGRESS_PROXY_PORT = 8788;
|
|
40
|
+
const HEALTH_PATH = "/.well-known/ingress-egress-proxy";
|
|
41
|
+
/** how long a host named in a command the hook let through stays usable by what that command started */
|
|
42
|
+
export const VISIBLE_HOST_TTL_MS = 10 * 60_000;
|
|
43
|
+
const proxyDir = (dir) => join(dir, "egress-proxy");
|
|
44
|
+
const safeName = (session) => session.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 80);
|
|
45
|
+
/** the same file the hooks keep a session's state in (agent-hooks.ts) */
|
|
46
|
+
const sessionStatePath = (dir, session) => join(dir, "sessions", `${safeName(session)}.json`);
|
|
47
|
+
const cleanHost = (h) => h.toLowerCase().replace(/^\[|\]$/g, "").replace(/\.$/, "");
|
|
48
|
+
const hmac = (secret, text) => createHmac("sha256", secret).update(text, "utf8").digest("hex");
|
|
49
|
+
function writeAtomic(path, text) {
|
|
50
|
+
writeFileSync(`${path}.${process.pid}.tmp`, text, { mode: 0o600 });
|
|
51
|
+
renameSync(`${path}.${process.pid}.tmp`, path);
|
|
52
|
+
}
|
|
53
|
+
function readJson(path) {
|
|
54
|
+
try {
|
|
55
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// ---------- tokens: whose connection is this ----------
|
|
62
|
+
/** The secret tokens are made with. Created on first use; readable by this user only. */
|
|
63
|
+
export function proxySecret(dir = stateDir()) {
|
|
64
|
+
const p = join(proxyDir(dir), "secret");
|
|
65
|
+
try {
|
|
66
|
+
const s = readFileSync(p, "utf8").trim();
|
|
67
|
+
if (s.length >= 32)
|
|
68
|
+
return s;
|
|
69
|
+
}
|
|
70
|
+
catch { /* made below */ }
|
|
71
|
+
mkdirSync(proxyDir(dir), { recursive: true, mode: 0o700 });
|
|
72
|
+
try {
|
|
73
|
+
writeFileSync(p, randomBytes(32).toString("hex") + "\n", { mode: 0o600, flag: "wx" });
|
|
74
|
+
}
|
|
75
|
+
catch { /* another process made it first */ }
|
|
76
|
+
return readFileSync(p, "utf8").trim();
|
|
77
|
+
}
|
|
78
|
+
/** A token for one session: the session's name and a keyed digest of it. Safe to put in a URL. */
|
|
79
|
+
export function sessionToken(secret, session) {
|
|
80
|
+
const s = session.slice(0, 200);
|
|
81
|
+
return `${Buffer.from(s, "utf8").toString("base64url")}.${hmac(secret, `session:${s}`).slice(0, 40)}`;
|
|
82
|
+
}
|
|
83
|
+
/** The session a token was made for, or undefined when it was not made with this secret. */
|
|
84
|
+
export function sessionOfToken(secret, token) {
|
|
85
|
+
const dot = token.lastIndexOf(".");
|
|
86
|
+
if (dot <= 0)
|
|
87
|
+
return undefined;
|
|
88
|
+
let session;
|
|
89
|
+
try {
|
|
90
|
+
session = Buffer.from(token.slice(0, dot), "base64url").toString("utf8");
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
if (!session)
|
|
96
|
+
return undefined;
|
|
97
|
+
const want = Buffer.from(hmac(secret, `session:${session}`).slice(0, 40)), got = Buffer.from(token.slice(dot + 1));
|
|
98
|
+
return want.length === got.length && timingSafeEqual(want, got) ? session : undefined;
|
|
99
|
+
}
|
|
100
|
+
/** The variables that point a command's network calls at the proxy. Loopback is left alone. */
|
|
101
|
+
export function proxyEnv(port, token) {
|
|
102
|
+
const url = `http://ingress:${token}@127.0.0.1:${port}`;
|
|
103
|
+
const none = "localhost,127.0.0.1,::1";
|
|
104
|
+
return {
|
|
105
|
+
HTTPS_PROXY: url, HTTP_PROXY: url, ALL_PROXY: url, https_proxy: url, http_proxy: url, all_proxy: url,
|
|
106
|
+
NO_PROXY: none, no_proxy: none,
|
|
107
|
+
NODE_USE_ENV_PROXY: "1", // Node 22.21 / 24 and later: fetch and http(s) honour the variables above
|
|
108
|
+
INGRESS_EGRESS_PROXY_URL: url,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
// ---------- what the hooks leave for the proxy, and the proxy for the hooks ----------
|
|
112
|
+
/** Hosts named in a command the egress hook let through: what that command starts may connect to them. */
|
|
113
|
+
export function noteVisibleHosts(dir, session, hosts, now = Date.now()) {
|
|
114
|
+
if (!session || !hosts.length)
|
|
115
|
+
return;
|
|
116
|
+
const p = join(proxyDir(dir), "visible", `${safeName(session)}.json`);
|
|
117
|
+
const seen = readJson(p) ?? {};
|
|
118
|
+
for (const h of hosts)
|
|
119
|
+
seen[cleanHost(h)] = now;
|
|
120
|
+
const kept = Object.entries(seen).filter(([, at]) => now - at < VISIBLE_HOST_TTL_MS).sort((a, b) => b[1] - a[1]).slice(0, 200);
|
|
121
|
+
mkdirSync(join(proxyDir(dir), "visible"), { recursive: true, mode: 0o700 });
|
|
122
|
+
writeAtomic(p, JSON.stringify(Object.fromEntries(kept)));
|
|
123
|
+
}
|
|
124
|
+
export function isVisibleHost(dir, session, host, now = Date.now()) {
|
|
125
|
+
const seen = readJson(join(proxyDir(dir), "visible", `${safeName(session)}.json`));
|
|
126
|
+
if (!seen)
|
|
127
|
+
return false;
|
|
128
|
+
const h = cleanHost(host);
|
|
129
|
+
return Object.entries(seen).some(([named, at]) => now - at < VISIBLE_HOST_TTL_MS && (h === named || h.endsWith("." + named)));
|
|
130
|
+
}
|
|
131
|
+
/** When a shell command started, so the hook that runs after it can ask what connected meanwhile. */
|
|
132
|
+
export function noteCommandStart(dir, session, toolUseId, now = Date.now()) {
|
|
133
|
+
if (!session)
|
|
134
|
+
return;
|
|
135
|
+
const p = join(proxyDir(dir), "commands", `${safeName(session)}.json`);
|
|
136
|
+
const all = readJson(p) ?? {};
|
|
137
|
+
all[toolUseId || "last"] = now;
|
|
138
|
+
all.last = now;
|
|
139
|
+
const kept = Object.entries(all).sort((a, b) => b[1] - a[1]).slice(0, 60);
|
|
140
|
+
mkdirSync(join(proxyDir(dir), "commands"), { recursive: true, mode: 0o700 });
|
|
141
|
+
writeAtomic(p, JSON.stringify(Object.fromEntries(kept)));
|
|
142
|
+
}
|
|
143
|
+
export function commandStart(dir, session, toolUseId) {
|
|
144
|
+
const all = readJson(join(proxyDir(dir), "commands", `${safeName(session)}.json`));
|
|
145
|
+
return all?.[toolUseId || "last"];
|
|
146
|
+
}
|
|
147
|
+
const logPath = (dir) => join(proxyDir(dir), "connections.jsonl");
|
|
148
|
+
/** Connections a session opened (or was refused) since a moment. One entry per connection, with the bytes when it has closed. */
|
|
149
|
+
export function connectionsSince(dir, session, sinceMs) {
|
|
150
|
+
let text;
|
|
151
|
+
try {
|
|
152
|
+
text = readFileSync(logPath(dir), "utf8");
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
if (text.length > 4_000_000)
|
|
158
|
+
text = text.slice(-4_000_000);
|
|
159
|
+
const byId = new Map();
|
|
160
|
+
for (const line of text.split("\n")) {
|
|
161
|
+
if (!line.includes(`"session":${JSON.stringify(session)}`))
|
|
162
|
+
continue;
|
|
163
|
+
let e;
|
|
164
|
+
try {
|
|
165
|
+
e = JSON.parse(line);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (e.session !== session || typeof e.id !== "string")
|
|
171
|
+
continue;
|
|
172
|
+
const first = byId.get(e.id);
|
|
173
|
+
if (first)
|
|
174
|
+
byId.set(e.id, { ...first, up: e.up, down: e.down, ms: e.ms, flag: e.flag ?? first.flag, closed: true });
|
|
175
|
+
else if (Date.parse(e.at) >= sinceMs && !e.closed)
|
|
176
|
+
byId.set(e.id, e);
|
|
177
|
+
}
|
|
178
|
+
return [...byId.values()];
|
|
179
|
+
}
|
|
180
|
+
export const DEFAULT_PROXY_POLICY = { mode: "log", allowHosts: [], allowPrivate: [], ports: [], contentHosts: "refuse", uploadFlagBytes: 1_000_000 };
|
|
181
|
+
const hostIn = (host, list) => list.some((a) => { const x = cleanHost(a); return x.startsWith(".") ? host === x.slice(1) || host.endsWith(x) : host === x; });
|
|
182
|
+
/**
|
|
183
|
+
* Decide one connection. Pure: everything it needs is handed in.
|
|
184
|
+
* `addresses` are what the name resolved to (or the literal address); the caller connects to one of exactly those.
|
|
185
|
+
* Without `addresses` only the rules about the name are applied. The proxy asks that way first: a name it is
|
|
186
|
+
* going to refuse is never looked up, because a lookup of `<anything>.their-domain` is itself a message to
|
|
187
|
+
* whoever runs that domain.
|
|
188
|
+
*/
|
|
189
|
+
export function decideConnection(c, ctx) {
|
|
190
|
+
const host = cleanHost(c.host), p = ctx.policy;
|
|
191
|
+
if (ctx.stopped)
|
|
192
|
+
return { decision: "refuse", code: "stopped", reason: `a person has stopped all agent actions (${ctx.stopped})` };
|
|
193
|
+
if (c.port !== 80 && c.port !== 443 && !p.ports.includes(c.port))
|
|
194
|
+
return { decision: "refuse", code: "port", reason: `port ${c.port} is not one this proxy carries (80 and 443${p.ports.length ? `, ${p.ports.join(", ")}` : ""})` };
|
|
195
|
+
const listed = ctx.blocklist.match(host) ?? c.addresses?.map((a) => ctx.blocklist.match(a)).find(Boolean);
|
|
196
|
+
if (listed)
|
|
197
|
+
return { decision: "refuse", code: "blocklisted", reason: `${host} is on a blocklist of malware and botnet infrastructure (${listed})` };
|
|
198
|
+
if (c.addresses && !c.addresses.length)
|
|
199
|
+
return { decision: "refuse", code: "unresolvable", reason: `${host} does not resolve to an address` };
|
|
200
|
+
const local = host === "localhost" || /\.(localhost|local|internal|arpa)$/.test(host) || (c.addresses ?? []).some((a) => isPrivateIP(a));
|
|
201
|
+
if (local && !hostIn(host, p.allowPrivate))
|
|
202
|
+
return { decision: "refuse", code: "private", reason: `${host} is this machine or a private network address` };
|
|
203
|
+
if (ctx.standing.allowed || hostIn(host, p.allowHosts))
|
|
204
|
+
return { decision: "allow", code: "allowlisted", reason: "on the allowlist" };
|
|
205
|
+
if (ctx.standing.typed)
|
|
206
|
+
return { decision: "allow", code: "typed", reason: "named by the person" };
|
|
207
|
+
if (ctx.visible)
|
|
208
|
+
return { decision: "allow", code: "named-in-command", reason: "named in a command the egress gate let through" };
|
|
209
|
+
if (ctx.standing.fromContent !== undefined) {
|
|
210
|
+
const reason = `${host} first appeared in ${ctx.standing.fromContent}, not in anything the person typed, and no command names it`;
|
|
211
|
+
return { decision: p.contentHosts === "ask" ? "ask" : "refuse", code: "content-only", reason };
|
|
212
|
+
}
|
|
213
|
+
if (p.mode === "strict")
|
|
214
|
+
return { decision: "refuse", code: "not-listed", reason: `${host} is not on the allowlist, was not named by the person and is not named in a command (strict mode)` };
|
|
215
|
+
return { decision: "allow", code: "default", reason: "no rule against it" };
|
|
216
|
+
}
|
|
217
|
+
const defaultResolve = async (host) => (await lookup(host, { all: true, verbatim: true })).map((a) => a.address);
|
|
218
|
+
/** Start the proxy on 127.0.0.1. Resolves once it is listening. */
|
|
219
|
+
export function startEgressProxy(o = {}) {
|
|
220
|
+
const dir = o.dir ?? stateDir();
|
|
221
|
+
const policy = { ...DEFAULT_PROXY_POLICY, ...(o.policy ?? {}) };
|
|
222
|
+
const now = o.now ?? Date.now;
|
|
223
|
+
const resolve = o.resolve ?? defaultResolve;
|
|
224
|
+
const secret = proxySecret(dir);
|
|
225
|
+
const since = new Date(now()).toISOString();
|
|
226
|
+
mkdirSync(proxyDir(dir), { recursive: true, mode: 0o700 });
|
|
227
|
+
let blocklist = o.blocklist ?? Blocklist.load(join(dir, "blocklists")), blocklistAt = now();
|
|
228
|
+
let lastUse = now(), seq = 0;
|
|
229
|
+
const answers = new Map();
|
|
230
|
+
const asking = new Map();
|
|
231
|
+
const log = (e) => {
|
|
232
|
+
try {
|
|
233
|
+
const p = logPath(dir);
|
|
234
|
+
try {
|
|
235
|
+
if (statSync(p).size > 8_000_000)
|
|
236
|
+
renameSync(p, `${p}.1`);
|
|
237
|
+
}
|
|
238
|
+
catch { /* no log yet */ }
|
|
239
|
+
appendFileSync(p, JSON.stringify({ at: new Date(now()).toISOString(), ...e }) + "\n", { mode: 0o600 });
|
|
240
|
+
}
|
|
241
|
+
catch { /* a connection is decided whether or not it could be recorded */ }
|
|
242
|
+
};
|
|
243
|
+
const sessionOf = (req) => {
|
|
244
|
+
const h = String(req.headers["proxy-authorization"] ?? "");
|
|
245
|
+
const m = h.match(/^Basic\s+([A-Za-z0-9+/=_-]+)$/i);
|
|
246
|
+
if (!m)
|
|
247
|
+
return undefined;
|
|
248
|
+
const creds = Buffer.from(m[1], "base64").toString("utf8");
|
|
249
|
+
return sessionOfToken(secret, creds.slice(creds.indexOf(":") + 1));
|
|
250
|
+
};
|
|
251
|
+
/** Ask a person about one host for one session; one dialog at a time per host, and the answer is kept a while. */
|
|
252
|
+
const askPerson = (session, host, port, reason, sources) => {
|
|
253
|
+
const key = `${session}\n${host}`;
|
|
254
|
+
const kept = answers.get(key);
|
|
255
|
+
if (kept && now() - kept.at < (kept.answer === "approved" ? VISIBLE_HOST_TTL_MS : 120_000))
|
|
256
|
+
return Promise.resolve(kept.answer);
|
|
257
|
+
const running = asking.get(key);
|
|
258
|
+
if (running)
|
|
259
|
+
return running;
|
|
260
|
+
const args = JSON.stringify({ host, port });
|
|
261
|
+
const digest = hmac("approval", `${session}\n${args}`);
|
|
262
|
+
const req = { id: digest.slice(0, 16), runId: session, tool: "network connection (a program the agent started)", canonicalArgs: args, digest, reasons: [reason], destinations: [host], fields: [], sources, createdAt: now(), expiresAt: now() + (o.approvalMs ?? 90_000) };
|
|
263
|
+
let timer;
|
|
264
|
+
const p = (async () => {
|
|
265
|
+
let a;
|
|
266
|
+
try {
|
|
267
|
+
a = await Promise.race([o.approver.request(req), new Promise((r) => { timer = setTimeout(() => r("timeout"), o.approvalMs ?? 90_000); })]);
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
a = "denied";
|
|
271
|
+
}
|
|
272
|
+
finally {
|
|
273
|
+
if (timer)
|
|
274
|
+
clearTimeout(timer);
|
|
275
|
+
}
|
|
276
|
+
const answer = a === "approved" ? "approved" : "denied";
|
|
277
|
+
answers.set(key, { answer, at: now() });
|
|
278
|
+
asking.delete(key);
|
|
279
|
+
return answer;
|
|
280
|
+
})();
|
|
281
|
+
asking.set(key, p);
|
|
282
|
+
return p;
|
|
283
|
+
};
|
|
284
|
+
/** Everything up to "may this connection be made, and to which address". */
|
|
285
|
+
const admit = async (session, hostRaw, port) => {
|
|
286
|
+
lastUse = now();
|
|
287
|
+
const host = cleanHost(hostRaw), id = `${process.pid}-${++seq}`;
|
|
288
|
+
if (!o.blocklist && now() - blocklistAt > 10 * 60_000) {
|
|
289
|
+
blocklist = Blocklist.load(join(dir, "blocklists"));
|
|
290
|
+
blocklistAt = now();
|
|
291
|
+
}
|
|
292
|
+
const broker = new ActionBroker({ allowHosts: policy.allowHosts });
|
|
293
|
+
const run = readJson(sessionStatePath(dir, session));
|
|
294
|
+
broker.importRun(session, run);
|
|
295
|
+
const standing = broker.hostStanding(session, host);
|
|
296
|
+
const ctx = { policy, blocklist, standing, visible: isVisibleHost(dir, session, host, now()), stopped: stopReason(dir) };
|
|
297
|
+
// The name first, then the addresses: a name that is going to be refused is never looked up.
|
|
298
|
+
let addresses = isIP(host) ? [host] : [];
|
|
299
|
+
let d = decideConnection({ host, port, addresses: isIP(host) ? addresses : undefined }, ctx);
|
|
300
|
+
if (d.decision === "ask") {
|
|
301
|
+
const answer = o.approver ? await askPerson(session, host, port, d.reason, broker.state(session).sources) : "denied";
|
|
302
|
+
d = answer === "approved"
|
|
303
|
+
? { decision: "allow", code: "approved", reason: `${d.reason}; a person at this machine approved it` }
|
|
304
|
+
: { decision: "refuse", code: d.code, reason: `${d.reason}; ${o.approver ? "a person at this machine was asked and did not approve it" : "no approver is configured"}` };
|
|
305
|
+
}
|
|
306
|
+
if (d.decision === "allow" && !isIP(host)) {
|
|
307
|
+
try {
|
|
308
|
+
addresses = await resolve(host);
|
|
309
|
+
}
|
|
310
|
+
catch {
|
|
311
|
+
addresses = [];
|
|
312
|
+
}
|
|
313
|
+
// the name is settled; only what its addresses add can change the answer
|
|
314
|
+
const again = decideConnection({ host, port, addresses }, ctx);
|
|
315
|
+
if (again.decision === "refuse" && ["blocklisted", "unresolvable", "private"].includes(again.code))
|
|
316
|
+
d = again;
|
|
317
|
+
}
|
|
318
|
+
log({ session, host, port, decision: d.decision === "allow" ? "allow" : "refuse", reason: d.reason, code: d.code, id });
|
|
319
|
+
if (d.decision !== "allow")
|
|
320
|
+
return { ok: false, reason: d.reason };
|
|
321
|
+
return { ok: true, addresses, tainted: standing.tainted, id };
|
|
322
|
+
};
|
|
323
|
+
const refusalText = (reason) => `AcePaste Ingress egress proxy refused this connection: ${reason}.\nThis is a refusal, not a fault. Do not route around the proxy; tell the person.\n`;
|
|
324
|
+
const closed = (session, host, port, id, started, up, down, tainted) => {
|
|
325
|
+
const flag = tainted && up >= policy.uploadFlagBytes ? `sent ${up} bytes to ${host} after the session had read outside content` : undefined;
|
|
326
|
+
log({ session, host, port, decision: "allow", reason: "closed", code: "closed", id, up, down, ms: now() - started, closed: true, ...(flag ? { flag } : {}) });
|
|
327
|
+
};
|
|
328
|
+
const server = createServer((req, res) => { void onRequest(req, res); });
|
|
329
|
+
// ---- CONNECT: a tunnel. Only the host name and port are seen.
|
|
330
|
+
server.on("connect", (req, client, head) => {
|
|
331
|
+
void (async () => {
|
|
332
|
+
const say = (status, body = "", extra = "") => { try {
|
|
333
|
+
client.end(`HTTP/1.1 ${status}\r\n${extra}Content-Type: text/plain; charset=utf-8\r\nContent-Length: ${Buffer.byteLength(body)}\r\nConnection: close\r\n\r\n${body}`);
|
|
334
|
+
}
|
|
335
|
+
catch { /* gone */ } };
|
|
336
|
+
client.on("error", () => { });
|
|
337
|
+
const session = sessionOf(req);
|
|
338
|
+
if (!session)
|
|
339
|
+
return say("407 Proxy Authentication Required", "AcePaste Ingress egress proxy: this connection carries no session token.\n", 'Proxy-Authenticate: Basic realm="ingress-egress"\r\n');
|
|
340
|
+
const m = String(req.url ?? "").match(/^(\[[0-9a-fA-F:.]+\]|[^:\s]+):(\d{1,5})$/);
|
|
341
|
+
if (!m)
|
|
342
|
+
return say("400 Bad Request", "AcePaste Ingress egress proxy: not a host and port.\n");
|
|
343
|
+
const host = cleanHost(m[1]), port = Number(m[2]);
|
|
344
|
+
const a = await admit(session, host, port);
|
|
345
|
+
if (!a.ok)
|
|
346
|
+
return say("403 Forbidden", refusalText(a.reason), "X-Ingress-Egress: refused\r\n");
|
|
347
|
+
const started = now();
|
|
348
|
+
let up = head.length, down = 0, done = false, tried = 0;
|
|
349
|
+
const dial = () => {
|
|
350
|
+
const upstream = netConnect({ host: a.addresses[tried], port });
|
|
351
|
+
let established = false;
|
|
352
|
+
upstream.setTimeout(30_000, () => upstream.destroy(new Error("timeout")));
|
|
353
|
+
upstream.once("connect", () => {
|
|
354
|
+
established = true;
|
|
355
|
+
upstream.setTimeout(0);
|
|
356
|
+
client.write("HTTP/1.1 200 Connection Established\r\n\r\n");
|
|
357
|
+
if (head.length)
|
|
358
|
+
upstream.write(head);
|
|
359
|
+
client.on("data", (c) => { up += c.length; });
|
|
360
|
+
upstream.on("data", (c) => { down += c.length; });
|
|
361
|
+
client.pipe(upstream);
|
|
362
|
+
upstream.pipe(client);
|
|
363
|
+
const finish = () => { if (done)
|
|
364
|
+
return; done = true; client.destroy(); upstream.destroy(); closed(session, host, port, a.id, started, up, down, a.tainted); };
|
|
365
|
+
client.on("close", finish);
|
|
366
|
+
upstream.on("close", finish);
|
|
367
|
+
});
|
|
368
|
+
upstream.on("error", () => {
|
|
369
|
+
if (established)
|
|
370
|
+
return; // the tunnel was up: its close event finishes it
|
|
371
|
+
if (++tried < a.addresses.length)
|
|
372
|
+
return dial();
|
|
373
|
+
say("502 Bad Gateway", `AcePaste Ingress egress proxy: could not reach ${host}:${port}.\n`);
|
|
374
|
+
});
|
|
375
|
+
};
|
|
376
|
+
dial();
|
|
377
|
+
})();
|
|
378
|
+
});
|
|
379
|
+
// ---- plain HTTP: the request line carries a full URL. Headers and body pass through unread.
|
|
380
|
+
async function onRequest(req, res) {
|
|
381
|
+
const url = String(req.url ?? "");
|
|
382
|
+
const send = (status, body, headers = {}) => { res.writeHead(status, { "content-type": "text/plain; charset=utf-8", ...headers }); res.end(body); };
|
|
383
|
+
if (url.startsWith("/")) {
|
|
384
|
+
// who is listening here: proved by a keyed digest of the caller's challenge, so a hook is not fooled by another program on this port
|
|
385
|
+
const u = new URL(url, "http://127.0.0.1");
|
|
386
|
+
if (req.method === "GET" && u.pathname === HEALTH_PATH)
|
|
387
|
+
return send(200, JSON.stringify({ ok: true, proof: hmac(secret, `health:${u.searchParams.get("c") ?? ""}`), pid: process.pid, since, mode: policy.mode }), { "content-type": "application/json" });
|
|
388
|
+
return send(400, "AcePaste Ingress egress proxy: this is a forward proxy; ask for a full URL.\n");
|
|
389
|
+
}
|
|
390
|
+
const session = sessionOf(req);
|
|
391
|
+
if (!session)
|
|
392
|
+
return send(407, "AcePaste Ingress egress proxy: this connection carries no session token.\n", { "proxy-authenticate": 'Basic realm="ingress-egress"' });
|
|
393
|
+
let target;
|
|
394
|
+
try {
|
|
395
|
+
target = new URL(url);
|
|
396
|
+
}
|
|
397
|
+
catch {
|
|
398
|
+
return send(400, "AcePaste Ingress egress proxy: not a URL.\n");
|
|
399
|
+
}
|
|
400
|
+
if (target.protocol !== "http:")
|
|
401
|
+
return send(400, "AcePaste Ingress egress proxy: only http and CONNECT are carried.\n");
|
|
402
|
+
const host = cleanHost(target.hostname), port = target.port ? Number(target.port) : 80;
|
|
403
|
+
const a = await admit(session, host, port);
|
|
404
|
+
if (!a.ok)
|
|
405
|
+
return send(403, refusalText(a.reason), { "x-ingress-egress": "refused" });
|
|
406
|
+
const started = now();
|
|
407
|
+
let up = 0, down = 0, done = false;
|
|
408
|
+
const headers = { ...req.headers };
|
|
409
|
+
for (const h of ["proxy-authorization", "proxy-connection"])
|
|
410
|
+
delete headers[h];
|
|
411
|
+
const out = httpRequest({ host: a.addresses[0], port, method: req.method, path: `${target.pathname}${target.search}`, headers, setHost: false, timeout: 60_000 }, (back) => {
|
|
412
|
+
res.writeHead(back.statusCode ?? 502, back.headers);
|
|
413
|
+
back.on("data", (c) => { down += c.length; });
|
|
414
|
+
back.pipe(res);
|
|
415
|
+
});
|
|
416
|
+
const finish = () => { if (done)
|
|
417
|
+
return; done = true; closed(session, host, port, a.id, started, up, down, a.tainted); };
|
|
418
|
+
req.on("data", (c) => { up += c.length; });
|
|
419
|
+
out.on("timeout", () => out.destroy(new Error("timeout")));
|
|
420
|
+
out.on("error", () => { if (!res.headersSent)
|
|
421
|
+
send(502, `AcePaste Ingress egress proxy: could not reach ${host}:${port}.\n`);
|
|
422
|
+
else
|
|
423
|
+
res.destroy(); finish(); });
|
|
424
|
+
res.on("close", finish);
|
|
425
|
+
req.pipe(out);
|
|
426
|
+
}
|
|
427
|
+
return new Promise((ok, fail) => {
|
|
428
|
+
server.once("error", fail);
|
|
429
|
+
server.listen(o.port ?? EGRESS_PROXY_PORT, "127.0.0.1", () => {
|
|
430
|
+
const addr = server.address();
|
|
431
|
+
const port = typeof addr === "object" && addr ? addr.port : o.port ?? EGRESS_PROXY_PORT;
|
|
432
|
+
try {
|
|
433
|
+
writeAtomic(join(proxyDir(dir), "proxy.json"), JSON.stringify({ port, pid: process.pid, since }));
|
|
434
|
+
}
|
|
435
|
+
catch { /* informational */ }
|
|
436
|
+
let idle;
|
|
437
|
+
if (o.idleExitMs) {
|
|
438
|
+
idle = setInterval(() => { if (now() - lastUse > o.idleExitMs)
|
|
439
|
+
o.onIdle?.(); }, 60_000);
|
|
440
|
+
idle.unref();
|
|
441
|
+
}
|
|
442
|
+
ok({ port, server, close: () => new Promise((done) => { if (idle)
|
|
443
|
+
clearInterval(idle); server.closeAllConnections?.(); server.close(() => done()); }) });
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
/** Options from the environment, shared by `ingress egress-proxy` and the HTTP service. */
|
|
448
|
+
export function egressProxyOptionsFromEnv(env = process.env) {
|
|
449
|
+
const list = (v) => (v ?? "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
450
|
+
const port = Number(env.INGRESS_EGRESS_PROXY_PORT);
|
|
451
|
+
const flag = Number(env.INGRESS_EGRESS_UPLOAD_FLAG_BYTES);
|
|
452
|
+
return {
|
|
453
|
+
port: Number.isInteger(port) && port > 0 && port < 65536 ? port : EGRESS_PROXY_PORT,
|
|
454
|
+
policy: {
|
|
455
|
+
mode: env.INGRESS_EGRESS_PROXY_MODE === "strict" ? "strict" : "log",
|
|
456
|
+
allowHosts: list(env.INGRESS_EGRESS_ALLOW_HOSTS),
|
|
457
|
+
allowPrivate: list(env.INGRESS_EGRESS_PROXY_ALLOW_PRIVATE),
|
|
458
|
+
ports: list(env.INGRESS_EGRESS_PROXY_PORTS).map(Number).filter((n) => Number.isInteger(n) && n > 0 && n < 65536),
|
|
459
|
+
...(Number.isFinite(flag) && flag > 0 ? { uploadFlagBytes: flag } : {}),
|
|
460
|
+
},
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
export const egressProxyOn = (env = process.env) => /^(on|1|true|yes)$/i.test(env.INGRESS_EGRESS_PROXY ?? "");
|
|
464
|
+
// ---------- is it running; start it ----------
|
|
465
|
+
/** Is our proxy (one that holds this state folder's secret) listening on the port? */
|
|
466
|
+
export async function proxyHealth(port, dir = stateDir(), timeoutMs = 800) {
|
|
467
|
+
const c = randomBytes(8).toString("hex");
|
|
468
|
+
try {
|
|
469
|
+
const res = await fetch(`http://127.0.0.1:${port}${HEALTH_PATH}?c=${c}`, { signal: AbortSignal.timeout(timeoutMs) });
|
|
470
|
+
const body = (await res.json().catch(() => undefined));
|
|
471
|
+
return body?.proof === hmac(proxySecret(dir), `health:${c}`) ? "ours" : "other";
|
|
472
|
+
}
|
|
473
|
+
catch {
|
|
474
|
+
// no answer to the question: is anything listening at all?
|
|
475
|
+
return new Promise((done) => {
|
|
476
|
+
const s = netConnect({ host: "127.0.0.1", port });
|
|
477
|
+
const say = (v) => { s.destroy(); done(v); };
|
|
478
|
+
s.setTimeout(timeoutMs, () => say("other"));
|
|
479
|
+
s.once("connect", () => say("other"));
|
|
480
|
+
s.once("error", () => say("down"));
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Make sure the proxy is listening, starting `ingress egress-proxy` as a detached process when it is not.
|
|
486
|
+
* Never throws. "other": something else has the port, and nothing was started.
|
|
487
|
+
*/
|
|
488
|
+
export async function ensureEgressProxy(o = {}) {
|
|
489
|
+
const port = o.port ?? EGRESS_PROXY_PORT, dir = o.dir ?? stateDir();
|
|
490
|
+
try {
|
|
491
|
+
const state = await proxyHealth(port, dir);
|
|
492
|
+
if (state === "ours")
|
|
493
|
+
return { ok: true };
|
|
494
|
+
if (state === "other")
|
|
495
|
+
return { ok: false, why: `port ${port} on this machine answers, but not as this installation's egress proxy` };
|
|
496
|
+
const cli = o.cli ?? process.argv[1];
|
|
497
|
+
if (!o.spawnImpl && (!cli || !existsSync(cli)))
|
|
498
|
+
return { ok: false, why: "the ingress command could not be found to start it" };
|
|
499
|
+
(o.spawnImpl ?? ((command, args) => { spawn(command, args, { detached: true, stdio: "ignore", env: { ...process.env, INGRESS_EGRESS_PROXY_PORT: String(port) } }).unref(); }))(process.execPath, [cli, "egress-proxy", "--idle-exit"]);
|
|
500
|
+
const until = Date.now() + (o.waitMs ?? 4000);
|
|
501
|
+
while (Date.now() < until) {
|
|
502
|
+
await new Promise((r) => setTimeout(r, 120));
|
|
503
|
+
if ((await proxyHealth(port, dir, 400)) === "ours")
|
|
504
|
+
return { ok: true };
|
|
505
|
+
}
|
|
506
|
+
return { ok: false, why: "it was started and did not answer in time" };
|
|
507
|
+
}
|
|
508
|
+
catch (e) {
|
|
509
|
+
return { ok: false, why: String(e.message ?? e) };
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
// ---------- the sandbox wrapper: programs that ignore proxy settings ----------
|
|
513
|
+
const SANDBOX_EXEC = "/usr/bin/sandbox-exec";
|
|
514
|
+
const sb = (s) => `"${s.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
515
|
+
const real = (p) => { try {
|
|
516
|
+
return realpathSync(p);
|
|
517
|
+
}
|
|
518
|
+
catch {
|
|
519
|
+
return p;
|
|
520
|
+
} };
|
|
521
|
+
/**
|
|
522
|
+
* A macOS sandbox profile (the language `sandbox-exec` reads): everything is allowed except the process's own
|
|
523
|
+
* outbound network, which may only reach the proxy.
|
|
524
|
+
* loopback "proxy-only" nothing but the proxy's port
|
|
525
|
+
* loopback "allow" other ports on this machine too (a test suite talking to its own server), except
|
|
526
|
+
* the name-service ports: a resolver listening on this machine would carry data out
|
|
527
|
+
* Name lookups through the system (which is how nearly every program resolves a name) go over a local socket
|
|
528
|
+
* to a system service; "deny network-outbound" covers that socket, so they fail. A program using the proxy
|
|
529
|
+
* does not need them: the proxy resolves. The files that would let a program speak for another session, or
|
|
530
|
+
* use the Ingress service as a relay, cannot be read.
|
|
531
|
+
*/
|
|
532
|
+
export function sandboxProfile(o) {
|
|
533
|
+
const lines = ["(version 1)", "(allow default)", "(deny network-outbound)"];
|
|
534
|
+
if (o.loopback === "allow")
|
|
535
|
+
lines.push('(allow network-outbound (remote ip "localhost:*"))', ...[53, 853, 5353].filter((p) => p !== o.proxyPort).map((p) => `(deny network-outbound (remote ip "localhost:${p}"))`));
|
|
536
|
+
lines.push(`(allow network-outbound (remote ip "localhost:${o.proxyPort}"))`);
|
|
537
|
+
for (const p of o.denyRead ?? [])
|
|
538
|
+
lines.push(`(deny file-read* (literal ${sb(real(p))}))`);
|
|
539
|
+
return lines.join("\n") + "\n";
|
|
540
|
+
}
|
|
541
|
+
/** Files a sandboxed program must not read: the token secret, and the keys of the local Ingress service. */
|
|
542
|
+
export function sandboxSecrets(dir = stateDir()) {
|
|
543
|
+
const home = join(homedir(), ".acepaste-ingress");
|
|
544
|
+
return [join(proxyDir(dir), "secret"), join(home, "local-api-key"), join(home, "private.pem")];
|
|
545
|
+
}
|
|
546
|
+
/** Can this process start a sandbox? Not on other systems, and not from inside another sandbox. */
|
|
547
|
+
export function sandboxUsable() {
|
|
548
|
+
if (process.platform !== "darwin")
|
|
549
|
+
return { ok: false, why: "the sandbox wrapper is built for macOS only (sandbox-exec); nothing equivalent is built for this system" };
|
|
550
|
+
if (!existsSync(SANDBOX_EXEC))
|
|
551
|
+
return { ok: false, why: `${SANDBOX_EXEC} is not on this Mac` };
|
|
552
|
+
const r = spawnSync(SANDBOX_EXEC, ["-p", "(version 1)(allow default)", "/usr/bin/true"], { stdio: "ignore", timeout: 10_000 });
|
|
553
|
+
if (r.status !== 0)
|
|
554
|
+
return { ok: false, why: "this process cannot start a sandbox, which is what happens inside another sandbox (an agent harness's own sandbox mode, or an app sandbox). Run it outside that sandbox, or rely on that sandbox's own network rules" };
|
|
555
|
+
return { ok: true };
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Run a command with its network confined to the proxy. Returns the command's exit status.
|
|
559
|
+
* The command inherits this process's terminal. Its children are confined too: a sandbox cannot be left.
|
|
560
|
+
*/
|
|
561
|
+
export function runSandboxed(argv, o) {
|
|
562
|
+
const profile = sandboxProfile({ proxyPort: o.proxyPort, loopback: o.loopback, denyRead: sandboxSecrets(o.dir ?? stateDir()) });
|
|
563
|
+
const r = spawnSync(SANDBOX_EXEC, ["-p", profile, ...argv], { stdio: "inherit", env: { ...(o.env ?? process.env), ...proxyEnv(o.proxyPort, o.token) } });
|
|
564
|
+
if (r.error)
|
|
565
|
+
throw r.error;
|
|
566
|
+
return r.status ?? (r.signal ? 128 : 1);
|
|
567
|
+
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -54,6 +54,9 @@ export { oneTimeCodes, carriesCode, credentialsLeaving, hostsMentioned, Blocklis
|
|
|
54
54
|
export type { PeripheralEgress } from "./egress.js";
|
|
55
55
|
export { updateBlocklists, blocklistsRefreshedAt, blocklistRefreshDue, claimBlocklistRefresh, startBlocklistRefresh, BLOCKLIST_MAX_AGE_MS, BLOCKLIST_RETRY_MS } from "./blocklist-update.js";
|
|
56
56
|
export type { FeedOutcome, RefreshStart } from "./blocklist-update.js";
|
|
57
|
+
export { startEgressProxy, decideConnection, egressProxyOptionsFromEnv, egressProxyOn, ensureEgressProxy, proxyHealth, proxySecret, sessionToken, sessionOfToken, proxyEnv, noteVisibleHosts, isVisibleHost, noteCommandStart, commandStart, connectionsSince, sandboxProfile, sandboxSecrets, sandboxUsable, runSandboxed, EGRESS_PROXY_PORT, DEFAULT_PROXY_POLICY, VISIBLE_HOST_TTL_MS, } from "./egress-proxy.js";
|
|
58
|
+
export type { EgressProxyOptions, RunningEgressProxy, ProxyPolicy, ProxyConnection, ConnectionDecision } from "./egress-proxy.js";
|
|
59
|
+
export { egressProxyBefore } from "./agent-hooks.js";
|
|
57
60
|
export { StreamScanner, scanStream } from "./stream.js";
|
|
58
61
|
export type { StreamOptions, StreamStep, StreamSummary } from "./stream.js";
|
|
59
62
|
export { runSandbox, DEFAULT_SCRIPT_HOSTS } from "./sandbox.js";
|
package/dist/src/index.js
CHANGED
|
@@ -32,6 +32,8 @@ export { ProvenanceLedger, describeMark, downloadOrigin } from "./provenance.js"
|
|
|
32
32
|
export { postToolUse, preToolUse, preToolUseAnswered, localPortProgram, userPromptSubmit, preCompact, postCompact, sessionStart, remoteGate, withheld, PASTE_OVERRIDE } from "./agent-hooks.js";
|
|
33
33
|
export { oneTimeCodes, carriesCode, credentialsLeaving, hostsMentioned, Blocklist, BLOCKLIST_FEEDS, peripheralEgress, loopbackPorts, isForwarder, HomeCalls } from "./egress.js";
|
|
34
34
|
export { updateBlocklists, blocklistsRefreshedAt, blocklistRefreshDue, claimBlocklistRefresh, startBlocklistRefresh, BLOCKLIST_MAX_AGE_MS, BLOCKLIST_RETRY_MS } from "./blocklist-update.js";
|
|
35
|
+
export { startEgressProxy, decideConnection, egressProxyOptionsFromEnv, egressProxyOn, ensureEgressProxy, proxyHealth, proxySecret, sessionToken, sessionOfToken, proxyEnv, noteVisibleHosts, isVisibleHost, noteCommandStart, commandStart, connectionsSince, sandboxProfile, sandboxSecrets, sandboxUsable, runSandboxed, EGRESS_PROXY_PORT, DEFAULT_PROXY_POLICY, VISIBLE_HOST_TTL_MS, } from "./egress-proxy.js";
|
|
36
|
+
export { egressProxyBefore } from "./agent-hooks.js";
|
|
35
37
|
export { StreamScanner, scanStream } from "./stream.js";
|
|
36
38
|
export { runSandbox, DEFAULT_SCRIPT_HOSTS } from "./sandbox.js";
|
|
37
39
|
export { createTaintGuard } from "./taint.js";
|
package/dist/src/types.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* at the same source would not see is suspect by default.
|
|
6
6
|
*/
|
|
7
7
|
import type { NetPolicy } from "./netpolicy.js";
|
|
8
|
-
export declare const ENGINE_VERSION = "0.9.
|
|
8
|
+
export declare const ENGINE_VERSION = "0.9.26";
|
|
9
9
|
/** bump whenever a detector's behavior changes; receipts carry it so verdicts are reproducible */
|
|
10
10
|
export declare const RULESET_VERSION = "2026.11.19-3";
|
|
11
11
|
/**
|
package/dist/src/types.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Governing principle: anything the model would receive that a human looking
|
|
5
5
|
* at the same source would not see is suspect by default.
|
|
6
6
|
*/
|
|
7
|
-
export const ENGINE_VERSION = "0.9.
|
|
7
|
+
export const ENGINE_VERSION = "0.9.26";
|
|
8
8
|
/** bump whenever a detector's behavior changes; receipts carry it so verdicts are reproducible */
|
|
9
9
|
export const RULESET_VERSION = "2026.11.19-3";
|
|
10
10
|
export const CONTENT_KINDS = ["html", "markdown", "json", "text", "javascript", "log", "email", "calendar", "message", "notification", "nearby", "automation", "captions", "image", "audio"];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acepaste/ingress",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.26",
|
|
4
4
|
"description": "AcePaste Ingress engine: acquire, visibility, character, script, supply-chain, sandbox, meaning, verdict, envelope, receipts.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/src/index.js",
|