@capybara-hq/kernel 0.1.0-rc.57 → 0.1.0-rc.59
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/extensions.controller.d.ts +2 -10
- package/dist/admin/extensions.controller.js +2 -24
- package/dist/admin/extensions.schemas.d.ts +21 -0
- package/dist/admin/extensions.schemas.js +37 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/org-catalog.controller.d.ts +0 -5
- package/dist/catalog/org-catalog.controller.js +3 -17
- package/dist/catalog/org-catalog.schemas.d.ts +43 -0
- package/dist/catalog/org-catalog.schemas.js +43 -2
- package/dist/catalog/org-extension-switch.d.ts +3 -1
- package/dist/catalog/org-extension-switch.js +12 -5
- package/dist/catalog/org-extension.controller.d.ts +2 -21
- package/dist/catalog/org-extension.controller.js +1 -38
- package/dist/catalog/org-extension.schemas.d.ts +31 -0
- package/dist/catalog/org-extension.schemas.js +65 -0
- package/dist/catalog/plugin-install.d.ts +2 -0
- package/dist/catalog/plugin-install.js +3 -3
- package/dist/channels/actions.js +2 -0
- package/dist/channels/cards.d.ts +8 -0
- package/dist/channels/cards.js +18 -0
- package/dist/channels/channels.schemas.d.ts +3 -0
- package/dist/channels/channels.schemas.js +8 -0
- package/dist/channels/connections.service.d.ts +17 -2
- package/dist/channels/connections.service.js +30 -5
- package/dist/channels/outbound.js +25 -1
- package/dist/channels/phrases.d.ts +10 -0
- package/dist/channels/phrases.js +10 -0
- package/dist/conversations/conversations.module.js +4 -0
- package/dist/conversations/events.d.ts +35 -0
- package/dist/conversations/events.js +29 -0
- package/dist/conversations/leak-check.d.ts +26 -0
- package/dist/conversations/leak-check.js +69 -0
- package/dist/ctx/secrets.d.ts +13 -1
- package/dist/ctx/secrets.js +19 -1
- package/dist/db/kernel-schema.d.ts +34 -0
- package/dist/devices/working-root.d.ts +2 -3
- package/dist/errors.d.ts +19 -0
- package/dist/errors.js +19 -0
- package/dist/extensions/deployment-config.d.ts +6 -2
- package/dist/extensions/deployment-config.js +35 -23
- package/dist/extensions/org-config.d.ts +2 -0
- package/dist/extensions/org-config.js +1 -0
- package/dist/extensions/org-overrides.d.ts +2 -0
- package/dist/extensions/org-overrides.js +3 -2
- package/dist/http/api.module.js +6 -0
- package/dist/identity/initial-passwords.job.d.ts +3 -2
- package/dist/identity/initial-passwords.job.js +11 -5
- package/dist/orgs/invitations.controller.d.ts +3 -11
- package/dist/orgs/invitations.controller.js +9 -10
- package/dist/orgs/invitations.service.d.ts +12 -2
- package/dist/orgs/invitations.service.js +14 -7
- package/dist/orgs/members.service.d.ts +12 -2
- package/dist/orgs/members.service.js +10 -8
- package/dist/orgs/orgs.controller.d.ts +4 -12
- package/dist/orgs/orgs.controller.js +10 -10
- package/dist/orgs/orgs.schemas.d.ts +26 -0
- package/dist/orgs/orgs.schemas.js +22 -0
- package/dist/platform/connector-person-fields.d.ts +67 -0
- package/dist/platform/connector-person-fields.js +129 -0
- package/dist/platform/exclusions.js +6 -141
- package/dist/platform/method.js +1 -0
- package/dist/platform/operation.d.ts +61 -2
- package/dist/platform/operation.js +4 -4
- package/dist/platform/ops/accounts.ops.js +1 -0
- package/dist/platform/ops/channels.ops.d.ts +10 -3
- package/dist/platform/ops/channels.ops.js +223 -5
- package/dist/platform/ops/connectors.ops.d.ts +22 -4
- package/dist/platform/ops/connectors.ops.js +664 -13
- package/dist/platform/ops/extensions-admin.ops.d.ts +15 -2
- package/dist/platform/ops/extensions-admin.ops.js +153 -5
- package/dist/platform/ops/extensions.ops.d.ts +19 -5
- package/dist/platform/ops/extensions.ops.js +285 -10
- package/dist/platform/ops/org-plugins.ops.d.ts +15 -2
- package/dist/platform/ops/org-plugins.ops.js +323 -4
- package/dist/platform/ops/orgs.ops.d.ts +40 -3
- package/dist/platform/ops/orgs.ops.js +293 -12
- package/dist/platform/ops/person-inputs.d.ts +65 -0
- package/dist/platform/ops/person-inputs.js +129 -0
- package/dist/platform/ops/plugins.ops.d.ts +3 -1
- package/dist/platform/ops/plugins.ops.js +33 -20
- package/dist/platform/parity.d.ts +7 -2
- package/dist/platform/parity.js +75 -13
- package/dist/platform/person-fields.d.ts +119 -0
- package/dist/platform/person-fields.js +403 -0
- package/dist/platform/platform.module.d.ts +4 -3
- package/dist/platform/platform.module.js +8 -5
- package/dist/platform/route-schemas.js +2 -0
- package/dist/platform/sealed-inputs.d.ts +44 -0
- package/dist/platform/sealed-inputs.js +116 -0
- package/dist/platform/sealed-reveals.controller.d.ts +20 -0
- package/dist/platform/sealed-reveals.controller.js +48 -0
- package/dist/platform/sealed-reveals.d.ts +52 -0
- package/dist/platform/sealed-reveals.js +153 -0
- package/dist/runtime/project/knowledge.js +24 -19
- package/dist/runtime/waits.js +8 -2
- package/dist/sandbox/permission-facts.d.ts +5 -1
- package/dist/sandbox/permission-facts.js +8 -2
- package/dist/setup/setup-guides.service.d.ts +5 -0
- package/dist/setup/setup-guides.service.js +2 -1
- package/dist/tools/approvals.controller.d.ts +1 -0
- package/dist/tools/approvals.controller.js +19 -0
- package/dist/tools/approvals.service.d.ts +50 -1
- package/dist/tools/approvals.service.js +118 -10
- package/dist/tools/builtins/builtin-tools.d.ts +3 -0
- package/dist/tools/builtins/builtin-tools.js +31 -9
- package/dist/tools/builtins/platform.d.ts +20 -0
- package/dist/tools/builtins/platform.js +214 -15
- package/dist/tools/connectors.controller.d.ts +6 -7
- package/dist/tools/connectors.controller.js +4 -44
- package/dist/tools/connectors.schemas.d.ts +170 -1
- package/dist/tools/connectors.schemas.js +82 -3
- package/dist/tools/connectors.service.d.ts +16 -6
- package/dist/tools/connectors.service.js +39 -24
- package/dist/tools/oauth/oauth.controller.d.ts +3 -6
- package/dist/tools/oauth/oauth.controller.js +3 -12
- package/dist/tools/oauth/oauth.schemas.d.ts +14 -0
- package/dist/tools/oauth/oauth.schemas.js +27 -0
- package/dist/tools/pipeline.d.ts +31 -2
- package/dist/tools/pipeline.js +89 -17
- package/dist/tools/policy.d.ts +4 -0
- package/dist/tools/policy.js +8 -0
- package/dist/tools/registry.d.ts +29 -0
- package/dist/tools/tools.module.js +3 -0
- package/package.json +3 -3
- package/src/migrations/0169_platform_sealed_inputs.sql +23 -0
- package/src/migrations/0170_platform_sealed_reveals.sql +27 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { hintedSecretPaths } from "../extensions/config-json-schema.js";
|
|
4
|
+
/**
|
|
5
|
+
* Person fields (platform web parity spec §4.1, §4.2): the secret fields of a write's target config
|
|
6
|
+
* schema that the person types on the approval card instead of the model passing them in params.
|
|
7
|
+
* Pure rules shared by the Platform operations: find the secret fields of a JSON Schema produced by
|
|
8
|
+
* the SDK's `configJsonSchema` (`"x-secret": true`), plan which ones one call asks for, and check
|
|
9
|
+
* one typed value. Paths are the dot paths the config routes use in `values` (ruling R1); secret
|
|
10
|
+
* records (`x-ui.secrets`, `secret(z.record(...))`) and secrets inside array items cannot be
|
|
11
|
+
* addressed that way and are reported as `recordOnly` (entered on the web page).
|
|
12
|
+
*/
|
|
13
|
+
/** UTF-8 bytes of one value the person types. */
|
|
14
|
+
export const PERSON_FIELD_MAX_BYTES = 16 * 1024;
|
|
15
|
+
/** Person fields one call may ask for (one approval card). */
|
|
16
|
+
export const PERSON_FIELDS_MAX = 32;
|
|
17
|
+
/** Characters of one dot path in `askPerson` / `clearSecrets`. */
|
|
18
|
+
const PATH_MAX_CHARS = 200;
|
|
19
|
+
/** Path segments that would reach the prototype chain: never a field name. */
|
|
20
|
+
const FORBIDDEN_SEGMENTS = new Set(["__proto__", "prototype", "constructor"]);
|
|
21
|
+
const pathList = (what) => z
|
|
22
|
+
.array(z
|
|
23
|
+
.string()
|
|
24
|
+
.min(1)
|
|
25
|
+
.max(PATH_MAX_CHARS)
|
|
26
|
+
.refine((p) => p.split(".").every((seg) => !FORBIDDEN_SEGMENTS.has(seg)), {
|
|
27
|
+
message: "Not a field path: __proto__, prototype and constructor are not field names",
|
|
28
|
+
}))
|
|
29
|
+
.max(PERSON_FIELDS_MAX)
|
|
30
|
+
.default([])
|
|
31
|
+
.describe(what);
|
|
32
|
+
/** The params every operation with person fields adds to its input schema. */
|
|
33
|
+
export const personFieldParams = {
|
|
34
|
+
askPerson: pathList("Dot paths of secret fields the person enters on the confirmation card. Never put a secret in values; required secrets without a stored value are asked even when not listed."),
|
|
35
|
+
clearSecrets: pathList("Dot paths of stored secret fields to clear."),
|
|
36
|
+
};
|
|
37
|
+
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
38
|
+
/** Keywords that carry values, never kept in a person field's schema. */
|
|
39
|
+
const VALUE_KEYWORDS = new Set(["default", "examples", "const", "enum"]);
|
|
40
|
+
/** Hints that carry values, never kept in a person field's schema. */
|
|
41
|
+
const VALUE_HINTS = ["options", "presets"];
|
|
42
|
+
/** Keywords whose value maps names to sub-schemas: their keys are field names. */
|
|
43
|
+
const SCHEMA_MAPS = new Set(["properties", "patternProperties", "$defs", "definitions"]);
|
|
44
|
+
/** Widgets drawn as a multi-line input. */
|
|
45
|
+
const MULTILINE_WIDGETS = new Set(["textarea", "json"]);
|
|
46
|
+
/** The non-null shape of a node: itself, or the one non-null branch of a nullable `anyOf` / `oneOf`. */
|
|
47
|
+
function shapeOf(node) {
|
|
48
|
+
const branches = node.anyOf ?? node.oneOf;
|
|
49
|
+
if (node.type === undefined && Array.isArray(branches)) {
|
|
50
|
+
const rest = branches.filter((b) => isObj(b) && b.type !== "null");
|
|
51
|
+
if (rest.length === 1 && rest[0] !== undefined)
|
|
52
|
+
return rest[0];
|
|
53
|
+
}
|
|
54
|
+
return node;
|
|
55
|
+
}
|
|
56
|
+
/** The node `$ref` points to inside `root` (`#/$defs/…` or `#/definitions/…`), if any. */
|
|
57
|
+
function resolveRef(root, ref) {
|
|
58
|
+
if (typeof ref !== "string" || !ref.startsWith("#/"))
|
|
59
|
+
return undefined;
|
|
60
|
+
let node = root;
|
|
61
|
+
for (const raw of ref.slice(2).split("/")) {
|
|
62
|
+
const seg = raw.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
63
|
+
if (!isObj(node) || !Object.hasOwn(node, seg))
|
|
64
|
+
return undefined;
|
|
65
|
+
node = node[seg];
|
|
66
|
+
}
|
|
67
|
+
return isObj(node) ? node : undefined;
|
|
68
|
+
}
|
|
69
|
+
/** Whether a subtree holds `"x-secret": true` anywhere, following `$ref`s into `root`. */
|
|
70
|
+
function containsSecret(node, root, seen = new Set()) {
|
|
71
|
+
if (seen.has(node))
|
|
72
|
+
return false;
|
|
73
|
+
if (Array.isArray(node)) {
|
|
74
|
+
seen.add(node);
|
|
75
|
+
return node.some((n) => containsSecret(n, root, seen));
|
|
76
|
+
}
|
|
77
|
+
if (!isObj(node))
|
|
78
|
+
return false;
|
|
79
|
+
seen.add(node);
|
|
80
|
+
if (node["x-secret"] === true)
|
|
81
|
+
return true;
|
|
82
|
+
if (node.$ref !== undefined) {
|
|
83
|
+
const target = resolveRef(root, node.$ref);
|
|
84
|
+
// An unresolvable reference may hide a secret: fail closed.
|
|
85
|
+
if (target === undefined || containsSecret(target, root, seen))
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
for (const [k, v] of Object.entries(node)) {
|
|
89
|
+
if (k === "x-ui" || VALUE_KEYWORDS.has(k))
|
|
90
|
+
continue;
|
|
91
|
+
if (containsSecret(v, root, seen))
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
/** A shallow copy of a node without some keywords. */
|
|
97
|
+
function without(node, keys) {
|
|
98
|
+
const out = { ...node };
|
|
99
|
+
for (const k of keys)
|
|
100
|
+
delete out[k];
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
/** Whether the node only accepts strings (or strings and null). */
|
|
104
|
+
function isStringNode(shape) {
|
|
105
|
+
const t = shape.type;
|
|
106
|
+
if (t === "string")
|
|
107
|
+
return true;
|
|
108
|
+
return Array.isArray(t) && t.includes("string") && t.every((x) => x === "string" || x === "null");
|
|
109
|
+
}
|
|
110
|
+
/** A deep copy without value keywords (and value hints). */
|
|
111
|
+
function scrub(node) {
|
|
112
|
+
if (Array.isArray(node))
|
|
113
|
+
return node.map(scrub);
|
|
114
|
+
if (!isObj(node))
|
|
115
|
+
return node;
|
|
116
|
+
const out = {};
|
|
117
|
+
for (const [k, v] of Object.entries(node)) {
|
|
118
|
+
if (VALUE_KEYWORDS.has(k))
|
|
119
|
+
continue;
|
|
120
|
+
if (k === "x-ui" && isObj(v)) {
|
|
121
|
+
const hints = JSON.parse(JSON.stringify(v));
|
|
122
|
+
for (const h of VALUE_HINTS)
|
|
123
|
+
delete hints[h];
|
|
124
|
+
out[k] = hints;
|
|
125
|
+
}
|
|
126
|
+
else if (SCHEMA_MAPS.has(k) && isObj(v)) {
|
|
127
|
+
out[k] = Object.fromEntries(Object.entries(v).map(([name, sub]) => [name, scrub(sub)]));
|
|
128
|
+
}
|
|
129
|
+
else
|
|
130
|
+
out[k] = scrub(v);
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
/** An English text of a hint: a plain string or the `en` of a localized one. */
|
|
135
|
+
function hintText(v) {
|
|
136
|
+
if (typeof v === "string" && v !== "")
|
|
137
|
+
return v;
|
|
138
|
+
if (isObj(v) && typeof v.en === "string" && v.en !== "")
|
|
139
|
+
return v.en;
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
/** The Chinese text of a localized hint (`{ zh, en }`); a plain string has none. */
|
|
143
|
+
function hintZh(v) {
|
|
144
|
+
return isObj(v) && typeof v.zh === "string" && v.zh !== "" ? v.zh : undefined;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* A text in both languages from hints in order of preference: the first English text, and the
|
|
148
|
+
* first Chinese text of a localized hint, else the English one (a plain `title` is often the
|
|
149
|
+
* English of an `x-ui.label` that also carries `zh`).
|
|
150
|
+
*/
|
|
151
|
+
function bilingual(hints) {
|
|
152
|
+
const en = hints.map(hintText).find((t) => t !== undefined);
|
|
153
|
+
if (en === undefined)
|
|
154
|
+
return undefined;
|
|
155
|
+
return { zh: hints.map(hintZh).find((t) => t !== undefined) ?? en, en };
|
|
156
|
+
}
|
|
157
|
+
function fieldOf(node, shape, path, required, isSet) {
|
|
158
|
+
const hints = isObj(shape["x-ui"]) ? shape["x-ui"] : isObj(node["x-ui"]) ? node["x-ui"] : {};
|
|
159
|
+
const label = bilingual([shape.title, node.title, hints.label]) ?? { zh: path, en: path };
|
|
160
|
+
const description = bilingual([shape.description, node.description, hints.help]);
|
|
161
|
+
const spec = {
|
|
162
|
+
path,
|
|
163
|
+
label,
|
|
164
|
+
multiline: typeof hints.widget === "string" && MULTILINE_WIDGETS.has(hints.widget),
|
|
165
|
+
required,
|
|
166
|
+
isSet,
|
|
167
|
+
schema: scrub(node),
|
|
168
|
+
};
|
|
169
|
+
if (description !== undefined)
|
|
170
|
+
spec.description = description;
|
|
171
|
+
return spec;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The secret fields of a config JSON Schema (`configJsonSchema` output): every string node marked
|
|
175
|
+
* `"x-secret": true` (or under a secret object) reachable through `properties`, by dot path, in
|
|
176
|
+
* declaration order. `required` holds when every object along the path lists the next name in
|
|
177
|
+
* its `required`; `isSet` when `stored` holds the path. Secrets the card cannot address go to
|
|
178
|
+
* `recordOnly`: the paths `x-ui.secrets` names, secret nodes that are not strings (records), and
|
|
179
|
+
* secrets inside array items (as `list[].key`), and a secret object that takes undeclared keys
|
|
180
|
+
* (`additionalProperties`, `patternProperties`) for those keys. It fails closed on shapes it cannot see through:
|
|
181
|
+
* a `$ref`, an `allOf` or a union of two or more non-null branches whose subtree holds a secret
|
|
182
|
+
* puts the node's own path into `recordOnly`.
|
|
183
|
+
*/
|
|
184
|
+
export function secretFieldsOf(jsonSchema, stored) {
|
|
185
|
+
const fields = [];
|
|
186
|
+
const recordOnly = new Set();
|
|
187
|
+
if (!isObj(jsonSchema))
|
|
188
|
+
return { fields, recordOnly: [] };
|
|
189
|
+
const hinted = hintedSecretPaths(jsonSchema);
|
|
190
|
+
const walk = (node, path, required, inSecret, inArray) => {
|
|
191
|
+
const shape = shapeOf(node);
|
|
192
|
+
const secret = inSecret || node["x-secret"] === true || shape["x-secret"] === true;
|
|
193
|
+
if (!secret) {
|
|
194
|
+
// Fail closed: the walker descends only `properties` and `items`. A secret anywhere else
|
|
195
|
+
// in the node (`$ref`, `allOf`, unions, `additionalProperties`, `patternProperties`,
|
|
196
|
+
// `prefixItems`, `if` / `then` / `else`, `not`, …) makes the whole node web-only. `$defs` are
|
|
197
|
+
// reached only through a `$ref`, which containsSecret follows.
|
|
198
|
+
const descended = isObj(shape.properties)
|
|
199
|
+
? ["properties"]
|
|
200
|
+
: isObj(shape.items)
|
|
201
|
+
? ["items"]
|
|
202
|
+
: [];
|
|
203
|
+
const outer = shape === node ? {} : without(node, ["anyOf", "oneOf"]);
|
|
204
|
+
if (containsSecret(outer, jsonSchema) ||
|
|
205
|
+
containsSecret(without(shape, [...descended, "x-ui", "$defs", "definitions"]), jsonSchema)) {
|
|
206
|
+
recordOnly.add(path);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (isObj(shape.properties)) {
|
|
211
|
+
// A secret object that also takes undeclared keys (`additionalProperties`,
|
|
212
|
+
// `patternProperties`): those keys are secrets the card cannot name, so the object is web
|
|
213
|
+
// only for them and a value under it is refused (its declared fields stay person fields).
|
|
214
|
+
if (secret &&
|
|
215
|
+
((shape.additionalProperties !== undefined && shape.additionalProperties !== false) ||
|
|
216
|
+
shape.patternProperties !== undefined)) {
|
|
217
|
+
recordOnly.add(path);
|
|
218
|
+
}
|
|
219
|
+
const req = Array.isArray(shape.required) ? shape.required : [];
|
|
220
|
+
for (const [name, child] of Object.entries(shape.properties)) {
|
|
221
|
+
if (!isObj(child))
|
|
222
|
+
continue;
|
|
223
|
+
const childPath = path === "" ? name : `${path}.${name}`;
|
|
224
|
+
walk(child, childPath, required && req.includes(name), secret, inArray);
|
|
225
|
+
}
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (isObj(shape.items) && !secret) {
|
|
229
|
+
walk(shape.items, `${path}[]`, false, false, true);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (!secret || path === "")
|
|
233
|
+
return;
|
|
234
|
+
if (inArray || hinted.has(path) || !isStringNode(shape))
|
|
235
|
+
recordOnly.add(path);
|
|
236
|
+
else
|
|
237
|
+
fields.push(fieldOf(node, shape, path, required, stored.has(path)));
|
|
238
|
+
};
|
|
239
|
+
walk(jsonSchema, "", true, false, false);
|
|
240
|
+
for (const p of hinted)
|
|
241
|
+
recordOnly.add(p);
|
|
242
|
+
const asked = new Set(fields.map((f) => f.path));
|
|
243
|
+
return { fields, recordOnly: [...recordOnly].filter((p) => !asked.has(p)) };
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* A refusal of a call's person fields (model-visible English, never a value): thrown by the plan,
|
|
247
|
+
* or by an operation's `personFields` for an input the plan cannot see (a dynamic record such as
|
|
248
|
+
* the install's `extensionConfig`). The tool denies the call with the message before the card.
|
|
249
|
+
*/
|
|
250
|
+
export class PersonFieldsRefused extends Error {
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Every (dot path, leaf value) pair of `values`, keyed by dot path or nesting objects: own
|
|
254
|
+
* properties only, plain objects descended, arrays, scalars and null leaves.
|
|
255
|
+
*/
|
|
256
|
+
function flatten(values, prefix = "", out = []) {
|
|
257
|
+
for (const [key, v] of Object.entries(values)) {
|
|
258
|
+
const path = prefix === "" ? key : `${prefix}.${key}`;
|
|
259
|
+
// An empty object carries nothing: no pair.
|
|
260
|
+
if (isObj(v))
|
|
261
|
+
flatten(v, path, out);
|
|
262
|
+
else
|
|
263
|
+
out.push([path, v]);
|
|
264
|
+
}
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Whether a pair at `pairPath` touches the secret at `secretPath`: at it, below it, or above it
|
|
269
|
+
* (a leaf in place of the object holding it). `""` (an opaque root) touches every pair.
|
|
270
|
+
*/
|
|
271
|
+
function touches(pairPath, secretPath) {
|
|
272
|
+
return (secretPath === "" ||
|
|
273
|
+
pairPath === secretPath ||
|
|
274
|
+
pairPath.startsWith(`${secretPath}.`) ||
|
|
275
|
+
secretPath.startsWith(`${pairPath}.`));
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* The paths of `paths` that `values` touches (a pair at, below or above one, null included),
|
|
279
|
+
* flattened like the plan's check: e.g. the read-only fields a call tries to write.
|
|
280
|
+
*/
|
|
281
|
+
export function pathsTouched(values, paths) {
|
|
282
|
+
const pairs = flatten((values ?? {}));
|
|
283
|
+
return paths.filter((p) => pairs.some(([path]) => touches(path, p)));
|
|
284
|
+
}
|
|
285
|
+
/** The path the values check guards for a recordOnly path: an array-item secret's array root. */
|
|
286
|
+
function guardedPath(path) {
|
|
287
|
+
const cut = path.indexOf("[]");
|
|
288
|
+
return cut < 0 ? path : path.slice(0, cut).replace(/\.$/, "");
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Plans the person fields of one call (spec §4.1). Refusals are English for the model:
|
|
292
|
+
* - a non-null value in `values` at, below or above a secret field or a recordOnly path (an
|
|
293
|
+
* array-item secret: its array root) -> the leaked-secret copy, naming only the path, never the
|
|
294
|
+
* value; `values` is flattened once, so flat, nested and prefix keys all count; a null at a
|
|
295
|
+
* secret field -> pointed to clearSecrets;
|
|
296
|
+
* - an `askPerson` / `clearSecrets` path that is not a secret field of the target;
|
|
297
|
+
* - a path both asked and cleared;
|
|
298
|
+
* - more than {@link PERSON_FIELDS_MAX} fields to ask.
|
|
299
|
+
* A required secret without a stored value joins `ask` even when not named (unless it is cleared).
|
|
300
|
+
*/
|
|
301
|
+
export function planPersonFields(t, p) {
|
|
302
|
+
const byPath = new Map(t.fields.map((f) => [f.path, f]));
|
|
303
|
+
const pairs = flatten(p.values ?? {});
|
|
304
|
+
const leaks = (secretPath) => pairs.some(([path, v]) => v !== null && v !== undefined && touches(path, secretPath));
|
|
305
|
+
for (const f of t.fields) {
|
|
306
|
+
if (leaks(f.path)) {
|
|
307
|
+
return {
|
|
308
|
+
ok: false,
|
|
309
|
+
message: `Secret fields are entered by the person on the confirmation card; leave \`${f.path}\` out of values and list it in askPerson. A value passed in params has been seen by the model and counts as leaked: tell the person to rotate it.`,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
for (const path of t.recordOnly ?? []) {
|
|
314
|
+
if (leaks(guardedPath(path))) {
|
|
315
|
+
return {
|
|
316
|
+
ok: false,
|
|
317
|
+
message: `\`${path}\` is a secret record the person enters on the web page; leave it out of values. A value passed in params has been seen by the model and counts as leaked: tell the person to rotate it.`,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
for (const f of t.fields) {
|
|
322
|
+
if (pairs.some(([path, v]) => v === null && path === f.path)) {
|
|
323
|
+
return {
|
|
324
|
+
ok: false,
|
|
325
|
+
message: `To clear \`${f.path}\`, leave it out of values and list it in clearSecrets.`,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
const known = t.fields.map((f) => f.path).join(", ");
|
|
330
|
+
const unknownIn = (paths, verb) => {
|
|
331
|
+
const bad = paths.find((x) => !byPath.has(x));
|
|
332
|
+
if (bad === undefined)
|
|
333
|
+
return null;
|
|
334
|
+
return known === ""
|
|
335
|
+
? `${bad} is not a secret field of this target; it has no secret fields the person can enter.`
|
|
336
|
+
: `${bad} is not a secret field of this target; only these can be ${verb}: ${known}`;
|
|
337
|
+
};
|
|
338
|
+
const unknownAsk = unknownIn(p.askPerson, "asked");
|
|
339
|
+
if (unknownAsk !== null)
|
|
340
|
+
return { ok: false, message: unknownAsk };
|
|
341
|
+
const unknownClear = unknownIn(p.clearSecrets, "cleared");
|
|
342
|
+
if (unknownClear !== null)
|
|
343
|
+
return { ok: false, message: unknownClear };
|
|
344
|
+
const clear = new Set(p.clearSecrets);
|
|
345
|
+
const both = p.askPerson.find((x) => clear.has(x));
|
|
346
|
+
if (both !== undefined) {
|
|
347
|
+
return {
|
|
348
|
+
ok: false,
|
|
349
|
+
message: `\`${both}\` is in both askPerson and clearSecrets; list it in one of them: askPerson to have the person enter a new value, clearSecrets to remove the stored one.`,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
const named = new Set(p.askPerson);
|
|
353
|
+
const ask = t.fields.filter((f) => named.has(f.path) || (f.required && !f.isSet && !clear.has(f.path)));
|
|
354
|
+
if (ask.length > PERSON_FIELDS_MAX) {
|
|
355
|
+
return {
|
|
356
|
+
ok: false,
|
|
357
|
+
message: `This call would ask the person for ${ask.length} fields; one confirmation card holds at most ${PERSON_FIELDS_MAX}. Split the change into several calls.`,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
return { ok: true, ask, clear: t.fields.filter((f) => clear.has(f.path)).map((f) => f.path) };
|
|
361
|
+
}
|
|
362
|
+
/** One validator for every person field, configured like the tool pipeline's (`tools/validate.ts`). */
|
|
363
|
+
const ajv = new Ajv2020({ strict: false, allErrors: true, logger: false });
|
|
364
|
+
/** Compiled validators by schema node (null: the node does not compile). */
|
|
365
|
+
const compiled = new WeakMap();
|
|
366
|
+
function validatorOf(schema) {
|
|
367
|
+
const hit = compiled.get(schema);
|
|
368
|
+
if (hit !== undefined)
|
|
369
|
+
return hit;
|
|
370
|
+
const { $schema: _ignored, $id: _id, ...node } = schema;
|
|
371
|
+
let fn;
|
|
372
|
+
try {
|
|
373
|
+
fn = ajv.compile(node);
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
fn = null;
|
|
377
|
+
}
|
|
378
|
+
finally {
|
|
379
|
+
// Ajv keeps every compiled schema object; the WeakMap is the only cache.
|
|
380
|
+
ajv.removeSchema(node);
|
|
381
|
+
}
|
|
382
|
+
compiled.set(schema, fn);
|
|
383
|
+
return fn;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Checks one value the person typed against its field: at most {@link PERSON_FIELD_MAX_BYTES}
|
|
387
|
+
* UTF-8 bytes, not empty, and valid for the field's JSON Schema node (ajv, JSON Schema 2020-12,
|
|
388
|
+
* as the tool pipeline validates inputs). Returns null when valid, else an English problem that
|
|
389
|
+
* names the field and never echoes the value. A node ajv cannot compile accepts the value: the
|
|
390
|
+
* write validates the whole config again.
|
|
391
|
+
*/
|
|
392
|
+
export function checkPersonValue(spec, value) {
|
|
393
|
+
if (Buffer.byteLength(value, "utf8") > PERSON_FIELD_MAX_BYTES) {
|
|
394
|
+
return `${spec.label.en} is longer than 16 KiB.`;
|
|
395
|
+
}
|
|
396
|
+
if (value === "")
|
|
397
|
+
return `${spec.label.en} is empty.`;
|
|
398
|
+
const validate = validatorOf(spec.schema);
|
|
399
|
+
if (validate === null || validate(value))
|
|
400
|
+
return null;
|
|
401
|
+
const problems = (validate.errors ?? []).map((e) => e.message ?? "is invalid");
|
|
402
|
+
return `${spec.label.en} ${[...new Set(problems)].join("; ")}.`;
|
|
403
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The `Platform` tool's core (Platform tool spec §3, §6, §7): the operation registry, the actor
|
|
3
|
-
* facts
|
|
4
|
-
*
|
|
5
|
-
* `
|
|
3
|
+
* facts, the agent docs, the sealed person inputs (platform web parity spec §4.3) and the sealed
|
|
4
|
+
* reveals (invitation links, §7.2). It imports
|
|
5
|
+
* no domain module (ruling P2), so `ToolsModule` can import it without a cycle; domain providers
|
|
6
|
+
* register their operations into `PlatformOperations` from `onModuleInit`.
|
|
6
7
|
*/
|
|
7
8
|
export declare class PlatformModule {
|
|
8
9
|
}
|
|
@@ -8,18 +8,21 @@ import { Module } from "@nestjs/common";
|
|
|
8
8
|
import { PlatformActor } from "./actor.js";
|
|
9
9
|
import { AgentDocs } from "./agent-docs.js";
|
|
10
10
|
import { PlatformOperations } from "./registry.js";
|
|
11
|
+
import { SealedInputs } from "./sealed-inputs.js";
|
|
12
|
+
import { SealedReveals } from "./sealed-reveals.js";
|
|
11
13
|
/**
|
|
12
14
|
* The `Platform` tool's core (Platform tool spec §3, §6, §7): the operation registry, the actor
|
|
13
|
-
* facts
|
|
14
|
-
*
|
|
15
|
-
* `
|
|
15
|
+
* facts, the agent docs, the sealed person inputs (platform web parity spec §4.3) and the sealed
|
|
16
|
+
* reveals (invitation links, §7.2). It imports
|
|
17
|
+
* no domain module (ruling P2), so `ToolsModule` can import it without a cycle; domain providers
|
|
18
|
+
* register their operations into `PlatformOperations` from `onModuleInit`.
|
|
16
19
|
*/
|
|
17
20
|
let PlatformModule = class PlatformModule {
|
|
18
21
|
};
|
|
19
22
|
PlatformModule = __decorate([
|
|
20
23
|
Module({
|
|
21
|
-
providers: [PlatformOperations, PlatformActor, AgentDocs],
|
|
22
|
-
exports: [PlatformOperations, PlatformActor, AgentDocs],
|
|
24
|
+
providers: [PlatformOperations, PlatformActor, AgentDocs, SealedInputs, SealedReveals],
|
|
25
|
+
exports: [PlatformOperations, PlatformActor, AgentDocs, SealedInputs, SealedReveals],
|
|
23
26
|
})
|
|
24
27
|
], PlatformModule);
|
|
25
28
|
export { PlatformModule };
|
|
@@ -34,6 +34,7 @@ import { PLATFORM_ICON_ROUTES } from "../themes/platform-icon.schemas.js";
|
|
|
34
34
|
import { THEME_ROUTES } from "../themes/themes.schemas.js";
|
|
35
35
|
import { APPROVAL_ROUTES } from "../tools/approvals.schemas.js";
|
|
36
36
|
import { CONNECTOR_ROUTES } from "../tools/connectors.schemas.js";
|
|
37
|
+
import { OAUTH_ROUTES } from "../tools/oauth/oauth.schemas.js";
|
|
37
38
|
import { UI_ROUTES } from "../ui/ui.schemas.js";
|
|
38
39
|
/**
|
|
39
40
|
* Every route a Platform operation mirrors, from the exported `<domain>.schemas.ts` next to each
|
|
@@ -48,6 +49,7 @@ export const ROUTE_SCHEMAS = [
|
|
|
48
49
|
...ADMIN_CATALOG_ROUTES,
|
|
49
50
|
...MARKETPLACE_ROUTES,
|
|
50
51
|
...CONNECTOR_ROUTES,
|
|
52
|
+
...OAUTH_ROUTES,
|
|
51
53
|
...COMPONENT_SCOPE_ROUTES,
|
|
52
54
|
...ORG_EXTENSION_ROUTES,
|
|
53
55
|
...CONVERSATION_ROUTES,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type Keyring } from "../ctx/secrets.js";
|
|
2
|
+
import type { KernelSchemaDb } from "../db/kernel-schema.js";
|
|
3
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
5
|
+
/** How long a person's sealed card values wait for the resumed call, from the decision. */
|
|
6
|
+
export declare const SEALED_INPUT_TTL_MS: number;
|
|
7
|
+
/**
|
|
8
|
+
* The sealed values are gone: consumed already, expired, not this recipient's, or no longer
|
|
9
|
+
* unwrappable. Says nothing about which.
|
|
10
|
+
*/
|
|
11
|
+
export declare class SealedInputsExpiredError extends Error {
|
|
12
|
+
constructor();
|
|
13
|
+
}
|
|
14
|
+
/** The approved call the values belong to, and the person who typed them. */
|
|
15
|
+
export interface SealedInputsRow {
|
|
16
|
+
approvalId: string;
|
|
17
|
+
recipientAccountId: string;
|
|
18
|
+
conversationId: string;
|
|
19
|
+
/** The run of the call: a call id is unique only within its run. */
|
|
20
|
+
runId: string;
|
|
21
|
+
callId: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Values a person typed on an approval card for a Platform call's secret fields (platform web
|
|
25
|
+
* parity spec §4.3). They are sealed with the master keyring in the decision's transaction and
|
|
26
|
+
* consumed once by the resumed call, in the transaction that applies the write. They never reach
|
|
27
|
+
* the event log, a tool result, the approval row, the audit or a log line.
|
|
28
|
+
*/
|
|
29
|
+
export declare class SealedInputs {
|
|
30
|
+
private readonly db;
|
|
31
|
+
readonly txHost: TransactionHost;
|
|
32
|
+
private readonly keyring;
|
|
33
|
+
constructor(db: KernelDb, txHost: TransactionHost, keyring: Keyring);
|
|
34
|
+
/** Seals `values` for the approval, in the caller's (the decision's) transaction. */
|
|
35
|
+
sealIn(row: SealedInputsRow, values: Readonly<Record<string, string>>): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Locks, deletes and unwraps the approval's values for `recipientAccountId`, then runs `apply`
|
|
38
|
+
* with them in the same transaction: when `apply` throws, the row comes back. A row that is
|
|
39
|
+
* missing, expired, another person's or not unwrappable is {@link SealedInputsExpiredError}.
|
|
40
|
+
*/
|
|
41
|
+
consume<T>(approvalId: string, recipientAccountId: string, apply: (values: Readonly<Record<string, string>>) => Promise<T>): Promise<T>;
|
|
42
|
+
}
|
|
43
|
+
/** Deletes the sealed person inputs past `expires_at`; returns how many. */
|
|
44
|
+
export declare function deleteExpiredSealedInputs(db: KernelSchemaDb): Promise<number>;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
import { sql } from "kysely";
|
|
12
|
+
import { platformSealedInputAad } from "../ctx/secrets.js";
|
|
13
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
14
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
15
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
16
|
+
import { KEYRING } from "../infra/tokens.js";
|
|
17
|
+
/** How long a person's sealed card values wait for the resumed call, from the decision. */
|
|
18
|
+
export const SEALED_INPUT_TTL_MS = 15 * 60 * 1000;
|
|
19
|
+
/**
|
|
20
|
+
* The sealed values are gone: consumed already, expired, not this recipient's, or no longer
|
|
21
|
+
* unwrappable. Says nothing about which.
|
|
22
|
+
*/
|
|
23
|
+
export class SealedInputsExpiredError extends Error {
|
|
24
|
+
constructor() {
|
|
25
|
+
super("The sealed person inputs are no longer available");
|
|
26
|
+
this.name = "SealedInputsExpiredError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Values a person typed on an approval card for a Platform call's secret fields (platform web
|
|
31
|
+
* parity spec §4.3). They are sealed with the master keyring in the decision's transaction and
|
|
32
|
+
* consumed once by the resumed call, in the transaction that applies the write. They never reach
|
|
33
|
+
* the event log, a tool result, the approval row, the audit or a log line.
|
|
34
|
+
*/
|
|
35
|
+
let SealedInputs = class SealedInputs {
|
|
36
|
+
db;
|
|
37
|
+
txHost;
|
|
38
|
+
keyring;
|
|
39
|
+
constructor(db, txHost, keyring) {
|
|
40
|
+
this.db = db;
|
|
41
|
+
this.txHost = txHost;
|
|
42
|
+
this.keyring = keyring;
|
|
43
|
+
}
|
|
44
|
+
/** Seals `values` for the approval, in the caller's (the decision's) transaction. */
|
|
45
|
+
async sealIn(row, values) {
|
|
46
|
+
const paths = Object.keys(values).sort();
|
|
47
|
+
const plain = Buffer.from(JSON.stringify(values), "utf8");
|
|
48
|
+
const { data, masterKeyId } = this.keyring.wrap(plain, platformSealedInputAad(row.approvalId, paths));
|
|
49
|
+
plain.fill(0);
|
|
50
|
+
await this.db
|
|
51
|
+
.insertInto("platform_sealed_inputs")
|
|
52
|
+
.values({
|
|
53
|
+
approval_id: row.approvalId,
|
|
54
|
+
recipient_account_id: row.recipientAccountId,
|
|
55
|
+
conversation_id: row.conversationId,
|
|
56
|
+
run_id: row.runId,
|
|
57
|
+
call_id: row.callId,
|
|
58
|
+
paths,
|
|
59
|
+
master_key_id: masterKeyId,
|
|
60
|
+
ciphertext: data,
|
|
61
|
+
// the DB clock, the same one consume and the cleanup compare against
|
|
62
|
+
expires_at: sql `now() + make_interval(secs => ${SEALED_INPUT_TTL_MS / 1000})`,
|
|
63
|
+
})
|
|
64
|
+
.execute();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Locks, deletes and unwraps the approval's values for `recipientAccountId`, then runs `apply`
|
|
68
|
+
* with them in the same transaction: when `apply` throws, the row comes back. A row that is
|
|
69
|
+
* missing, expired, another person's or not unwrappable is {@link SealedInputsExpiredError}.
|
|
70
|
+
*/
|
|
71
|
+
async consume(approvalId, recipientAccountId, apply) {
|
|
72
|
+
const row = await this.db
|
|
73
|
+
.deleteFrom("platform_sealed_inputs")
|
|
74
|
+
.where("approval_id", "=", approvalId)
|
|
75
|
+
.where("recipient_account_id", "=", recipientAccountId)
|
|
76
|
+
.where("expires_at", ">", sql `now()`)
|
|
77
|
+
.returning(["approval_id", "paths", "master_key_id", "ciphertext"])
|
|
78
|
+
.executeTakeFirst();
|
|
79
|
+
if (row === undefined)
|
|
80
|
+
throw new SealedInputsExpiredError();
|
|
81
|
+
let values;
|
|
82
|
+
let plain;
|
|
83
|
+
try {
|
|
84
|
+
plain = this.keyring.unwrap(row.master_key_id, Buffer.from(row.ciphertext), platformSealedInputAad(row.approval_id, row.paths));
|
|
85
|
+
values = JSON.parse(plain.toString("utf8"));
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
throw new SealedInputsExpiredError();
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
plain?.fill(0);
|
|
92
|
+
}
|
|
93
|
+
return apply(values);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
__decorate([
|
|
97
|
+
Transactional({ propagation: "mandatory" })
|
|
98
|
+
], SealedInputs.prototype, "sealIn", null);
|
|
99
|
+
__decorate([
|
|
100
|
+
Transactional()
|
|
101
|
+
], SealedInputs.prototype, "consume", null);
|
|
102
|
+
SealedInputs = __decorate([
|
|
103
|
+
Injectable(),
|
|
104
|
+
__param(0, Inject(KernelDb)),
|
|
105
|
+
__param(1, Inject(TransactionHost)),
|
|
106
|
+
__param(2, Inject(KEYRING))
|
|
107
|
+
], SealedInputs);
|
|
108
|
+
export { SealedInputs };
|
|
109
|
+
/** Deletes the sealed person inputs past `expires_at`; returns how many. */
|
|
110
|
+
export async function deleteExpiredSealedInputs(db) {
|
|
111
|
+
const r = await db
|
|
112
|
+
.deleteFrom("platform_sealed_inputs")
|
|
113
|
+
.where("expires_at", "<=", sql `now()`)
|
|
114
|
+
.executeTakeFirst();
|
|
115
|
+
return Number(r.numDeletedRows);
|
|
116
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type Principal } from "../identity/principal.js";
|
|
3
|
+
import { type OrgContext } from "../orgs/org.guard.js";
|
|
4
|
+
import { SealedReveals } from "./sealed-reveals.js";
|
|
5
|
+
declare const ApprovalParams: z.ZodObject<{
|
|
6
|
+
approvalId: z.ZodString;
|
|
7
|
+
}, z.core.$strip>;
|
|
8
|
+
/**
|
|
9
|
+
* The link of an invitation an org admin's agent created (platform web parity spec §7.2): shown
|
|
10
|
+
* once, on the web, to the person who approved the `orgs.invite` call. A POST because reading
|
|
11
|
+
* consumes it.
|
|
12
|
+
*/
|
|
13
|
+
export declare class SealedRevealsController {
|
|
14
|
+
private readonly reveals;
|
|
15
|
+
constructor(reveals: SealedReveals);
|
|
16
|
+
reveal(principal: Principal, org: OrgContext, params: z.infer<typeof ApprovalParams>): Promise<{
|
|
17
|
+
url: string;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
export {};
|