@namzu/sandbox 2.0.3 → 4.0.0

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,75 @@
1
+ import type { LookupAddress } from 'node:dns';
2
+ /**
3
+ * Why this address must not be reached, or `null` if it may be.
4
+ *
5
+ * Returns a reason rather than a boolean because the reason is what a
6
+ * denied operator needs: "link-local" and "private" are different mistakes
7
+ * with different fixes, and a bare `false` sends them to read this file.
8
+ */
9
+ export declare function blockedAddressReason(address: string): string | null;
10
+ /**
11
+ * Screen a target that is already an address rather than a name.
12
+ *
13
+ * A `lookup` hook cannot cover this case and it is not obvious why: the
14
+ * socket layer skips resolution entirely when the host is a valid IP
15
+ * literal, so the screening resolver is never called. The whole existing
16
+ * egress suite passed with the resolver in place *because* its upstream is
17
+ * `127.0.0.1` — a literal, never resolved, never screened. A green suite
18
+ * was the evidence the hole was still open.
19
+ *
20
+ * So the two cases need two checks: names are screened inside the resolver
21
+ * the socket calls, literals are screened here before it dials.
22
+ */
23
+ export declare function blockedLiteralReason(host: string): string | null;
24
+ export declare class EgressAddressDenied extends Error {
25
+ readonly host: string;
26
+ readonly address: string;
27
+ readonly reason: string;
28
+ constructor(host: string, address: string, reason: string);
29
+ }
30
+ export interface ScreeningLookupOptions {
31
+ /**
32
+ * Hosts permitted to resolve inward anyway, matched by the allowlist's
33
+ * own rules so `.internal.example` covers subdomains.
34
+ *
35
+ * Per host, never a global switch. An operator who genuinely proxies to
36
+ * one service on a private network needs that service exempted, and
37
+ * turning the screen off entirely to get it would hand every other
38
+ * allowlisted name the same reach.
39
+ */
40
+ readonly allowInwardFor?: readonly string[];
41
+ /** Injected in tests. Defaults to the platform resolver. */
42
+ readonly resolve?: AddressResolver;
43
+ }
44
+ /**
45
+ * The one shape this module asks a resolver for: every address, in the
46
+ * order the resolver returned them.
47
+ *
48
+ * Narrower than the platform signature on purpose. The screen has to see
49
+ * ALL the addresses to be worth anything, so a resolver that can be asked
50
+ * for one is a resolver this code could accidentally ask wrongly.
51
+ */
52
+ export type AddressResolver = (hostname: string, options: {
53
+ readonly all: true;
54
+ readonly verbatim: true;
55
+ }, callback: (err: NodeJS.ErrnoException | null, addresses: LookupAddress[]) => void) => void;
56
+ type LookupCallback = (err: NodeJS.ErrnoException | null, address: string | LookupAddress[], family?: number) => void;
57
+ /**
58
+ * A `lookup` implementation that screens before the socket uses the answer.
59
+ *
60
+ * This is deliberately not "resolve, check, then connect to the address we
61
+ * checked". That shape leaves the socket free to resolve again, and the
62
+ * second answer is the one that decides where the bytes go — so a name that
63
+ * alternates records walks straight through a check that passed a moment
64
+ * earlier. Screening inside the resolver the socket itself calls means there
65
+ * is one resolution, and the address that was screened is the address that
66
+ * gets connected to.
67
+ *
68
+ * Every returned address is screened, not just the one chosen. A record set
69
+ * mixing a public address with an inward one is the ordinary shape of this
70
+ * attack, and screening only the winner makes the outcome depend on which
71
+ * record the resolver happened to order first.
72
+ */
73
+ export declare function createScreeningLookup(options?: ScreeningLookupOptions, isExempt?: (host: string, patterns: readonly string[]) => boolean): (hostname: string, opts: unknown, callback: LookupCallback) => void;
74
+ export {};
75
+ //# sourceMappingURL=address.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/egress/address.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AA6H7C;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAoCnE;AA6BD;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAahE;AAED,qBAAa,mBAAoB,SAAQ,KAAK;IAC7C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;gBAEX,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CASzD;AAED,MAAM,WAAW,sBAAsB;IACtC;;;;;;;;OAQG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAC3C,4DAA4D;IAC5D,QAAQ,CAAC,OAAO,CAAC,EAAE,eAAe,CAAA;CAClC;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GAAG,CAC7B,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE;IAAE,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAA;CAAE,EACxD,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,cAAc,GAAG,IAAI,EAAE,SAAS,EAAE,aAAa,EAAE,KAAK,IAAI,KAC7E,IAAI,CAAA;AAMT,KAAK,cAAc,GAAG,CACrB,GAAG,EAAE,MAAM,CAAC,cAAc,GAAG,IAAI,EACjC,OAAO,EAAE,MAAM,GAAG,aAAa,EAAE,EACjC,MAAM,CAAC,EAAE,MAAM,KACX,IAAI,CAAA;AAET;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,GAAE,sBAA2B,EACpC,QAAQ,GAAE,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,MAAM,EAAE,KAAK,OAAqB,GAC5E,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,cAAc,KAAK,IAAI,CAiErE"}
@@ -0,0 +1,289 @@
1
+ import { lookup as dnsLookup } from 'node:dns';
2
+ import { isIP } from 'node:net';
3
+ const V4_RANGES = [
4
+ { reason: 'loopback', matches: (o) => o[0] === 127 },
5
+ { reason: 'this-host', matches: (o) => o[0] === 0 },
6
+ { reason: 'private', matches: (o) => o[0] === 10 },
7
+ { reason: 'private', matches: (o) => o[0] === 172 && (o[1] ?? 0) >= 16 && (o[1] ?? 0) <= 31 },
8
+ { reason: 'private', matches: (o) => o[0] === 192 && o[1] === 168 },
9
+ // 169.254.0.0/16. The metadata address every major host platform answers
10
+ // on lives here, which is why this range is the one that turns a name
11
+ // check into a credential leak.
12
+ { reason: 'link-local', matches: (o) => o[0] === 169 && o[1] === 254 },
13
+ {
14
+ reason: 'shared-address-space',
15
+ matches: (o) => o[0] === 100 && (o[1] ?? 0) >= 64 && (o[1] ?? 0) <= 127,
16
+ },
17
+ { reason: 'benchmarking', matches: (o) => o[0] === 198 && (o[1] === 18 || o[1] === 19) },
18
+ { reason: 'multicast', matches: (o) => (o[0] ?? 0) >= 224 && (o[0] ?? 0) <= 239 },
19
+ { reason: 'reserved', matches: (o) => (o[0] ?? 0) >= 240 },
20
+ ];
21
+ function parseV4(address) {
22
+ const parts = address.split('.');
23
+ if (parts.length !== 4)
24
+ return null;
25
+ const octets = [];
26
+ for (const part of parts) {
27
+ if (!/^\d{1,3}$/.test(part))
28
+ return null;
29
+ const n = Number(part);
30
+ if (n > 255)
31
+ return null;
32
+ octets.push(n);
33
+ }
34
+ return octets;
35
+ }
36
+ /**
37
+ * Expand an IPv6 literal into its eight 16-bit groups, or `null`.
38
+ *
39
+ * Written out rather than pattern-matched on the text, because a prefix
40
+ * matched as a STRING is a different question from a prefix matched as a
41
+ * NUMBER, and the two disagree exactly where it hurts. `/^f[cd]/` was the
42
+ * first spelling of the unique-local check here, and `fd::1` matches it —
43
+ * but `fd::1` is `00fd:0:…`, an ordinary global address, so that check
44
+ * deleted a slice of the internet. `fe8::1` did the same against
45
+ * `/^fe[89ab]/`. Both are the `>=`-where-`>`-was-meant mistake wearing a
46
+ * regex.
47
+ *
48
+ * It fails the other way too. `0:0:0:0:0:ffff:169.254.169.254` is the
49
+ * metadata address written long, and no `^::`-anchored pattern sees it.
50
+ */
51
+ function parseV6(address) {
52
+ // A zone id (`%eth0`) names an interface, not a different address.
53
+ const text = (address.toLowerCase().split('%')[0] ?? '').trim();
54
+ if (text.length === 0)
55
+ return null;
56
+ // A trailing dotted quad is the last two groups written in v4 notation.
57
+ let head = text;
58
+ let tail = [];
59
+ const lastColon = text.lastIndexOf(':');
60
+ const after = lastColon >= 0 ? text.slice(lastColon + 1) : '';
61
+ if (after.includes('.')) {
62
+ const quad = parseV4(after);
63
+ if (!quad)
64
+ return null;
65
+ const [a, b, c, d] = quad;
66
+ tail = [(a << 8) | b, (c << 8) | d];
67
+ head = text.slice(0, lastColon + 1);
68
+ if (head.endsWith(':') && !head.endsWith('::'))
69
+ head = head.slice(0, -1);
70
+ }
71
+ const halves = head.split('::');
72
+ if (halves.length > 2)
73
+ return null;
74
+ const toGroups = (part) => {
75
+ if (part.length === 0)
76
+ return [];
77
+ const out = [];
78
+ for (const piece of part.split(':')) {
79
+ if (!/^[0-9a-f]{1,4}$/.test(piece))
80
+ return null;
81
+ out.push(Number.parseInt(piece, 16));
82
+ }
83
+ return out;
84
+ };
85
+ if (halves.length === 1) {
86
+ const only = toGroups(halves[0] ?? '');
87
+ if (!only)
88
+ return null;
89
+ const groups = [...only, ...tail];
90
+ return groups.length === 8 ? groups : null;
91
+ }
92
+ const left = toGroups(halves[0] ?? '');
93
+ const right = toGroups(halves[1] ?? '');
94
+ if (!left || !right)
95
+ return null;
96
+ const known = left.length + right.length + tail.length;
97
+ if (known > 8)
98
+ return null;
99
+ return [...left, ...new Array(8 - known).fill(0), ...right, ...tail];
100
+ }
101
+ /** Whether the first `count` groups are all zero. */
102
+ function zeroPrefix(groups, count) {
103
+ return groups.slice(0, count).every((g) => g === 0);
104
+ }
105
+ /**
106
+ * Why this address must not be reached, or `null` if it may be.
107
+ *
108
+ * Returns a reason rather than a boolean because the reason is what a
109
+ * denied operator needs: "link-local" and "private" are different mistakes
110
+ * with different fixes, and a bare `false` sends them to read this file.
111
+ */
112
+ export function blockedAddressReason(address) {
113
+ const literal = unbracket(address);
114
+ const v4 = parseV4(literal);
115
+ if (v4) {
116
+ for (const range of V4_RANGES) {
117
+ if (range.matches(v4))
118
+ return range.reason;
119
+ }
120
+ return null;
121
+ }
122
+ const groups = parseV6(literal);
123
+ if (!groups)
124
+ return null;
125
+ // `::ffff:a.b.c.d` (v4-mapped) and the deprecated `::a.b.c.d`
126
+ // (v4-compatible) are both the v4 address wearing a v6 spelling, and both
127
+ // reach the v4 host. A screen that only understands dotted quads passes
128
+ // them, which is a documented way through this kind of filter rather than
129
+ // an oversight worth ignoring.
130
+ if (zeroPrefix(groups, 5) && groups[5] === 0xffff) {
131
+ return blockedAddressReason(v4FromGroups(groups));
132
+ }
133
+ if (zeroPrefix(groups, 6) && !(groups[6] === 0 && (groups[7] ?? 0) <= 1)) {
134
+ return blockedAddressReason(v4FromGroups(groups));
135
+ }
136
+ if (zeroPrefix(groups, 7)) {
137
+ if (groups[7] === 1)
138
+ return 'loopback';
139
+ if (groups[7] === 0)
140
+ return 'unspecified';
141
+ }
142
+ const first = groups[0] ?? 0;
143
+ // Masked, not prefix-matched: fc00::/7, fe80::/10, ff00::/8.
144
+ if ((first & 0xfe00) === 0xfc00)
145
+ return 'unique-local';
146
+ if ((first & 0xffc0) === 0xfe80)
147
+ return 'link-local';
148
+ if ((first & 0xff00) === 0xff00)
149
+ return 'multicast';
150
+ return null;
151
+ }
152
+ function v4FromGroups(groups) {
153
+ const high = groups[6] ?? 0;
154
+ const low = groups[7] ?? 0;
155
+ return `${high >> 8}.${high & 0xff}.${low >> 8}.${low & 0xff}`;
156
+ }
157
+ /**
158
+ * An IPv6 literal arrives bracketed from one of the two paths.
159
+ *
160
+ * `new URL('http://[::1]/').hostname` is `[::1]`, brackets included, and
161
+ * `parseTarget` reads exactly that. The `Host`-header path hands the same
162
+ * address over bare, because `splitAuthority` strips the brackets itself —
163
+ * two spellings of one address, normalised here rather than at either call
164
+ * site.
165
+ *
166
+ * This is a layer, not a hole closed, and the difference was measured rather
167
+ * than assumed: Node does not read a bracketed string as an IP literal
168
+ * either, so without this the host falls through to `dns.lookup` and the
169
+ * screening resolver refuses it there. What this buys is that
170
+ * `blockedLiteralReason` stops answering `null` about an address it plainly
171
+ * recognises — a true-looking answer the next caller would build on.
172
+ */
173
+ function unbracket(host) {
174
+ const trimmed = host.trim();
175
+ return trimmed.startsWith('[') && trimmed.endsWith(']') ? trimmed.slice(1, -1) : trimmed;
176
+ }
177
+ /**
178
+ * Screen a target that is already an address rather than a name.
179
+ *
180
+ * A `lookup` hook cannot cover this case and it is not obvious why: the
181
+ * socket layer skips resolution entirely when the host is a valid IP
182
+ * literal, so the screening resolver is never called. The whole existing
183
+ * egress suite passed with the resolver in place *because* its upstream is
184
+ * `127.0.0.1` — a literal, never resolved, never screened. A green suite
185
+ * was the evidence the hole was still open.
186
+ *
187
+ * So the two cases need two checks: names are screened inside the resolver
188
+ * the socket calls, literals are screened here before it dials.
189
+ */
190
+ export function blockedLiteralReason(host) {
191
+ // The `isIP` guard states the contract: only an address is screened here.
192
+ //
193
+ // It is defence in depth rather than the thing that saves a hostname, and
194
+ // that was measured — removing it kills no test, because `parseV6` refuses
195
+ // `fdsomething.example` and `ff-cdn.example` on its own. It earns its place
196
+ // against the version of this file that does not: the first screen written
197
+ // here matched `/^f[cd]/` against the raw text, and under that screen this
198
+ // line was the only thing standing between an ordinary CDN hostname and a
199
+ // refusal. Keeping it means a future loosening of the parser cannot quietly
200
+ // turn an address screen back into a name filter.
201
+ if (isIP(unbracket(host)) === 0)
202
+ return null;
203
+ return blockedAddressReason(host);
204
+ }
205
+ export class EgressAddressDenied extends Error {
206
+ host;
207
+ address;
208
+ reason;
209
+ constructor(host, address, reason) {
210
+ super(`Egress denied: ${host} resolves to ${address}, which is a ${reason} address. The host is on the allowlist; the address it resolves to is not reachable from a sandbox.`);
211
+ this.name = 'EgressAddressDenied';
212
+ this.host = host;
213
+ this.address = address;
214
+ this.reason = reason;
215
+ }
216
+ }
217
+ const platformResolver = (hostname, options, callback) => {
218
+ dnsLookup(hostname, options, callback);
219
+ };
220
+ /**
221
+ * A `lookup` implementation that screens before the socket uses the answer.
222
+ *
223
+ * This is deliberately not "resolve, check, then connect to the address we
224
+ * checked". That shape leaves the socket free to resolve again, and the
225
+ * second answer is the one that decides where the bytes go — so a name that
226
+ * alternates records walks straight through a check that passed a moment
227
+ * earlier. Screening inside the resolver the socket itself calls means there
228
+ * is one resolution, and the address that was screened is the address that
229
+ * gets connected to.
230
+ *
231
+ * Every returned address is screened, not just the one chosen. A record set
232
+ * mixing a public address with an inward one is the ordinary shape of this
233
+ * attack, and screening only the winner makes the outcome depend on which
234
+ * record the resolver happened to order first.
235
+ */
236
+ export function createScreeningLookup(options = {}, isExempt = () => false) {
237
+ const resolver = options.resolve ?? platformResolver;
238
+ const exemptions = options.allowInwardFor ?? [];
239
+ return (hostname, opts, callback) => {
240
+ const wantsAll = typeof opts === 'object' && opts !== null && opts.all === true;
241
+ const family = typeof opts === 'number'
242
+ ? opts
243
+ : typeof opts === 'object' && opts !== null
244
+ ? (opts.family ?? 0)
245
+ : 0;
246
+ resolver(hostname, { all: true, verbatim: true }, (err, addresses) => {
247
+ if (err) {
248
+ callback(err, '', 0);
249
+ return;
250
+ }
251
+ const found = addresses ?? [];
252
+ if (found.length === 0) {
253
+ const empty = new Error(`Egress denied: ${hostname} resolved to no addresses.`);
254
+ empty.code = 'ENOTFOUND';
255
+ callback(empty, '', 0);
256
+ return;
257
+ }
258
+ if (!(exemptions.length > 0 && isExempt(hostname, exemptions))) {
259
+ for (const candidate of found) {
260
+ const reason = blockedAddressReason(candidate.address);
261
+ if (reason) {
262
+ callback(new EgressAddressDenied(hostname, candidate.address, reason), '', 0);
263
+ return;
264
+ }
265
+ }
266
+ }
267
+ const usable = family === 0 ? found : found.filter((a) => a.family === family);
268
+ if (usable.length === 0) {
269
+ const mismatch = new Error(`Egress denied: ${hostname} has no address in the requested family.`);
270
+ mismatch.code = 'ENOTFOUND';
271
+ callback(mismatch, '', 0);
272
+ return;
273
+ }
274
+ if (wantsAll) {
275
+ callback(null, usable);
276
+ return;
277
+ }
278
+ const first = usable[0];
279
+ if (!first) {
280
+ const none = new Error(`Egress denied: ${hostname} resolved to no usable address.`);
281
+ none.code = 'ENOTFOUND';
282
+ callback(none, '', 0);
283
+ return;
284
+ }
285
+ callback(null, first.address, first.family);
286
+ });
287
+ };
288
+ }
289
+ //# sourceMappingURL=address.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.js","sourceRoot":"","sources":["../../src/egress/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,UAAU,CAAA;AAE9C,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AAyB/B,MAAM,SAAS,GAAqB;IACnC,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;IACpD,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE;IACnD,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,EAAE;IAClD,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE;IAC7F,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;IACnE,yEAA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CAAC,CAAC,CAAC,CAAA;YACvB,IAAI,CAAC,KAAK,EAAE,CAAC;gBACZ,MAAM,IAAI,GAA0B,IAAI,KAAK,CAC5C,kBAAkB,QAAQ,iCAAiC,CAC3D,CAAA;gBACD,IAAI,CAAC,IAAI,GAAG,WAAW,CAAA;gBACvB,QAAQ,CAAC,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC,CAAA;gBACrB,OAAM;YACP,CAAC;YACD,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;QAC5C,CAAC,CAAC,CAAA;IACH,CAAC,CAAA;AACF,CAAC"}
@@ -1,3 +1,5 @@
1
+ export { blockedAddressReason, blockedLiteralReason, createScreeningLookup, EgressAddressDenied, } from './address.js';
2
+ export type { AddressResolver, ScreeningLookupOptions } from './address.js';
1
3
  export { isHostAllowed, splitAuthority } from './allowlist.js';
