@substrat-run/kernel 0.127.0 → 0.129.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/invocation-log.d.ts +58 -13
- package/dist/invocation-log.d.ts.map +1 -1
- package/dist/invocation-log.js +27 -21
- package/dist/invocation-log.js.map +1 -1
- package/dist/lifecycle-flow.d.ts +41 -0
- package/dist/lifecycle-flow.d.ts.map +1 -0
- package/dist/lifecycle-flow.js +358 -0
- package/dist/lifecycle-flow.js.map +1 -0
- package/dist/platform-sweep.d.ts +12 -1
- package/dist/platform-sweep.d.ts.map +1 -1
- package/dist/platform-sweep.js +13 -3
- package/dist/platform-sweep.js.map +1 -1
- package/dist/scope-host.d.ts +17 -1
- package/dist/scope-host.d.ts.map +1 -1
- package/dist/scope-host.js.map +1 -1
- package/dist/scope-repoint.d.ts +33 -3
- package/dist/scope-repoint.d.ts.map +1 -1
- package/dist/scope-repoint.js +79 -6
- package/dist/scope-repoint.js.map +1 -1
- package/dist/scope-tuple-seat.d.ts +6 -2
- package/dist/scope-tuple-seat.d.ts.map +1 -1
- package/dist/scope-tuple-seat.js +10 -4
- package/dist/scope-tuple-seat.js.map +1 -1
- package/dist/spine-guard.d.ts +12 -0
- package/dist/spine-guard.d.ts.map +1 -1
- package/dist/spine-guard.js +32 -125
- package/dist/spine-guard.js.map +1 -1
- package/dist/spine-restore.d.ts +87 -0
- package/dist/spine-restore.d.ts.map +1 -0
- package/dist/spine-restore.js +142 -0
- package/dist/spine-restore.js.map +1 -0
- package/dist/system-switch-record.d.ts +8 -0
- package/dist/system-switch-record.d.ts.map +1 -1
- package/dist/system-switch-record.js +11 -1
- package/dist/system-switch-record.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scope-repoint.d.ts","sourceRoot":"","sources":["../src/scope-repoint.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"scope-repoint.d.ts","sourceRoot":"","sources":["../src/scope-repoint.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEpD,0FAA0F;AAC1F,MAAM,WAAW,aAAa;IAC5B,4CAA4C;IAC5C,OAAO,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgEG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,SAAS,EACd,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,aAAa,GAAG,SAAS;AACjC,sFAAsF;AACtF,GAAG,EAAE,MAAM,GACV,IAAI,CA0BN"}
|
package/dist/scope-repoint.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { scopeId, substratError } from '@substrat-run/contracts';
|
|
2
|
+
import { liveTupleSql } from './permission-eval.js';
|
|
2
3
|
/**
|
|
3
4
|
* Re-point a dump's scope-level grants at the scope it lands in, the step every restore, fork
|
|
4
5
|
* and preview carry ends with (#1869). One definition for both adapters' `importDump`, so the
|
|
@@ -33,19 +34,48 @@ import { scopeId, substratError } from '@substrat-run/contracts';
|
|
|
33
34
|
* `WHERE`: the update rewrites the very rows the probe looks for, and the answer must not
|
|
34
35
|
* change part-way through.
|
|
35
36
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
37
|
+
* **Which row wins a collision (#1882).** `(subject, relation, object)` is the primary key,
|
|
38
|
+
* so a moved row can land on a row the dump already holds for the destination. That used to
|
|
39
|
+
* be `UPDATE OR REPLACE`, so the moved row always won, `expires_at` and `revoked_at` with it:
|
|
40
|
+
* a revoked or expired source row could replace a live grant on the destination. Now each
|
|
41
|
+
* row is ranked, and the higher rank is kept:
|
|
42
|
+
*
|
|
43
|
+
* 1. **live**: not revoked, and not expired at `now`;
|
|
44
|
+
* 2. **revoked**: a K-21 tombstone, the evidence of a revocation (#1659's reconcile never
|
|
45
|
+
* re-seats a grant it finds tombstoned, so losing one would un-revoke it);
|
|
46
|
+
* 3. **expired**: not revoked, but past its `expires_at`, which is evidence of nothing.
|
|
47
|
+
*
|
|
48
|
+
* | moved row \ destination row | live | revoked | expired |
|
|
49
|
+
* |-----------------------------|-------------|-------------|-------------|
|
|
50
|
+
* | live | destination | moved | moved |
|
|
51
|
+
* | revoked | destination | destination | moved |
|
|
52
|
+
* | expired | destination | destination | destination |
|
|
53
|
+
*
|
|
54
|
+
* On a tie the destination row stays. Two moved rows that would land on the same key (only
|
|
55
|
+
* the fallback moves more than one object) are settled by the same rank, and on a tie by the
|
|
56
|
+
* lower `object`, before either meets the destination. Whichever live row is kept takes the
|
|
57
|
+
* EARLIEST expiry among the live rows for its key (no expiry counting as the latest), so a
|
|
58
|
+
* restore never widens a grant's life, whichever side it came from. The loser is deleted: it cannot stay under its old object, because a row naming another scope
|
|
59
|
+
* is exactly what a later restore of this scope refuses. Everything runs inside the load's
|
|
60
|
+
* transaction, and the final `UPDATE` is a plain one, so a collision this missed fails the
|
|
61
|
+
* restore instead of replacing a row.
|
|
62
|
+
*
|
|
63
|
+
* `COLLATE BINARY` is written out on every comparison. Since #1883 the tuples table is always
|
|
64
|
+
* built from the kernel's DDL, never the dump's, so its columns are BINARY already; the
|
|
65
|
+
* collation stays as a second guard, so this function never depends on the table it is
|
|
66
|
+
* handed having the right one.
|
|
38
67
|
*/
|
|
39
|
-
export function repointScopeGrants(sql, destScopeId, source
|
|
68
|
+
export function repointScopeGrants(sql, destScopeId, source,
|
|
69
|
+
/** The instant `expires_at` is judged against, as the adapter's checker judges it. */
|
|
70
|
+
now) {
|
|
40
71
|
// `exact` is a claim about a named source; with none named it would silently take the fallback.
|
|
41
72
|
if (source !== undefined && !source.scopeId) {
|
|
42
73
|
throw substratError('validation_failed', 'restore refused: `exact` needs the scope the dump came from');
|
|
43
74
|
}
|
|
44
75
|
const dest = `scope:${destScopeId}`;
|
|
45
|
-
const update = 'UPDATE OR REPLACE _substrat_tuples SET object = ? WHERE object <> ? COLLATE BINARY AND';
|
|
46
76
|
const from = source === undefined ? undefined : `scope:${source.scopeId}`;
|
|
47
77
|
if (from === undefined || (!source?.exact && sql.all(SOURCE_PROBE, from).length === 0)) {
|
|
48
|
-
sql
|
|
78
|
+
moveOnto(sql, dest, now, undefined);
|
|
49
79
|
return;
|
|
50
80
|
}
|
|
51
81
|
const strays = source.exact
|
|
@@ -59,7 +89,50 @@ export function repointScopeGrants(sql, destScopeId, source) {
|
|
|
59
89
|
`(${source.scopeId}) and this scope (${destScopeId}), which a restore cannot re-point: ` +
|
|
60
90
|
`${strays.slice(0, 3).join(', ')}${strays.length > 3 ? ', …' : ''}`);
|
|
61
91
|
}
|
|
62
|
-
sql
|
|
92
|
+
moveOnto(sql, dest, now, from);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Move onto `dest` every row naming `from` (the exact rule), or every `scope:` row when `from`
|
|
96
|
+
* is undefined (the fallback), settling each key collision by the rule in
|
|
97
|
+
* `repointScopeGrants`'s header. A row already on `dest` is never moved.
|
|
98
|
+
*/
|
|
99
|
+
function moveOnto(sql, dest, now, from) {
|
|
100
|
+
// Whether the row under `alias` moves. Binds `moved` (below), in that order.
|
|
101
|
+
const isMoved = (alias) => `(${from === undefined ? `substr(${alias}.object, 1, 6) = 'scope:'` : `${alias}.object = ?`} COLLATE BINARY ` +
|
|
102
|
+
`AND ${alias}.object <> ? COLLATE BINARY)`;
|
|
103
|
+
const moved = from === undefined ? [dest] : [from, dest];
|
|
104
|
+
// The rank in the header: 2 live (the checker's own definition, so a restore keeps the row a
|
|
105
|
+
// check would honour), 1 revoked, 0 expired. Binds `now`.
|
|
106
|
+
const rank = (alias) => `(CASE WHEN ${liveTupleSql(alias)} THEN 2 WHEN ${alias}.revoked_at IS NOT NULL THEN 1 ELSE 0 END)`;
|
|
107
|
+
// A row competing for `dest`: moved there, or already on it. Binds `moved`, then `dest`.
|
|
108
|
+
const competes = (alias) => `(${isMoved(alias)} OR ${alias}.object = ? COLLATE BINARY)`;
|
|
109
|
+
const competing = [...moved, dest];
|
|
110
|
+
const live = (alias) => `(${liveTupleSql(alias)})`;
|
|
111
|
+
const sameKey = (a, b) => `${a}.subject = ${b}.subject COLLATE BINARY AND ${a}.relation = ${b}.relation COLLATE BINARY`;
|
|
112
|
+
// `_substrat_tuples` is the row being judged; SQLite takes no alias on a DELETE's target.
|
|
113
|
+
const T = '_substrat_tuples';
|
|
114
|
+
// 0. Every live row that meets another live one, carrying an earlier expiry, for its key
|
|
115
|
+
// takes the earliest, so the row kept below never outlives any of them. MIN skips NULL,
|
|
116
|
+
// which is "no expiry", the latest.
|
|
117
|
+
sql.run(`UPDATE ${T} SET expires_at = (
|
|
118
|
+
SELECT MIN(o.expires_at) FROM ${T} o WHERE ${sameKey('o', T)} AND ${competes('o')} AND ${live('o')})
|
|
119
|
+
WHERE ${live(T)} AND ${competes(T)} AND EXISTS (
|
|
120
|
+
SELECT 1 FROM ${T} o WHERE ${sameKey('o', T)} AND o.object <> ${T}.object COLLATE BINARY
|
|
121
|
+
AND ${competes('o')} AND ${live('o')} AND o.expires_at IS NOT NULL
|
|
122
|
+
AND (${T}.expires_at IS NULL OR o.expires_at < ${T}.expires_at))`, ...competing, now, now, ...competing, ...competing, now);
|
|
123
|
+
// 1. Between two moved rows for one key: the higher rank, then the lower object.
|
|
124
|
+
sql.run(`DELETE FROM ${T} WHERE ${isMoved(T)} AND EXISTS (
|
|
125
|
+
SELECT 1 FROM ${T} o WHERE ${sameKey('o', T)} AND ${isMoved('o')} AND o.object <> ${T}.object COLLATE BINARY
|
|
126
|
+
AND (${rank('o')} > ${rank(T)} OR (${rank('o')} = ${rank(T)} AND o.object < ${T}.object COLLATE BINARY)))`, ...moved, ...moved, now, now, now, now);
|
|
127
|
+
// 2. A moved row loses to the destination row unless it ranks higher.
|
|
128
|
+
sql.run(`DELETE FROM ${T} WHERE ${isMoved(T)} AND EXISTS (
|
|
129
|
+
SELECT 1 FROM ${T} d WHERE ${sameKey('d', T)} AND d.object = ? COLLATE BINARY
|
|
130
|
+
AND ${rank('d')} >= ${rank(T)})`, ...moved, dest, now, now);
|
|
131
|
+
// 3. Every moved row still standing beat the destination row for its key, which goes.
|
|
132
|
+
sql.run(`DELETE FROM ${T} WHERE object = ? COLLATE BINARY AND EXISTS (
|
|
133
|
+
SELECT 1 FROM ${T} o WHERE ${sameKey('o', T)} AND ${isMoved('o')})`, dest, ...moved);
|
|
134
|
+
// 4. No key collides now. A plain UPDATE, so one that still did would fail the restore.
|
|
135
|
+
sql.run(`UPDATE ${T} SET object = ? WHERE ${isMoved(T)}`, dest, ...moved);
|
|
63
136
|
}
|
|
64
137
|
const SOURCE_PROBE = 'SELECT 1 FROM _substrat_tuples WHERE object = ? COLLATE BINARY LIMIT 1';
|
|
65
138
|
// Live rows only: a revoked grant (K-21 tombstone) on a third scope authorizes nothing anywhere.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scope-repoint.js","sourceRoot":"","sources":["../src/scope-repoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"scope-repoint.js","sourceRoot":"","sources":["../src/scope-repoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAepD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgEG;AACH,MAAM,UAAU,kBAAkB,CAChC,GAAc,EACd,WAAmB,EACnB,MAAiC;AACjC,sFAAsF;AACtF,GAAW;IAEX,gGAAg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G,CACJ,CAAC;IACF,sFAAsF;IACtF,GAAG,CAAC,GAAG,CACL,eAAe,CAAC;uBACG,CAAC,YAAY,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,QAAQ,OAAO,CAAC,GAAG,CAAC,GAAG,EACtE,IAAI,EACJ,GAAG,KAAK,CACT,CAAC;IACF,wFAAwF;IACxF,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,yBAAyB,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,CAAC;AAC5E,CAAC;AAED,MAAM,YAAY,GAAG,wEAAwE,CAAC;AAC9F,iGAAiG;AACjG,MAAM,YAAY,GAAG;;;kBAGH,CAAC"}
|
|
@@ -64,9 +64,13 @@ export declare function seatScopeTuple(subject: string, relation: string, object
|
|
|
64
64
|
* So: a non-revoked, unexpired role tuple JOINED to a non-revoked definition of the same
|
|
65
65
|
* key for this tenant. Expiry is compared as ISO text, as every tuple walk here does.
|
|
66
66
|
* Returns one row, `effective` 0 or 1.
|
|
67
|
+
*
|
|
68
|
+
* `subject` narrows the same question to one holder (#1665): does THIS principal hold a role
|
|
69
|
+
* the scope can expand? An owner hand-over asks it of the successor, so the scope is not
|
|
70
|
+
* handed to a principal whose member role was taken back.
|
|
67
71
|
*/
|
|
68
|
-
export declare function effectiveRoleGrantQuery(tenantId: string, now: string): {
|
|
72
|
+
export declare function effectiveRoleGrantQuery(tenantId: string, now: string, subject?: string): {
|
|
69
73
|
sql: string;
|
|
70
|
-
params: [
|
|
74
|
+
params: string[];
|
|
71
75
|
};
|
|
72
76
|
//# sourceMappingURL=scope-tuple-seat.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scope-tuple-seat.d.ts","sourceRoot":"","sources":["../src/scope-tuple-seat.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAAG,IAAI,GACvB;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC,CAAA;CAAE,CAQ1E;AAED
|
|
1
|
+
{"version":3,"file":"scope-tuple-seat.d.ts","sourceRoot":"","sources":["../src/scope-tuple-seat.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAAG,IAAI,GACvB;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC,CAAA;CAAE,CAQ1E;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,MAAM,GACf;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAE,CAoBnC"}
|
package/dist/scope-tuple-seat.js
CHANGED
|
@@ -70,24 +70,30 @@ export function seatScopeTuple(subject, relation, object, expiresAt) {
|
|
|
70
70
|
* So: a non-revoked, unexpired role tuple JOINED to a non-revoked definition of the same
|
|
71
71
|
* key for this tenant. Expiry is compared as ISO text, as every tuple walk here does.
|
|
72
72
|
* Returns one row, `effective` 0 or 1.
|
|
73
|
+
*
|
|
74
|
+
* `subject` narrows the same question to one holder (#1665): does THIS principal hold a role
|
|
75
|
+
* the scope can expand? An owner hand-over asks it of the successor, so the scope is not
|
|
76
|
+
* handed to a principal whose member role was taken back.
|
|
73
77
|
*/
|
|
74
|
-
export function effectiveRoleGrantQuery(tenantId, now) {
|
|
78
|
+
export function effectiveRoleGrantQuery(tenantId, now, subject) {
|
|
79
|
+
const only = subject === undefined ? '' : ' AND subject = ?';
|
|
80
|
+
const who = subject === undefined ? [] : [subject];
|
|
75
81
|
return {
|
|
76
82
|
sql: `SELECT (
|
|
77
83
|
EXISTS (
|
|
78
84
|
SELECT 1 FROM _substrat_tuples t
|
|
79
85
|
JOIN _substrat_roles r
|
|
80
86
|
ON r.tenant_id = ? AND r.revoked_at IS NULL AND t.relation = 'role:' || r.role_key
|
|
81
|
-
WHERE t.revoked_at IS NULL AND (t.expires_at IS NULL OR t.expires_at > ?)
|
|
87
|
+
WHERE t.revoked_at IS NULL AND (t.expires_at IS NULL OR t.expires_at > ?)${only.replace('subject', 't.subject')}
|
|
82
88
|
)
|
|
83
89
|
OR EXISTS (
|
|
84
90
|
SELECT 1 FROM _substrat_tenant_tuples tt
|
|
85
91
|
JOIN _substrat_roles r
|
|
86
92
|
ON r.tenant_id = tt.tenant_id AND r.revoked_at IS NULL AND tt.relation = 'role:' || r.role_key
|
|
87
|
-
WHERE tt.tenant_id = ? AND tt.revoked_at IS NULL AND (tt.expires_at IS NULL OR tt.expires_at > ?)
|
|
93
|
+
WHERE tt.tenant_id = ? AND tt.revoked_at IS NULL AND (tt.expires_at IS NULL OR tt.expires_at > ?)${only.replace('subject', 'tt.subject')}
|
|
88
94
|
)
|
|
89
95
|
) AS effective`,
|
|
90
|
-
params: [tenantId, now, tenantId, now],
|
|
96
|
+
params: [tenantId, now, ...who, tenantId, now, ...who],
|
|
91
97
|
};
|
|
92
98
|
}
|
|
93
99
|
//# sourceMappingURL=scope-tuple-seat.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scope-tuple-seat.js","sourceRoot":"","sources":["../src/scope-tuple-seat.ts"],"names":[],"mappings":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O,CAAC,SAAS,EAAE,YAAY,CAAC;;qBAE9H;QACjB,MAAM,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,GAAG,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC;KACvD,CAAC;AACJ,CAAC"}
|
package/dist/spine-guard.d.ts
CHANGED
|
@@ -7,6 +7,18 @@ import type { ScopedSql } from './scope-host.js';
|
|
|
7
7
|
* the table so the author sees which line to delete.
|
|
8
8
|
*/
|
|
9
9
|
export declare function assertNoSpineWrite(sql: string): void;
|
|
10
|
+
/**
|
|
11
|
+
* Refuse a statement whose `REFERENCES` clause names a `_substrat_*` table (#1898), for the
|
|
12
|
+
* same reason `CREATE TRIGGER … ON` is refused above: the spine's rows become the parent of a
|
|
13
|
+
* module's, and with foreign keys enforced the kernel's own writes to that table (a revoke, a
|
|
14
|
+
* restore's re-point, an outbox prune) then fail on the module's rows. That denies the spine
|
|
15
|
+
* rather than forging it. `what` names the SQL's source in the message.
|
|
16
|
+
*
|
|
17
|
+
* `ctx.sql` passes through here (`assertNoSpineWrite`), and so does every migration a scope
|
|
18
|
+
* applies, which reaches the database on the kernel's own handle rather than through
|
|
19
|
+
* `ctx.sql`. A dump's replayed DDL is held to the same rule by `assertReplayableDump`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function assertNoSpineReference(sql: string, what: string): void;
|
|
10
22
|
/**
|
|
11
23
|
* Wrap a module-facing `ScopedSql` so every statement passes `assertNoSpineWrite`
|
|
12
24
|
* first. `query` is guarded too: SQLite runs `INSERT … RETURNING` perfectly well
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spine-guard.d.ts","sourceRoot":"","sources":["../src/spine-guard.ts"],"names":[],"mappings":"AAqCA,OAAO,KAAK,EAAE,SAAS,EAAY,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"spine-guard.d.ts","sourceRoot":"","sources":["../src/spine-guard.ts"],"names":[],"mappings":"AAqCA,OAAO,KAAK,EAAE,SAAS,EAAY,MAAM,iBAAiB,CAAC;AAsD3D;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAsBpD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAEtE;AAcD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,GAAG,SAAS,CAWtD"}
|
package/dist/spine-guard.js
CHANGED
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
* What is refused: a statement whose write TARGET is a `_substrat_*` table —
|
|
19
19
|
* `INSERT`/`REPLACE INTO`, `UPDATE`, `DELETE FROM`, `DROP`, `ALTER`, `CREATE` — and
|
|
20
20
|
* the second table a statement can reach past the one it names first: `CREATE
|
|
21
|
-
* TRIGGER … ON`, `CREATE INDEX … ON`, `ALTER TABLE … RENAME TO
|
|
21
|
+
* TRIGGER … ON`, `CREATE INDEX … ON`, `ALTER TABLE … RENAME TO`, and a `REFERENCES` clause
|
|
22
|
+
* naming the spine (`assertNoSpineReference`, #1898).
|
|
22
23
|
* What is allowed, deliberately: every read of the spine, including one that feeds
|
|
23
24
|
* a write — `INSERT INTO my_timeline SELECT … FROM _substrat_events` is the
|
|
24
25
|
* projection pattern CLAUDE.md explicitly blesses, and only the target is judged.
|
|
@@ -27,16 +28,13 @@
|
|
|
27
28
|
* runtime refuses them outright, and widening this guard to cover them would make
|
|
28
29
|
* it a second read-only console rather than a rule about forging.
|
|
29
30
|
*
|
|
30
|
-
* The scan
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* (`main._substrat_tuples`) merge into one token, and every part is checked.
|
|
31
|
+
* The scan is `tokenizeSql` from `@substrat-run/contracts`, the one reading of the grammar
|
|
32
|
+
* the dump checks share: comments and string literals skipped, quoted identifiers kept as
|
|
33
|
+
* tokens, dotted names merged into one, and every part of one checked.
|
|
34
34
|
* Multiple statements in one string are walked in full: the DO's `sql.exec` accepts
|
|
35
35
|
* them, so a forge chained after a legitimate write must not slip past.
|
|
36
36
|
*/
|
|
37
|
-
import { substratError } from '@substrat-run/contracts';
|
|
38
|
-
/** The platform spine's table prefix — the same one `isSystemTable` groups on. */
|
|
39
|
-
const SPINE_PREFIX = '_substrat';
|
|
37
|
+
import { namesSpineTable, referencedTablesIn, substratError, tokenizeSql } from '@substrat-run/contracts';
|
|
40
38
|
/**
|
|
41
39
|
* Tokens that may stand between a write verb and the table it names. The first
|
|
42
40
|
* following token that is NOT one of these (or that is quoted) is the target.
|
|
@@ -62,120 +60,6 @@ const MODIFIERS = {
|
|
|
62
60
|
'exists',
|
|
63
61
|
]),
|
|
64
62
|
};
|
|
65
|
-
function tokenize(sql) {
|
|
66
|
-
const tokens = [];
|
|
67
|
-
const n = sql.length;
|
|
68
|
-
let i = 0;
|
|
69
|
-
// Set when the previous token ended on a `.`, so `main . tbl` folds into one name.
|
|
70
|
-
let continues = false;
|
|
71
|
-
const push = (text, quoted) => {
|
|
72
|
-
const prev = tokens[tokens.length - 1];
|
|
73
|
-
if (continues && prev) {
|
|
74
|
-
tokens[tokens.length - 1] = { text: `${prev.text}.${text}`, quoted: prev.quoted || quoted };
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
tokens.push({ text, quoted });
|
|
78
|
-
}
|
|
79
|
-
// Look ahead for the dot that joins this name to the next part. SQLite treats a
|
|
80
|
-
// COMMENT as whitespace, so `main /* … */ . _substrat_tuples` is one qualified
|
|
81
|
-
// name — skipping only spaces here would record `main` as the target and miss it.
|
|
82
|
-
let j = i;
|
|
83
|
-
for (;;) {
|
|
84
|
-
if (j < n && /\s/.test(sql[j])) {
|
|
85
|
-
j += 1;
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
if (sql[j] === '-' && sql[j + 1] === '-') {
|
|
89
|
-
while (j < n && sql[j] !== '\n')
|
|
90
|
-
j += 1;
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
if (sql[j] === '/' && sql[j + 1] === '*') {
|
|
94
|
-
j += 2;
|
|
95
|
-
while (j < n && !(sql[j] === '*' && sql[j + 1] === '/'))
|
|
96
|
-
j += 1;
|
|
97
|
-
j += 2;
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
if (sql[j] === '.') {
|
|
103
|
-
continues = true;
|
|
104
|
-
i = j + 1;
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
continues = false;
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
while (i < n) {
|
|
111
|
-
const c = sql[i];
|
|
112
|
-
const c2 = sql[i + 1];
|
|
113
|
-
if (c === '-' && c2 === '-') {
|
|
114
|
-
while (i < n && sql[i] !== '\n')
|
|
115
|
-
i += 1;
|
|
116
|
-
continue;
|
|
117
|
-
}
|
|
118
|
-
if (c === '/' && c2 === '*') {
|
|
119
|
-
i += 2;
|
|
120
|
-
while (i < n && !(sql[i] === '*' && sql[i + 1] === '/'))
|
|
121
|
-
i += 1;
|
|
122
|
-
i += 2;
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
if (c === "'" || c === '"' || c === '`') {
|
|
126
|
-
// A quoted identifier, or a string literal SQLite would still accept as one
|
|
127
|
-
// in a table position. The closing quote doubles to escape itself.
|
|
128
|
-
i += 1;
|
|
129
|
-
let text = '';
|
|
130
|
-
while (i < n) {
|
|
131
|
-
if (sql[i] === c) {
|
|
132
|
-
if (sql[i + 1] === c) {
|
|
133
|
-
text += c;
|
|
134
|
-
i += 2;
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
i += 1;
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
text += sql[i];
|
|
141
|
-
i += 1;
|
|
142
|
-
}
|
|
143
|
-
push(text, true);
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
if (c === '[') {
|
|
147
|
-
i += 1;
|
|
148
|
-
let text = '';
|
|
149
|
-
while (i < n && sql[i] !== ']') {
|
|
150
|
-
text += sql[i];
|
|
151
|
-
i += 1;
|
|
152
|
-
}
|
|
153
|
-
i += 1;
|
|
154
|
-
push(text, true);
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
if (/[A-Za-z_]/.test(c)) {
|
|
158
|
-
let j = i;
|
|
159
|
-
while (j < n && /[A-Za-z0-9_$]/.test(sql[j]))
|
|
160
|
-
j += 1;
|
|
161
|
-
const text = sql.slice(i, j);
|
|
162
|
-
i = j;
|
|
163
|
-
push(text, false);
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
// Whitespace does not break a dotted name (`main . tbl` is one); anything else does.
|
|
167
|
-
if (!/\s/.test(c))
|
|
168
|
-
continues = false;
|
|
169
|
-
i += 1;
|
|
170
|
-
}
|
|
171
|
-
return tokens;
|
|
172
|
-
}
|
|
173
|
-
/** True when any part of a (possibly dotted, possibly quoted) name is spine. */
|
|
174
|
-
function namesSpine(token) {
|
|
175
|
-
return token.text
|
|
176
|
-
.split('.')
|
|
177
|
-
.some((part) => part.toLowerCase().startsWith(SPINE_PREFIX));
|
|
178
|
-
}
|
|
179
63
|
/**
|
|
180
64
|
* A SECOND table a statement can reach, past the one it names first.
|
|
181
65
|
*
|
|
@@ -190,7 +74,7 @@ const SECOND_TARGET = { create: 'on', alter: 'to' };
|
|
|
190
74
|
/** The first token at or after `from` that names the spine, following this verb's grammar. */
|
|
191
75
|
function spineTargetFrom(tokens, from, verb) {
|
|
192
76
|
const first = tokens[from];
|
|
193
|
-
if (first &&
|
|
77
|
+
if (first && namesSpineTable(first.text))
|
|
194
78
|
return first;
|
|
195
79
|
const keyword = SECOND_TARGET[verb];
|
|
196
80
|
if (!keyword)
|
|
@@ -199,7 +83,7 @@ function spineTargetFrom(tokens, from, verb) {
|
|
|
199
83
|
if (tokens[k].quoted || tokens[k].text.toLowerCase() !== keyword)
|
|
200
84
|
continue;
|
|
201
85
|
const after = tokens[k + 1];
|
|
202
|
-
return after &&
|
|
86
|
+
return after && namesSpineTable(after.text) ? after : undefined;
|
|
203
87
|
}
|
|
204
88
|
return undefined;
|
|
205
89
|
}
|
|
@@ -211,7 +95,7 @@ function spineTargetFrom(tokens, from, verb) {
|
|
|
211
95
|
* the table so the author sees which line to delete.
|
|
212
96
|
*/
|
|
213
97
|
export function assertNoSpineWrite(sql) {
|
|
214
|
-
const tokens =
|
|
98
|
+
const tokens = tokenizeSql(sql);
|
|
215
99
|
for (let i = 0; i < tokens.length; i += 1) {
|
|
216
100
|
const token = tokens[i];
|
|
217
101
|
if (token.quoted)
|
|
@@ -230,6 +114,29 @@ export function assertNoSpineWrite(sql) {
|
|
|
230
114
|
throw substratError('forbidden', `ctx.sql cannot write the platform spine: ${verb.toUpperCase()} on '${target.text}'. ` +
|
|
231
115
|
'Reads are fine; writes go through ctx.emit / ctx.link / ctx.grant.', { reason: 'spine_write' });
|
|
232
116
|
}
|
|
117
|
+
refuseSpineReference(referencedTablesIn(tokens), 'ctx.sql');
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Refuse a statement whose `REFERENCES` clause names a `_substrat_*` table (#1898), for the
|
|
121
|
+
* same reason `CREATE TRIGGER … ON` is refused above: the spine's rows become the parent of a
|
|
122
|
+
* module's, and with foreign keys enforced the kernel's own writes to that table (a revoke, a
|
|
123
|
+
* restore's re-point, an outbox prune) then fail on the module's rows. That denies the spine
|
|
124
|
+
* rather than forging it. `what` names the SQL's source in the message.
|
|
125
|
+
*
|
|
126
|
+
* `ctx.sql` passes through here (`assertNoSpineWrite`), and so does every migration a scope
|
|
127
|
+
* applies, which reaches the database on the kernel's own handle rather than through
|
|
128
|
+
* `ctx.sql`. A dump's replayed DDL is held to the same rule by `assertReplayableDump`.
|
|
129
|
+
*/
|
|
130
|
+
export function assertNoSpineReference(sql, what) {
|
|
131
|
+
refuseSpineReference(referencedTablesIn(tokenizeSql(sql)), what);
|
|
132
|
+
}
|
|
133
|
+
/** The refusal itself, over a statement's `REFERENCES` targets. */
|
|
134
|
+
function refuseSpineReference(referenced, what) {
|
|
135
|
+
const target = referenced.find(namesSpineTable);
|
|
136
|
+
if (target === undefined)
|
|
137
|
+
return;
|
|
138
|
+
throw substratError('forbidden', `${what} cannot declare a foreign key to the platform spine: REFERENCES '${target}'. ` +
|
|
139
|
+
'A module table may reference its own tables; spine rows are reached through the kernel, never as a parent.', { reason: 'spine_write' });
|
|
233
140
|
}
|
|
234
141
|
/**
|
|
235
142
|
* Wrap a module-facing `ScopedSql` so every statement passes `assertNoSpineWrite`
|
package/dist/spine-guard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spine-guard.js","sourceRoot":"","sources":["../src/spine-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"spine-guard.js","sourceRoot":"","sources":["../src/spine-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,aAAa,EAAE,WAAW,EAAiB,MAAM,yBAAyB,CAAC;AAGzH;;;GAGG;AACH,MAAM,SAAS,GAAkD;IAC/D,MAAM,EAAE,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;IACjF,OAAO,EAAE,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;IAC1B,MAAM,EAAE,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC;IACzE,MAAM,EAAE,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;IACzB,IAAI,EA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,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;AACnE,CAAC;AAED,mEAAmE;AACnE,SAAS,oBAAoB,CAAC,UAA6B,EAAE,IAAY;IACvE,MAAM,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;IAChD,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO;IACjC,MAAM,aAAa,CACjB,WAAW,EACX,GAAG,IAAI,oEAAoE,MAAM,KAAK;QACpF,4GAA4G,EAC9G,EAAE,MAAM,EAAE,aAAa,EAAE,CAC1B,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,KAAgB;IACzC,OAAO;QACL,KAAK,EAAE,CAA+B,GAAW,EAAE,MAA4B,EAAO,EAAE;YACtF,kBAAkB,CAAC,GAAG,CAAC,CAAC;YACxB,OAAO,KAAK,CAAC,KAAK,CAAI,GAAG,EAAE,MAAM,CAAC,CAAC;QACrC,CAAC;QACD,IAAI,EAAE,CAAC,GAAW,EAAE,MAA4B,EAAE,EAAE;YAClD,kBAAkB,CAAC,GAAG,CAAC,CAAC;YACxB,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QACjC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a restore, fork or carry loads a dump's `_substrat_*` spine tables (#1883). One
|
|
3
|
+
* definition for both adapters' loaders, so the DO and the pure host build the same tables
|
|
4
|
+
* from the same dump.
|
|
5
|
+
*
|
|
6
|
+
* A dump used to be replayed table by table, `CREATE TABLE` from the dump and then its rows,
|
|
7
|
+
* spine included; the kernel's `CREATE TABLE IF NOT EXISTS` afterwards filled only the gaps.
|
|
8
|
+
* So a dump could decide the schema of the tables the permission checker reads. A dump that
|
|
9
|
+
* declared `_substrat_tuples.object` as `COLLATE NOCASE` made every `=` the checker and the
|
|
10
|
+
* spine SQL wrote on it ignore case: a grant on `aiTurn:x` answered for `aiturn:x`.
|
|
11
|
+
*
|
|
12
|
+
* Now the loader builds every spine table from its own kernel DDL, and the dump contributes
|
|
13
|
+
* rows only, inserted by column name:
|
|
14
|
+
* - a column the kernel's table does not have (a dump from a newer kernel) is added as a plain
|
|
15
|
+
* untyped column, unless it is named for SQLite's rowid (refused, since it would shadow it), with no type, collation, constraint or default, and keeps its values
|
|
16
|
+
* (`spineColumnAdditions`). It is not one the checker reads, and it carries nothing that
|
|
17
|
+
* could change how the columns the checker does read compare. When a later kernel adds the
|
|
18
|
+
* column for real, its additive ALTER meets it and tolerates it as a duplicate;
|
|
19
|
+
* - a column the dump lacks takes the kernel's default (NULL for every additive column), so
|
|
20
|
+
* a dump taken before a column existed still restores. The one column that is not additive,
|
|
21
|
+
* `_substrat_schedule_state.kind` (#1288, part of the key), is derived from the row the way
|
|
22
|
+
* the #1288 rebuild derives it;
|
|
23
|
+
* - a spine table the kernel does not build is refused (`assertSpineTablesBuilt`), every such
|
|
24
|
+
* table named at once.
|
|
25
|
+
*
|
|
26
|
+
* Tables outside the spine keep the dump's own DDL: a vertical's schema is the dump's to say.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* A kernel-owned table, which a restore builds from the kernel's DDL rather than the dump's.
|
|
30
|
+
* The prefix is the spine guard's, compared the way SQLite compares table names, without case:
|
|
31
|
+
* a dump's `_Substrat_tuples` IS the tuples table, and read as a vertical one its DDL would be
|
|
32
|
+
* replayed and the kernel's `IF NOT EXISTS` would skip its own.
|
|
33
|
+
*/
|
|
34
|
+
export declare function isSpineTable(name: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Refuse a dump carrying `_substrat_*` tables this host's kernel does not build, naming every
|
|
37
|
+
* one of them. Called before any spine row goes in, inside the load's transaction; `columnsOf`
|
|
38
|
+
* is the same lookup `dumpRowsInsert` takes, `undefined` for a table the kernel did not build.
|
|
39
|
+
*
|
|
40
|
+
* The usual cause is a dump from the other kind of host: a Durable Object's scope builds spine
|
|
41
|
+
* tables (`_substrat_roles`, `_substrat_tenant_tuples`, …) that a node scope keeps in its
|
|
42
|
+
* directory instead. Loading one anyway would mean dropping its rows, which a restore does not
|
|
43
|
+
* do silently.
|
|
44
|
+
*
|
|
45
|
+
* Every `_substrat*` name is judged, the search index's namespace included (`namesSpineTable`,
|
|
46
|
+
* not `isSpineTable`): a scope restore has skipped its index tables before this, since it
|
|
47
|
+
* rebuilds them, and a directory has no search index, so there such a table is one more spine
|
|
48
|
+
* table the directory does not build (#1898).
|
|
49
|
+
*/
|
|
50
|
+
export declare function assertSpineTablesBuilt(names: readonly string[], columnsOf: KernelColumnsOf): void;
|
|
51
|
+
/** A table's columns as the kernel built it here, or `undefined` when it built no such table. */
|
|
52
|
+
export type KernelColumnsOf = (name: string) => readonly string[] | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* The `INSERT` that loads one dumped table's rows, one row of positional parameters per
|
|
55
|
+
* execution, in the dump's column order: `spineRowsInsert` for a spine table, and the dump's
|
|
56
|
+
* own columns for a vertical one. The names are quoted as given: the loader has already passed
|
|
57
|
+
* the dump through `assertReplayableDump`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function dumpRowsInsert(table: {
|
|
60
|
+
name: string;
|
|
61
|
+
columns: readonly string[];
|
|
62
|
+
}, columnsOf: KernelColumnsOf): string;
|
|
63
|
+
/**
|
|
64
|
+
* The statements that add to a kernel-built spine table each column the dump carries and the
|
|
65
|
+
* kernel does not, as a plain untyped column: `ADD COLUMN "<name>"`, lowercased, and nothing after it, so it
|
|
66
|
+
* has no type, collation, constraint or default. Run after `assertSpineTablesBuilt` and before
|
|
67
|
+
* the table's rows go in, inside the load's transaction. The names are the dump's, which
|
|
68
|
+
* `assertReplayableDump` has already held to the identifier rule. Empty for a table the kernel
|
|
69
|
+
* did not build, which `assertSpineTablesBuilt` has refused.
|
|
70
|
+
*/
|
|
71
|
+
export declare function spineColumnAdditions(table: {
|
|
72
|
+
name: string;
|
|
73
|
+
columns: readonly string[];
|
|
74
|
+
}, kernelColumns: readonly string[] | undefined): string[];
|
|
75
|
+
/**
|
|
76
|
+
* The `INSERT` that loads one dumped spine table's rows into the table the kernel built.
|
|
77
|
+
*
|
|
78
|
+
* `kernelColumns` is the kernel table's column list as the loader reads it back after
|
|
79
|
+
* `spineColumnAdditions`, or `undefined` when the kernel built no such table. Either refusal
|
|
80
|
+
* below is a backstop the loader's order makes unreachable, and throws `validation_failed`
|
|
81
|
+
* inside the load's transaction, so the target keeps what it held.
|
|
82
|
+
*/
|
|
83
|
+
export declare function spineRowsInsert(table: {
|
|
84
|
+
name: string;
|
|
85
|
+
columns: readonly string[];
|
|
86
|
+
}, kernelColumns: readonly string[] | undefined): string;
|
|
87
|
+
//# sourceMappingURL=spine-restore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spine-restore.d.ts","sourceRoot":"","sources":["../src/spine-restore.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAIlD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,EAAE,SAAS,EAAE,eAAe,GAAG,IAAI,CAGjG;AAED,iGAAiG;AACjG,MAAM,MAAM,eAAe,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,MAAM,EAAE,GAAG,SAAS,CAAC;AAmB9E;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,EAAE,SAAS,EAAE,eAAe,GAAG,MAAM,CAEtH;AASD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,EACnD,aAAa,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,GAC3C,MAAM,EAAE,CAkBV;AAKD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,EACnD,aAAa,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,GAC3C,MAAM,CAyBR"}
|