@botiverse/porthole 0.7.0 → 0.8.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/README.md +9 -2
- package/THIRD_PARTY_NOTICES.md +421 -0
- package/dist/bridge/bounded-text.d.ts +34 -0
- package/dist/bridge/bounded-text.js +188 -0
- package/dist/bridge/bridge.d.ts +25 -5
- package/dist/bridge/bridge.js +311 -73
- package/dist/bridge/json-data.d.ts +28 -0
- package/dist/bridge/json-data.js +86 -0
- package/dist/bridge/navigation.d.ts +5 -0
- package/dist/bridge/navigation.js +7 -0
- package/dist/bridge/refusal.js +1 -0
- package/dist/bridge/types.d.ts +32 -1
- package/dist/check/page-source.js +1 -1
- package/dist/check/page.js +2 -0
- package/dist/compile/compile.d.ts +6 -0
- package/dist/compile/compile.js +3 -1
- package/dist/compile/policy.d.ts +38 -2
- package/dist/compile/policy.js +122 -7
- package/dist/config/capabilities.d.ts +23 -4
- package/dist/config/capabilities.js +12 -2
- package/dist/config/schema.d.ts +11 -0
- package/dist/config/validate.d.ts +8 -2
- package/dist/config/validate.js +28 -6
- package/dist/config/warnings.d.ts +20 -0
- package/dist/config/warnings.js +25 -0
- package/dist/conformance/behavior.js +46 -1
- package/dist/conformance/cookies.d.ts +15 -0
- package/dist/conformance/cookies.js +80 -0
- package/dist/conformance/harness/page.d.ts +1 -0
- package/dist/conformance/harness/page.js +9 -2
- package/dist/conformance/harness-source.js +1 -1
- package/dist/conformance/host-kit.js +5 -1
- package/dist/conformance/index.d.ts +10 -0
- package/dist/conformance/limits.js +38 -1
- package/dist/conformance/runtime.js +10 -0
- package/dist/conformance/served.d.ts +2 -0
- package/dist/conformance/served.js +737 -4
- package/dist/files/paths.js +4 -0
- package/dist/guide/gates.d.ts +7 -1
- package/dist/guide/gates.js +12 -0
- package/dist/guide/guide.js +2 -2
- package/dist/guide/runtime.js +11 -3
- package/dist/host/create-host.js +49 -37
- package/dist/host/pages-of-set.d.ts +43 -0
- package/dist/host/pages-of-set.js +196 -0
- package/dist/host/types.d.ts +8 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.js +3 -2
- package/dist/mcp/adapter.js +11 -15
- package/dist/node/index.d.ts +36 -4
- package/dist/node/index.js +402 -101
- package/dist/presets/presets.d.ts +16 -8
- package/dist/presets/presets.js +39 -8
- package/dist/protocol/live-data.d.ts +6 -2
- package/dist/protocol/live-data.js +20 -1
- package/dist/protocol/mcp-apps.d.ts +13 -2
- package/dist/protocol/mcp-apps.js +10 -2
- package/dist/protocol/message-text.d.ts +8 -0
- package/dist/protocol/message-text.js +29 -0
- package/dist/protocol/runtime-calls.js +3 -0
- package/dist/protocol/shell-adapter.js +16 -22
- package/dist/react/context-notice.d.ts +32 -0
- package/dist/react/context-notice.js +28 -0
- package/dist/react/dialog.js +62 -18
- package/dist/react/frame.d.ts +12 -1
- package/dist/react/frame.js +79 -2
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +2 -1
- package/dist/runtime/contract.d.ts +9 -1
- package/dist/runtime/contract.js +31 -2
- package/dist/runtime/db.d.ts +41 -6
- package/dist/runtime/db.js +120 -16
- package/dist/runtime/declaration.d.ts +15 -1
- package/dist/runtime/declaration.js +184 -87
- package/dist/runtime/requests.js +27 -11
- package/dist/runtime/types.generated.js +1 -1
- package/dist/server/files.d.ts +5 -0
- package/dist/server/files.js +22 -7
- package/dist/server/host.d.ts +18 -1
- package/dist/server/host.js +132 -2
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/peer.d.ts +4 -0
- package/dist/server/peer.js +19 -0
- package/dist/server/proxy.d.ts +6 -0
- package/dist/server/proxy.js +12 -3
- package/dist/server/server.js +113 -26
- package/dist/server/types.d.ts +6 -3
- package/dist/shell/document.d.ts +8 -0
- package/dist/shell/document.js +3 -0
- package/dist/shell/runtime.js +361 -14
- package/docs/ADOPTING.md +119 -14
- package/docs/ARCHITECTURE.md +13 -3
- package/docs/DECISIONS.md +1455 -8
- package/docs/DESIGN.md +79 -25
- package/docs/THREAT_MODEL.md +616 -0
- package/package.json +14 -3
- package/src/bridge/bounded-text.ts +240 -0
- package/src/bridge/bridge.ts +373 -84
- package/src/bridge/json-data.ts +95 -0
- package/src/bridge/navigation.ts +10 -0
- package/src/bridge/refusal.ts +1 -0
- package/src/bridge/types.ts +30 -1
- package/src/check/page.ts +2 -0
- package/src/compile/compile.ts +9 -1
- package/src/compile/policy.ts +130 -7
- package/src/config/capabilities.ts +22 -3
- package/src/config/schema.ts +11 -0
- package/src/config/validate.ts +43 -5
- package/src/config/warnings.ts +42 -0
- package/src/conformance/behavior.ts +54 -1
- package/src/conformance/cookies.ts +93 -0
- package/src/conformance/harness/page.ts +11 -2
- package/src/conformance/host-kit.ts +10 -1
- package/src/conformance/index.ts +10 -0
- package/src/conformance/limits.ts +39 -1
- package/src/conformance/runtime.ts +21 -0
- package/src/conformance/served.ts +888 -4
- package/src/files/paths.ts +4 -0
- package/src/guide/gates.ts +13 -1
- package/src/guide/guide.ts +3 -3
- package/src/guide/runtime.ts +12 -3
- package/src/host/create-host.ts +54 -36
- package/src/host/pages-of-set.ts +226 -0
- package/src/host/types.ts +15 -1
- package/src/index.ts +5 -0
- package/src/mcp/adapter.ts +16 -20
- package/src/node/index.ts +507 -116
- package/src/presets/presets.ts +60 -17
- package/src/protocol/live-data.ts +31 -3
- package/src/protocol/mcp-apps.ts +11 -2
- package/src/protocol/message-text.ts +36 -0
- package/src/protocol/runtime-calls.ts +3 -0
- package/src/protocol/shell-adapter.ts +27 -23
- package/src/react/context-notice.tsx +95 -0
- package/src/react/dialog.tsx +77 -17
- package/src/react/frame.tsx +101 -0
- package/src/react/index.ts +8 -1
- package/src/runtime/contract.ts +36 -2
- package/src/runtime/db.ts +140 -20
- package/src/runtime/declaration.ts +227 -89
- package/src/runtime/requests.ts +27 -11
- package/src/runtime/types.generated.ts +1 -1
- package/src/server/files.ts +29 -11
- package/src/server/host.ts +158 -3
- package/src/server/index.ts +1 -0
- package/src/server/peer.ts +21 -0
- package/src/server/proxy.ts +13 -3
- package/src/server/server.ts +139 -36
- package/src/server/types.ts +6 -3
- package/src/shell/document.ts +8 -0
- package/src/shell/runtime.ts +361 -14
- package/types/runtime/v1/context.d.ts +13 -2
package/src/bridge/bridge.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
MAX_CONTEXT_CHARS,
|
|
2
3
|
MAX_COPY_CHARS,
|
|
3
4
|
capabilitySpec,
|
|
4
5
|
type CapabilityArgs,
|
|
@@ -9,12 +10,16 @@ import type { AnnotationReport } from "../annotate/types.ts";
|
|
|
9
10
|
import { parseReport } from "../annotate/validate.ts";
|
|
10
11
|
import type { PortholeConfig } from "../config/schema.ts";
|
|
11
12
|
import { callGates, effectiveGates } from "../compile/policy.ts";
|
|
12
|
-
import {
|
|
13
|
+
import { boundedText } from "./bounded-text.ts";
|
|
14
|
+
import { isJsonData } from "./json-data.ts";
|
|
15
|
+
import { blockedNavigationMessage, cutText, withoutSealedAddresses } from "./navigation.ts";
|
|
13
16
|
import {
|
|
14
17
|
RUNTIME_HOST_FUNCTIONS,
|
|
15
18
|
familyOf,
|
|
19
|
+
methodGates,
|
|
16
20
|
methodLevel,
|
|
17
21
|
methodOf,
|
|
22
|
+
methodsOfCall,
|
|
18
23
|
assetCodeRuns,
|
|
19
24
|
rosterOf,
|
|
20
25
|
runtimeCapability,
|
|
@@ -54,13 +59,21 @@ import type {
|
|
|
54
59
|
DataCache,
|
|
55
60
|
HostFunctionBridge,
|
|
56
61
|
Refusal,
|
|
62
|
+
Show,
|
|
57
63
|
} from "./types.ts";
|
|
58
64
|
|
|
59
65
|
/**
|
|
60
66
|
* The host-function bridge: every capability call from a frame passes here, and nowhere else
|
|
61
|
-
* reaches a host handler. In order:
|
|
62
|
-
*
|
|
63
|
-
*
|
|
67
|
+
* reaches a host handler. In order (`call`): the run not stopped (`close`) → declared by the
|
|
68
|
+
* config → declared by the page, where it has a declaration (P086) → before the arguments are
|
|
69
|
+
* read (`precheck`): for the runtime's own, a method by its `op` and the viewer's level, then for
|
|
70
|
+
* every capability its host function's own rate (P091, P095) → arguments well formed and within
|
|
71
|
+
* their limits → who may (P087: the level of each method the call runs for the viewer, a page's
|
|
72
|
+
* named data with or without a viewer, db's rules) → not muted, if confirm-gated → the own rate
|
|
73
|
+
* of each method it runs (P094: a batch's writes count each) → a user gesture, if gated so → the
|
|
74
|
+
* viewer's consent, if gated so → a person's confirmation, if gated so (at most 10 waiting) →
|
|
75
|
+
* shown to the person, if gated so (P100) → the handler (or, for a read, a result the host already
|
|
76
|
+
* gave for it, or for a stale-allowed one the host's cache).
|
|
64
77
|
*/
|
|
65
78
|
|
|
66
79
|
export type BridgeEvent =
|
|
@@ -83,6 +96,11 @@ export interface BridgeOptions {
|
|
|
83
96
|
* such call with it, asking nothing (P080).
|
|
84
97
|
*/
|
|
85
98
|
readonly consent?: Consent | Refused;
|
|
99
|
+
/**
|
|
100
|
+
* Puts a `show`-gated call's whole effect in front of the person before its handler runs (P100).
|
|
101
|
+
* Absent: those calls are refused.
|
|
102
|
+
*/
|
|
103
|
+
readonly show?: Show;
|
|
86
104
|
/** The host's cache, for reads that may be answered stale (`CallOptions.stale`, P079). */
|
|
87
105
|
readonly cached?: DataCache;
|
|
88
106
|
/** Whether the embedding page has user activation now; undefined where it can't tell. */
|
|
@@ -108,6 +126,15 @@ const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
|
108
126
|
|
|
109
127
|
const MAX_URL = 4_096;
|
|
110
128
|
|
|
129
|
+
/**
|
|
130
|
+
* The most confirmations one display has waiting at once (P095): past it a call is refused as
|
|
131
|
+
* rate-limited, asking no one (and counted as no decline).
|
|
132
|
+
*/
|
|
133
|
+
const MAX_WAITING_CONFIRMS = 10;
|
|
134
|
+
|
|
135
|
+
/** The most of an error report the host is told. */
|
|
136
|
+
const MAX_ERROR_CHARS = 2_000;
|
|
137
|
+
|
|
111
138
|
/** An http(s) URL without credentials in it (`https://bank.example@evil.example` reads as a lie). */
|
|
112
139
|
export function linkTarget(value: unknown): string | null {
|
|
113
140
|
if (typeof value !== "string" || value.length === 0 || value.length > MAX_URL) {
|
|
@@ -135,6 +162,139 @@ function resourceUri(args: unknown): { uri: string } | null {
|
|
|
135
162
|
return typeof uri === "string" && uri !== "" && uri.length <= MAX_URL ? { uri } : null;
|
|
136
163
|
}
|
|
137
164
|
|
|
165
|
+
/** The most text blocks one model context update holds. */
|
|
166
|
+
const MAX_CONTEXT_BLOCKS = 100;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* What a person can't see but a model reads (the review of P100): format characters (zero-width
|
|
170
|
+
* ones, bidi controls, the tags U+E0000–E007F, a soft hyphen, a byte-order mark), variation
|
|
171
|
+
* selectors (U+FE00–FE0F, U+E0100–E01EF), private-use characters, lone surrogates, every
|
|
172
|
+
* default-ignorable code point (the re-check: U+034F, Mongolian's free variation selectors, the
|
|
173
|
+
* Hangul fillers, Khmer's inherent vowels), and controls other than a new line and a tab. An update with any is refused, never escaped: an escaped one
|
|
174
|
+
* reads to the model differently from what the person was shown.
|
|
175
|
+
*/
|
|
176
|
+
const UNSEEN =
|
|
177
|
+
/[\p{Cf}\p{Co}\p{Cs}\p{Default_Ignorable_Code_Point}\u{FE00}-\u{FE0F}\u{E0100}-\u{E01EF}]|[^\P{Cc}\n\t]/u;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The parts of real emoji that draw as nothing alone, allowed only where an emoji has them (the
|
|
181
|
+
* re-review of P100): U+FE0E or U+FE0F directly after a pictograph, or after a keycap base
|
|
182
|
+
* (`[0-9#*]`) that U+20E3 follows; U+200D only between two pictographs, a selector or a skin tone
|
|
183
|
+
* after the first. Each carries at most one choice beside a visible emoji (a selector there or
|
|
184
|
+
* not, a joiner there or not), never letters a model would read. Tag characters stay refused:
|
|
185
|
+
* subdivision flags too.
|
|
186
|
+
*/
|
|
187
|
+
const EMOJI_PARTS =
|
|
188
|
+
"(?<=\\p{Extended_Pictographic})[\\uFE0E\\uFE0F]|(?<=[0-9#*])[\\uFE0E\\uFE0F](?=\\u20E3)|(?<=\\p{Extended_Pictographic}[\\uFE0F\\p{Emoji_Modifier}]?)\\u200D(?=\\p{Extended_Pictographic})";
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The scripts whose letters U+200C and U+200D shape (Unicode's notes on the joiners: the cursive
|
|
192
|
+
* scripts, where they break or make a join, and the Indic ones and Myanmar, where they choose a
|
|
193
|
+
* conjunct, a half form or a ligature), by Script_Extensions.
|
|
194
|
+
*/
|
|
195
|
+
const SHAPING_SCRIPTS = [
|
|
196
|
+
"Arabic",
|
|
197
|
+
"Syriac",
|
|
198
|
+
"Nko",
|
|
199
|
+
"Mongolian",
|
|
200
|
+
"Devanagari",
|
|
201
|
+
"Bengali",
|
|
202
|
+
"Gurmukhi",
|
|
203
|
+
"Gujarati",
|
|
204
|
+
"Oriya",
|
|
205
|
+
"Tamil",
|
|
206
|
+
"Telugu",
|
|
207
|
+
"Kannada",
|
|
208
|
+
"Malayalam",
|
|
209
|
+
"Sinhala",
|
|
210
|
+
"Myanmar",
|
|
211
|
+
] as const;
|
|
212
|
+
|
|
213
|
+
const SHAPED = `[${SHAPING_SCRIPTS.map((name) => `\\p{scx=${name}}`).join("")}]`;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* A joiner or non-joiner between two letters or marks of those scripts (Persian's میخواهم, a
|
|
217
|
+
* Hindi half form): the shape it chooses is what the person sees. One at a letter boundary, never
|
|
218
|
+
* doubled, first or last, or beside a letter of another script.
|
|
219
|
+
*/
|
|
220
|
+
const SHAPING_JOINERS = `(?<=[\\p{L}\\p{M}])(?<=${SHAPED})[\\u200C\\u200D](?=[\\p{L}\\p{M}])(?=${SHAPED})`;
|
|
221
|
+
|
|
222
|
+
/** What may be invisible alone and is allowed where it is seen: one pass, on the text as sent. */
|
|
223
|
+
const SEEN_IN_PLACE = new RegExp(`${EMOJI_PARTS}|${SHAPING_JOINERS}`, "gu");
|
|
224
|
+
|
|
225
|
+
/** Whether `text` holds what a person can't see, the parts of emoji and shaped letters aside. */
|
|
226
|
+
const unseen = (text: string): boolean => UNSEEN.test(text.replace(SEEN_IN_PLACE, ""));
|
|
227
|
+
|
|
228
|
+
/** Whether any string in `value`, a key or a value, holds what can't be seen (bounded JSON data). */
|
|
229
|
+
function holdsUnseen(value: unknown): boolean {
|
|
230
|
+
if (typeof value === "string") {
|
|
231
|
+
return unseen(value);
|
|
232
|
+
}
|
|
233
|
+
if (value === null || typeof value !== "object") {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
if (Array.isArray(value)) {
|
|
237
|
+
return (value as readonly unknown[]).some(holdsUnseen);
|
|
238
|
+
}
|
|
239
|
+
return Object.entries(value).some(([key, each]) => unseen(key) || holdsUnseen(each));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* A model context update (P100), as the person is shown it and the model reads it: text blocks
|
|
244
|
+
* only (what the host offers, `updateModelContext: { text, structuredContent }`), each rebuilt as
|
|
245
|
+
* `{ type, text }` so nothing rides along unshown (`_meta`, `annotations`); structured content
|
|
246
|
+
* JSON data by P095's one rule (`isJsonData`: each object once, no cycles, bounded); at most
|
|
247
|
+
* `MAX_CONTEXT_CHARS` together as the person is shown it, its JSON measured only once that walk
|
|
248
|
+
* has bounded it; nothing in a text or a string they can't see (`UNSEEN`).
|
|
249
|
+
*/
|
|
250
|
+
function modelContext(args: unknown): CapabilityArgs["setModelContext"] | "too_large" | null {
|
|
251
|
+
if (!isRecord(args)) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
const { content, structuredContent } = args;
|
|
255
|
+
let chars = 0;
|
|
256
|
+
let blocks: { type: "text"; text: string }[] | undefined;
|
|
257
|
+
if (content !== undefined) {
|
|
258
|
+
if (!Array.isArray(content) || content.length > MAX_CONTEXT_BLOCKS) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
blocks = [];
|
|
262
|
+
for (const block of content as unknown[]) {
|
|
263
|
+
if (
|
|
264
|
+
!isRecord(block) ||
|
|
265
|
+
block.type !== "text" ||
|
|
266
|
+
typeof block.text !== "string" ||
|
|
267
|
+
unseen(block.text)
|
|
268
|
+
) {
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
chars += block.text.length;
|
|
272
|
+
if (chars > MAX_CONTEXT_CHARS) {
|
|
273
|
+
return "too_large";
|
|
274
|
+
}
|
|
275
|
+
blocks.push({ type: "text", text: block.text });
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (structuredContent !== undefined) {
|
|
279
|
+
// P095's one rule for JSON data; then this update's own size, as the person is shown it.
|
|
280
|
+
if (!isRecord(structuredContent) || !isJsonData(structuredContent)) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
chars += JSON.stringify(structuredContent).length;
|
|
284
|
+
if (chars > MAX_CONTEXT_CHARS) {
|
|
285
|
+
return "too_large";
|
|
286
|
+
}
|
|
287
|
+
// Within the size now: each string, value or key, is walked once.
|
|
288
|
+
if (holdsUnseen(structuredContent)) {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
...(blocks === undefined ? {} : { content: blocks }),
|
|
294
|
+
...(structuredContent === undefined ? {} : { structuredContent }),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
138
298
|
/** A call's arguments checked: the capability's, or null (malformed), or "too_large". */
|
|
139
299
|
type Validator<N extends CapabilityName> = (
|
|
140
300
|
args: unknown,
|
|
@@ -156,7 +316,11 @@ const VALIDATORS: { readonly [N in CapabilityName]: Validator<N> } = {
|
|
|
156
316
|
},
|
|
157
317
|
downloadFile: (args) => {
|
|
158
318
|
const contents = isRecord(args) ? args.contents : undefined;
|
|
159
|
-
|
|
319
|
+
// Plain, bounded JSON only (P095): what reaches the dialog and the handler is what MCP says.
|
|
320
|
+
return Array.isArray(contents) &&
|
|
321
|
+
contents.length > 0 &&
|
|
322
|
+
contents.length <= 100 &&
|
|
323
|
+
isJsonData(contents)
|
|
160
324
|
? { contents }
|
|
161
325
|
: null;
|
|
162
326
|
},
|
|
@@ -170,26 +334,11 @@ const VALIDATORS: { readonly [N in CapabilityName]: Validator<N> } = {
|
|
|
170
334
|
return null;
|
|
171
335
|
}
|
|
172
336
|
const given = args.arguments ?? {};
|
|
173
|
-
return isRecord(given) ? { name: args.name, arguments: given } : null;
|
|
337
|
+
return isRecord(given) && isJsonData(given) ? { name: args.name, arguments: given } : null;
|
|
174
338
|
},
|
|
175
339
|
readData: (args) => resourceUri(args),
|
|
176
340
|
subscribeData: (args) => resourceUri(args),
|
|
177
|
-
setModelContext: (args) =>
|
|
178
|
-
if (!isRecord(args)) {
|
|
179
|
-
return null;
|
|
180
|
-
}
|
|
181
|
-
const { content, structuredContent } = args;
|
|
182
|
-
if (
|
|
183
|
-
(content !== undefined && !Array.isArray(content)) ||
|
|
184
|
-
(structuredContent !== undefined && !isRecord(structuredContent))
|
|
185
|
-
) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
return {
|
|
189
|
-
...(content === undefined ? {} : { content }),
|
|
190
|
-
...(structuredContent === undefined ? {} : { structuredContent }),
|
|
191
|
-
};
|
|
192
|
-
},
|
|
341
|
+
setModelContext: (args) => modelContext(args),
|
|
193
342
|
reportError: (args, config) => {
|
|
194
343
|
// A navigation the shell cancelled (P080): Porthole words it, naming the address without its
|
|
195
344
|
// query; the address itself goes to the host whole.
|
|
@@ -202,10 +351,14 @@ const VALIDATORS: { readonly [N in CapabilityName]: Validator<N> } = {
|
|
|
202
351
|
const url = withoutSealedAddresses(blocked);
|
|
203
352
|
return { message: blockedNavigationMessage(url, config), navigationBlocked: url };
|
|
204
353
|
}
|
|
354
|
+
// What content said (S05, P095): any value, read only now, after its rate (`precheck`), into
|
|
355
|
+
// bounded text, then scrubbed, then cut to what the host is told.
|
|
205
356
|
const message = isRecord(args) ? args.message : undefined;
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
357
|
+
if (message === undefined) {
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
const text = cutText(withoutSealedAddresses(boundedText(message)), MAX_ERROR_CHARS);
|
|
361
|
+
return text === "" ? null : { message: text };
|
|
209
362
|
},
|
|
210
363
|
copyText: (args) => {
|
|
211
364
|
const text = isRecord(args) ? args.text : undefined;
|
|
@@ -267,6 +420,18 @@ export function onceConsent(consent: Consent): Consent {
|
|
|
267
420
|
};
|
|
268
421
|
}
|
|
269
422
|
|
|
423
|
+
/** Whether a page's `data` names the resource or tool a data call is for (P086). */
|
|
424
|
+
function dataNamed(
|
|
425
|
+
declaration: PageDeclaration,
|
|
426
|
+
capability: CapabilityName,
|
|
427
|
+
args: unknown,
|
|
428
|
+
): boolean {
|
|
429
|
+
const data = declaration.capabilities.data;
|
|
430
|
+
return capability === "callTool"
|
|
431
|
+
? declaredMatch(data?.tools, (args as CapabilityArgs["callTool"]).name)
|
|
432
|
+
: declaredMatch(data?.resources, (args as CapabilityArgs["readData"]).uri);
|
|
433
|
+
}
|
|
434
|
+
|
|
270
435
|
/** What the page declared for one runtime capability, if it declared it. */
|
|
271
436
|
function declaredOf(
|
|
272
437
|
declaration: PageDeclaration | undefined,
|
|
@@ -280,12 +445,29 @@ function declaredOf(
|
|
|
280
445
|
: undefined;
|
|
281
446
|
}
|
|
282
447
|
|
|
283
|
-
|
|
448
|
+
/** A display's bridge: `close()` when its run stops (a restart, its end; P095, after the review). */
|
|
449
|
+
export interface DisplayBridge extends HostFunctionBridge {
|
|
450
|
+
/**
|
|
451
|
+
* The run is over: every call waiting for a confirmation is refused at once, a yes that comes
|
|
452
|
+
* later (or its `proceed`) runs nothing, and no new call is taken.
|
|
453
|
+
*/
|
|
454
|
+
close(): void;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** What a stopped display's calls are refused with (the host's words; its frame is gone). */
|
|
458
|
+
const STOPPED = "the display stopped before this was answered";
|
|
459
|
+
|
|
460
|
+
export function createBridge(options: BridgeOptions): DisplayBridge {
|
|
284
461
|
const { config, display, handlers, onEvent } = options;
|
|
285
462
|
const now = options.now ?? (() => Date.now());
|
|
286
463
|
const calls = new Map<string, number[]>();
|
|
287
464
|
let declines = 0;
|
|
288
465
|
let muted = false;
|
|
466
|
+
/** Confirmations asked and not yet answered: a frame's calls can't queue without end (P095). */
|
|
467
|
+
let waiting = 0;
|
|
468
|
+
/** Closed (`close`): its run stopped. What each waiting confirmation's wait is ended by. */
|
|
469
|
+
let closed = false;
|
|
470
|
+
const ending = new Set<() => void>();
|
|
289
471
|
|
|
290
472
|
const refuse = <T>(
|
|
291
473
|
capability: CapabilityName,
|
|
@@ -302,42 +484,39 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
302
484
|
};
|
|
303
485
|
};
|
|
304
486
|
|
|
305
|
-
|
|
487
|
+
/** Counts `times` calls against `key`'s rate, unless they would pass it (then none). */
|
|
488
|
+
const withinRate = (key: string, perMinute: number, times = 1): boolean => {
|
|
306
489
|
const at = now();
|
|
307
|
-
const recent = (calls.get(
|
|
308
|
-
if (recent.length
|
|
309
|
-
calls.set(
|
|
490
|
+
const recent = (calls.get(key) ?? []).filter((time) => at - time < 60_000);
|
|
491
|
+
if (recent.length + times > perMinute) {
|
|
492
|
+
calls.set(key, recent);
|
|
310
493
|
return false;
|
|
311
494
|
}
|
|
312
|
-
|
|
313
|
-
|
|
495
|
+
for (let each = 0; each < times; each += 1) {
|
|
496
|
+
recent.push(at);
|
|
497
|
+
}
|
|
498
|
+
calls.set(key, recent);
|
|
314
499
|
return true;
|
|
315
500
|
};
|
|
316
501
|
|
|
317
|
-
/**
|
|
502
|
+
/**
|
|
503
|
+
* The least level a call needs: the highest of the methods it runs (its own, a db batch's
|
|
504
|
+
* writes', a watch's read's: after the review, neither escapes the levels of what it does).
|
|
505
|
+
*/
|
|
318
506
|
function levelOf(
|
|
319
507
|
capability: CapabilityName,
|
|
320
508
|
family: RuntimeCapabilityName,
|
|
321
|
-
method: string,
|
|
322
509
|
args: unknown,
|
|
323
510
|
): ViewerLevel | null {
|
|
324
511
|
const declared = declaredOf(options.declaration ?? undefined, family);
|
|
325
|
-
const own =
|
|
326
|
-
|
|
327
|
-
|
|
512
|
+
const [own, ...inner] = methodsOfCall(capability, args);
|
|
513
|
+
const level = own === undefined ? null : methodLevel(config, family, own, declared);
|
|
514
|
+
if (level === null) {
|
|
515
|
+
return null;
|
|
328
516
|
}
|
|
329
|
-
// A batch is each of its writes; a watch is a read (after the review: neither escapes the
|
|
330
|
-
// levels of what it does).
|
|
331
|
-
const request = args as DbRequest;
|
|
332
|
-
const inner =
|
|
333
|
-
request.op === "batch"
|
|
334
|
-
? request.writes.map((write) => write.op)
|
|
335
|
-
: request.op === "watch"
|
|
336
|
-
? [request.path.split("/").length % 2 === 0 ? "get" : "query"]
|
|
337
|
-
: [];
|
|
338
517
|
return inner.reduce<ViewerLevel>(
|
|
339
|
-
(most,
|
|
340
|
-
|
|
518
|
+
(most, method) => higher(most, methodLevel(config, family, method, declared) ?? most),
|
|
519
|
+
level,
|
|
341
520
|
);
|
|
342
521
|
}
|
|
343
522
|
|
|
@@ -346,7 +525,8 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
346
525
|
* call's level (the contract's, the host's, the page's; a batch's writes', a watch's read's),
|
|
347
526
|
* the resources and tools a page's `data` names, and a `db` call's paths (another person's data
|
|
348
527
|
* refused, `me` resolved, the page's rules). Without a viewer: the runtime's own are refused,
|
|
349
|
-
* and under a runtime anything above view; a 0.6 config's functions run as before.
|
|
528
|
+
* and under a runtime anything above view; a 0.6 config's functions run as before. The names a
|
|
529
|
+
* page's declaration gives are held whether or not there is a viewer (P094).
|
|
350
530
|
*/
|
|
351
531
|
function checkAudience<N extends CapabilityName>(
|
|
352
532
|
capability: N,
|
|
@@ -355,30 +535,29 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
355
535
|
): CapabilityArgs[N] | "undeclared" | "level" {
|
|
356
536
|
const { viewer } = options;
|
|
357
537
|
const declaration = options.declaration ?? undefined;
|
|
358
|
-
|
|
359
|
-
if (method === null) {
|
|
538
|
+
if (methodOf(capability, args) === null) {
|
|
360
539
|
return args;
|
|
361
540
|
}
|
|
362
|
-
const needed = levelOf(capability, family,
|
|
541
|
+
const needed = levelOf(capability, family, args);
|
|
542
|
+
// What the page's `data` names (P086): the declaration's word, not the viewer's, so it holds
|
|
543
|
+
// without a viewer too (P094). A page with no declaration is 0.6's: the host's handlers decide.
|
|
544
|
+
const named =
|
|
545
|
+
family !== "data" || declaration === undefined || dataNamed(declaration, capability, args);
|
|
363
546
|
if (viewer === undefined) {
|
|
364
547
|
if (RUNTIME_HOST_FUNCTIONS.includes(capability)) {
|
|
365
548
|
return "level";
|
|
366
549
|
}
|
|
367
550
|
// Under a runtime, a level the host set is never skipped for want of a viewer.
|
|
368
|
-
|
|
551
|
+
if (config.runtime !== undefined && needed !== null && needed !== "view") {
|
|
552
|
+
return "level";
|
|
553
|
+
}
|
|
554
|
+
return named ? args : "undeclared";
|
|
369
555
|
}
|
|
370
556
|
if (needed !== null && !atLeast(viewer.level, needed)) {
|
|
371
557
|
return "level";
|
|
372
558
|
}
|
|
373
|
-
if (
|
|
374
|
-
|
|
375
|
-
const named =
|
|
376
|
-
capability === "callTool"
|
|
377
|
-
? declaredMatch(data?.tools, (args as CapabilityArgs["callTool"]).name)
|
|
378
|
-
: declaredMatch(data?.resources, (args as CapabilityArgs["readData"]).uri);
|
|
379
|
-
if (!named) {
|
|
380
|
-
return "undeclared";
|
|
381
|
-
}
|
|
559
|
+
if (!named) {
|
|
560
|
+
return "undeclared";
|
|
382
561
|
}
|
|
383
562
|
if (capability === "db") {
|
|
384
563
|
const rules = declaration?.capabilities.db?.rules;
|
|
@@ -398,13 +577,14 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
398
577
|
}
|
|
399
578
|
|
|
400
579
|
/**
|
|
401
|
-
* The cheap checks
|
|
402
|
-
*
|
|
403
|
-
* host function's rate
|
|
580
|
+
* The cheap checks, before a call's arguments are read (after the review: a malformed call
|
|
581
|
+
* costs nothing but its rate): for the runtime's own, a method by its `op` and a viewer at its
|
|
582
|
+
* level; for every capability, its host function's rate (P095: what content says, in any shape,
|
|
583
|
+
* is read only within it).
|
|
404
584
|
*/
|
|
405
585
|
function precheck(capability: CapabilityName, raw: unknown): Refusal | null {
|
|
406
586
|
if (!RUNTIME_HOST_FUNCTIONS.includes(capability)) {
|
|
407
|
-
return null;
|
|
587
|
+
return withinOwnRates(capability) ? null : "rate-limited";
|
|
408
588
|
}
|
|
409
589
|
const family = familyOf(capability);
|
|
410
590
|
const method = methodOf(capability, raw);
|
|
@@ -421,15 +601,28 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
421
601
|
if (viewer === undefined || (needed !== null && !atLeast(viewer.level, needed))) {
|
|
422
602
|
return "level";
|
|
423
603
|
}
|
|
604
|
+
return withinOwnRates(capability) ? null : "rate-limited";
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/** Whether a call is within its host function's own rates, each counted. */
|
|
608
|
+
function withinOwnRates(capability: CapabilityName): boolean {
|
|
424
609
|
for (const gate of effectiveGates(config, capability)) {
|
|
425
610
|
if (typeof gate === "object" && !withinRate(capability, gate.rateLimit.perMinute)) {
|
|
426
|
-
return
|
|
611
|
+
return false;
|
|
427
612
|
}
|
|
428
613
|
}
|
|
429
|
-
return
|
|
614
|
+
return true;
|
|
430
615
|
}
|
|
431
616
|
|
|
432
617
|
return {
|
|
618
|
+
close() {
|
|
619
|
+
closed = true;
|
|
620
|
+
// Each ends a wait; the wait leaves the set later, in its own `finally`.
|
|
621
|
+
for (const end of ending) {
|
|
622
|
+
end();
|
|
623
|
+
}
|
|
624
|
+
},
|
|
625
|
+
|
|
433
626
|
declared: (capability) => config.capabilities[capability] !== undefined,
|
|
434
627
|
|
|
435
628
|
roster: () =>
|
|
@@ -446,6 +639,9 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
446
639
|
callOptions?: CallOptions,
|
|
447
640
|
): Promise<CallOutcome<CapabilityResults[N]>> {
|
|
448
641
|
type Result = CapabilityResults[N];
|
|
642
|
+
if (closed) {
|
|
643
|
+
return refuse<Result>(capability, "failed", STOPPED);
|
|
644
|
+
}
|
|
449
645
|
if (config.capabilities[capability] === undefined) {
|
|
450
646
|
return refuse<Result>(capability, "not-declared");
|
|
451
647
|
}
|
|
@@ -483,23 +679,32 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
483
679
|
}
|
|
484
680
|
args = audience;
|
|
485
681
|
}
|
|
486
|
-
// Its host function's gates, and
|
|
682
|
+
// Its host function's gates, and the own gates of each method it runs (after the review;
|
|
683
|
+
// P094: a db batch's writes', a watch's read's): all apply.
|
|
487
684
|
const gates = callGates(config, capability, args);
|
|
488
685
|
const own = new Set(effectiveGates(config, capability));
|
|
489
686
|
const confirms = gates.includes("confirm");
|
|
490
687
|
if (confirms && muted) {
|
|
491
688
|
return refuse<Result>(capability, "muted");
|
|
492
689
|
}
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
690
|
+
// Its host function's own rate was counted before its arguments were read (`precheck`).
|
|
691
|
+
// A method's rate counts each time the call runs it, with its direct calls: a batch of
|
|
692
|
+
// three sets is three sets (P094).
|
|
693
|
+
if (family !== null) {
|
|
694
|
+
const times = new Map<string, number>();
|
|
695
|
+
for (const method of methodsOfCall(capability, args)) {
|
|
696
|
+
times.set(method, (times.get(method) ?? 0) + 1);
|
|
497
697
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
698
|
+
for (const [method, count] of times) {
|
|
699
|
+
for (const gate of methodGates(config, family.name, method)) {
|
|
700
|
+
if (
|
|
701
|
+
typeof gate === "object" &&
|
|
702
|
+
!own.has(gate) &&
|
|
703
|
+
!withinRate(`${capability}.${method}`, gate.rateLimit.perMinute, count)
|
|
704
|
+
) {
|
|
705
|
+
return refuse<Result>(capability, "rate-limited");
|
|
706
|
+
}
|
|
707
|
+
}
|
|
503
708
|
}
|
|
504
709
|
}
|
|
505
710
|
if (gates.includes("user-activation")) {
|
|
@@ -533,23 +738,98 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
533
738
|
return refuse<Result>(capability, "denied");
|
|
534
739
|
}
|
|
535
740
|
}
|
|
741
|
+
// The run may have stopped while the viewer's consent was awaited: then no one is asked.
|
|
742
|
+
if (closed) {
|
|
743
|
+
return refuse<Result>(capability, "failed", STOPPED);
|
|
744
|
+
}
|
|
745
|
+
// Shown to the person first (P100): only a call that will go, so after their yes; nobody
|
|
746
|
+
// shown it (no `show`, one that failed, a run that stopped meanwhile), it doesn't go.
|
|
747
|
+
const shows = gates.includes("show");
|
|
748
|
+
if (shows && options.show === undefined) {
|
|
749
|
+
return refuse<Result>(capability, "no-show");
|
|
750
|
+
}
|
|
751
|
+
const shown = async (): Promise<CallOutcome<Result>> => {
|
|
752
|
+
let end = () => {};
|
|
753
|
+
const ended = new Promise<typeof STOPPED>((resolve) => {
|
|
754
|
+
end = () => resolve(STOPPED);
|
|
755
|
+
ending.add(end);
|
|
756
|
+
});
|
|
757
|
+
try {
|
|
758
|
+
// Called now (inside a confirm's click, where it runs from `proceed`); a show that never
|
|
759
|
+
// ends is ended by `close`, like a confirmation.
|
|
760
|
+
await Promise.race([
|
|
761
|
+
(async () => options.show?.({ capability, args, display } as ConfirmRequest))(),
|
|
762
|
+
ended,
|
|
763
|
+
]);
|
|
764
|
+
} catch (error) {
|
|
765
|
+
return refuse<Result>(
|
|
766
|
+
capability,
|
|
767
|
+
"failed",
|
|
768
|
+
error instanceof Error ? error.message : String(error),
|
|
769
|
+
);
|
|
770
|
+
} finally {
|
|
771
|
+
ending.delete(end);
|
|
772
|
+
}
|
|
773
|
+
if (closed) {
|
|
774
|
+
return refuse<Result>(capability, "failed", STOPPED);
|
|
775
|
+
}
|
|
776
|
+
return perform(capability, args, callOptions);
|
|
777
|
+
};
|
|
536
778
|
// The effect, run once: when the person's "yes" calls `proceed` (inside its own click, so an
|
|
537
779
|
// effect that needs a gesture gets one), or after `confirm` resolves accepted.
|
|
538
780
|
let effect: Promise<CallOutcome<Result>> | null = null;
|
|
781
|
+
// Never once the bridge is closed: a yes to a stopped run's request does nothing.
|
|
539
782
|
const run = (): Promise<CallOutcome<Result>> =>
|
|
540
|
-
(effect ??=
|
|
783
|
+
(effect ??= closed
|
|
784
|
+
? Promise.resolve(refuse<Result>(capability, "failed", STOPPED))
|
|
785
|
+
: shows
|
|
786
|
+
? shown()
|
|
787
|
+
: perform(capability, args, callOptions));
|
|
541
788
|
if (confirms) {
|
|
542
789
|
if (options.confirm === undefined) {
|
|
543
790
|
return refuse<Result>(capability, "no-confirm");
|
|
544
791
|
}
|
|
545
|
-
|
|
792
|
+
if (waiting >= MAX_WAITING_CONFIRMS) {
|
|
793
|
+
return refuse<Result>(capability, "rate-limited", undefined, {
|
|
794
|
+
code: "rate_limited",
|
|
795
|
+
message: "Too many confirmations are waiting: try again once they are answered.",
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
let answer: ConfirmAnswer | typeof STOPPED = false;
|
|
799
|
+
waiting += 1;
|
|
800
|
+
let end = () => {};
|
|
801
|
+
const ended = new Promise<typeof STOPPED>((resolve) => {
|
|
802
|
+
end = () => resolve(STOPPED);
|
|
803
|
+
ending.add(end);
|
|
804
|
+
});
|
|
546
805
|
try {
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
806
|
+
// Which of the methods the call runs asked for this yes (P094): a batch's gated writes.
|
|
807
|
+
const askedBy =
|
|
808
|
+
family === null
|
|
809
|
+
? []
|
|
810
|
+
: [...new Set(methodsOfCall(capability, args))].filter((each) =>
|
|
811
|
+
methodGates(config, family.name, each).includes("confirm"),
|
|
812
|
+
);
|
|
813
|
+
answer = await Promise.race([
|
|
814
|
+
options.confirm(
|
|
815
|
+
{
|
|
816
|
+
capability,
|
|
817
|
+
args,
|
|
818
|
+
display,
|
|
819
|
+
...(askedBy.length === 0 ? {} : { askedBy }),
|
|
820
|
+
} as ConfirmRequest,
|
|
821
|
+
() => void run(),
|
|
822
|
+
),
|
|
823
|
+
ended,
|
|
824
|
+
]);
|
|
551
825
|
} catch {
|
|
552
826
|
answer = effect === null ? false : "accepted";
|
|
827
|
+
} finally {
|
|
828
|
+
waiting -= 1;
|
|
829
|
+
ending.delete(end);
|
|
830
|
+
}
|
|
831
|
+
if (closed && effect === null) {
|
|
832
|
+
return refuse<Result>(capability, "failed", STOPPED);
|
|
553
833
|
}
|
|
554
834
|
if (effect === null && answer !== true && answer !== "accepted") {
|
|
555
835
|
if (answer === "redirected") {
|
|
@@ -606,6 +886,15 @@ export function createBridge(options: BridgeOptions): HostFunctionBridge {
|
|
|
606
886
|
} as Result,
|
|
607
887
|
};
|
|
608
888
|
}
|
|
889
|
+
// A result the host already gave for this read (a subscription's `changed(result)`, the
|
|
890
|
+
// fresh read after a stale answer), past every gate a read has (P094): no handler is asked,
|
|
891
|
+
// and none is needed (P079).
|
|
892
|
+
if (capability === "readData" && callOptions?.held !== undefined) {
|
|
893
|
+
// Kept for another URI (an adapter that mixed its keys): no answer, never that one's data.
|
|
894
|
+
return callOptions.held.uri === (args as CapabilityArgs["readData"]).uri
|
|
895
|
+
? { ok: true, value: callOptions.held.value as Result }
|
|
896
|
+
: refuse<Result>(capability, "invalid");
|
|
897
|
+
}
|
|
609
898
|
const handler = (handlers as Record<string, CapabilityHandler<N> | undefined>)[capability];
|
|
610
899
|
if (handler === undefined) {
|
|
611
900
|
return refuse<Result>(capability, "not-declared");
|