@capybara-hq/kernel 0.1.0-rc.80 → 0.1.0-rc.81
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.
- package/dist/admin/outbox-dead.service.js +133 -120
- package/dist/background/store.js +78 -67
- package/dist/background/tasks.service.d.ts +6 -0
- package/dist/background/tasks.service.js +3 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/plugin-import.js +45 -40
- package/dist/catalog/scope-sync-workers.js +3 -0
- package/dist/channels/inbound.d.ts +6 -1
- package/dist/channels/inbound.js +18 -1
- package/dist/client/client-reports.service.js +9 -6
- package/dist/config/schema.d.ts +5 -0
- package/dist/config/schema.js +6 -0
- package/dist/conversations/attachments.service.js +101 -96
- package/dist/conversations/events.d.ts +7 -0
- package/dist/conversations/events.js +9 -0
- package/dist/ctx/bus.js +5 -3
- package/dist/ctx/queue.js +4 -2
- package/dist/db/pool.d.ts +6 -0
- package/dist/db/pool.js +3 -2
- package/dist/devices/legacy-cleanup.job.js +73 -70
- package/dist/dial-in/gateway.d.ts +8 -0
- package/dist/dial-in/gateway.js +22 -2
- package/dist/extensions/admin-ops.js +22 -19
- package/dist/extensions/lifecycle-hooks.js +3 -1
- package/dist/http/app.js +3 -1
- package/dist/http/plugin-dispatch.js +8 -0
- package/dist/identity/auth.guard.js +13 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/infra/database/database.module.js +1 -0
- package/dist/infra/database/org-scope.d.ts +59 -0
- package/dist/infra/database/org-scope.js +68 -0
- package/dist/infra/database/rls-role.d.ts +11 -0
- package/dist/infra/database/rls-role.js +33 -0
- package/dist/infra/database/scoped-dialect.d.ts +18 -0
- package/dist/infra/database/scoped-dialect.js +245 -0
- package/dist/infra/outbox/outbox.dispatcher.js +98 -90
- package/dist/infra/outbox/outbox.service.js +29 -26
- package/dist/infra/queue/run-job.d.ts +8 -2
- package/dist/infra/queue/run-job.js +16 -3
- package/dist/infra/schedule/locked-tick.js +12 -4
- package/dist/infra/schedule/scheduled-runs.js +3 -1
- package/dist/lifecycle/kernel-lifecycle.d.ts +1 -0
- package/dist/lifecycle/kernel-lifecycle.js +16 -1
- package/dist/orgs/org.guard.js +4 -0
- package/dist/platform/sealed-reveals.js +9 -6
- package/dist/runtime/maintenance.js +6 -1
- package/dist/runtime/run-execution.d.ts +1 -0
- package/dist/runtime/run-execution.js +5 -0
- package/dist/runtime/run-orphan-check.job.js +20 -15
- package/dist/sandbox/legacy-retirement.js +438 -397
- package/dist/sandbox/modes.d.ts +10 -0
- package/dist/sandbox/modes.js +23 -0
- package/dist/sandbox/operation-reconcile.job.d.ts +14 -1
- package/dist/sandbox/operation-reconcile.job.js +35 -3
- package/dist/sandbox/permission-config-publication.js +83 -77
- package/dist/sandbox/sandbox.service.d.ts +24 -0
- package/dist/sandbox/sandbox.service.js +159 -62
- package/dist/schedules/store.js +17 -14
- package/dist/tools/builtins/sandbox/bash.js +16 -1
- package/dist/tools/drift.js +31 -24
- package/dist/tools/outputs.js +20 -17
- package/package.json +2 -2
- package/src/migrations/0182_rls_policies.sql +202 -0
package/dist/sandbox/modes.d.ts
CHANGED
|
@@ -183,6 +183,16 @@ export declare function appliedWider(a: AppliedLimits | null | undefined, b: App
|
|
|
183
183
|
* is {@link sameApplied}'s. The mode is not a dimension ({@link AppliedLimits}).
|
|
184
184
|
*/
|
|
185
185
|
export declare function appliedLacksHost(a: AppliedLimits | null | undefined, b: AppliedLimits): boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Whether the network a provider reports its instance enforces (`installed`, from
|
|
188
|
+
* `DeviceProvider.appliedNetwork`) may allow more than the kernel's record `recorded` (issue
|
|
189
|
+
* #766): the open public network over an allowlist, or a host no recorded entry covers (hosts
|
|
190
|
+
* compare lowercased). An unknown record (null) compares with `target`. Anything that is not a
|
|
191
|
+
* network the kernel can read (null: the provider cannot tell; a malformed answer) counts as wider:
|
|
192
|
+
* the remedy only applies the desired limits again, so failing closed never widens. Equal or
|
|
193
|
+
* narrower is false. The mode is not a dimension ({@link AppliedLimits}).
|
|
194
|
+
*/
|
|
195
|
+
export declare function installedWider(installed: unknown, recorded: AppliedLimits | null, target: AppliedLimits): boolean;
|
|
186
196
|
/**
|
|
187
197
|
* `device_instances.instance_limits` read leniently: `applied` is what the instance runs with
|
|
188
198
|
* (null: unknown, e.g. an instance made before auto mode recorded tiers, which differs from every
|
package/dist/sandbox/modes.js
CHANGED
|
@@ -183,6 +183,29 @@ export function appliedLacksHost(a, b) {
|
|
|
183
183
|
return false;
|
|
184
184
|
return b.network.hosts.some((host) => !a.network.hosts.some((p) => coversHost(p, host)));
|
|
185
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Whether the network a provider reports its instance enforces (`installed`, from
|
|
188
|
+
* `DeviceProvider.appliedNetwork`) may allow more than the kernel's record `recorded` (issue
|
|
189
|
+
* #766): the open public network over an allowlist, or a host no recorded entry covers (hosts
|
|
190
|
+
* compare lowercased). An unknown record (null) compares with `target`. Anything that is not a
|
|
191
|
+
* network the kernel can read (null: the provider cannot tell; a malformed answer) counts as wider:
|
|
192
|
+
* the remedy only applies the desired limits again, so failing closed never widens. Equal or
|
|
193
|
+
* narrower is false. The mode is not a dimension ({@link AppliedLimits}).
|
|
194
|
+
*/
|
|
195
|
+
export function installedWider(installed, recorded, target) {
|
|
196
|
+
const against = recorded ?? target;
|
|
197
|
+
if (typeof installed !== "object" || installed === null)
|
|
198
|
+
return true;
|
|
199
|
+
const { kind, hosts } = installed;
|
|
200
|
+
if (kind === "open-public")
|
|
201
|
+
return against.network.kind !== "open-public";
|
|
202
|
+
if (kind !== "allowlist" || !Array.isArray(hosts))
|
|
203
|
+
return true;
|
|
204
|
+
if (!hosts.every((h) => typeof h === "string"))
|
|
205
|
+
return true;
|
|
206
|
+
const network = normalizeNetwork({ kind: "allowlist", hosts });
|
|
207
|
+
return appliedWider({ mode: against.mode, network }, against);
|
|
208
|
+
}
|
|
186
209
|
const isMode = (v) => v === "auto" || v === "sandbox";
|
|
187
210
|
function appliedFrom(v) {
|
|
188
211
|
if (typeof v !== "object" || v === null)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BackgroundTaskStopper } from "../background/stop.js";
|
|
2
2
|
import { BackgroundTaskStore } from "../background/store.js";
|
|
3
|
+
import { EventLog } from "../conversations/event-log.js";
|
|
3
4
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
4
5
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
5
6
|
import type { KernelLogger } from "../log.js";
|
|
@@ -17,10 +18,22 @@ export declare class SandboxOperationReconcileJob {
|
|
|
17
18
|
private readonly config;
|
|
18
19
|
/** Where a recovered start's refusal keeps the provider's own message (never the task's). */
|
|
19
20
|
private readonly log?;
|
|
21
|
+
/** Tells a page listing the task that reconciliation moved it (a hint, its loss harmless). */
|
|
22
|
+
private readonly eventLog?;
|
|
20
23
|
constructor(intents: SandboxOperationIntents, db: KernelDb, providers: Pick<DeviceProviders, "forDevice">, sandbox: Pick<SandboxService, "ifRunning">, tasks: Pick<BackgroundTaskStore, "byOperation" | "settleOwedCancel">, stopper: Pick<BackgroundTaskStopper, "stop" | "settleRevokedUnconfirmed">, config: Pick<KernelConfigService, "get">,
|
|
21
24
|
/** Where a recovered start's refusal keeps the provider's own message (never the task's). */
|
|
22
|
-
log?: Pick<KernelLogger, "warn"> | undefined
|
|
25
|
+
log?: Pick<KernelLogger, "warn"> | undefined,
|
|
26
|
+
/** Tells a page listing the task that reconciliation moved it (a hint, its loss harmless). */
|
|
27
|
+
eventLog?: Pick<EventLog, "publishChanged"> | undefined);
|
|
23
28
|
reconcile(operationId: string): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* After reconciling a managed start (each step commits its own transaction): when the task's
|
|
31
|
+
* launch or status moved (confirmed running, refused, given up), the pages listing it read the
|
|
32
|
+
* list again (`changed: background`, the owner and, for a sub-agent's task, the conversation
|
|
33
|
+
* that owns the sandbox), so a thread row shown as reconciling settles without a reload. A
|
|
34
|
+
* failed publish only logs.
|
|
35
|
+
*/
|
|
36
|
+
private announceMoved;
|
|
24
37
|
/**
|
|
25
38
|
* A managed start: past its retention an unresolved launch is given up (terminal task, owed
|
|
26
39
|
* exact revoke); a revoked one owes its revoke; a `dispatching` one still has its dispatcher
|
|
@@ -12,6 +12,7 @@ import { Inject, Injectable, Optional } from "@nestjs/common";
|
|
|
12
12
|
import { MANAGED_STOP_GRACE_MS, ManagedInstanceMismatchError } from "../background/managed.js";
|
|
13
13
|
import { BackgroundTaskStopper } from "../background/stop.js";
|
|
14
14
|
import { BackgroundTaskStore } from "../background/store.js";
|
|
15
|
+
import { EventLog } from "../conversations/event-log.js";
|
|
15
16
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
16
17
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
17
18
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
@@ -34,9 +35,12 @@ let SandboxOperationReconcileJob = class SandboxOperationReconcileJob {
|
|
|
34
35
|
stopper;
|
|
35
36
|
config;
|
|
36
37
|
log;
|
|
38
|
+
eventLog;
|
|
37
39
|
constructor(intents, db, providers, sandbox, tasks, stopper, config,
|
|
38
40
|
/** Where a recovered start's refusal keeps the provider's own message (never the task's). */
|
|
39
|
-
log
|
|
41
|
+
log,
|
|
42
|
+
/** Tells a page listing the task that reconciliation moved it (a hint, its loss harmless). */
|
|
43
|
+
eventLog) {
|
|
40
44
|
this.intents = intents;
|
|
41
45
|
this.db = db;
|
|
42
46
|
this.providers = providers;
|
|
@@ -45,12 +49,19 @@ let SandboxOperationReconcileJob = class SandboxOperationReconcileJob {
|
|
|
45
49
|
this.stopper = stopper;
|
|
46
50
|
this.config = config;
|
|
47
51
|
this.log = log;
|
|
52
|
+
this.eventLog = eventLog;
|
|
48
53
|
}
|
|
49
54
|
async reconcile(operationId) {
|
|
50
55
|
await this.intents.expirePayload(operationId);
|
|
51
56
|
const row = await this.intents.read(operationId);
|
|
52
57
|
if (row.method === "backgroundStart") {
|
|
53
|
-
await this.
|
|
58
|
+
const before = await this.tasks.byOperation(operationId);
|
|
59
|
+
try {
|
|
60
|
+
await this.reconcileBackground(row);
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
await this.announceMoved(before, operationId);
|
|
64
|
+
}
|
|
54
65
|
return;
|
|
55
66
|
}
|
|
56
67
|
if (row.state === "revoked") {
|
|
@@ -61,6 +72,25 @@ let SandboxOperationReconcileJob = class SandboxOperationReconcileJob {
|
|
|
61
72
|
if (row.state === "dispatching")
|
|
62
73
|
await this.intents.unknown(operationId);
|
|
63
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* After reconciling a managed start (each step commits its own transaction): when the task's
|
|
77
|
+
* launch or status moved (confirmed running, refused, given up), the pages listing it read the
|
|
78
|
+
* list again (`changed: background`, the owner and, for a sub-agent's task, the conversation
|
|
79
|
+
* that owns the sandbox), so a thread row shown as reconciling settles without a reload. A
|
|
80
|
+
* failed publish only logs.
|
|
81
|
+
*/
|
|
82
|
+
async announceMoved(before, operationId) {
|
|
83
|
+
if (!before || !this.eventLog)
|
|
84
|
+
return;
|
|
85
|
+
const after = await this.tasks.byOperation(operationId);
|
|
86
|
+
if (!after ||
|
|
87
|
+
(after.status === before.status &&
|
|
88
|
+
after.launchState === before.launchState &&
|
|
89
|
+
(after.executionRef === null) === (before.executionRef === null)))
|
|
90
|
+
return;
|
|
91
|
+
for (const c of new Set([after.conversationId, after.sandboxConversationId]))
|
|
92
|
+
await this.eventLog.publishChanged(c, "background");
|
|
93
|
+
}
|
|
64
94
|
/**
|
|
65
95
|
* A managed start: past its retention an unresolved launch is given up (terminal task, owed
|
|
66
96
|
* exact revoke); a revoked one owes its revoke; a `dispatching` one still has its dispatcher
|
|
@@ -295,6 +325,8 @@ SandboxOperationReconcileJob = __decorate([
|
|
|
295
325
|
__param(5, Inject(BackgroundTaskStopper)),
|
|
296
326
|
__param(6, Inject(KernelConfigService)),
|
|
297
327
|
__param(7, Optional()),
|
|
298
|
-
__param(7, Inject(KERNEL_LOGGER))
|
|
328
|
+
__param(7, Inject(KERNEL_LOGGER)),
|
|
329
|
+
__param(8, Optional()),
|
|
330
|
+
__param(8, Inject(EventLog))
|
|
299
331
|
], SandboxOperationReconcileJob);
|
|
300
332
|
export { SandboxOperationReconcileJob };
|
|
@@ -14,6 +14,7 @@ import { sql } from "kysely";
|
|
|
14
14
|
import { z } from "zod";
|
|
15
15
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
16
16
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
17
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
17
18
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
18
19
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
19
20
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
@@ -374,74 +375,77 @@ let PermissionConfigPublication = class PermissionConfigPublication {
|
|
|
374
375
|
throw new PermissionConfigActivationConflict(previous, fingerprint);
|
|
375
376
|
}
|
|
376
377
|
async publishLocked(previous, next) {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
.
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
378
|
+
return asPlatform("platform publication: the kill switch spans every org", async () => {
|
|
379
|
+
const fingerprint = permissionProjectionFingerprint(next);
|
|
380
|
+
const row = await this.db
|
|
381
|
+
.selectFrom("permission_config_publication")
|
|
382
|
+
.select(["fingerprint", "projection"])
|
|
383
|
+
.where("id", "=", "platform")
|
|
384
|
+
.executeTakeFirst();
|
|
385
|
+
if (!row)
|
|
386
|
+
throw new PermissionConfigActivationConflict(undefined, fingerprint);
|
|
387
|
+
const stored = this.validateStored(row);
|
|
388
|
+
const before = stored.projection;
|
|
389
|
+
if (row.fingerprint === fingerprint)
|
|
390
|
+
return "matching";
|
|
391
|
+
if (row.fingerprint !== previous)
|
|
392
|
+
throw new PermissionConfigActivationConflict(row.fingerprint, fingerprint);
|
|
393
|
+
await this.replaceLocked(previous, next);
|
|
394
|
+
// the stored keys convert at every publication (old keys an old replica wrote meanwhile
|
|
395
|
+
// too); an older predecessor also pins its cloud devices, in this same transaction
|
|
396
|
+
await this.convertLocked(next, stored.legacy);
|
|
397
|
+
// format only: the same semantics publish the new fingerprint (the barrier) and nothing else
|
|
398
|
+
if (permissionProjectionFingerprint(before) === fingerprint)
|
|
399
|
+
return "activated";
|
|
400
|
+
const storedChoice = await readStoredDeviceSandbox(this.db.db, this.log, configuredOf(next.network.ceiling));
|
|
401
|
+
const effective = (projection) => resolveDeviceSandboxSettings(configuredView(projection), storedChoice);
|
|
402
|
+
const beforeView = effective(before), nextView = effective(next);
|
|
403
|
+
const beforeMode = beforeView.outsideSandbox.permission, nextMode = nextView.outsideSandbox.permission;
|
|
404
|
+
// a network opened anywhere is a widening: consent given under the narrower network resets
|
|
405
|
+
const networkWidened = (beforeView.network.ceiling === "allowlist" && nextView.network.ceiling === "open") ||
|
|
406
|
+
(beforeView.network.default === "allowlist" && nextView.network.default === "open");
|
|
407
|
+
const orgs = await this.db.selectFrom("orgs").select(["id", "settings"]).execute();
|
|
408
|
+
const settings = new Map(orgs.map((org) => [org.id, orgDeviceSandboxOf(org.settings).deviceNetwork]));
|
|
409
|
+
// the kill switch lifted is a widening for every device (closing it only narrows)
|
|
410
|
+
// org-filter: platform publication: the kill switch is deployment-wide, every org's devices
|
|
411
|
+
if (before.egress === "none" && next.egress !== "none")
|
|
412
|
+
await this.ceilings.invalidateDeviceConsentUnderPlatform((await this.db.selectFrom("devices").select("id").execute()).map((device) => device.id));
|
|
413
|
+
// a network ceiling narrowed to the allowlist, or closed by the kill switch, takes running
|
|
414
|
+
// open-network work
|
|
415
|
+
const networkNarrowed = (beforeView.network.ceiling === "open" && nextView.network.ceiling === "allowlist") ||
|
|
416
|
+
(before.egress !== "none" && next.egress === "none");
|
|
417
|
+
// a default narrowed to the allowlist takes it from the devices following it
|
|
418
|
+
const following = !networkNarrowed &&
|
|
419
|
+
beforeView.network.default === "open" &&
|
|
420
|
+
nextView.network.default === "allowlist"
|
|
421
|
+
? new Set([...(await devicesFollowingDefault(this.db.db))]
|
|
422
|
+
.filter(([, org]) => settings.get(org)?.default === undefined)
|
|
423
|
+
.map(([id]) => id))
|
|
424
|
+
: null;
|
|
425
|
+
// the per-call host list widened: hosts the platform list did not cover, or orgs allowed
|
|
426
|
+
// beyond it again (`within` to `extend`)
|
|
427
|
+
const hostsWidened = (before.orgHosts === "within" && next.orgHosts === "extend") ||
|
|
428
|
+
next.allowHosts.some((h) => !before.allowHosts.some((p) => coversHost(p.toLowerCase(), h.toLowerCase())));
|
|
429
|
+
const grantable = (orgId, host) => {
|
|
430
|
+
const org = settings.get(orgId);
|
|
431
|
+
const ceiling = nextView.network.ceiling === "allowlist" || org?.ceiling === "allowlist"
|
|
432
|
+
? "allowlist"
|
|
433
|
+
: "open";
|
|
434
|
+
return hostGrantable(hostGrantCeiling({ boundary: "operation", networkMode: "allowlist", networkCeiling: ceiling }, next, org === undefined ? {} : { deviceNetwork: org }), host);
|
|
435
|
+
};
|
|
436
|
+
await this.ceilings.publishLocked({}, "platform", (scope, row) => {
|
|
437
|
+
if (needsOpenNetwork(scope) &&
|
|
438
|
+
(networkNarrowed || following?.has(row.device_id) === true))
|
|
439
|
+
return true;
|
|
440
|
+
// escalated work goes with the outside-sandbox switch, and with the kill switch closing
|
|
441
|
+
if (needsFullCeiling(scope))
|
|
442
|
+
return nextMode === "off" || (before.egress !== "none" && next.egress === "none");
|
|
443
|
+
return hostsNarrowed(scope, row, orgEffectiveHosts(next, settings.get(row.org_id)), (host) => next.egress !== "none" && grantable(row.org_id, host));
|
|
444
|
+
},
|
|
445
|
+
// only a widening resets Full consent: a narrowing keeps what the consent covered
|
|
446
|
+
(beforeMode === "off" && nextMode === "allowed") || networkWidened || hostsWidened);
|
|
397
447
|
return "activated";
|
|
398
|
-
|
|
399
|
-
const effective = (projection) => resolveDeviceSandboxSettings(configuredView(projection), storedChoice);
|
|
400
|
-
const beforeView = effective(before), nextView = effective(next);
|
|
401
|
-
const beforeMode = beforeView.outsideSandbox.permission, nextMode = nextView.outsideSandbox.permission;
|
|
402
|
-
// a network opened anywhere is a widening: consent given under the narrower network resets
|
|
403
|
-
const networkWidened = (beforeView.network.ceiling === "allowlist" && nextView.network.ceiling === "open") ||
|
|
404
|
-
(beforeView.network.default === "allowlist" && nextView.network.default === "open");
|
|
405
|
-
const orgs = await this.db.selectFrom("orgs").select(["id", "settings"]).execute();
|
|
406
|
-
const settings = new Map(orgs.map((org) => [org.id, orgDeviceSandboxOf(org.settings).deviceNetwork]));
|
|
407
|
-
// the kill switch lifted is a widening for every device (closing it only narrows)
|
|
408
|
-
// org-filter: platform publication: the kill switch is deployment-wide, every org's devices
|
|
409
|
-
if (before.egress === "none" && next.egress !== "none")
|
|
410
|
-
await this.ceilings.invalidateDeviceConsentUnderPlatform((await this.db.selectFrom("devices").select("id").execute()).map((device) => device.id));
|
|
411
|
-
// a network ceiling narrowed to the allowlist, or closed by the kill switch, takes running
|
|
412
|
-
// open-network work
|
|
413
|
-
const networkNarrowed = (beforeView.network.ceiling === "open" && nextView.network.ceiling === "allowlist") ||
|
|
414
|
-
(before.egress !== "none" && next.egress === "none");
|
|
415
|
-
// a default narrowed to the allowlist takes it from the devices following it
|
|
416
|
-
const following = !networkNarrowed &&
|
|
417
|
-
beforeView.network.default === "open" &&
|
|
418
|
-
nextView.network.default === "allowlist"
|
|
419
|
-
? new Set([...(await devicesFollowingDefault(this.db.db))]
|
|
420
|
-
.filter(([, org]) => settings.get(org)?.default === undefined)
|
|
421
|
-
.map(([id]) => id))
|
|
422
|
-
: null;
|
|
423
|
-
// the per-call host list widened: hosts the platform list did not cover, or orgs allowed
|
|
424
|
-
// beyond it again (`within` to `extend`)
|
|
425
|
-
const hostsWidened = (before.orgHosts === "within" && next.orgHosts === "extend") ||
|
|
426
|
-
next.allowHosts.some((h) => !before.allowHosts.some((p) => coversHost(p.toLowerCase(), h.toLowerCase())));
|
|
427
|
-
const grantable = (orgId, host) => {
|
|
428
|
-
const org = settings.get(orgId);
|
|
429
|
-
const ceiling = nextView.network.ceiling === "allowlist" || org?.ceiling === "allowlist"
|
|
430
|
-
? "allowlist"
|
|
431
|
-
: "open";
|
|
432
|
-
return hostGrantable(hostGrantCeiling({ boundary: "operation", networkMode: "allowlist", networkCeiling: ceiling }, next, org === undefined ? {} : { deviceNetwork: org }), host);
|
|
433
|
-
};
|
|
434
|
-
await this.ceilings.publishLocked({}, "platform", (scope, row) => {
|
|
435
|
-
if (needsOpenNetwork(scope) && (networkNarrowed || following?.has(row.device_id) === true))
|
|
436
|
-
return true;
|
|
437
|
-
// escalated work goes with the outside-sandbox switch, and with the kill switch closing
|
|
438
|
-
if (needsFullCeiling(scope))
|
|
439
|
-
return nextMode === "off" || (before.egress !== "none" && next.egress === "none");
|
|
440
|
-
return hostsNarrowed(scope, row, orgEffectiveHosts(next, settings.get(row.org_id)), (host) => next.egress !== "none" && grantable(row.org_id, host));
|
|
441
|
-
},
|
|
442
|
-
// only a widening resets Full consent: a narrowing keeps what the consent covered
|
|
443
|
-
(beforeMode === "off" && nextMode === "allowed") || networkWidened || hostsWidened);
|
|
444
|
-
return "activated";
|
|
448
|
+
});
|
|
445
449
|
}
|
|
446
450
|
async assertCurrent() {
|
|
447
451
|
const row = await this.db
|
|
@@ -503,21 +507,23 @@ export async function lockDevicesFollowingTheirOrg(db) {
|
|
|
503
507
|
*/
|
|
504
508
|
// org-filter: deployment-wide idempotent copy (migrations 0165/0167, publication) over every org
|
|
505
509
|
export async function copyCloudSnapshotNetworks(db) {
|
|
506
|
-
|
|
510
|
+
return asPlatform("deployment-wide idempotent copy", async () => {
|
|
511
|
+
const readable = sql `jsonb_typeof(outer_network -> 'hosts') = 'array'
|
|
507
512
|
AND NOT EXISTS (
|
|
508
513
|
SELECT 1 FROM jsonb_array_elements(outer_network -> 'hosts') AS h(v)
|
|
509
514
|
WHERE jsonb_typeof(h.v) <> 'string'
|
|
510
515
|
)`;
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
516
|
+
await db
|
|
517
|
+
.updateTable("devices")
|
|
518
|
+
.set({
|
|
519
|
+
network_mode: sql `CASE
|
|
515
520
|
WHEN ${readable} AND jsonb_array_length(outer_network -> 'hosts') > 0 THEN 'allowlist'
|
|
516
521
|
ELSE 'none' END`,
|
|
517
|
-
|
|
522
|
+
network_hosts: sql `CASE WHEN ${readable} THEN outer_network -> 'hosts'
|
|
518
523
|
ELSE '[]'::jsonb END`,
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
524
|
+
})
|
|
525
|
+
.where("network_mode", "is", null)
|
|
526
|
+
.where(sql `outer_network ->> 'kind' = 'allowlist'`)
|
|
527
|
+
.execute();
|
|
528
|
+
});
|
|
523
529
|
}
|
|
@@ -416,6 +416,17 @@ export declare class SandboxService {
|
|
|
416
416
|
* the in-flight target, and only a newer call with this holder's higher token settles it.
|
|
417
417
|
*/
|
|
418
418
|
private mustApply;
|
|
419
|
+
/**
|
|
420
|
+
* Under the exclusive lease, at a lifecycle step on the instance `handle` of `row` (issue #766):
|
|
421
|
+
* whether the network the provider reports it enforces (`appliedNetwork`) may allow more than
|
|
422
|
+
* the recorded limits ({@link installedWider}), so the desired limits must be applied again even
|
|
423
|
+
* though the record matches them. It closes the residual of a provider whose `update` cannot
|
|
424
|
+
* honor the fence: a stale holder's late `update` that landed after the newer holder's. Only a
|
|
425
|
+
* provider with both `update` and `appliedNetwork` is asked. An unknown answer, a failed read or
|
|
426
|
+
* an abort counts as wider (fail closed: applying the desired limits never widens anything).
|
|
427
|
+
* Never throws.
|
|
428
|
+
*/
|
|
429
|
+
private installedNetworkWider;
|
|
419
430
|
/**
|
|
420
431
|
* The device's instance state as shown to people (read only, no lease): `running` /
|
|
421
432
|
* `suspended` and the last use. No instance, a destroyed one, or one stuck in `creating` (being
|
|
@@ -816,6 +827,14 @@ export declare class SandboxService {
|
|
|
816
827
|
* rebuilt it the new one is used.
|
|
817
828
|
*/
|
|
818
829
|
private ensure;
|
|
830
|
+
/**
|
|
831
|
+
* {@link create} from {@link ensure}, then, for a provider whose limits are fixed at its file
|
|
832
|
+
* system (the new instance keeps the network its file system enforces, E2B), the installed
|
|
833
|
+
* network check of {@link installedNetworkWider} on the new instance (issue #766): wider than
|
|
834
|
+
* the record applies the desired limits again before the use. Any other provider's new instance
|
|
835
|
+
* got the desired limits at `create`.
|
|
836
|
+
*/
|
|
837
|
+
private createSettled;
|
|
819
838
|
/**
|
|
820
839
|
* Under the lease: fenced reset to `creating`, the file system when there is none
|
|
821
840
|
* (`createFileSystem`, its handle written fenced), then the instance on it, then a fenced write
|
|
@@ -837,6 +856,11 @@ export declare class SandboxService {
|
|
|
837
856
|
* instance create is recreated (warned); after an abort, or for a provider bound to its file
|
|
838
857
|
* system, the instance is suspended ({@link quarantine}) and the error thrown, so the next use
|
|
839
858
|
* resumes it and tries again.
|
|
859
|
+
*
|
|
860
|
+
* `installedWider`: the call narrows an instance whose installed network allows more than its
|
|
861
|
+
* record (issue #766). A provider bound to its file system that cannot apply `want` then is
|
|
862
|
+
* quarantined too, with the intent kept, instead of being recorded `unapplied` and used: the
|
|
863
|
+
* instance would run wider than the record claims.
|
|
840
864
|
*/
|
|
841
865
|
private applyLimits;
|
|
842
866
|
/**
|