2
4
  export { EgressProxy } from './proxy.js';
3
5
  export type { BrokeredCredential, EgressProxyOptions, RunningEgressProxy, } from './proxy.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/egress/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxC,YAAY,EACX,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,GAClB,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/egress/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,GACnB,MAAM,cAAc,CAAA;AACrB,YAAY,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAC3E,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxC,YAAY,EACX,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,GAClB,MAAM,YAAY,CAAA"}
@@ -1,3 +1,4 @@
1
+ export { blockedAddressReason, blockedLiteralReason, createScreeningLookup, EgressAddressDenied, } from './address.js';
1
2
  export { isHostAllowed, splitAuthority } from './allowlist.js';
2
3
  export { EgressProxy } from './proxy.js';
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/egress/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/egress/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,GACnB,MAAM,cAAc,CAAA;AAErB,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA"}
@@ -1,3 +1,4 @@
1
+ import type { ScreeningLookupOptions } from './address.js';
1
2
  /**
2
3
  * The network boundary a sandbox's egress policy is enforced at.
3
4
  *
@@ -49,6 +50,18 @@ export interface EgressProxyOptions {
49
50
  */
50
51
  readonly upgradeToHttps?: boolean;
51
52
  readonly onDenied?: (host: string, reason: string) => void;
53
+ /**
54
+ * Allowlisted hosts permitted to resolve to an inward address anyway.
55
+ *
56
+ * Matched by the allowlist's own rules, so `.internal.example` covers
57
+ * subdomains. Per host on purpose: an operator who genuinely proxies to
58
+ * one service on a private network needs that one exempted, and a global
59
+ * switch to get it would hand every other allowlisted name the same
60
+ * reach — which is the hole this screen exists to close.
61
+ */
62
+ readonly allowInwardFor?: readonly string[];
63
+ /** Injected in tests. Defaults to the platform resolver. */
64
+ readonly resolveAddresses?: ScreeningLookupOptions['resolve'];
52
65
  }
