@capybara-hq/plugin-sdk 0.1.0-rc.89 → 0.1.0-rc.90

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.
@@ -1083,8 +1083,16 @@ export interface DeviceAccessCtx {
1083
1083
  done: boolean;
1084
1084
  }): Promise<void>;
1085
1085
  ensureService(call: ToolCallContext, spec: DeviceServiceSpec): Promise<DeviceServiceStatus>;
1086
+ grantLoopbackPort(scope: DeviceAccessScope & {
1087
+ service: string;
1088
+ port: number;
1089
+ }): Promise<LoopbackGrantView>;
1090
+ loopbackPorts(scope: DeviceAccessScope & {
1091
+ service: string;
1092
+ }): Promise<LoopbackGrantView[]>;
1086
1093
  reportBlockedHosts(call: ToolCallContext, hosts: ServiceBlockedHost[], opts?: {
1087
1094
  url?: string;
1095
+ service?: string;
1088
1096
  }): void;
1089
1097
  request(call: ToolCallContext, req: {
1090
1098
  service: string;
@@ -1092,6 +1100,10 @@ export interface DeviceAccessCtx {
1092
1100
  timeoutMs: number;
1093
1101
  ifRunning?: boolean;
1094
1102
  }): Promise<Uint8Array>;
1103
+ revokeLoopbackPort(scope: DeviceAccessScope & {
1104
+ service: string;
1105
+ port: number;
1106
+ }): Promise<void>;
1095
1107
  streamTicket(scope: DeviceAccessScope & {
1096
1108
  service: string;
1097
1109
  }): Promise<{
@@ -1111,7 +1123,18 @@ export class DeviceAccessError extends Error {
1111
1123
  // @public
1112
1124
  export type DeviceAccessErrorCode = "no_device" | "provider_unavailable" | "capability_missing" | "not_owner" | "service_not_declared"
1113
1125
  /** An `ifRunning` request found the device's instance or the service not running. */
1114
- | "not_running";
1126
+ | "not_running"
1127
+ /** A loopback port call from an extension without `permissions.deviceAccess.loopbackPorts`. */
1128
+ | "loopback_not_declared"
1129
+ /**
1130
+ * A loopback port cannot be granted here: running outside the sandbox is off (platform, org,
1131
+ * device or owner setting), the run is unattended, or the provider has no loopback support.
1132
+ */
1133
+ | "loopback_unavailable"
1134
+ /** The service already holds `LOOPBACK_MAX_PORTS_PER_SERVICE` live loopback port grants. */
1135
+ | "loopback_limit"
1136
+ /** A malformed argument (a loopback port that is not an integer from 1 to 65535). */
1137
+ | "invalid_request";
1115
1138
 
1116
1139
  // @public
1117
1140
  export type DeviceAccessScope = {
@@ -1159,6 +1182,7 @@ export interface DeviceProvider {
1159
1182
  readonly dialIn?: DialInHandler;
1160
1183
  ensureService?(h: SandboxHandle, spec: DeviceServiceSpec, signal: AbortSignal, opts?: {
1161
1184
  ifRunning?: boolean;
1185
+ loopbackPorts?: readonly number[];
1162
1186
  }): Promise<DeviceServiceStatus>;
1163
1187
  exec(h: SandboxHandle, args: {
1164
1188
  command: string;
@@ -1267,6 +1291,13 @@ export function deviceServiceFilesDigest(files: Record<string, Uint8Array>): str
1267
1291
  // @public
1268
1292
  export function deviceServiceId(extension: string, name: string): string;
1269
1293
 
1294
+ // @public
1295
+ export interface DeviceServiceNetwork {
1296
+ hosts: string[];
1297
+ kind: "open-public" | "allowlist";
1298
+ loopbackPorts?: number[];
1299
+ }
1300
+
1270
1301
  // @public
1271
1302
  export interface DeviceServiceRollCheck {
1272
1303
  busy: boolean;
@@ -1946,6 +1977,7 @@ export interface ExtensionPermissions {
1946
1977
  };
1947
1978
  deviceAccess?: {
1948
1979
  services: string[];
1980
+ loopbackPorts?: boolean;
1949
1981
  };
1950
1982
  dialIn?: {
1951
1983
  protocol: string;
@@ -2612,6 +2644,9 @@ export function isFileSystemGoneError(e: unknown): e is FileSystemGoneError;
2612
2644
  // @public
2613
2645
  export function isIdentitySourceRefused(err: unknown): boolean;
2614
2646
 
2647
+ // @public
2648
+ export function isLoopbackGrantHost(host: string): boolean;
2649
+
2615
2650
  // @public
2616
2651
  export function isModelError(e: unknown): e is ModelError;
2617
2652
 
@@ -2723,6 +2758,26 @@ export interface Logger {
2723
2758
  warn(msg: string, fields?: Record<string, unknown>): void;
2724
2759
  }
2725
2760
 
2761
+ // @public
2762
+ export const LOOPBACK_DATABASE_PORTS: readonly number[];
2763
+
2764
+ // @public
2765
+ export const LOOPBACK_GRANT_HOSTS: readonly ["127.0.0.1", "::1", "localhost"];
2766
+
2767
+ // @public
2768
+ export const LOOPBACK_MAX_PORTS_PER_SERVICE = 32;
2769
+
2770
+ // @public
2771
+ export type LoopbackGrantSource = "human" | "review-model" | "full-access" | "owner-panel";
2772
+
2773
+ // @public
2774
+ export interface LoopbackGrantView {
2775
+ database: boolean;
2776
+ expiresAt: string;
2777
+ port: number;
2778
+ source: LoopbackGrantSource;
2779
+ }
2780
+
2726
2781
  // @public
2727
2782
  export const MARKETPLACE_ICON_RE: RegExp;
2728
2783
 
@@ -3428,6 +3483,9 @@ export function parseGitSource(git: unknown): GitSource | {
3428
3483
  errors: string[];
3429
3484
  };
3430
3485
 
3486
+ // @public
3487
+ export function parseLoopbackPorts(value: unknown, what: string): number[];
3488
+
3431
3489
  // @public
3432
3490
  export function parseMarketplaceManifest(json: unknown, opts?: MarketplaceParseOptions): MarketplaceManifest | {
3433
3491
  errors: string[];
@@ -4257,6 +4315,7 @@ export interface SandboxPermissionSupport {
4257
4315
  readonly openPublic: boolean;
4258
4316
  readonly allowlist: boolean;
4259
4317
  readonly perOperationHosts: boolean;
4318
+ readonly loopbackPorts?: boolean;
4260
4319
  };
4261
4320
  readonly outsideSandbox: boolean;
4262
4321
  readonly restricted: {
@@ -4338,6 +4397,7 @@ export type SandboxStopResult = {
4338
4397
 
4339
4398
  // @public
4340
4399
  export interface SandboxSupportLimitedBy {
4400
+ readonly loopbackPorts?: "device-update";
4341
4401
  readonly openPublic?: "owner";
4342
4402
  readonly outsideSandbox?: "owner";
4343
4403
  }
@@ -4362,7 +4422,7 @@ export function sanitizeThemeSvg(bytes: Uint8Array): {
4362
4422
  };
4363
4423
 
4364
4424
  // @public
4365
- export const SDK_VERSION = "0.1.0-rc.89";
4425
+ export const SDK_VERSION = "0.1.0-rc.90";
4366
4426
 
4367
4427
  // @public
4368
4428
  export function secret<T extends z.ZodTypeAny>(schema: T): T;
@@ -4375,8 +4435,10 @@ export const SERVICE_CONNECTION_KINDS: readonly ["request", "stream"];
4375
4435
 
4376
4436
  // @public
4377
4437
  export interface ServiceBlockedHost {
4438
+ class?: AddressClass;
4378
4439
  host: string;
4379
4440
  layer: NetworkBlockLayer;
4441
+ port?: number;
4380
4442
  }
4381
4443
 
4382
4444
  // @public
package/dist/ctx.d.ts CHANGED
@@ -220,9 +220,16 @@ export interface DeviceAccessCtx {
220
220
  * planning still applies (a host the device list cannot take is never asked about; an IP
221
221
  * literal is always `address`). A host reported twice keeps its most restrictive layer.
222
222
  * `opts.url` is the page the refusals came from, shown with the ask.
223
+ *
224
+ * A loopback refusal (`layer: "address"`, a loopback grant host with its `port`) of an extension
225
+ * that declares `permissions.deviceAccess.loopbackPorts` raises a loopback port ask instead,
226
+ * where the device's outside-sandbox ceilings allow one (a note otherwise). `opts.service` is the
227
+ * declared service the refusals came from; it is required for a loopback ask when the
228
+ * extension declares more than one service.
223
229
  */
224
230
  reportBlockedHosts(call: ToolCallContext, hosts: ServiceBlockedHost[], opts?: {
225
231
  url?: string;
232
+ service?: string;
226
233
  }): void;
227
234
  /**
228
235
  * A short-lived, single-use ticket for one viewer stream to the service (the person's panel
@@ -242,6 +249,50 @@ export interface DeviceAccessCtx {
242
249
  addNetworkHosts(scope: DeviceAccessScope & {
243
250
  hosts: string[];
244
251
  }): Promise<void>;
252
+ /**
253
+ * Grants the service one loopback port of the device (`LOOPBACK_GRANT_HOSTS` on `port`) for the
254
+ * scope conversation's tree: the grant is held by the conversation's root, so its sub-agents
255
+ * share it. For the person's own click in the extension's panel (needs
256
+ * `permissions.deviceAccess.loopbackPorts`, else `loopback_not_declared`; a port outside
257
+ * 1–65535 is `invalid_request`).
258
+ *
259
+ * What the kernel checks: `scope.accountId` owns the conversation, its root and the root's
260
+ * personal device; the call is not made inside a run's tool step; the tree is not unattended by
261
+ * the kernel's records (an automation's conversation, or an automation run active in it now);
262
+ * the device's runtime can run outside its sandbox now, and the outside-sandbox ceilings
263
+ * (platform, org, device and owner settings) and the provider's loopback support allow it. Any
264
+ * of these refuses with `loopback_unavailable`; more than `LOOPBACK_MAX_PORTS_PER_SERVICE` live
265
+ * ports refuse with `loopback_limit`. The kernel cannot verify that a person clicked: beyond
266
+ * these checks, the owner-panel path rests on the extension, governed by the blocking approval
267
+ * of `permissions.deviceAccess.loopbackPorts`.
268
+ *
269
+ * Recorded with source `owner-panel`; the grant lives 24 hours (or until revoked, the
270
+ * conversation is deleted or the outside-sandbox setting is switched off), and the running
271
+ * service is refreshed with it. Granting a live port again answers the existing grant.
272
+ */
273
+ grantLoopbackPort(scope: DeviceAccessScope & {
274
+ service: string;
275
+ port: number;
276
+ }): Promise<LoopbackGrantView>;
277
+ /**
278
+ * Revokes the scope conversation root's grant of `port` to the service (owner only, needs
279
+ * `permissions.deviceAccess.loopbackPorts`); succeeds when there is none. The running service is
280
+ * refreshed without the port.
281
+ */
282
+ revokeLoopbackPort(scope: DeviceAccessScope & {
283
+ service: string;
284
+ port: number;
285
+ }): Promise<void>;
286
+ /**
287
+ * The live loopback port grants of the service held by the scope conversation's root, sorted by
288
+ * port (owner only, needs `permissions.deviceAccess.loopbackPorts`). Grants live 24 hours from
289
+ * issue and follow the same outside-sandbox ceilings as {@link grantLoopbackPort}. Empty while
290
+ * the tree is unattended (an automation's conversation, or an automation's run with no person
291
+ * joined): a grant is never an unattended run's, so a per-tab check refuses that tab.
292
+ */
293
+ loopbackPorts(scope: DeviceAccessScope & {
294
+ service: string;
295
+ }): Promise<LoopbackGrantView[]>;
245
296
  /**
246
297
  * Answers the handoff question of this extension's tool call `callId` from the panel.
247
298
  * `handoffConversationId` is the conversation whose tool call raised the handoff (the extension
@@ -257,10 +308,36 @@ export interface DeviceAccessCtx {
257
308
  done: boolean;
258
309
  }): Promise<void>;
259
310
  }
311
+ /**
312
+ * Who granted a loopback port: the person deciding an ask (`human`), the review model
313
+ * (`review-model`), a Full access profile within its ceilings (`full-access`), or the owner's own
314
+ * click in the panel (`owner-panel`, {@link DeviceAccessCtx.grantLoopbackPort}).
315
+ */
316
+ export type LoopbackGrantSource = "human" | "review-model" | "full-access" | "owner-panel";
317
+ /** One live loopback port grant of a device service ({@link DeviceAccessCtx.loopbackPorts}). */
318
+ export interface LoopbackGrantView {
319
+ port: number;
320
+ source: LoopbackGrantSource;
321
+ /** ISO 8601 time the grant ends (24 hours after issue). */
322
+ expiresAt: string;
323
+ /** The port is a well-known database port (`LOOPBACK_DATABASE_PORTS`). */
324
+ database: boolean;
325
+ }
260
326
  /** Why a {@link DeviceAccessCtx} call was refused. */
261
327
  export type DeviceAccessErrorCode = "no_device" | "provider_unavailable" | "capability_missing" | "not_owner" | "service_not_declared"
262
328
  /** An `ifRunning` request found the device's instance or the service not running. */
263
- | "not_running";
329
+ | "not_running"
330
+ /** A loopback port call from an extension without `permissions.deviceAccess.loopbackPorts`. */
331
+ | "loopback_not_declared"
332
+ /**
333
+ * A loopback port cannot be granted here: running outside the sandbox is off (platform, org,
334
+ * device or owner setting), the run is unattended, or the provider has no loopback support.
335
+ */
336
+ | "loopback_unavailable"
337
+ /** The service already holds `LOOPBACK_MAX_PORTS_PER_SERVICE` live loopback port grants. */
338
+ | "loopback_limit"
339
+ /** A malformed argument (a loopback port that is not an integer from 1 to 65535). */
340
+ | "invalid_request";
264
341
  /** A {@link DeviceAccessCtx} call was refused; the message is actionable English for the model. */
265
342
  export declare class DeviceAccessError extends Error {
266
343
  readonly code: DeviceAccessErrorCode;
@@ -140,6 +140,12 @@ export interface SandboxPermissionSupport {
140
140
  readonly openPublic: boolean;
141
141
  readonly allowlist: boolean;
142
142
  readonly perOperationHosts: boolean;
143
+ /**
144
+ * Whether a device service can be granted exact loopback ports
145
+ * (`DeviceProvider.ensureService` `loopbackPorts`). Needs `outsideSandbox` on an operation
146
+ * boundary; absent means false (a provider built on an earlier SDK).
147
+ */
148
+ readonly loopbackPorts?: boolean;
143
149
  };
144
150
  readonly outsideSandbox: boolean;
145
151
  readonly background: boolean;
@@ -149,13 +155,16 @@ export interface SandboxPermissionSupport {
149
155
  /**
150
156
  * Who keeps a closed crossing of a {@link SandboxPermissionSupport} closed, when it is not the
151
157
  * provider itself: `owner` is the owner of the machine the device runs on (for example a local
152
- * connection's owner ceiling, set on that machine). Only a closed crossing may name one:
153
- * `outsideSandbox` with `outsideSandbox: false`, `openPublic` with `network.openPublic: false`.
158
+ * connection's owner ceiling, set on that machine); `device-update` means the program on that
159
+ * machine is too old to offer it (an update would). Only a closed crossing may name one:
160
+ * `outsideSandbox` with `outsideSandbox: false`, `openPublic` with `network.openPublic: false`,
161
+ * `loopbackPorts` with `network.loopbackPorts` false or absent.
154
162
  * Display only: the kernel names that layer in its refusals and never enforces from it.
155
163
  */
156
164
  export interface SandboxSupportLimitedBy {
157
165
  readonly outsideSandbox?: "owner";
158
166
  readonly openPublic?: "owner";
167
+ readonly loopbackPorts?: "device-update";
159
168
  }
160
169
  /**
161
170
  * Whether a report is a supported instance boundary: `boundary: "instance"` with no
@@ -22,9 +22,11 @@ export type DeviceOp = "create" | "exec" | "readFile" | "writeFile" | "stat" | "
22
22
  /**
23
23
  * Optional capabilities a device announces in its hello: `networkReport` means exec results may
24
24
  * carry `network`; `projects` means the device serves `list` and `scan` (the provider never sends
25
- * `list` or `scan` to a device that did not announce `projects`).
25
+ * `list` or `scan` to a device that did not announce `projects`); `loopbackPorts` means the device
26
+ * accepts `service.ensure` `network.loopbackPorts` (the provider never sends that key to a device
27
+ * that did not announce it).
26
28
  */
27
- export type DeviceFeature = "networkReport" | "projects";
29
+ export type DeviceFeature = "networkReport" | "projects" | "loopbackPorts";
28
30
  /**
29
31
  * What a device knows of its local policy and reports (in the hello or a `policy` event). The owner
30
32
  * ceiling travels separately, in the owner report ({@link DeviceOwnerCeiling}).
@@ -17,7 +17,7 @@ export const DEVICE_PROTOCOL_VERSION = 6;
17
17
  * this module's ({@link parseDeviceFrame}).
18
18
  */
19
19
  export const DEVICE_DIAL_IN_PROTOCOL = `capybara-device/${DEVICE_PROTOCOL_VERSION}`;
20
- const DEVICE_FEATURES = ["networkReport", "projects"];
20
+ const DEVICE_FEATURES = ["networkReport", "projects", "loopbackPorts"];
21
21
  /** The most project directories a policy summary's `sharedProjectRoots` lists. */
22
22
  export const MAX_DEVICE_SHARED_PROJECT_ROOTS = 256;
23
23
  /** The most blocked hosts an exec result reports; the parser cuts longer lists. */
@@ -505,12 +505,15 @@ export function parseSandboxPermissionSupport(value) {
505
505
  throw new DeviceProtocolError(`Invalid ${what}: restricted support needs all four claims`);
506
506
  restricted = { filesystem: true, network: true, native: true, concurrentScopes: true };
507
507
  }
508
- const n = strictRecord(o.network, ["openPublic", "allowlist", "perOperationHosts"], `${what} network`);
508
+ const n = strictRecord(o.network, ["openPublic", "allowlist", "perOperationHosts", "loopbackPorts"], `${what} network`);
509
509
  const network = {
510
510
  openPublic: strictBoolean(n, "openPublic", what),
511
511
  allowlist: strictBoolean(n, "allowlist", what),
512
512
  perOperationHosts: strictBoolean(n, "perOperationHosts", what),
513
513
  };
514
+ // Absent means false: a provider built on an earlier SDK still parses.
515
+ if (n.loopbackPorts !== undefined)
516
+ network.loopbackPorts = strictBoolean(n, "loopbackPorts", what);
514
517
  if (o.boundary === "operation" &&
515
518
  restricted === null &&
516
519
  (network.openPublic || network.allowlist || network.perOperationHosts))
@@ -522,11 +525,13 @@ export function parseSandboxPermissionSupport(value) {
522
525
  if (o.boundary === "instance" &&
523
526
  (restricted !== null || outsideSandbox || network.perOperationHosts))
524
527
  throw new DeviceProtocolError(`Invalid ${what}: an instance boundary claims no restricted scope, outside escalation or per-operation hosts`);
528
+ if (network.loopbackPorts === true && (o.boundary !== "operation" || !outsideSandbox))
529
+ throw new DeviceProtocolError(`Invalid ${what}: loopbackPorts needs outsideSandbox on an operation boundary`);
525
530
  if (o.unavailableReason !== undefined)
526
531
  string(o.unavailableReason, 4096);
527
532
  let limitedBy;
528
533
  if (o.limitedBy !== undefined) {
529
- const l = strictRecord(o.limitedBy, ["outsideSandbox", "openPublic"], `${what} limitedBy`);
534
+ const l = strictRecord(o.limitedBy, ["outsideSandbox", "openPublic", "loopbackPorts"], `${what} limitedBy`);
530
535
  limitedBy = {};
531
536
  for (const [key, open] of [
532
537
  ["outsideSandbox", outsideSandbox],
@@ -540,6 +545,13 @@ export function parseSandboxPermissionSupport(value) {
540
545
  throw new DeviceProtocolError(`Invalid ${what}: limitedBy.${key} names who keeps a closed crossing closed, but it is open`);
541
546
  limitedBy[key] = "owner";
542
547
  }
548
+ if (l.loopbackPorts !== undefined) {
549
+ if (l.loopbackPorts !== "device-update")
550
+ throw new DeviceProtocolError(`Invalid ${what}: limitedBy.loopbackPorts must be device-update`);
551
+ if (network.loopbackPorts === true)
552
+ throw new DeviceProtocolError(`Invalid ${what}: limitedBy.loopbackPorts names who keeps a closed crossing closed, but it is open`);
553
+ limitedBy.loopbackPorts = "device-update";
554
+ }
543
555
  }
544
556
  return freeze({
545
557
  version: 2,
@@ -10,6 +10,7 @@
10
10
  */
11
11
  import type { SandboxBackground, SandboxBrowse, SandboxBrowseScope, SandboxOperation, SandboxOperationRef, SandboxOperationScope, SandboxPermissionSupport, SandboxPreparation, SandboxPreparationScope } from "./device-operation.js";
12
12
  import type { DialInHandler } from "./dial-in.js";
13
+ import type { AddressClass } from "./network-classes.js";
13
14
  /** Device provider extension point ref. */
14
15
  export declare const DEVICE_PROVIDER_EXTENSION_POINT = "device-provider@1";
15
16
  /** The roots inside the sandbox the model may access. */
@@ -73,12 +74,41 @@ export interface SandboxActor {
73
74
  export type NetworkBlockLayer = "device" | "org" | "address" | "unknown";
74
75
  /**
75
76
  * A host a device service saw refused, with the layer that refused it
76
- * (`ctx.deviceAccess.reportBlockedHosts`). The service reports host names only (no port).
77
+ * (`ctx.deviceAccess.reportBlockedHosts`), the refused port and, for a closed address, its class.
78
+ * A `localhost` or loopback literal refused at the `address` layer is what a loopback port ask
79
+ * starts from.
77
80
  */
78
81
  export interface ServiceBlockedHost {
79
82
  host: string;
80
83
  layer: NetworkBlockLayer;
84
+ /** The refused port; absent: the HTTPS port (443). */
85
+ port?: number;
86
+ /**
87
+ * The closed address class when `layer` is `address` and known (`classifyAddress`, or
88
+ * `loopback` for `localhost`).
89
+ */
90
+ class?: AddressClass;
81
91
  }
92
+ /**
93
+ * The only hosts a loopback port grant reaches (`ctx.deviceAccess.grantLoopbackPort`): the IPv4
94
+ * and IPv6 loopback literals and the name `localhost`, which the device dials without DNS. Every
95
+ * other loopback literal (`127.0.0.2`) and every private, link-local or metadata address stays
96
+ * closed.
97
+ */
98
+ export declare const LOOPBACK_GRANT_HOSTS: readonly ["127.0.0.1", "::1", "localhost"];
99
+ /**
100
+ * Whether `host` is one of {@link LOOPBACK_GRANT_HOSTS}: `::1` may also be written `[::1]`, and
101
+ * `localhost` matches case-insensitively. A trailing dot, a subdomain of `localhost` or an
102
+ * IPv4-mapped address is not one.
103
+ */
104
+ export declare function isLoopbackGrantHost(host: string): boolean;
105
+ /**
106
+ * Well-known database ports (PostgreSQL, MySQL, Redis, MongoDB): a grant to one is allowed but
107
+ * flagged (`LoopbackGrantView.database`) and named in the approval and review texts.
108
+ */
109
+ export declare const LOOPBACK_DATABASE_PORTS: readonly number[];
110
+ /** The most live loopback port grants one device service holds at once. */
111
+ export declare const LOOPBACK_MAX_PORTS_PER_SERVICE = 32;
82
112
  /**
83
113
  * The layer of a refusal by a device's sandbox proxy, read from the reason the proxy gives: the
84
114
  * status phrase of its 403 answer to a CONNECT, or the reason in its violation line
@@ -698,9 +728,19 @@ export interface DeviceProvider {
698
728
  * answers its `stopped` status (with `stateDir`, and `codeDir` where kept apart) and stays
699
729
  * stopped. A running one answers its `filesDigest` when known, so the kernel reviews a start on
700
730
  * new files before calling this without `ifRunning`.
731
+ *
732
+ * `opts.loopbackPorts` is the whole sorted set of the service's live loopback port grants
733
+ * (`DeviceServiceNetwork.loopbackPorts`, at most {@link LOOPBACK_MAX_PORTS_PER_SERVICE}): the
734
+ * service may reach {@link LOOPBACK_GRANT_HOSTS} on exactly these ports and no other loopback
735
+ * port. The kernel sends only a non-empty set, while the device's permission support reports
736
+ * `network.loopbackPorts` or is not known (none, or one naming an `unavailableReason`), and its
737
+ * ceilings allow running outside the sandbox; on a known refusal the key is absent. A provider forwards it only to a machine that
738
+ * announced the feature. A changed set applies like changed network limits (the running service
739
+ * is restarted on it), and an absent or empty set closes every loopback port again.
701
740
  */
702
741
  ensureService?(h: SandboxHandle, spec: DeviceServiceSpec, signal: AbortSignal, opts?: {
703
742
  ifRunning?: boolean;
743
+ loopbackPorts?: readonly number[];
704
744
  }): Promise<DeviceServiceStatus>;
705
745
  /** Stops the named service; succeeds when it is not running. */
706
746
  stopService?(h: SandboxHandle, name: string, signal: AbortSignal): Promise<void>;
@@ -24,6 +24,29 @@ export const DEFAULT_SANDBOX_ROOTS = Object.freeze({
24
24
  export function rootsOf(h) {
25
25
  return h.roots ?? DEFAULT_SANDBOX_ROOTS;
26
26
  }
27
+ /**
28
+ * The only hosts a loopback port grant reaches (`ctx.deviceAccess.grantLoopbackPort`): the IPv4
29
+ * and IPv6 loopback literals and the name `localhost`, which the device dials without DNS. Every
30
+ * other loopback literal (`127.0.0.2`) and every private, link-local or metadata address stays
31
+ * closed.
32
+ */
33
+ export const LOOPBACK_GRANT_HOSTS = Object.freeze(["127.0.0.1", "::1", "localhost"]);
34
+ /**
35
+ * Whether `host` is one of {@link LOOPBACK_GRANT_HOSTS}: `::1` may also be written `[::1]`, and
36
+ * `localhost` matches case-insensitively. A trailing dot, a subdomain of `localhost` or an
37
+ * IPv4-mapped address is not one.
38
+ */
39
+ export function isLoopbackGrantHost(host) {
40
+ const h = host.toLowerCase();
41
+ return h === "127.0.0.1" || h === "::1" || h === "[::1]" || h === "localhost";
42
+ }
43
+ /**
44
+ * Well-known database ports (PostgreSQL, MySQL, Redis, MongoDB): a grant to one is allowed but
45
+ * flagged (`LoopbackGrantView.database`) and named in the approval and review texts.
46
+ */
47
+ export const LOOPBACK_DATABASE_PORTS = Object.freeze([5432, 3306, 6379, 27017]);
48
+ /** The most live loopback port grants one device service holds at once. */
49
+ export const LOOPBACK_MAX_PORTS_PER_SERVICE = 32;
27
50
  /**
28
51
  * The layer of a refusal by a device's sandbox proxy, read from the reason the proxy gives: the
29
52
  * status phrase of its 403 answer to a CONNECT, or the reason in its violation line
@@ -30,6 +30,27 @@
30
30
  * kernel sends two request bodies of its own: the roll check ({@link DEVICE_SERVICE_ROLL_CHECK})
31
31
  * and the conversation-deleted notice ({@link encodeConversationDeleted}).
32
32
  */
33
+ /**
34
+ * The network a device service runs under (`service.ensure`): the device's network setting plus
35
+ * the service's live loopback port grants, which let it reach the loopback grant hosts
36
+ * (`LOOPBACK_GRANT_HOSTS`) on exactly those ports. The set is always sent whole, so an absent or
37
+ * empty one closes every loopback port again.
38
+ */
39
+ export interface DeviceServiceNetwork {
40
+ kind: "open-public" | "allowlist";
41
+ hosts: string[];
42
+ /**
43
+ * Live granted loopback ports, sorted ascending, unique, 1–65535, at most
44
+ * {@link LOOPBACK_MAX_PORTS_PER_SERVICE}.
45
+ */
46
+ loopbackPorts?: number[];
47
+ }
48
+ /**
49
+ * Strictly parses a set of loopback ports ({@link DeviceServiceNetwork.loopbackPorts}): an array
50
+ * of unique integers 1–65535, at most {@link LOOPBACK_MAX_PORTS_PER_SERVICE}; returns a sorted
51
+ * copy. Throws a `TypeError` naming `what` otherwise.
52
+ */
53
+ export declare function parseLoopbackPorts(value: unknown, what: string): number[];
33
54
  /**
34
55
  * The request body the kernel sends a running service before restarting it on new files (an
35
56
  * upgraded extension, {@link DeviceServiceRollCheck}). A service that answers
@@ -30,6 +30,27 @@
30
30
  * kernel sends two request bodies of its own: the roll check ({@link DEVICE_SERVICE_ROLL_CHECK})
31
31
  * and the conversation-deleted notice ({@link encodeConversationDeleted}).
32
32
  */
33
+ import { LOOPBACK_MAX_PORTS_PER_SERVICE } from "./device-provider.js";
34
+ /**
35
+ * Strictly parses a set of loopback ports ({@link DeviceServiceNetwork.loopbackPorts}): an array
36
+ * of unique integers 1–65535, at most {@link LOOPBACK_MAX_PORTS_PER_SERVICE}; returns a sorted
37
+ * copy. Throws a `TypeError` naming `what` otherwise.
38
+ */
39
+ export function parseLoopbackPorts(value, what) {
40
+ if (!Array.isArray(value))
41
+ throw new TypeError(`${what} must be an array of ports`);
42
+ if (value.length > LOOPBACK_MAX_PORTS_PER_SERVICE)
43
+ throw new TypeError(`${what} lists at most ${LOOPBACK_MAX_PORTS_PER_SERVICE} ports`);
44
+ const seen = new Set();
45
+ for (const p of value) {
46
+ if (typeof p !== "number" || !Number.isInteger(p) || p < 1 || p > 65535)
47
+ throw new TypeError(`${what} must list integer ports from 1 to 65535`);
48
+ if (seen.has(p))
49
+ throw new TypeError(`${what} lists port ${p} twice`);
50
+ seen.add(p);
51
+ }
52
+ return [...seen].sort((a, b) => a - b);
53
+ }
33
54
  /**
34
55
  * The request body the kernel sends a running service before restarting it on new files (an
35
56
  * upgraded extension, {@link DeviceServiceRollCheck}). A service that answers
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const SDK_VERSION = "0.1.0-rc.89";
1
+ export declare const SDK_VERSION = "0.1.0-rc.90";
2
2
  export * from "./agent-template.js";
3
3
  export * from "./agents-ctx.js";
4
4
  export * from "./avatar-presets.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export const SDK_VERSION = "0.1.0-rc.89";
1
+ export const SDK_VERSION = "0.1.0-rc.90";
2
2
  export * from "./agent-template.js";
3
3
  export * from "./agents-ctx.js";
4
4
  export * from "./avatar-presets.js";
@@ -71,10 +71,14 @@ export interface ExtensionPermissions {
71
71
  * `ctx.deviceAccess`: services this extension runs in the conversation's device and reaches
72
72
  * through requests and viewer streams. `services` lists 1 to
73
73
  * {@link DEVICE_ACCESS_MAX_SERVICES} service names ({@link DEVICE_SERVICE_NAME_RE}); a service
74
- * not listed is refused. Any extension may declare it (no `provides` needed).
74
+ * not listed is refused. Any extension may declare it (no `provides` needed). `loopbackPorts`:
75
+ * the extension's services may be granted exact loopback ports of the device
76
+ * (`ctx.deviceAccess.grantLoopbackPort`, and the loopback ask raised from `reportBlockedHosts`);
77
+ * without it a loopback refusal only gets a note.
75
78
  */
76
79
  deviceAccess?: {
77
80
  services: string[];
81
+ loopbackPorts?: boolean;
78
82
  };
79
83
  /**
80
84
  * `ctx.triggers`: the event types this extension may emit (at most {@link TRIGGER_MAX_TYPES},
@@ -236,8 +236,11 @@ export function validatePermissions(plugin, p, provides = []) {
236
236
  }
237
237
  }
238
238
  if (p.deviceAccess !== undefined) {
239
- if (!isObj(p.deviceAccess) || Object.keys(p.deviceAccess).some((k) => k !== "services")) {
240
- errs.push("permissions.deviceAccess must be { services: string[] }");
239
+ if (!isObj(p.deviceAccess) ||
240
+ Object.keys(p.deviceAccess).some((k) => k !== "services" && k !== "loopbackPorts") ||
241
+ (p.deviceAccess.loopbackPorts !== undefined &&
242
+ typeof p.deviceAccess.loopbackPorts !== "boolean")) {
243
+ errs.push("permissions.deviceAccess must be { services: string[], loopbackPorts?: boolean }");
241
244
  }
242
245
  else {
243
246
  const names = strList("permissions.deviceAccess.services", p.deviceAccess.services, errs);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/plugin-sdk",
3
- "version": "0.1.0-rc.89",
3
+ "version": "0.1.0-rc.90",
4
4
  "description": "Capybara plugin SDK: the types and pure functions plugins are written against",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {