@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.
@@ -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;