@acepaste/ingress 0.9.25 → 0.9.27

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.
@@ -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 {};
@@ -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
- const bashSource = name === "Bash" ? shellOutputSource(String(input.command ?? ""), { cwd, ownPort: o.ownPort }) : undefined;
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 ?? {}));
@@ -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;
@@ -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")
@@ -146,15 +151,39 @@ async function main() {
146
151
  process.exitCode = 1;
147
152
  return;
148
153
  }
154
+ // ingress --video <file>: gate a video (text and codes on screen and how long they are shown, subtitle tracks, tags, sound)
155
+ if (flag("video")) {
156
+ const file = args.find((x) => !x.startsWith("--"));
157
+ if (!file) {
158
+ console.error("usage: ingress --video <file> [--json] [--captions=file.vtt] [--max-seconds=600] [--max-frames=240]");
159
+ process.exit(2);
160
+ }
161
+ const engine = new IngressEngine({ ...(opt("policy") ? loadPolicyFile(opt("policy")) : {}), sandbox: "off" });
162
+ const r = await engine.scanVideo(readFileSync(file), { url: `file:${file}`, captions: opt("captions") ? readFileSync(opt("captions"), "utf8") : undefined, maxSeconds: opt("max-seconds") ? Number(opt("max-seconds")) : undefined, maxFrames: opt("max-frames") ? Number(opt("max-frames")) : undefined, override: flag("override") });
163
+ if (flag("json"))
164
+ console.log(JSON.stringify(r, null, 2));
165
+ else {
166
+ console.log(r.envelope);
167
+ const c = r.coverage?.video;
168
+ if (c && !c.frames)
169
+ console.error(`\nnote: the video's frames were not read (${c.framesReason}).`);
170
+ else if (c)
171
+ console.error(`\ncoverage: ${Math.round(c.examinedSeconds ?? 0)} of ${Math.round(c.seconds ?? 0)} seconds examined; ${c.framesRead} of ${c.framesTotal} frames read for text (${c.sampling}); sound ${c.audio}${c.audioReason ? ` (${c.audioReason})` : ""}; ${c.subtitleCues} caption cue(s)`);
172
+ }
173
+ if (r.tier !== "clean")
174
+ process.exitCode = 1;
175
+ return;
176
+ }
149
177
  // ingress --image <file>: gate a picture (text in it, codes, what only a machine would read)
