@capybara-hq/kernel 0.1.0-rc.145 → 0.1.0-rc.146
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/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/provenance.d.ts +69 -0
- package/dist/catalog/provenance.js +108 -0
- package/dist/ctx/create-ctx.d.ts +16 -0
- package/dist/ctx/create-ctx.js +269 -34
- package/dist/extensions/execution-realm.d.ts +96 -0
- package/dist/extensions/execution-realm.js +141 -0
- package/dist/extensions/extensions.module.js +3 -0
- package/dist/extensions/grant-epoch.d.ts +4 -2
- package/dist/extensions/grant-epoch.js +4 -2
- package/dist/extensions/host.d.ts +25 -2
- package/dist/extensions/host.js +120 -8
- package/dist/extensions/instance.d.ts +2 -0
- package/dist/extensions/instance.js +1 -0
- package/dist/extensions/isolated/admission.d.ts +6 -0
- package/dist/extensions/isolated/admission.js +17 -0
- package/dist/extensions/ledger.d.ts +5 -0
- package/dist/extensions/ledger.js +16 -0
- package/dist/extensions/permission-view.d.ts +2 -2
- package/dist/extensions/registry.d.ts +6 -0
- package/dist/extensions/registry.js +8 -0
- package/dist/extensions/review-hash.d.ts +114 -0
- package/dist/extensions/review-hash.js +145 -0
- package/dist/extensions/revision-grants.d.ts +10 -0
- package/dist/extensions/revocation-leases.d.ts +273 -0
- package/dist/extensions/revocation-leases.js +533 -0
- package/dist/extensions/revocation.d.ts +78 -0
- package/dist/extensions/revocation.js +202 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
- package/dist/lifecycle/kernel-lifecycle.js +24 -18
- package/dist/overrides.d.ts +5 -1
- package/dist/overrides.js +2 -0
- package/dist/packages/official-publisher.d.ts +39 -0
- package/dist/packages/official-publisher.js +125 -47
- package/package.json +2 -2
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { AGENTS_KEY, CONVERSATIONS_KEY, DEVICE_ACCESS_KEY_PREFIX, DEVICE_HOST_KEY_PREFIX, DEVICE_LOOPBACK_PORTS_KEY, DEVICE_SITE_ACCESS_KEY, DIAL_IN_KEY_PREFIX, INBOX_KEY, PEOPLE_KEY, POLICY_HOOK_KEY_PREFIX, SETUP_CHECK_KEY_PREFIX, TRIGGERS_KEY_PREFIX, UI_COMPONENT_KEY_PREFIX, UI_VIEW_KEY_PREFIX, } from "@capybara-hq/plugin-sdk";
|
|
3
|
+
import { GRANT_FULL_REVALIDATION_MS } from "./isolated/authorization.js";
|
|
4
|
+
import { EXECUTION_REALMS, effectiveKeys, fullAccessKey, } from "./revision-grants.js";
|
|
5
|
+
/**
|
|
6
|
+
* Revocation on a serving replica (extension permission model spec 2026-12-04, "Revocation, audit
|
|
7
|
+
* and people"; plan task 1.3e). The write side is {@link ExtensionRevocations} in `revocation.ts`;
|
|
8
|
+
* this file holds what the host and `ctx` need without depending on the Nest service: the key
|
|
9
|
+
* classes, the serving surface of a grant, and the replica-local permission leases.
|
|
10
|
+
*
|
|
11
|
+
* A lease belongs to one extension instance (one loaded generation of one package revision). It
|
|
12
|
+
* caches the revision's grant state, valid only at the grant epoch it was confirmed at and while
|
|
13
|
+
* that epoch is fresh: every committed grant change advances the epoch (the grant tables' triggers
|
|
14
|
+
* of migration 0235, on the 0234 epoch),
|
|
15
|
+
* so a replica that missed the fan-out hint still revalidates within the 5 s bound. Revalidations
|
|
16
|
+
* are coalesced, and a read started before a change can only confirm the epoch it started at.
|
|
17
|
+
*
|
|
18
|
+
* Shadow mode until plan task 1.5a (the plan's rollout fence): the revocation is written, audited and
|
|
19
|
+
* advances the epoch, and every lease computes its decision within the bound and reports it (logs,
|
|
20
|
+
* {@link RevisionLeases.statesOf}, the Partially revoked view), but nothing is refused, aborted,
|
|
21
|
+
* withdrawn or unloaded on account of it, and no call waits on a lease read: the legacy approval
|
|
22
|
+
* path keeps governing. Task 1.5a sets {@link REVOCATION_ENFORCED} and removes the shadow branch.
|
|
23
|
+
* When enforcing, a revision with no grant never refuses, and one with a grant fails closed while
|
|
24
|
+
* its state cannot be confirmed.
|
|
25
|
+
*/
|
|
26
|
+
/** The rollout fence: false until plan task 1.5a turns enforcement on and deletes this switch. */
|
|
27
|
+
export const REVOCATION_ENFORCED = false;
|
|
28
|
+
/**
|
|
29
|
+
* The kernel test seam that enforces revocation before task 1.5a (a kernel test's
|
|
30
|
+
* `KernelAppOptions.overrides`, value `true`), so the dual-replica tests exercise the enforcing
|
|
31
|
+
* branches. Task 1.5a removes it with {@link REVOCATION_ENFORCED}.
|
|
32
|
+
*/
|
|
33
|
+
export const REVOCATION_ENFORCEMENT = Symbol("REVOCATION_ENFORCEMENT");
|
|
34
|
+
/**
|
|
35
|
+
* A revision whose grant no longer lets it run where it would (whole grant revoked, or server full
|
|
36
|
+
* access revoked while the remaining grant still needs it, or no isolated form): it waits for a new
|
|
37
|
+
* review instead of failing (enforcing only).
|
|
38
|
+
*/
|
|
39
|
+
export class RevocationPendingError extends Error {
|
|
40
|
+
constructor(message) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = "RevocationPendingError";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const REGISTRATION_PREFIXES = [
|
|
46
|
+
"provides:",
|
|
47
|
+
"permissions.http",
|
|
48
|
+
"permissions.tools:",
|
|
49
|
+
"permissions.hooks:",
|
|
50
|
+
POLICY_HOOK_KEY_PREFIX,
|
|
51
|
+
UI_VIEW_KEY_PREFIX,
|
|
52
|
+
UI_COMPONENT_KEY_PREFIX,
|
|
53
|
+
"ui.query:",
|
|
54
|
+
"ui.action:",
|
|
55
|
+
SETUP_CHECK_KEY_PREFIX,
|
|
56
|
+
];
|
|
57
|
+
/** Capability keys `ctx` refuses (create-ctx.ts gates each of them). */
|
|
58
|
+
const CAPABILITY_KEYS = new Set([
|
|
59
|
+
"permissions.secrets",
|
|
60
|
+
"permissions.storage",
|
|
61
|
+
"permissions.kv",
|
|
62
|
+
"permissions.lock",
|
|
63
|
+
"permissions.bus",
|
|
64
|
+
"permissions.queue",
|
|
65
|
+
"permissions.outbox",
|
|
66
|
+
INBOX_KEY,
|
|
67
|
+
AGENTS_KEY,
|
|
68
|
+
CONVERSATIONS_KEY,
|
|
69
|
+
PEOPLE_KEY,
|
|
70
|
+
DEVICE_LOOPBACK_PORTS_KEY,
|
|
71
|
+
DEVICE_SITE_ACCESS_KEY,
|
|
72
|
+
]);
|
|
73
|
+
const CAPABILITY_PREFIXES = [
|
|
74
|
+
"permissions.models:",
|
|
75
|
+
DIAL_IN_KEY_PREFIX,
|
|
76
|
+
DEVICE_ACCESS_KEY_PREFIX,
|
|
77
|
+
DEVICE_HOST_KEY_PREFIX,
|
|
78
|
+
TRIGGERS_KEY_PREFIX,
|
|
79
|
+
];
|
|
80
|
+
export function classifyKey(key) {
|
|
81
|
+
for (const realm of EXECUTION_REALMS) {
|
|
82
|
+
if (key === fullAccessKey(realm))
|
|
83
|
+
return { kind: "full_access", realm };
|
|
84
|
+
}
|
|
85
|
+
if (REGISTRATION_PREFIXES.some((prefix) => key.startsWith(prefix))) {
|
|
86
|
+
return { kind: "registration" };
|
|
87
|
+
}
|
|
88
|
+
if (CAPABILITY_KEYS.has(key) || CAPABILITY_PREFIXES.some((prefix) => key.startsWith(prefix))) {
|
|
89
|
+
return { kind: "capability" };
|
|
90
|
+
}
|
|
91
|
+
return { kind: "unenforced" };
|
|
92
|
+
}
|
|
93
|
+
/** The keys a partial revocation cannot honour on its own (revoke the whole grant instead). */
|
|
94
|
+
export function unrevocableKeys(keys) {
|
|
95
|
+
return keys.filter((key) => classifyKey(key).kind === "unenforced");
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* What a full-access realm whose key no longer serves becomes: `isolated` when the remaining grant
|
|
99
|
+
* fits the sandbox, otherwise `pending_approval`. The remaining grant fits when none of the realm's
|
|
100
|
+
* forcing keys still applies: each was retired, or is a permission key that no longer serves (a
|
|
101
|
+
* host reason such as `host.no-isolation` is never a grant key, so it keeps the realm pending).
|
|
102
|
+
*/
|
|
103
|
+
export function realmAfterRevocation(grant, realm) {
|
|
104
|
+
if (grant.state === "revoked" || grant.state === "retired")
|
|
105
|
+
return "pending_approval";
|
|
106
|
+
const serving = new Set(effectiveKeys(grant));
|
|
107
|
+
const retired = new Set(grant.retiredForcing[realm] ?? []);
|
|
108
|
+
const inForce = (grant.realms[realm] ?? []).filter((key) => !retired.has(key) && (serving.has(key) || !grant.keys.includes(key)));
|
|
109
|
+
return inForce.length === 0 ? "isolated" : "pending_approval";
|
|
110
|
+
}
|
|
111
|
+
/** The realms a grant reviewed whose full access no longer serves. */
|
|
112
|
+
function revokedRealms(grant) {
|
|
113
|
+
const serving = new Set(grant.state === "revoked" ? [] : effectiveKeys(grant));
|
|
114
|
+
return EXECUTION_REALMS.filter((realm) => grant.realms[realm] !== undefined && !serving.has(fullAccessKey(realm)));
|
|
115
|
+
}
|
|
116
|
+
export function revocationView(grant) {
|
|
117
|
+
const serving = grant.state === "revoked" || grant.state === "retired" ? [] : effectiveKeys(grant);
|
|
118
|
+
const kept = new Set(serving);
|
|
119
|
+
const withdrawn = grant.keys.filter((key) => !kept.has(key));
|
|
120
|
+
const registrations = [];
|
|
121
|
+
const capabilities = [];
|
|
122
|
+
for (const key of withdrawn) {
|
|
123
|
+
const kind = classifyKey(key).kind;
|
|
124
|
+
if (kind === "registration")
|
|
125
|
+
registrations.push(key);
|
|
126
|
+
else if (kind === "capability")
|
|
127
|
+
capabilities.push(key);
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
state: grant.state,
|
|
131
|
+
generation: grant.generation,
|
|
132
|
+
withdrawn,
|
|
133
|
+
serving,
|
|
134
|
+
registrations,
|
|
135
|
+
capabilities,
|
|
136
|
+
fullAccess: revokedRealms(grant).map((realm) => ({
|
|
137
|
+
realm,
|
|
138
|
+
next: realmAfterRevocation(grant, realm),
|
|
139
|
+
})),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
export const NO_GRANT = { status: "no_grant", refused: new Set(), realmsRevoked: [] };
|
|
143
|
+
/**
|
|
144
|
+
* The lease state of a revision's newest grant generation. A retired grant is not a revocation
|
|
145
|
+
* (its revision was superseded; admission of a superseded revision belongs to the enforcing
|
|
146
|
+
* cutover), so it refuses nothing here.
|
|
147
|
+
*/
|
|
148
|
+
export function leaseStateOf(grant) {
|
|
149
|
+
if (!grant)
|
|
150
|
+
return NO_GRANT;
|
|
151
|
+
if (grant.state === "retired") {
|
|
152
|
+
return {
|
|
153
|
+
status: "retired",
|
|
154
|
+
generation: grant.generation,
|
|
155
|
+
refused: new Set(),
|
|
156
|
+
realmsRevoked: [],
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
const serving = new Set(grant.state === "revoked" ? [] : effectiveKeys(grant));
|
|
160
|
+
return {
|
|
161
|
+
status: grant.state,
|
|
162
|
+
generation: grant.generation,
|
|
163
|
+
refused: new Set(grant.keys.filter((key) => !serving.has(key))),
|
|
164
|
+
realmsRevoked: revokedRealms(grant),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
const hasGrant = (state) => state !== undefined && state.status !== "no_grant" && state.status !== "retired";
|
|
168
|
+
/**
|
|
169
|
+
* The newest grant generation of each revision stored under the given package sha256s (the
|
|
170
|
+
* package's sha512 names the revision): one query for all of them. A package without a recorded
|
|
171
|
+
* digest or a review has no entry. Grants and packages are deployment rows (no org_id), read
|
|
172
|
+
* outside any org scope.
|
|
173
|
+
*/
|
|
174
|
+
export async function readGrantSurfacesBySha256(db, sha256s) {
|
|
175
|
+
const out = new Map();
|
|
176
|
+
if (sha256s.length === 0)
|
|
177
|
+
return out;
|
|
178
|
+
const rows = await db
|
|
179
|
+
.selectFrom("extension_packages as p")
|
|
180
|
+
.innerJoin("extension_revision_grants as g", (join) => join
|
|
181
|
+
.onRef("g.plugin", "=", "p.name")
|
|
182
|
+
.onRef("g.version", "=", "p.version")
|
|
183
|
+
.onRef("g.sha512", "=", "p.sha512"))
|
|
184
|
+
.select([
|
|
185
|
+
"p.sha256",
|
|
186
|
+
"g.generation",
|
|
187
|
+
"g.keys",
|
|
188
|
+
"g.disabled_keys",
|
|
189
|
+
"g.retired_keys",
|
|
190
|
+
"g.realms",
|
|
191
|
+
"g.retired_forcing",
|
|
192
|
+
"g.state",
|
|
193
|
+
])
|
|
194
|
+
.where("p.sha256", "in", [...new Set(sha256s)])
|
|
195
|
+
.orderBy("g.generation", "desc")
|
|
196
|
+
.execute();
|
|
197
|
+
for (const row of rows) {
|
|
198
|
+
if (out.has(row.sha256))
|
|
199
|
+
continue;
|
|
200
|
+
out.set(row.sha256, {
|
|
201
|
+
generation: row.generation,
|
|
202
|
+
keys: row.keys,
|
|
203
|
+
disabledKeys: row.disabled_keys,
|
|
204
|
+
retiredKeys: row.retired_keys,
|
|
205
|
+
realms: row.realms,
|
|
206
|
+
retiredForcing: row.retired_forcing,
|
|
207
|
+
state: row.state,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
return out;
|
|
211
|
+
}
|
|
212
|
+
/** The newest grant generation of the revision stored under one package sha256, or undefined. */
|
|
213
|
+
export async function readGrantSurfaceBySha256(db, sha256) {
|
|
214
|
+
return (await readGrantSurfacesBySha256(db, [sha256])).get(sha256);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The enforcing decision for one key under a confirmed lease state: refused when the grant no
|
|
218
|
+
* longer serves it, when the whole grant is revoked, or when an in-process generation lost its
|
|
219
|
+
* server full access. Keys the grant never reviewed are not revocation's to refuse.
|
|
220
|
+
*/
|
|
221
|
+
export function leaseAllows(state, key, inProcess) {
|
|
222
|
+
if (inProcess && state.realmsRevoked.includes("server"))
|
|
223
|
+
return false;
|
|
224
|
+
return !state.refused.has(key);
|
|
225
|
+
}
|
|
226
|
+
const NEVER_ABORTED = new AbortController().signal;
|
|
227
|
+
/**
|
|
228
|
+
* The revision authority of one extension instance. {@link check} answers synchronously while the
|
|
229
|
+
* state is confirmed at the fresh epoch, and otherwise after one coalesced revalidation.
|
|
230
|
+
*/
|
|
231
|
+
export class PermissionLease {
|
|
232
|
+
owner;
|
|
233
|
+
plugin;
|
|
234
|
+
sha256;
|
|
235
|
+
inProcess;
|
|
236
|
+
state;
|
|
237
|
+
/** The epoch the state was confirmed at; undefined while unconfirmed. */
|
|
238
|
+
epoch;
|
|
239
|
+
checkedAt = Number.NEGATIVE_INFINITY;
|
|
240
|
+
/** The last revalidation failed (no epoch or no grant read): what is known is not confirmed. */
|
|
241
|
+
readFailed = false;
|
|
242
|
+
pending;
|
|
243
|
+
controllers = new Map();
|
|
244
|
+
/** Aborted when the whole lease is fenced: closes every handle that relied on it. */
|
|
245
|
+
fenced = new AbortController();
|
|
246
|
+
listeners = new Set();
|
|
247
|
+
/** Keys whose computed refusal was already reported in shadow mode. */
|
|
248
|
+
reported = new Set();
|
|
249
|
+
closed = false;
|
|
250
|
+
constructor(owner, plugin, sha256, inProcess) {
|
|
251
|
+
this.owner = owner;
|
|
252
|
+
this.plugin = plugin;
|
|
253
|
+
this.sha256 = sha256;
|
|
254
|
+
this.inProcess = inProcess;
|
|
255
|
+
}
|
|
256
|
+
/** Whether this replica enforces revocation (false until plan task 1.5a). */
|
|
257
|
+
get enforced() {
|
|
258
|
+
return this.owner.enforced;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Whether `key` may be used now. In shadow mode always true and never waiting: a stale lease
|
|
262
|
+
* refreshes in the background, and a computed refusal is reported once per key. When enforcing:
|
|
263
|
+
* {@link wouldAllow} after one coalesced revalidation when the state is not confirmed.
|
|
264
|
+
*/
|
|
265
|
+
check(key) {
|
|
266
|
+
const epoch = this.owner.epochs.fresh();
|
|
267
|
+
const confirmed = epoch !== undefined && this.valid(epoch);
|
|
268
|
+
if (!this.owner.enforced) {
|
|
269
|
+
if (!confirmed)
|
|
270
|
+
void this.revalidate();
|
|
271
|
+
if (!this.wouldAllow(key) && !this.reported.has(key)) {
|
|
272
|
+
this.reported.add(key);
|
|
273
|
+
this.owner.shadow(this, { refusedCall: key });
|
|
274
|
+
}
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
if (confirmed)
|
|
278
|
+
return this.wouldAllow(key);
|
|
279
|
+
if (this.state !== undefined && !hasGrant(this.state) && !this.readFailed) {
|
|
280
|
+
// no grant: nothing to revoke; never block a call on a read, refresh in the background
|
|
281
|
+
void this.revalidate();
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
return this.revalidate().then(() => this.wouldAllow(key));
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The computed decision from what is known: {@link leaseAllows} under the last state, and a
|
|
288
|
+
* refusal when the last read failed and the revision has a grant or was never confirmed (fail
|
|
289
|
+
* closed; a revision confirmed without a grant has nothing to revoke).
|
|
290
|
+
*/
|
|
291
|
+
wouldAllow(key) {
|
|
292
|
+
if (this.readFailed && (this.state === undefined || hasGrant(this.state)))
|
|
293
|
+
return false;
|
|
294
|
+
return this.state ? leaseAllows(this.state, key, this.inProcess) : true;
|
|
295
|
+
}
|
|
296
|
+
/** The keys refused by the last confirmed state. */
|
|
297
|
+
refusedKeys() {
|
|
298
|
+
return [...(this.state?.refused ?? [])];
|
|
299
|
+
}
|
|
300
|
+
/** The last confirmed state, if any. */
|
|
301
|
+
current() {
|
|
302
|
+
return this.state;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Aborted when `key` is refused or the lease is fenced (enforcing only): the handles that relied
|
|
306
|
+
* on it close. A key restored by a new grant gets a fresh signal.
|
|
307
|
+
*/
|
|
308
|
+
signal(key) {
|
|
309
|
+
if (!this.owner.enforced)
|
|
310
|
+
return NEVER_ABORTED;
|
|
311
|
+
let controller = this.controllers.get(key);
|
|
312
|
+
if (!controller) {
|
|
313
|
+
controller = new AbortController();
|
|
314
|
+
if (this.state && !leaseAllows(this.state, key, this.inProcess))
|
|
315
|
+
controller.abort();
|
|
316
|
+
this.controllers.set(key, controller);
|
|
317
|
+
}
|
|
318
|
+
return AbortSignal.any([controller.signal, this.fenced.signal]);
|
|
319
|
+
}
|
|
320
|
+
onRefused(listener) {
|
|
321
|
+
this.listeners.add(listener);
|
|
322
|
+
return () => this.listeners.delete(listener);
|
|
323
|
+
}
|
|
324
|
+
close() {
|
|
325
|
+
this.closed = true;
|
|
326
|
+
this.listeners.clear();
|
|
327
|
+
this.owner.forget(this);
|
|
328
|
+
}
|
|
329
|
+
/** Drops the confirmation (a hint or a fence): the next check reads the grant again. */
|
|
330
|
+
invalidate() {
|
|
331
|
+
this.epoch = undefined;
|
|
332
|
+
this.pending = undefined;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* The settle deadline or an epoch outage: a lease with a grant that is not confirmed at the
|
|
336
|
+
* current epoch is unconfirmed; when enforcing it also closes its handles, and the next check
|
|
337
|
+
* revalidates (failing closed if it cannot).
|
|
338
|
+
*/
|
|
339
|
+
fenceIfStale(epoch) {
|
|
340
|
+
if (!hasGrant(this.state) || (epoch !== undefined && this.valid(epoch)))
|
|
341
|
+
return;
|
|
342
|
+
this.invalidate();
|
|
343
|
+
if (!this.owner.enforced)
|
|
344
|
+
return;
|
|
345
|
+
this.fenced.abort();
|
|
346
|
+
this.fenced = new AbortController();
|
|
347
|
+
}
|
|
348
|
+
/** Revalidates alone, or with a read shared by every lease of a fence or epoch change. */
|
|
349
|
+
revalidate(shared) {
|
|
350
|
+
if (this.pending)
|
|
351
|
+
return this.pending;
|
|
352
|
+
// declared first: the read compares itself with `this.pending` after its awaits
|
|
353
|
+
let pending;
|
|
354
|
+
pending = (async () => {
|
|
355
|
+
const read = await (shared ?? this.owner.readOne(this.sha256));
|
|
356
|
+
if (this.pending !== pending) {
|
|
357
|
+
// A hint superseded this read: share the newer one.
|
|
358
|
+
return this.closed ? undefined : this.revalidate();
|
|
359
|
+
}
|
|
360
|
+
this.pending = undefined;
|
|
361
|
+
if (this.closed)
|
|
362
|
+
return undefined;
|
|
363
|
+
const next = read.epoch === undefined ? undefined : read.state(this.sha256);
|
|
364
|
+
if (next === undefined || read.epoch === undefined) {
|
|
365
|
+
this.readFailed = true;
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
368
|
+
this.readFailed = false;
|
|
369
|
+
this.apply(next, read.epoch, read.checkedAt);
|
|
370
|
+
return next;
|
|
371
|
+
})();
|
|
372
|
+
this.pending = pending;
|
|
373
|
+
return pending;
|
|
374
|
+
}
|
|
375
|
+
valid(epoch) {
|
|
376
|
+
return (this.pending === undefined &&
|
|
377
|
+
!this.readFailed &&
|
|
378
|
+
this.epoch === epoch &&
|
|
379
|
+
this.owner.now() - this.checkedAt < GRANT_FULL_REVALIDATION_MS);
|
|
380
|
+
}
|
|
381
|
+
apply(next, epoch, checkedAt) {
|
|
382
|
+
const previous = this.state;
|
|
383
|
+
this.state = next;
|
|
384
|
+
this.epoch = epoch;
|
|
385
|
+
this.checkedAt = checkedAt;
|
|
386
|
+
const added = [...next.refused].filter((key) => !previous?.refused.has(key));
|
|
387
|
+
const restored = [...(previous?.refused ?? [])].filter((key) => !next.refused.has(key));
|
|
388
|
+
const hadServer = previous?.realmsRevoked.includes("server") ?? false;
|
|
389
|
+
const lostServer = this.inProcess && next.realmsRevoked.includes("server") && !hadServer;
|
|
390
|
+
const regainedServer = this.inProcess && hadServer && !next.realmsRevoked.includes("server");
|
|
391
|
+
const revokedWhole = next.status === "revoked" && previous?.status !== "revoked";
|
|
392
|
+
if (!this.owner.enforced) {
|
|
393
|
+
// computed and reported only: nothing is aborted, withdrawn or unloaded before 1.5a
|
|
394
|
+
if (added.length > 0 || lostServer || revokedWhole) {
|
|
395
|
+
this.owner.shadow(this, {
|
|
396
|
+
refused: added,
|
|
397
|
+
lostServerFullAccess: lostServer,
|
|
398
|
+
revoked: revokedWhole,
|
|
399
|
+
state: next,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
// A key a new grant serves again gets a fresh controller (an aborted one would refuse it).
|
|
405
|
+
for (const key of restored)
|
|
406
|
+
this.controllers.delete(key);
|
|
407
|
+
if (regainedServer)
|
|
408
|
+
this.controllers.clear();
|
|
409
|
+
if (lostServer || revokedWhole) {
|
|
410
|
+
for (const controller of this.controllers.values())
|
|
411
|
+
controller.abort();
|
|
412
|
+
this.fenced.abort();
|
|
413
|
+
this.fenced = new AbortController();
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
for (const key of added)
|
|
417
|
+
this.controllers.get(key)?.abort();
|
|
418
|
+
}
|
|
419
|
+
if (added.length > 0) {
|
|
420
|
+
for (const listener of [...this.listeners]) {
|
|
421
|
+
try {
|
|
422
|
+
listener(added, next);
|
|
423
|
+
}
|
|
424
|
+
catch (err) {
|
|
425
|
+
this.owner.fault(this, err);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (lostServer || revokedWhole)
|
|
430
|
+
this.owner.mustUnload(this);
|
|
431
|
+
else if (restored.length > 0 || regainedServer)
|
|
432
|
+
this.owner.restored(this);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/** The leases of every instance on this replica (one per instance with a package). */
|
|
436
|
+
export class RevisionLeases {
|
|
437
|
+
options;
|
|
438
|
+
leases = new Set();
|
|
439
|
+
epochs;
|
|
440
|
+
now;
|
|
441
|
+
enforced;
|
|
442
|
+
/** Callbacks run in the context the leases were built in, never a caller's org scope. */
|
|
443
|
+
inOwnContext = AsyncLocalStorage.snapshot();
|
|
444
|
+
constructor(options) {
|
|
445
|
+
this.options = options;
|
|
446
|
+
this.epochs = options.epochs;
|
|
447
|
+
this.now = options.now ?? (() => performance.now());
|
|
448
|
+
this.enforced = options.enforce ?? REVOCATION_ENFORCED;
|
|
449
|
+
}
|
|
450
|
+
/** Opens the lease of an instance and starts confirming it; undefined without a package. */
|
|
451
|
+
open({ plugin, sha256, inProcess }) {
|
|
452
|
+
if (sha256 === null)
|
|
453
|
+
return undefined;
|
|
454
|
+
this.epochs.start();
|
|
455
|
+
const lease = new PermissionLease(this, plugin, sha256, inProcess);
|
|
456
|
+
this.leases.add(lease);
|
|
457
|
+
void this.inOwnContext(() => lease.revalidate());
|
|
458
|
+
return lease;
|
|
459
|
+
}
|
|
460
|
+
/** The confirmed states of a plugin's leases on this replica (the computed decisions). */
|
|
461
|
+
statesOf(plugin) {
|
|
462
|
+
return [...this.leases]
|
|
463
|
+
.filter((lease) => lease.plugin === plugin)
|
|
464
|
+
.flatMap((lease) => {
|
|
465
|
+
const state = lease.current();
|
|
466
|
+
return state ? [state] : [];
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
/** The local fence after a commit, or a fan-out hint: revalidate the plugin's leases now. */
|
|
470
|
+
async fence(plugin) {
|
|
471
|
+
await this.revalidateAll([...this.leases].filter((l) => plugin === undefined || l.plugin === plugin));
|
|
472
|
+
}
|
|
473
|
+
/** The grant epoch advanced: every lease may be stale; revalidate them all with one read. */
|
|
474
|
+
async epochChanged() {
|
|
475
|
+
await this.revalidateAll([...this.leases]);
|
|
476
|
+
}
|
|
477
|
+
/** The settle deadline after an epoch change: close what was not confirmed at the new epoch. */
|
|
478
|
+
fenceStale() {
|
|
479
|
+
const epoch = this.epochs.fresh();
|
|
480
|
+
for (const lease of this.leases)
|
|
481
|
+
lease.fenceIfStale(epoch);
|
|
482
|
+
}
|
|
483
|
+
/** No epoch read succeeded for the whole bound: close every lease that has a grant. */
|
|
484
|
+
fenceAll() {
|
|
485
|
+
for (const lease of this.leases)
|
|
486
|
+
lease.fenceIfStale(undefined);
|
|
487
|
+
}
|
|
488
|
+
/** One lease's read (its first confirmation, or a check that found it stale). */
|
|
489
|
+
readOne(sha256) {
|
|
490
|
+
return this.readStates([sha256]);
|
|
491
|
+
}
|
|
492
|
+
forget(lease) {
|
|
493
|
+
this.leases.delete(lease);
|
|
494
|
+
}
|
|
495
|
+
mustUnload(lease) {
|
|
496
|
+
this.inOwnContext(() => this.options.onUnload(lease.plugin));
|
|
497
|
+
}
|
|
498
|
+
restored(lease) {
|
|
499
|
+
this.inOwnContext(() => this.options.onRestored(lease.plugin));
|
|
500
|
+
}
|
|
501
|
+
shadow(lease, decision) {
|
|
502
|
+
this.options.onShadow?.(lease.plugin, decision);
|
|
503
|
+
}
|
|
504
|
+
fault(lease, err) {
|
|
505
|
+
this.options.onFault?.(lease.plugin, err);
|
|
506
|
+
}
|
|
507
|
+
async revalidateAll(leases) {
|
|
508
|
+
if (leases.length === 0)
|
|
509
|
+
return;
|
|
510
|
+
for (const lease of leases)
|
|
511
|
+
lease.invalidate();
|
|
512
|
+
const shared = this.readStates(leases.map((lease) => lease.sha256));
|
|
513
|
+
await Promise.all(leases.map((lease) => this.inOwnContext(() => lease.revalidate(shared))));
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* The epoch is captured before the grants are read: a revocation committed after the read's
|
|
517
|
+
* snapshot advances the epoch past this value, so the result never outlives it.
|
|
518
|
+
*/
|
|
519
|
+
async readStates(sha256s) {
|
|
520
|
+
const epoch = await this.epochs.ensureFresh();
|
|
521
|
+
const checkedAt = this.now();
|
|
522
|
+
if (epoch === undefined)
|
|
523
|
+
return { epoch, checkedAt, state: () => undefined };
|
|
524
|
+
try {
|
|
525
|
+
const states = await this.inOwnContext(() => this.options.read(sha256s));
|
|
526
|
+
return { epoch, checkedAt, state: (sha256) => states.get(sha256) ?? NO_GRANT };
|
|
527
|
+
}
|
|
528
|
+
catch (err) {
|
|
529
|
+
this.options.onFault?.("*", err);
|
|
530
|
+
return { epoch: undefined, checkedAt, state: () => undefined };
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
3
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
4
|
+
import type { KernelLogger } from "../log.js";
|
|
5
|
+
import { ExtensionHost } from "./host.js";
|
|
6
|
+
import { ExtensionRevisionGrants, type RevisionGrant, type RevisionIdentity } from "./revision-grants.js";
|
|
7
|
+
import { type RevocationView } from "./revocation-leases.js";
|
|
8
|
+
export { classifyKey, type KeyClass, type LeaseState, PermissionLease, RevisionLeases, type RevocationView, realmAfterRevocation, revocationView, } from "./revocation-leases.js";
|
|
9
|
+
/**
|
|
10
|
+
* Revoking revision grants (extension permission model spec 2026-12-04, "Revocation, audit and
|
|
11
|
+
* people"; plan task 1.3e).
|
|
12
|
+
*
|
|
13
|
+
* The grant state and its audit change in one transaction; the store's write advances the grant
|
|
14
|
+
* epoch in the same transaction (the grant tables' triggers of migration 0235). After the commit
|
|
15
|
+
* this replica fences
|
|
16
|
+
* its leases of the plugin at once (the local fence) and publishes {@link GRANT_REVOKED_TOPIC} to
|
|
17
|
+
* the others. The hint is only the fast path: a replica that misses it sees the epoch change and
|
|
18
|
+
* revalidates its leases within the 5 s bound (`grant-epoch.ts`).
|
|
19
|
+
*
|
|
20
|
+
* Shadow mode until plan task 1.5a (`REVOCATION_ENFORCED`): the write, audit, epoch bump, fence and
|
|
21
|
+
* hint are real, and the leases compute and report the decision (logs, the Partially revoked view),
|
|
22
|
+
* but nothing below is enforced yet. Once enforcing, on each replica the leases:
|
|
23
|
+
* - withdraw the registrations of revoked registration keys (routes, tools, providers, hooks,
|
|
24
|
+
* views, setup checks); a revoked policy hook stays registered and denies, so a required
|
|
25
|
+
* policy mount fails closed until the admin disables the extension;
|
|
26
|
+
* - refuse `ctx` operations of revoked capability keys with `ExtensionPermissionError` and abort
|
|
27
|
+
* the handles that relied on them; unaffected keys keep serving;
|
|
28
|
+
* - for revoked server full access, refuse the in-process generation's `ctx` and converge: the
|
|
29
|
+
* generation is unloaded, and an isolated one starts when the remaining grant fits the sandbox
|
|
30
|
+
* (otherwise it waits for a new review).
|
|
31
|
+
*
|
|
32
|
+
* Plugin data stays; durable effects keep their delivery-time authority checks.
|
|
33
|
+
*/
|
|
34
|
+
/** The fan-out hint after a committed revocation: `{ plugin }`. Losing it is harmless. */
|
|
35
|
+
export declare const GRANT_REVOKED_TOPIC = "kernel.extensions.grant-revoked";
|
|
36
|
+
export interface RevocationActor {
|
|
37
|
+
/** The acting account; null for the kernel or an operator command. */
|
|
38
|
+
accountId: string | null;
|
|
39
|
+
tokenId?: string;
|
|
40
|
+
reason?: string;
|
|
41
|
+
}
|
|
42
|
+
export interface RevocationOutcome {
|
|
43
|
+
grant: RevisionGrant;
|
|
44
|
+
/** Keys withdrawn by this revocation (not already revoked or retired before). */
|
|
45
|
+
withdrawn: string[];
|
|
46
|
+
view: RevocationView;
|
|
47
|
+
}
|
|
48
|
+
export declare class ExtensionRevocations {
|
|
49
|
+
private readonly kdb;
|
|
50
|
+
readonly txHost: TransactionHost;
|
|
51
|
+
private readonly grants;
|
|
52
|
+
private readonly host;
|
|
53
|
+
private readonly bus;
|
|
54
|
+
private readonly log;
|
|
55
|
+
private unsubscribe;
|
|
56
|
+
constructor(kdb: KernelDb, txHost: TransactionHost, grants: ExtensionRevisionGrants, host: Pick<ExtensionHost, "fenceRevocation">, bus: Pick<ExtensionContext["bus"], "publish" | "subscribe">, log: KernelLogger);
|
|
57
|
+
/**
|
|
58
|
+
* Every replica takes the hints of the others (and its own, a no-op after the local fence).
|
|
59
|
+
* Called by the kernel's start sequence (never in the CLI, which loads no extension).
|
|
60
|
+
*/
|
|
61
|
+
start(): Promise<void>;
|
|
62
|
+
stop(): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Revokes keys of a revision's live grant (**Partially revoked** while others still serve). A
|
|
65
|
+
* key no running surface can withdraw on its own (network, config, tables, embeds, dependencies,
|
|
66
|
+
* client code, raw egress, migration warnings) is refused (`unknown_keys`): the Partially revoked
|
|
67
|
+
* view never shows a still-serving key as withdrawn. Revoke the whole grant to take those away.
|
|
68
|
+
*/
|
|
69
|
+
revokeKeys(revision: RevisionIdentity, keys: readonly string[], by: RevocationActor): Promise<RevocationOutcome>;
|
|
70
|
+
/** Revokes a revision's live grant entirely: the revision is unloaded and waits for a new review. */
|
|
71
|
+
revoke(revision: RevisionIdentity, by: RevocationActor): Promise<RevocationOutcome>;
|
|
72
|
+
/** The newest grant of a revision with its withdrawn keys and serving surface. */
|
|
73
|
+
view(revision: RevisionIdentity): Promise<RevocationView | undefined>;
|
|
74
|
+
/** The live grant, locked for this transaction so the audit diff matches the write. */
|
|
75
|
+
private lockLive;
|
|
76
|
+
private record;
|
|
77
|
+
private fence;
|
|
78
|
+
}
|