53
66
  export interface RunningEgressProxy {
54
67
  readonly port: number;
@@ -65,10 +78,26 @@ export declare class EgressProxy {
65
78
  private readonly onDenied;
66
79
  /** See the loop guard in `listen`. */
67
80
  private selfPort;
81
+ /**
82
+ * The resolver both paths connect through.
83
+ *
84
+ * Held once rather than built per request so there is exactly one place
85
+ * the address screen can be bypassed, and it is visible from here.
86
+ */
87
+ private readonly lookup;
88
+ private readonly inwardAllowed;
68
89
  constructor(options: EgressProxyOptions);
69
90
  listen(port?: number): Promise<RunningEgressProxy>;
70
91
  private allowed;
71
92
  private deny;
93
+ /**
94
+ * Why this target may not be dialled as written, or `null`.
95
+ *
96
+ * Covers the literal-address case only; a name is screened inside
97
+ * `this.lookup`, which the socket calls. Both are needed — see
98
+ * `blockedLiteralReason` for why one cannot cover the other.
99
+ */
100
+ private literalDenial;
72
101
  /** Plain HTTP. The only path where a credential can be stamped on. */
73
102
  private handleRequest;
74
103
  /**
@@ -81,6 +110,18 @@ export declare class EgressProxy {
81
110
  * agent sends anywhere, a strictly larger risk than the one being
82
111
  * mitigated. A workload that needs brokering speaks plain HTTP to the
83
112
  * proxy and lets it upgrade upstream.
113
+ *
114
+ * **And the allowlist here is a check on the name in the CONNECT line,
115
+ * which is the only thing about this tunnel that is ever in clear text.**
116
+ * The address screen makes it a check on where that name goes, which is a
117
+ * real bound — a permitted name can no longer be a route to the host's own
118
+ * network. It is not a check on what travels afterwards. Inside the tunnel
119
+ * the bytes are the caller's, and the name the caller puts in its own TLS
120
+ * handshake or `Host` header is not visible to this process, so against a
121
+ * determined caller running inside the sandbox this path bounds the
122
+ * DESTINATION and nothing else. Say so plainly rather than let a reader
123
+ * infer that a tunnel to an allowlisted host carries only allowlisted
124
+ * traffic.
84
125
  */
85
126
  private handleConnect;
86
127
  /** Whether a target names this proxy. See the loop guard in `listen`. */
@@ -1 +1 @@
1
- {"version":3,"file":"proxy.d.ts","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E;IACvE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,kEAAkE;IAClE,eAAe,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,SAAS,MAAM,EAAE,CAAC,GAAG,IAAI,CAAA;IAChE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACtB;AAID,qBAAa,WAAW;IACvB,OAAO,CAAC,cAAc,CAAkC;IACxD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA+B;IAC3D,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAS;IACxC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAsD;IAC/E,sCAAsC;IACtC,OAAO,CAAC,QAAQ,CAAoB;IACpC;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0C;IACjE,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAmB;gBAErC,OAAO,EAAE,kBAAkB;IAejC,MAAM,CAAC,IAAI,SAAI,GAAG,OAAO,CAAC,kBAAkB,CAAC;YAyCrC,OAAO;IAWrB,OAAO,CAAC,IAAI;IAIZ;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAKrB,sEAAsE;YACxD,aAAa;IA4F3B;;;;;;;;;;;;;;;;;;;;;;OAsBG;YACW,aAAa;IA+C3B,yEAAyE;IACzE,OAAO,CAAC,MAAM;IAKd,OAAO,CAAC,aAAa;CAGrB"}
@@ -1,6 +1,7 @@
1
1
  import { createServer, request as httpRequest } from 'node:http';
2
2
  import { request as httpsRequest } from 'node:https';
3
3
  import { connect as netConnect } from 'node:net';
4
+ import { EgressAddressDenied, blockedLiteralReason, createScreeningLookup } from './address.js';
4
5
  import { isHostAllowed, splitAuthority } from './allowlist.js';
5
6
  const DENIED_STATUS = 403;
6
7
  export class EgressProxy {
@@ -10,11 +11,24 @@ export class EgressProxy {
10
11
  onDenied;
11
12
  /** See the loop guard in `listen`. */
12
13
  selfPort;
14
+ /**
15
+ * The resolver both paths connect through.
16
+ *
17
+ * Held once rather than built per request so there is exactly one place
18
+ * the address screen can be bypassed, and it is visible from here.
19
+ */
20
+ lookup;
21
+ inwardAllowed;
13
22
  constructor(options) {
14
23
  this.resolveAllowed = options.allowedHosts;
15
24
  this.credentials = options.credentials ?? [];
16
25
  this.upgradeToHttps = options.upgradeToHttps ?? true;
17
26
  this.onDenied = options.onDenied;
27
+ this.inwardAllowed = options.allowInwardFor ?? [];
28
+ this.lookup = createScreeningLookup({
29
+ ...(options.allowInwardFor ? { allowInwardFor: options.allowInwardFor } : {}),
30
+ ...(options.resolveAddresses ? { resolve: options.resolveAddresses } : {}),
31
+ }, isHostAllowed);
18
32
  }
19
33
  async listen(port = 0) {
20
34
  const server = createServer((req, res) => {
@@ -67,6 +81,18 @@ export class EgressProxy {
67
81
  deny(host, reason) {
68
82
  this.onDenied?.(host, reason);
69
83
  }
84
+ /**
85
+ * Why this target may not be dialled as written, or `null`.
86
+ *
87
+ * Covers the literal-address case only; a name is screened inside
88
+ * `this.lookup`, which the socket calls. Both are needed — see
89
+ * `blockedLiteralReason` for why one cannot cover the other.
90
+ */
91
+ literalDenial(host) {
92
+ if (this.inwardAllowed.length > 0 && isHostAllowed(host, this.inwardAllowed))
93
+ return null;
94
+ return blockedLiteralReason(host);
95
+ }
70
96
  /** Plain HTTP. The only path where a credential can be stamped on. */
71
97
  async handleRequest(req, res) {
72
98
  const target = parseTarget(req);
@@ -89,6 +115,17 @@ export class EgressProxy {
89
115
  res.end(`Egress denied: ${target.host} is not on this sandbox's allowlist.\n`);
90
116
  return;
91
117
  }
118
+ const literal = this.literalDenial(target.host);
119
+ if (literal) {
120
+ this.deny(target.host, `is a ${literal} address`);
121
+ res.writeHead(DENIED_STATUS, { 'content-type': 'text/plain' });
122
+ // Refused BEFORE the credential is looked up, let alone stamped.
123
+ // The ordering is the point: on this path the token goes on the
124
+ // headers a few lines below, so a check that ran after it would be
125
+ // deciding whether to send a request that already carried it.
126
+ res.end(`Egress denied: ${target.host} is a ${literal} address, which is not reachable from a sandbox.\n`);
127
+ return;
128
+ }
92
129
  const headers = { ...req.headers };
93
130
  // The proxy re-issues the request, so hop-by-hop headers about the
94
131
  // hop that just ended must not be forwarded.
@@ -108,11 +145,28 @@ export class EgressProxy {
108
145
  method: req.method,
109
146
  path: target.path,
110
147
  headers,
148
+ // `host` stays the NAME so SNI and certificate validation still
149
+ // check the name the allowlist approved; only the address the
150
+ // socket dials is screened. Swapping in the address here would
151
+ // break TLS verification, which is the wrong way to fix this.
152
+ lookup: this.lookup,
111
153
  }, (response) => {
112
154
  res.writeHead(response.statusCode ?? 502, response.headers);
113
155
  response.pipe(res);
114
156
  });
115
157
  upstream.on('error', (err) => {
158
+ // An address denial is a policy refusal, not a network fault, and
159
+ // telling them apart is the difference between an agent that stops
160
+ // and one that retries a forbidden host forever. The credential is
161
+ // already on `headers` at this point and has still not left the
162
+ // process: the socket never connected, so nothing was written.
163
+ if (err instanceof EgressAddressDenied) {
164
+ this.deny(err.host, `resolves to a ${err.reason} address`);
165
+ if (!res.headersSent)
166
+ res.writeHead(DENIED_STATUS, { 'content-type': 'text/plain' });
167
+ res.end(`${err.message}\n`);
168
+ return;
169
+ }
116
170
  if (!res.headersSent)
117
171
  res.writeHead(502, { 'content-type': 'text/plain' });
118
172
  res.end(`Upstream request failed: ${err.message}\n`);
@@ -129,6 +183,18 @@ export class EgressProxy {
129
183
  * agent sends anywhere, a strictly larger risk than the one being
130
184
  * mitigated. A workload that needs brokering speaks plain HTTP to the
131
185
  * proxy and lets it upgrade upstream.
186
+ *
187
+ * **And the allowlist here is a check on the name in the CONNECT line,
188
+ * which is the only thing about this tunnel that is ever in clear text.**
189
+ * The address screen makes it a check on where that name goes, which is a
190
+ * real bound — a permitted name can no longer be a route to the host's own
191
+ * network. It is not a check on what travels afterwards. Inside the tunnel
192
+ * the bytes are the caller's, and the name the caller puts in its own TLS
193
+ * handshake or `Host` header is not visible to this process, so against a
194
+ * determined caller running inside the sandbox this path bounds the
195
+ * DESTINATION and nothing else. Say so plainly rather than let a reader
196
+ * infer that a tunnel to an allowlisted host carries only allowlisted
197
+ * traffic.
132
198
  */
133
199
  async handleConnect(req, socket, head) {
134
200
  const { host, port } = splitAuthority(req.url ?? '');
@@ -138,14 +204,29 @@ export class EgressProxy {
138
204
  socket.end();
139
205
  return;
140
206
  }
141
- const upstream = netConnect(port ?? 443, host, () => {
207
+ const literal = this.literalDenial(host);
208
+ if (literal) {
209
+ this.deny(host, `is a ${literal} address`);
210
+ socket.write(`HTTP/1.1 ${DENIED_STATUS} Forbidden\r\nContent-Type: text/plain\r\n\r\nEgress denied: ${host} is a ${literal} address, which is not reachable from a sandbox.\n`);
211
+ socket.end();
212
+ return;
213
+ }
214
+ // Same screened resolver as the plain path. The tunnel carries no
215
+ // brokered credential, so the loss here is reach rather than a token —
216
+ // but an allowlisted name pointing inward still turns this proxy into
217
+ // a route to the host's own network, which is what a sandbox is for.
218
+ const upstream = netConnect({ port: port ?? 443, host, lookup: this.lookup }, () => {
142
219
  socket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
143
220
  if (head.length > 0)
144
221
  upstream.write(head);
145
222
  upstream.pipe(socket);
146
223
  socket.pipe(upstream);
147
224
  });
148
- upstream.on('error', () => {
225
+ upstream.on('error', (err) => {
226
+ if (err instanceof EgressAddressDenied) {
227
+ this.deny(err.host, `resolves to a ${err.reason} address`);
228
+ socket.write(`HTTP/1.1 ${DENIED_STATUS} Forbidden\r\nContent-Type: text/plain\r\n\r\n${err.message}\n`);
229
+ }
149
230
  socket.end();
150
231
  });
151
232
  socket.on('error', () => {