150
178
  if (flag("image")) {
151
179
  const file = args.find((x) => !x.startsWith("--"));
152
180
  if (!file) {
153
- console.error("usage: ingress --image <file> [--json] [--caption=words shown beside it]");
181
+ console.error("usage: ingress --image <file> [--json] [--caption=words shown beside it] [--capture=viewport [--page-host=example.com]] (--capture=viewport: the picture is a capture of a page's content area, as a browser tool's screenshot is)");
154
182
  process.exit(2);
155
183
  }
156
184
  const engine = new IngressEngine({ ...(opt("policy") ? loadPolicyFile(opt("policy")) : {}), sandbox: "off" });
157
- const r = await engine.scanImage(readFileSync(file), { url: `file:${file}`, caption: opt("caption"), override: flag("override") });
185
+ const capture = opt("capture");
186
+ const r = await engine.scanImage(readFileSync(file), { url: `file:${file}`, caption: opt("caption"), override: flag("override"), screen: capture === "viewport" || capture === "window" ? { capture, pageHost: opt("page-host") } : undefined });
158
187
  if (flag("json"))
159
188
  console.log(JSON.stringify(r, null, 2));
160
189
  else {
@@ -306,6 +335,100 @@ async function main() {
306
335
  console.error(`\n[timings] ${Object.entries(result.timings).map(([k, v]) => `${k}=${v.toFixed(1)}ms`).join(" ")}`);
307
336
  }
308
337
  main().catch((e) => { console.error(e); process.exit(1); });
338
+ /**
339
+ * ingress egress-proxy run the egress proxy on 127.0.0.1 (see egress-proxy.ts and docs/EGRESS.md)
340
+ * ingress egress-proxy status is it running
341
+ * ingress egress-proxy env [name] the variables that point a shell at it, for a session called <name>
342
+ * ingress egress-proxy log [n] the last n connections it decided (host, port, bytes, decision; never content)
343
+ * Settings: INGRESS_EGRESS_PROXY_PORT (8788), INGRESS_EGRESS_PROXY_MODE=strict, INGRESS_EGRESS_ALLOW_HOSTS,
344
+ * INGRESS_EGRESS_PROXY_ALLOW_PRIVATE, INGRESS_EGRESS_PROXY_PORTS, INGRESS_EGRESS_PROXY_APPROVER=macos-presence.
345
+ */
346
+ async function egressProxyCommand(a) {
347
+ const o = egressProxyOptionsFromEnv();
348
+ const port = o.port ?? 8788;
349
+ if (a[0] === "status") {
350
+ const s = await proxyHealth(port);
351
+ 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})`);
352
+ if (s !== "ours")
353
+ process.exitCode = 1;
354
+ return;
355
+ }
356
+ if (a[0] === "env") {
357
+ const name = a[1] ?? `shell-${process.ppid}`;
358
+ for (const [k, v] of Object.entries(proxyEnv(port, sessionToken(proxySecret(), name))))
359
+ console.log(`export ${k}='${v}'`);
360
+ return;
361
+ }
362
+ if (a[0] === "log") {
363
+ const n = Math.max(1, Math.min(1000, Number(a[1]) || 30));
364
+ let lines = [];
365
+ try {
366
+ lines = readFileSync(join(stateDir(), "egress-proxy", "connections.jsonl"), "utf8").trim().split("\n").slice(-n);
367
+ }
368
+ catch { /* nothing logged yet */ }
369
+ for (const l of lines) {
370
+ try {
371
+ const e = JSON.parse(l);
372
+ 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(" "));
373
+ }
374
+ catch { /* skip */ }
375
+ }
376
+ if (!lines.length)
377
+ console.log("no connections logged");
378
+ return;
379
+ }
380
+ // a host known only from outside content: refused, unless a person at this Mac is asked (Touch ID or password)
381
+ if ((process.env.INGRESS_EGRESS_PROXY_APPROVER ?? "") === "macos-presence" && process.platform === "darwin" && existsSync(presenceHelperPath())) {
382
+ o.approver = presenceApprover(macosDialogApprover({ timeoutSec: 60 }), { biometryOnly: process.env.INGRESS_PRESENCE_BIOMETRY_ONLY === "1" });
383
+ o.policy = { ...o.policy, contentHosts: "ask" };
384
+ }
385
+ // started on demand by a hook: leave after twelve hours with no connection (a hook starts it again when needed)
386
+ if (a.includes("--idle-exit")) {
387
+ o.idleExitMs = 12 * 3600 * 1000;
388
+ o.onIdle = () => process.exit(0);
389
+ }
390
+ try {
391
+ const p = await startEgressProxy(o);
392
+ 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");
393
+ }
394
+ catch (e) {
395
+ console.error(`ingress egress-proxy: could not listen on 127.0.0.1:${port} (${e.message})`);
396
+ process.exit(1);
397
+ }
398
+ }
399
+ /**
400
+ * ingress run [--loopback=allow] -- <command> [args...] run a command with its network confined to the egress proxy
401
+ * ingress run [--loopback=allow] -c '<shell script>' the same for a line of shell
402
+ * macOS only (sandbox-exec). The command and everything it starts can open network connections only to the
403
+ * proxy; with --loopback=allow, also to other ports on this machine (a test suite and its own server), except
404
+ * the name-service ports. Inside an agent session whose commands are already pointed at the proxy, the
405
+ * session's token is used, so the proxy judges the connections with what that session has read.
406
+ */
407
+ async function runCommand(a) {
408
+ const loopback = a.includes("--loopback=allow") ? "allow" : "proxy-only";
409
+ const sep = a.indexOf("--"), c = a.indexOf("-c");
410
+ const argv = c >= 0 && (sep < 0 || c < sep) ? [process.env.SHELL || "/bin/sh", "-c", a[c + 1] ?? ""] : sep >= 0 ? a.slice(sep + 1) : [];
411
+ if (!argv.length || (c >= 0 && !a[c + 1])) {
412
+ console.error("usage: ingress run [--loopback=allow] -- <command> [args...] | ingress run [--loopback=allow] -c '<shell script>'");
413
+ process.exit(2);
414
+ }
415
+ const usable = sandboxUsable();
416
+ if (!usable.ok) {
417
+ console.error(`ingress run: ${usable.why}.`);
418
+ process.exit(126);
419
+ }
420
+ const port = egressProxyOptionsFromEnv().port ?? 8788;
421
+ const up = await ensureEgressProxy({ port });
422
+ if (!up.ok) {
423
+ console.error(`ingress run: the egress proxy is not running (${up.why}). Nothing was run.`);
424
+ process.exit(126);
425
+ }
426
+ const secret = proxySecret();
427
+ // the token this shell was given at session start, if it is one of ours; else a session of its own
428
+ const given = (process.env.INGRESS_EGRESS_PROXY_URL ?? "").match(/^http:\/\/[^:@/]*:([^@/]+)@/)?.[1];
429
+ const token = given && sessionOfToken(secret, given) ? given : sessionToken(secret, `run-${process.pid}-${Date.now().toString(36)}`);
430
+ process.exit(runSandboxed(argv, { proxyPort: port, token, loopback }));
431
+ }
309
432
  /**
310
433
  * ingress hook <event> reply to an agent harness hook: the event's JSON on stdin, the reply on stdout
311
434
  * events: pre-tool-use, post-tool-use, user-prompt-submit, pre-compact, post-compact, session-start
@@ -368,13 +491,16 @@ async function hookCommand(event, stdin) {
368
491
  // INGRESS_EGRESS_ALLOW_HOSTS: comma-separated hosts data may go to without a question (".example.com" covers subdomains)
369
492
  case "pre-tool-use": {
370
493
  const egress = { ownPort, allowHosts: (process.env.INGRESS_EGRESS_ALLOW_HOSTS ?? "").split(",").map((s) => s.trim()).filter(Boolean) };
494
+ // INGRESS_EGRESS_PROXY=on: after the decision, a shell command's start and the hosts it names are noted for
495
+ // the egress proxy, and the proxy is started if it is not running. The command is never rewritten.
496
+ const proxied = (reply) => (egressProxyOn() ? egressProxyBefore(input, reply, { ensure: () => ensureEgressProxy({ port: egressProxyOptionsFromEnv().port }) }) : Promise.resolve(reply));
371
497
  // INGRESS_HOOK_APPROVER: who answers when the gate has a question. Unset: the harness's own prompt (which a
372
498
  // mode that approves prompts automatically answers for the person). "macos-presence": a person at this Mac,
373
499
  // through a system dialog showing the action and then Touch ID or the login password. "deny": nobody; every
374
500
  // question is a refusal. Anything else is a mistake in the settings and is treated as "deny", not ignored.
375
501
  const who = (process.env.INGRESS_HOOK_APPROVER ?? "").trim();
376
502
  if (!who)
377
- return out(preToolUse(input, egress));
503
+ return out(await proxied(preToolUse(input, egress)));
378
504
  // INGRESS_HOOK_APPROVER_SECONDS: how long a person has to answer (default 120, 20 to 540). The hook's timeout
379
505
  // in the harness settings must be longer than this, or the harness kills the hook and carries on without it.
380
506
  const budget = Math.max(20, Math.min(540, Number(process.env.INGRESS_HOOK_APPROVER_SECONDS) || 120));
@@ -383,14 +509,21 @@ async function hookCommand(event, stdin) {
383
509
  : process.platform !== "darwin" ? cannot("macos-presence needs macOS")
384
510
  : !existsSync(presenceHelperPath()) ? cannot("the presence helper is not built (ingress presence build)")
385
511
  : 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 });
512
+ const reply = await proxied(await preToolUseAnswered(input, approver, { ...egress, answerWithinMs: budget * 1000 }));
387
513
  // The answer is written, then the process ends at once: a dialog nobody answered may still be on the
388
514
  // screen, and waiting for it would hold the hook past the harness's timeout (which would count as no answer).
389
515
  if (reply !== undefined)
390
516
  await new Promise((done) => process.stdout.write(JSON.stringify(reply) + "\n", () => done()));
391
517
  process.exit(0);
392
518
  }
393
- case "post-tool-use": return out(await postToolUse(input, gate, { ownPort }));
519
+ case "post-tool-use": {
520
+ // with the egress proxy on: what it carried or refused for this session since the command started
521
+ const proxyConnections = !egressProxyOn() ? undefined : (session, toolUseId) => {
522
+ const since = commandStart(stateDir(), session, toolUseId);
523
+ return since === undefined ? [] : connectionsSince(stateDir(), session, since - 250);
524
+ };
525
+ return out(await postToolUse(input, gate, { ownPort, ...(proxyConnections ? { proxyConnections } : {}) }));
526
+ }
394
527
  case "user-prompt-submit": return out(await userPromptSubmit(input, gate));
395
528
  case "pre-compact":
396
529
  process.stdout.write(preCompact() + "\n");
@@ -401,6 +534,13 @@ async function hookCommand(event, stdin) {
401
534
  case "session-start":
402
535
  // stale blocklists are refreshed by a separate process; the reply does not wait for it
403
536
  startBlocklistRefresh();
537
+ // INGRESS_EGRESS_PROXY=on: the proxy is started if need be, and the session's shell commands are pointed at
538
+ // it through the file the harness reads variables from (CLAUDE_ENV_FILE, which only this hook is given)
539
+ if (egressProxyOn()) {
540
+ const port = egressProxyOptionsFromEnv().port ?? 8788;
541
+ await ensureEgressProxy({ port });
542
+ return out(sessionStart(input, { marker: process.env.INGRESS_SESSION_MARKER !== "0", egressProxy: { port, envFile: process.env.CLAUDE_ENV_FILE || undefined } }));
543
+ }
404
544
  return out(sessionStart(input, { marker: process.env.INGRESS_SESSION_MARKER !== "0" }));
405
545
  default:
406
546
  console.error("usage: ingress hook pre-tool-use|post-tool-use|user-prompt-submit|pre-compact|post-compact|session-start (event JSON on stdin)");
@@ -432,7 +572,14 @@ async function visionCommand(a) {
432
572
  }
433
573
  const dir = mkdtempSync(join(tmpdir(), "ingress-vision-selftest-"));
434
574
  try {
435
- execFileSync(visionHelperPath(), ["--make-fixtures", dir], { timeout: 120_000 });
575
+ try {
576
+ execFileSync(visionHelperPath(), ["--make-fixtures", dir], { timeout: 180_000 });
577
+ }
578
+ catch { /* an older helper writes the pictures and not the clips; the checks below say what is missing */ }
579
+ if (!existsSync(join(dir, "video-plain.mp4")) || !existsSync(join(dir, "screen-drawn-window.png"))) {
580
+ console.error("the vision helper on this machine was built by an earlier version and cannot write the video and screen tests (run: ingress vision build)");
581
+ process.exit(1);
582
+ }
436
583
  const engine = new IngressEngine({ sandbox: "off" }, { log: () => { } });
437
584
  const expect = {
438
585
  "plain.png": { refused: false }, "no-text.png": { refused: false }, "qr-link.png": { refused: false },
@@ -446,10 +593,39 @@ async function visionCommand(a) {
446
593
  const ok = (r.tier !== "clean") === want.refused && (!want.kind || r.findings.some((f) => f.kind === want.kind));
447
594
  if (!ok)
448
595
  bad++;
449
- console.log(`${ok ? "ok " : "FAIL"} ${name.padEnd(20)} ${r.tier}${want.kind ? ` (${want.kind})` : ""}`);
596
+ console.log(`${ok ? "ok " : "FAIL"} ${name.padEnd(24)} ${r.tier}${want.kind ? ` (${want.kind})` : ""}`);
597
+ }
598
+ // a browser window drawn into a page: counts only when the picture is known to be a capture of a page's content area
599
+ const drawn = readFileSync(join(dir, "screen-drawn-window.png"));
600
+ const screens = [
601
+ ["screen-drawn-window.png", drawn, { screen: { capture: "viewport", pageHost: "templates.example.org" } }, true, "image.drawn_browser_window"],
602
+ ["screen-drawn-window.png", drawn, {}, false],
603
+ ["screen-plain-form.png", readFileSync(join(dir, "screen-plain-form.png")), { screen: { capture: "viewport", pageHost: "templates.example.org" } }, false],
604
+ ];
605
+ for (const [name, bytes, o, refused, kind] of screens) {
606
+ const r = await engine.scanImage(bytes, { url: `selftest:${name}`, ...o });
607
+ const ok = (r.tier !== "clean") === refused && (!kind || r.findings.some((f) => f.kind === kind));
608
+ if (!ok)
609
+ bad++;
610
+ console.log(`${ok ? "ok " : "FAIL"} ${name.padEnd(24)} ${r.tier}${kind ? ` (${kind})` : ""}${o?.screen ? "" : " [nothing said about how it was taken]"}`);
611
+ }
612
+ // clips: the same words are refused when shown for a fifteenth of a second, in a code, and in a cue nobody can read
613
+ const clips = {
614
+ "video-plain.mp4": { refused: false }, "video-no-text.mp4": { refused: false },
615
+ "video-brief-frames.mp4": { refused: true, kind: "video.brief_frame_text" }, "video-code.mp4": { refused: true, kind: "image.code_payload" },
616
+ "video-subtitles.mov": { refused: true, kind: "captions.unreadable_cue" },
617
+ };
618
+ for (const [name, want] of Object.entries(clips)) {
619
+ const r = await engine.scanVideo(readFileSync(join(dir, name)), { url: `selftest:${name}`, transcriber: false });
620
+ const read = r.coverage?.video?.frames === true;
621
+ const ok = read && (r.tier !== "clean") === want.refused && (!want.kind || r.findings.some((f) => f.kind === want.kind));
622
+ if (!ok)
623
+ bad++;
624
+ console.log(`${ok ? "ok " : "FAIL"} ${name.padEnd(24)} ${r.tier}${want.kind ? ` (${want.kind})` : ""}${read ? "" : ` [frames not read: ${r.coverage?.video?.framesReason}]`}`);
450
625
  }
626
+ const checks = Object.keys(expect).length + screens.length + Object.keys(clips).length;
451
627
  if (bad) {
452
- console.error(`${bad} of ${Object.keys(expect).length} checks failed on this machine`);
628
+ console.error(`${bad} of ${checks} checks failed on this machine`);
453
629
  process.exitCode = 1;
454
630
  }
455
631
  }