@mycadet/cli 0.13.0-dev.202610201855.522ff0d → 0.13.0-dev.202610211419.6a397d7

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.
@@ -5,619 +5,386 @@ import path from "node:path";
5
5
 
6
6
  import {
7
7
  authorizePairedForwardHeaders,
8
- pairedGatewayTargetsFromLockfile,
9
- parsePairedGatewayUrl,
10
- readAllowedGatewayPorts,
11
- readPairedGatewayTargets,
8
+ gatewayTargetsFromLockfile,
9
+ parseAssistantGatewayUrl,
10
+ readGatewayTargets,
12
11
  resolveGatewayProxyTarget,
13
- resolvePairedGatewayProxyTarget,
14
12
  sanitizePairedForwardHeaders,
15
13
  } from "../gateway-proxy";
16
14
 
17
- const pairedRejection = {
15
+ const rejection = {
18
16
  kind: "reject",
19
17
  status: 403,
20
- message: "Assistant is not paired in lockfile",
18
+ message: "Assistant is not active in lockfile",
21
19
  } as const;
22
20
 
23
- const allow =
24
- (...ports: number[]) =>
25
- () =>
26
- new Set<number>(ports);
21
+ const loopback = (port: number) => ({ kind: "loopback", port }) as const;
22
+ const remote = (runtimeUrl: string) => ({ kind: "remote", runtimeUrl }) as const;
27
23
 
28
- describe("resolveGatewayProxyTarget", () => {
29
- test("passes non-gateway pathnames through untouched", () => {
30
- expect(resolveGatewayProxyTarget("/index.html", allow(8080))).toEqual({
31
- kind: "pass",
32
- });
33
- expect(resolveGatewayProxyTarget("/assistant/assets/app.js", allow())).toEqual({
34
- kind: "pass",
35
- });
36
- });
24
+ const targets =
25
+ (map: Record<string, { kind: "loopback"; port: number } | { kind: "remote"; runtimeUrl: string }>) =>
26
+ () =>
27
+ new Map(Object.entries(map));
37
28
 
38
- test("forwards an allowlisted port to its loopback target", () => {
29
+ describe("parseAssistantGatewayUrl", () => {
30
+ test("parses the unified id-keyed path", () => {
39
31
  expect(
40
- resolveGatewayProxyTarget("/__gateway/8080/v1/assistants", allow(8080)),
32
+ parseAssistantGatewayUrl("/__gateway/abc-123/v1/assistants"),
41
33
  ).toEqual({
42
- kind: "forward",
43
- target: { port: 8080, path: "/v1/assistants" },
34
+ match: true,
35
+ valid: true,
36
+ target: { assistantId: "abc-123", path: "/v1/assistants", search: "" },
44
37
  });
45
38
  });
46
39
 
47
40
  test("accepts the renderer's `/assistant` mount prefix", () => {
48
41
  expect(
49
- resolveGatewayProxyTarget("/assistant/__gateway/8080/auth/token", allow(8080)),
50
- ).toEqual({
51
- kind: "forward",
52
- target: { port: 8080, path: "/auth/token" },
53
- });
54
- });
55
-
56
- test("defaults a portless tail to the gateway root", () => {
57
- expect(resolveGatewayProxyTarget("/__gateway/8080", allow(8080))).toEqual({
58
- kind: "forward",
59
- target: { port: 8080, path: "/" },
60
- });
61
- });
62
-
63
- test("rejects ports outside the 1024–65535 range as invalid", () => {
64
- expect(resolveGatewayProxyTarget("/__gateway/80/v1", allow(80))).toEqual({
65
- kind: "invalid-port",
66
- });
67
- expect(resolveGatewayProxyTarget("/__gateway/70000/v1", allow(70000))).toEqual({
68
- kind: "invalid-port",
69
- });
70
- });
71
-
72
- test("forbids a well-formed port that isn't registered in the lockfile", () => {
73
- expect(
74
- resolveGatewayProxyTarget("/__gateway/9999/v1", allow(8080)),
75
- ).toEqual({ kind: "forbidden-port", port: 9999 });
76
- });
77
-
78
- test("forbids every gateway port when the allowlist is empty", () => {
79
- expect(resolveGatewayProxyTarget("/__gateway/8080/v1", allow())).toEqual({
80
- kind: "forbidden-port",
81
- port: 8080,
82
- });
83
- });
84
-
85
- test("allowlists ports from resources, loopback URLs, and docker runtimeUrls — never remote URLs", () => {
86
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "gateway-proxy-test-"));
87
- const lockfilePath = path.join(dir, "assistants.json");
88
- try {
89
- fs.writeFileSync(
90
- lockfilePath,
91
- JSON.stringify({
92
- assistants: [
93
- { assistantId: "local-a", resources: { gatewayPort: 7830 } },
94
- { assistantId: "local-b", localUrl: "http://127.0.0.1:7831" },
95
- // Docker entries record their published gateway only as a
96
- // loopback runtimeUrl.
97
- {
98
- assistantId: "docker-a",
99
- cloud: "docker",
100
- runtimeUrl: "http://localhost:7930",
101
- },
102
- // Remote runtimeUrls (managed / gcp / paired) must never widen
103
- // the allowlist.
104
- {
105
- assistantId: "remote-a",
106
- cloud: "gcp",
107
- runtimeUrl: "https://assistant.example.com:8443",
108
- },
109
- // A paired entry is skipped wholesale: even a loopback runtimeUrl
110
- // (rejected on import, but possibly pre-existing) must not open
111
- // the loopback proxy to arbitrary local services.
112
- {
113
- assistantId: "paired-loopback",
114
- cloud: "paired",
115
- paired: true,
116
- runtimeUrl: "http://127.0.0.1:5432",
117
- },
118
- ],
119
- }),
120
- );
121
- expect(readAllowedGatewayPorts([lockfilePath])).toEqual(
122
- new Set([7830, 7831, 7930]),
123
- );
124
- } finally {
125
- fs.rmSync(dir, { recursive: true, force: true });
126
- }
127
- });
128
-
129
- test("never reads the allowlist for non-gateway or invalid-port paths", () => {
130
- let reads = 0;
131
- const counting = () => {
132
- reads += 1;
133
- return new Set<number>([8080]);
134
- };
135
- resolveGatewayProxyTarget("/index.html", counting);
136
- resolveGatewayProxyTarget("/__gateway/80/v1", counting);
137
- expect(reads).toBe(0);
138
- resolveGatewayProxyTarget("/__gateway/8080/v1", counting);
139
- expect(reads).toBe(1);
140
- });
141
- });
142
-
143
- const pair =
144
- (entries: Record<string, string> = {}) =>
145
- () =>
146
- new Map<string, string>(Object.entries(entries));
147
-
148
- describe("parsePairedGatewayUrl", () => {
149
- test("matches with and without the renderer's `/assistant` mount prefix", () => {
150
- expect(parsePairedGatewayUrl("/__gateway-paired/abc/v1/foo")).toEqual({
151
- match: true,
152
- valid: true,
153
- target: { assistantId: "abc", path: "/v1/foo", search: "" },
154
- });
155
- expect(
156
- parsePairedGatewayUrl("/assistant/__gateway-paired/abc/v1/foo"),
42
+ parseAssistantGatewayUrl("/assistant/__gateway/abc-123/auth/token"),
157
43
  ).toEqual({
158
44
  match: true,
159
45
  valid: true,
160
- target: { assistantId: "abc", path: "/v1/foo", search: "" },
46
+ target: { assistantId: "abc-123", path: "/auth/token", search: "" },
161
47
  });
162
48
  });
163
49
 
164
- test("defaults a pathless tail to the gateway root", () => {
165
- expect(parsePairedGatewayUrl("/__gateway-paired/abc")).toEqual({
50
+ test("accepts the deprecated `-paired` alias with identical semantics", () => {
51
+ expect(
52
+ parseAssistantGatewayUrl("/assistant/__gateway-paired/abc-123/v1"),
53
+ ).toEqual(
54
+ parseAssistantGatewayUrl("/assistant/__gateway/abc-123/v1"),
55
+ );
56
+ expect(parseAssistantGatewayUrl("/__gateway-paired/abc-123")).toEqual({
166
57
  match: true,
167
58
  valid: true,
168
- target: { assistantId: "abc", path: "/", search: "" },
59
+ target: { assistantId: "abc-123", path: "/", search: "" },
169
60
  });
170
61
  });
171
62
 
172
- test("splits the query off the id instead of swallowing it", () => {
173
- expect(parsePairedGatewayUrl("/__gateway-paired/abc?x=1")).toEqual({
63
+ test("preserves the query and drops the fragment", () => {
64
+ expect(
65
+ parseAssistantGatewayUrl("/__gateway/abc-123/v1/messages?x=1#frag"),
66
+ ).toEqual({
174
67
  match: true,
175
68
  valid: true,
176
- target: { assistantId: "abc", path: "/", search: "?x=1" },
69
+ target: {
70
+ assistantId: "abc-123",
71
+ path: "/v1/messages",
72
+ search: "?x=1",
73
+ },
177
74
  });
178
75
  });
179
76
 
180
- test("carries the query through on a pathful tail and drops a fragment", () => {
181
- expect(parsePairedGatewayUrl("/__gateway-paired/abc/v1/foo?x=1#frag")).toEqual({
182
- match: true,
183
- valid: true,
184
- target: { assistantId: "abc", path: "/v1/foo", search: "?x=1" },
185
- });
186
- expect(parsePairedGatewayUrl("/__gateway-paired/abc/v1#frag")).toEqual({
77
+ test("decodes a percent-encoded id", () => {
78
+ expect(parseAssistantGatewayUrl("/__gateway/a%20b/v1")).toEqual({
187
79
  match: true,
188
80
  valid: true,
189
- target: { assistantId: "abc", path: "/v1", search: "" },
81
+ target: { assistantId: "a b", path: "/v1", search: "" },
190
82
  });
191
83
  });
192
84
 
193
- test("percent-decodes the assistant id", () => {
194
- expect(parsePairedGatewayUrl("/__gateway-paired/a%20b/v1")).toEqual({
85
+ test("rejects dot-segment tails (raw and percent-encoded)", () => {
86
+ expect(parseAssistantGatewayUrl("/__gateway/abc/../secret")).toMatchObject({
195
87
  match: true,
196
- valid: true,
197
- target: { assistantId: "a b", path: "/v1", search: "" },
88
+ valid: false,
198
89
  });
90
+ expect(
91
+ parseAssistantGatewayUrl("/__gateway/abc/%2e%2e/secret"),
92
+ ).toMatchObject({ match: true, valid: false });
199
93
  });
200
94
 
201
- test("treats malformed percent-encoding as invalid, not a crash", () => {
202
- expect(parsePairedGatewayUrl("/__gateway-paired/%zz/v1")).toEqual({
95
+ test("rejects malformed percent-encoding as an invalid parse", () => {
96
+ expect(parseAssistantGatewayUrl("/__gateway/%zz/v1")).toEqual({
203
97
  match: true,
204
98
  valid: false,
205
99
  });
206
100
  });
207
101
 
208
- test("rejects dot segments in the tail, raw or percent-encoded", () => {
209
- for (const url of [
210
- "/__gateway-paired/abc/../secrets",
211
- "/__gateway-paired/abc/v1/../../secrets",
212
- "/__gateway-paired/abc/./v1",
213
- "/__gateway-paired/abc/%2e%2e/secrets",
214
- "/__gateway-paired/abc/%2E%2E/secrets",
215
- "/__gateway-paired/abc/%2e/v1",
216
- "/__gateway-paired/abc/v1/%zz",
217
- ]) {
218
- expect(parsePairedGatewayUrl(url)).toEqual({
219
- match: true,
220
- valid: false,
221
- });
222
- }
223
- });
224
-
225
- test("does not treat dot-containing filenames as traversal", () => {
226
- expect(parsePairedGatewayUrl("/__gateway-paired/abc/v1/file.json")).toEqual(
227
- {
228
- match: true,
229
- valid: true,
230
- target: { assistantId: "abc", path: "/v1/file.json", search: "" },
231
- },
102
+ test("does not match unrelated paths", () => {
103
+ expect(parseAssistantGatewayUrl("/index.html").match).toBe(false);
104
+ expect(parseAssistantGatewayUrl("/assistant/assets/app.js").match).toBe(
105
+ false,
232
106
  );
233
107
  });
108
+ });
234
109
 
235
- test("does not match non-paired pathnames", () => {
236
- expect(parsePairedGatewayUrl("/__gateway/8080/v1")).toEqual({
237
- match: false,
238
- });
239
- expect(parsePairedGatewayUrl("/index.html")).toEqual({ match: false });
110
+ describe("resolveGatewayProxyTarget", () => {
111
+ const entry = targets({
112
+ "abc-123": loopback(8080),
113
+ "def-456": remote("https://gw.example.com"),
240
114
  });
241
- });
242
115
 
243
- describe("resolvePairedGatewayProxyTarget", () => {
244
- test("passes non-paired pathnames through untouched", () => {
245
- expect(
246
- resolvePairedGatewayProxyTarget("/index.html", pair({ abc: "https://gw.example.com" })),
247
- ).toEqual({ kind: "pass" });
116
+ test("passes non-gateway paths through untouched", () => {
117
+ expect(resolveGatewayProxyTarget("/index.html", entry)).toEqual({
118
+ kind: "pass",
119
+ });
248
120
  expect(
249
- resolvePairedGatewayProxyTarget("/__gateway/8080/v1", pair()),
121
+ resolveGatewayProxyTarget("/assistant/assets/app.js", targets({})),
250
122
  ).toEqual({ kind: "pass" });
251
123
  });
252
124
 
253
- test("forwards a paired id to its runtimeUrl, preserving the query", () => {
125
+ test("forwards an active loopback id to its port target", () => {
254
126
  expect(
255
- resolvePairedGatewayProxyTarget(
256
- "/assistant/__gateway-paired/abc/v1/foo?x=1",
257
- pair({ abc: "https://gw.example.com" }),
258
- ),
127
+ resolveGatewayProxyTarget("/__gateway/abc-123/v1/assistants", entry),
259
128
  ).toEqual({
260
129
  kind: "forward",
261
- url: "https://gw.example.com/v1/foo?x=1",
262
- runtimeUrl: "https://gw.example.com",
263
- assistantId: "abc",
130
+ assistantId: "abc-123",
131
+ path: "/v1/assistants",
132
+ search: "",
133
+ target: loopback(8080),
264
134
  });
265
135
  });
266
136
 
267
- test("forwards a query on a pathless tail instead of treating it as part of the id", () => {
137
+ test("forwards an imported pairing to its recorded runtimeUrl", () => {
268
138
  expect(
269
- resolvePairedGatewayProxyTarget(
270
- "/__gateway-paired/abc?x=1",
271
- pair({ abc: "https://gw.example.com" }),
272
- ),
139
+ resolveGatewayProxyTarget("/__gateway/def-456/v1/messages?x=1", entry),
273
140
  ).toEqual({
274
141
  kind: "forward",
275
- url: "https://gw.example.com/?x=1",
276
- runtimeUrl: "https://gw.example.com",
277
- assistantId: "abc",
142
+ assistantId: "def-456",
143
+ path: "/v1/messages",
144
+ search: "?x=1",
145
+ target: remote("https://gw.example.com"),
278
146
  });
279
147
  });
280
148
 
281
- test("rejects a dot-segment traversal tail", () => {
282
- expect(
283
- resolvePairedGatewayProxyTarget(
284
- "/__gateway-paired/abc/../../secrets",
285
- pair({ abc: "https://gw.example.com/edge" }),
286
- ),
287
- ).toEqual(pairedRejection);
288
- expect(
289
- resolvePairedGatewayProxyTarget(
290
- "/__gateway-paired/abc/%2e%2e/secrets",
291
- pair({ abc: "https://gw.example.com/edge" }),
292
- ),
293
- ).toEqual(pairedRejection);
149
+ test("rejects an id that is not active in the lockfile", () => {
150
+ expect(resolveGatewayProxyTarget("/__gateway/zzz-999/v1", entry)).toEqual(
151
+ rejection,
152
+ );
153
+ expect(resolveGatewayProxyTarget("/__gateway/abc/../secret", entry)).toEqual(
154
+ rejection,
155
+ );
294
156
  });
157
+ });
295
158
 
296
- test("strips the runtimeUrl's trailing slashes and keeps its path prefix", () => {
297
- expect(
298
- resolvePairedGatewayProxyTarget(
299
- "/__gateway-paired/abc/v1/foo",
300
- pair({ abc: "https://gw.example.com/" }),
301
- ),
302
- ).toEqual({
303
- kind: "forward",
304
- url: "https://gw.example.com/v1/foo",
305
- runtimeUrl: "https://gw.example.com/",
306
- assistantId: "abc",
307
- });
159
+ describe("gatewayTargetsFromLockfile", () => {
160
+ test("resolves a paired entry with a usable runtimeUrl to a remote target", () => {
308
161
  expect(
309
- resolvePairedGatewayProxyTarget(
310
- "/__gateway-paired/abc/v1/foo",
311
- pair({ abc: "https://gw.example.com/edge/" }),
312
- ),
313
- ).toEqual({
314
- kind: "forward",
315
- url: "https://gw.example.com/edge/v1/foo",
316
- runtimeUrl: "https://gw.example.com/edge/",
317
- assistantId: "abc",
318
- });
162
+ gatewayTargetsFromLockfile({
163
+ assistants: [
164
+ { assistantId: "abc-123", cloud: "paired", runtimeUrl: "https://gw.example.com" },
165
+ ],
166
+ }),
167
+ ).toEqual(
168
+ targets({ "abc-123": remote("https://gw.example.com") })(),
169
+ );
319
170
  });
320
171
 
321
- test("resolves a percent-encoded id against the decoded allowlist key", () => {
172
+ test("omits a paired entry whose runtimeUrl is unusable", () => {
322
173
  expect(
323
- resolvePairedGatewayProxyTarget(
324
- "/__gateway-paired/a%20b/v1",
325
- pair({ "a b": "https://gw.example.com" }),
326
- ),
327
- ).toEqual({
328
- kind: "forward",
329
- url: "https://gw.example.com/v1",
330
- runtimeUrl: "https://gw.example.com",
331
- assistantId: "a b",
332
- });
174
+ gatewayTargetsFromLockfile({
175
+ assistants: [
176
+ { assistantId: "abc-123", cloud: "paired", runtimeUrl: "not a url" },
177
+ { assistantId: "def-456", cloud: "paired" },
178
+ ],
179
+ }),
180
+ ).toEqual(new Map());
333
181
  });
334
182
 
335
- test("rejects an id that isn't paired in the lockfile", () => {
183
+ test("resolves a local entry's recorded gatewayPort to a loopback target", () => {
336
184
  expect(
337
- resolvePairedGatewayProxyTarget(
338
- "/__gateway-paired/unknown/v1",
339
- pair({ abc: "https://gw.example.com" }),
340
- ),
341
- ).toEqual(pairedRejection);
185
+ gatewayTargetsFromLockfile({
186
+ assistants: [
187
+ {
188
+ assistantId: "abc-123",
189
+ cloud: "local",
190
+ resources: { gatewayPort: 8080 },
191
+ },
192
+ ],
193
+ }),
194
+ ).toEqual(targets({ "abc-123": loopback(8080) })());
342
195
  });
343
196
 
344
- test("rejects a malformed percent-encoded id", () => {
197
+ test("resolves a docker entry's loopback runtimeUrl (no resources block)", () => {
345
198
  expect(
346
- resolvePairedGatewayProxyTarget(
347
- "/__gateway-paired/%zz/v1",
348
- pair({ "%zz": "https://gw.example.com" }),
349
- ),
350
- ).toEqual(pairedRejection);
351
- });
352
-
353
- test("never reads the allowlist for non-paired paths", () => {
354
- let reads = 0;
355
- const counting = () => {
356
- reads += 1;
357
- return new Map<string, string>([["abc", "https://gw.example.com"]]);
358
- };
359
- resolvePairedGatewayProxyTarget("/index.html", counting);
360
- resolvePairedGatewayProxyTarget("/__gateway/8080/v1", counting);
361
- expect(reads).toBe(0);
362
- resolvePairedGatewayProxyTarget("/__gateway-paired/abc/v1", counting);
363
- expect(reads).toBe(1);
199
+ gatewayTargetsFromLockfile({
200
+ assistants: [
201
+ {
202
+ assistantId: "abc-123",
203
+ cloud: "docker",
204
+ runtimeUrl: "http://127.0.0.1:9000",
205
+ },
206
+ ],
207
+ }),
208
+ ).toEqual(new Map([["abc-123", loopback(9000)]]));
364
209
  });
365
- });
366
210
 
367
- describe("pairedGatewayTargetsFromLockfile", () => {
368
- test("maps paired entries with usable runtimeUrls, excluding everything else", () => {
211
+ test("omits a local entry whose runtimeUrl is remote", () => {
369
212
  expect(
370
- pairedGatewayTargetsFromLockfile({
213
+ gatewayTargetsFromLockfile({
371
214
  assistants: [
372
215
  {
373
- assistantId: "paired-a",
374
- cloud: "paired",
375
- runtimeUrl: "https://gw.example.com",
216
+ assistantId: "abc-123",
217
+ cloud: "local",
218
+ runtimeUrl: "https://elsewhere.example.com",
376
219
  },
220
+ ],
221
+ }),
222
+ ).toEqual(new Map());
223
+ });
224
+
225
+ test("omits ports outside the 1024-65535 proxy range", () => {
226
+ expect(
227
+ gatewayTargetsFromLockfile({
228
+ assistants: [
377
229
  {
378
- assistantId: "paired-b",
379
- cloud: "paired",
380
- runtimeUrl: "http://192.0.2.10:8443/edge",
230
+ assistantId: "abc-123",
231
+ cloud: "local",
232
+ resources: { gatewayPort: 80 },
381
233
  },
382
- // Non-paired entries never become forwardable.
383
- { assistantId: "local-a", cloud: "local" },
384
234
  {
385
- assistantId: "docker-a",
235
+ assistantId: "def-456",
386
236
  cloud: "docker",
387
- runtimeUrl: "http://localhost:7930",
388
- },
389
- {
390
- assistantId: "remote-a",
391
- cloud: "gcp",
392
- runtimeUrl: "https://assistant.example.com:8443",
237
+ runtimeUrl: "http://127.0.0.1:70000",
393
238
  },
394
- // Paired entries without a usable absolute http(s) runtimeUrl are
395
- // excluded rather than forwarded blind.
396
- { assistantId: "paired-no-url", cloud: "paired" },
239
+ ],
240
+ }),
241
+ ).toEqual(new Map());
242
+ });
243
+
244
+ test("omits clouds the SPA does not drive over this proxy", () => {
245
+ expect(
246
+ gatewayTargetsFromLockfile({
247
+ assistants: [
248
+ { assistantId: "a", cloud: "cadet" },
249
+ { assistantId: "b", cloud: "apple-container" },
250
+ { assistantId: "c", cloud: "gcp", runtimeUrl: "https://gw.example.com" },
251
+ { assistantId: "d", cloud: "aws", runtimeUrl: "https://gw.example.com" },
252
+ { assistantId: "e", cloud: "custom", runtimeUrl: "https://gw.example.com" },
253
+ ],
254
+ }),
255
+ ).toEqual(new Map());
256
+ });
257
+
258
+ test("tolerates malformed entries", () => {
259
+ expect(
260
+ gatewayTargetsFromLockfile({
261
+ assistants: [
262
+ null,
263
+ 42,
264
+ { noId: true },
265
+ { assistantId: "", cloud: "paired" },
397
266
  {
398
- assistantId: "paired-bad-scheme",
399
- cloud: "paired",
400
- runtimeUrl: "ssh://gw.example.com",
267
+ assistantId: "abc-123",
268
+ cloud: "local",
269
+ resources: { gatewayPort: 8080 },
401
270
  },
271
+ ],
272
+ }),
273
+ ).toEqual(targets({ "abc-123": loopback(8080) })());
274
+ });
275
+ });
276
+
277
+ describe("readGatewayTargets", () => {
278
+ test("reads the id-keyed target map from disk", () => {
279
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "gateway-proxy-"));
280
+ const lockfile = path.join(dir, "lockfile.json");
281
+ fs.writeFileSync(
282
+ lockfile,
283
+ JSON.stringify({
284
+ assistants: [
402
285
  {
403
- assistantId: "paired-relative",
404
- cloud: "paired",
405
- runtimeUrl: "/not-absolute",
286
+ assistantId: "abc-123",
287
+ cloud: "local",
288
+ resources: { gatewayPort: 8080 },
406
289
  },
407
- // Malformed entries are tolerated, not fatal.
408
- null,
409
- "not-an-object",
410
- { cloud: "paired", runtimeUrl: "https://gw.example.com" },
411
290
  {
412
- assistantId: "",
291
+ assistantId: "def-456",
413
292
  cloud: "paired",
414
293
  runtimeUrl: "https://gw.example.com",
415
294
  },
416
295
  ],
417
296
  }),
418
- ).toEqual(
419
- new Map([
420
- ["paired-a", "https://gw.example.com"],
421
- ["paired-b", "http://192.0.2.10:8443/edge"],
422
- ]),
423
297
  );
424
- });
425
-
426
- test("returns an empty map for a lockfile with no assistants", () => {
427
- expect(pairedGatewayTargetsFromLockfile({ assistants: [] })).toEqual(
428
- new Map(),
298
+ expect(readGatewayTargets([lockfile])).toEqual(
299
+ targets({
300
+ "abc-123": loopback(8080),
301
+ "def-456": remote("https://gw.example.com"),
302
+ })(),
429
303
  );
304
+ fs.rmSync(dir, { recursive: true, force: true });
430
305
  });
431
- });
432
-
433
- describe("readPairedGatewayTargets", () => {
434
- const withLockfile = (
435
- contents: string,
436
- run: (lockfilePath: string) => void,
437
- ) => {
438
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "paired-gateway-test-"));
439
- const lockfilePath = path.join(dir, "assistants.json");
440
- try {
441
- fs.writeFileSync(lockfilePath, contents);
442
- run(lockfilePath);
443
- } finally {
444
- fs.rmSync(dir, { recursive: true, force: true });
445
- }
446
- };
447
306
 
448
- test("maps paired entries to their http(s) runtimeUrls, excluding everything else", () => {
449
- withLockfile(
307
+ test("the first readable lockfile is authoritative even when it yields no targets", () => {
308
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "gateway-proxy-"));
309
+ const first = path.join(dir, "first.json");
310
+ const second = path.join(dir, "second.json");
311
+ fs.writeFileSync(first, JSON.stringify({ assistants: [] }));
312
+ fs.writeFileSync(
313
+ second,
450
314
  JSON.stringify({
451
315
  assistants: [
452
316
  {
453
- assistantId: "paired-a",
454
- cloud: "paired",
455
- runtimeUrl: "https://gw.example.com",
317
+ assistantId: "abc-123",
318
+ cloud: "local",
319
+ resources: { gatewayPort: 8080 },
456
320
  },
457
- {
458
- assistantId: "paired-b",
459
- cloud: "paired",
460
- runtimeUrl: "http://192.0.2.10:8443/edge",
461
- },
462
- // Non-paired entries never become forwardable, loopback or not.
463
- { assistantId: "local-a", resources: { gatewayPort: 7830 } },
464
- {
465
- assistantId: "docker-a",
466
- cloud: "docker",
467
- runtimeUrl: "http://localhost:7930",
468
- },
469
- {
470
- assistantId: "remote-a",
471
- cloud: "gcp",
472
- runtimeUrl: "https://assistant.example.com:8443",
473
- },
474
- // Paired entries without a usable absolute http(s) runtimeUrl are
475
- // excluded rather than forwarded blind.
476
- { assistantId: "paired-no-url", cloud: "paired" },
477
- {
478
- assistantId: "paired-bad-scheme",
479
- cloud: "paired",
480
- runtimeUrl: "ssh://gw.example.com",
481
- },
482
- {
483
- assistantId: "paired-relative",
484
- cloud: "paired",
485
- runtimeUrl: "/not-absolute",
486
- },
487
- // Malformed entries are tolerated, not fatal.
488
- null,
489
- { cloud: "paired", runtimeUrl: "https://gw.example.com" },
490
321
  ],
491
322
  }),
492
- (lockfilePath) => {
493
- expect(readPairedGatewayTargets([lockfilePath])).toEqual(
494
- new Map([
495
- ["paired-a", "https://gw.example.com"],
496
- ["paired-b", "http://192.0.2.10:8443/edge"],
497
- ]),
498
- );
499
- },
500
323
  );
324
+ expect(readGatewayTargets([first, second])).toEqual(new Map());
325
+ fs.rmSync(dir, { recursive: true, force: true });
501
326
  });
502
327
 
503
- test("returns an empty map for malformed JSON", () => {
504
- withLockfile("not json", (lockfilePath) => {
505
- expect(readPairedGatewayTargets([lockfilePath])).toEqual(new Map());
506
- });
507
- });
508
-
509
- test("returns an empty map when no lockfile path exists", () => {
510
- expect(
511
- readPairedGatewayTargets(["/nonexistent/assistants.json"]),
512
- ).toEqual(new Map());
513
- });
514
-
515
- test("stops at the first parseable lockfile even when it yields no targets", () => {
516
- // The unpair write path targets the first lockfile path
517
- // (writeRawLockfile), so the allowlist must not fall through to a stale
518
- // legacy file just because the written file has no paired entries left.
519
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "paired-gateway-test-"));
520
- const primary = path.join(dir, "assistants.json");
521
- const legacy = path.join(dir, "legacy-assistants.json");
522
- try {
523
- fs.writeFileSync(primary, JSON.stringify({ assistants: [] }));
524
- fs.writeFileSync(
525
- legacy,
526
- JSON.stringify({
527
- assistants: [
528
- {
529
- assistantId: "paired-a",
530
- cloud: "paired",
531
- runtimeUrl: "https://gw.example.com",
532
- },
533
- ],
534
- }),
535
- );
536
- expect(readPairedGatewayTargets([primary, legacy])).toEqual(new Map());
537
- // A missing primary still falls through to the legacy file.
538
- fs.rmSync(primary);
539
- expect(readPairedGatewayTargets([primary, legacy])).toEqual(
540
- new Map([["paired-a", "https://gw.example.com"]]),
541
- );
542
- } finally {
543
- fs.rmSync(dir, { recursive: true, force: true });
544
- }
328
+ test("unreadable and missing candidates are skipped, absent files yield an empty map", () => {
329
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "gateway-proxy-"));
330
+ const unreadable = path.join(dir, "unreadable.json");
331
+ const good = path.join(dir, "good.json");
332
+ fs.writeFileSync(unreadable, "{ not json");
333
+ fs.writeFileSync(
334
+ good,
335
+ JSON.stringify({
336
+ assistants: [
337
+ {
338
+ assistantId: "abc-123",
339
+ cloud: "local",
340
+ resources: { gatewayPort: 8080 },
341
+ },
342
+ ],
343
+ }),
344
+ );
345
+ expect(readGatewayTargets([unreadable, good])).toEqual(
346
+ targets({ "abc-123": loopback(8080) })(),
347
+ );
348
+ expect(readGatewayTargets([path.join(dir, "absent.json")])).toEqual(
349
+ new Map(),
350
+ );
351
+ fs.rmSync(dir, { recursive: true, force: true });
545
352
  });
546
353
  });
547
354
 
548
- describe("sanitizePairedForwardHeaders", () => {
549
- test("strips renderer authorization and browser-ambient headers", () => {
355
+ describe("remote-forward header handling", () => {
356
+ test("sanitizePairedForwardHeaders strips renderer and browser-ambient headers", () => {
550
357
  const headers = new Headers({
551
- origin: "http://localhost:5173",
552
- referer: "http://localhost:5173/assistant",
553
- cookie: "sessionid=abc",
358
+ authorization: "Bearer renderer",
359
+ cookie: "session=1",
360
+ origin: "https://app.example.com",
361
+ referer: "https://app.example.com/x",
554
362
  "sec-fetch-site": "same-origin",
555
- "sec-fetch-mode": "cors",
556
- "sec-fetch-dest": "empty",
557
- authorization: "Bearer guardian-token",
558
- accept: "text/event-stream",
559
363
  "content-type": "application/json",
560
364
  });
561
365
  sanitizePairedForwardHeaders(headers);
562
- expect(headers.has("origin")).toBe(false);
563
- expect(headers.has("referer")).toBe(false);
564
- expect(headers.has("cookie")).toBe(false);
565
- expect(headers.has("sec-fetch-site")).toBe(false);
566
- expect(headers.has("sec-fetch-mode")).toBe(false);
567
- expect(headers.has("sec-fetch-dest")).toBe(false);
568
- expect(headers.has("authorization")).toBe(false);
569
- expect(headers.get("accept")).toBe("text/event-stream");
366
+ expect(headers.get("authorization")).toBeNull();
367
+ expect(headers.get("cookie")).toBeNull();
368
+ expect(headers.get("origin")).toBeNull();
369
+ expect(headers.get("referer")).toBeNull();
370
+ expect(headers.get("sec-fetch-site")).toBeNull();
570
371
  expect(headers.get("content-type")).toBe("application/json");
571
- });
572
-
573
- test("adds the ngrok interstitial bypass header", () => {
574
- const headers = new Headers({ authorization: "Bearer guardian-token" });
575
- sanitizePairedForwardHeaders(headers);
576
372
  expect(headers.get("ngrok-skip-browser-warning")).toBe("true");
577
373
  });
578
- });
579
-
580
- describe("authorizePairedForwardHeaders", () => {
581
- test("sanitizes before installing the host-owned bearer", async () => {
582
- const headers = new Headers({
583
- authorization: "Bearer renderer-token",
584
- cookie: "renderer-cookie=1",
585
- accept: "application/json",
586
- });
587
374
 
375
+ test("authorizePairedForwardHeaders installs the host's guardian bearer", async () => {
376
+ const headers = new Headers({ authorization: "Bearer renderer" });
588
377
  const result = await authorizePairedForwardHeaders(
589
- "paired-a",
378
+ "abc-123",
590
379
  "https://gw.example.com",
591
380
  headers,
592
381
  async (assistantId, runtimeUrl) => {
593
- expect(assistantId).toBe("paired-a");
382
+ expect(assistantId).toBe("abc-123");
594
383
  expect(runtimeUrl).toBe("https://gw.example.com");
595
- expect(headers.has("authorization")).toBe(false);
596
- expect(headers.has("cookie")).toBe(false);
597
384
  return { ok: true, accessToken: "host-token" };
598
385
  },
599
386
  );
600
-
601
387
  expect(result).toEqual({ ok: true });
602
388
  expect(headers.get("authorization")).toBe("Bearer host-token");
603
- expect(headers.get("accept")).toBe("application/json");
604
- });
605
-
606
- test("leaves renderer authorization stripped when token loading fails", async () => {
607
- const headers = new Headers({ authorization: "Bearer renderer-token" });
608
-
609
- const result = await authorizePairedForwardHeaders(
610
- "paired-a",
611
- "https://gw.example.com",
612
- headers,
613
- async () => ({ ok: false, status: 404, error: "Token not found" }),
614
- );
615
-
616
- expect(result).toEqual({
617
- ok: false,
618
- status: 404,
619
- error: "Token not found",
620
- });
621
- expect(headers.has("authorization")).toBe(false);
622
389
  });
623
390
  });