@botiverse/porthole 0.13.0 → 0.14.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.
Files changed (68) hide show
  1. package/dist/annotate/types.d.ts +32 -3
  2. package/dist/annotate/types.js +4 -0
  3. package/dist/annotate/validate.js +19 -2
  4. package/dist/check/page-source.js +1 -1
  5. package/dist/compile/policy.d.ts +7 -0
  6. package/dist/compile/policy.js +13 -1
  7. package/dist/config/schema.d.ts +11 -0
  8. package/dist/config/validate.js +19 -0
  9. package/dist/conformance/harness-source.js +1 -1
  10. package/dist/displays/displays.js +3 -1
  11. package/dist/guide/limits.js +1 -1
  12. package/dist/host/create-host.d.ts +2 -0
  13. package/dist/host/create-host.js +73 -3
  14. package/dist/host/pass-on.d.ts +15 -0
  15. package/dist/host/pass-on.js +65 -0
  16. package/dist/host/types.d.ts +22 -0
  17. package/dist/index.d.ts +4 -1
  18. package/dist/index.js +4 -1
  19. package/dist/isolation/types.d.ts +8 -0
  20. package/dist/isolation/types.js +8 -0
  21. package/dist/platform/focus-guard.d.ts +71 -2
  22. package/dist/platform/focus-guard.js +187 -12
  23. package/dist/platform/types.d.ts +8 -1
  24. package/dist/platform/web.js +60 -4
  25. package/dist/presets/presets.d.ts +2 -0
  26. package/dist/presets/presets.js +1 -0
  27. package/dist/server/files.js +19 -3
  28. package/dist/server/index.d.ts +1 -0
  29. package/dist/server/index.js +4 -0
  30. package/dist/server/nested.d.ts +37 -0
  31. package/dist/server/nested.js +70 -0
  32. package/dist/server/server.js +57 -7
  33. package/dist/shell/annotate.d.ts +1 -1
  34. package/dist/shell/annotate.js +157 -7
  35. package/dist/shell/frame.d.ts +45 -0
  36. package/dist/shell/frame.js +250 -0
  37. package/dist/shell/plan.js +2 -1
  38. package/dist/shell/runtime.js +49 -14
  39. package/docs/ADOPTING.md +56 -2
  40. package/docs/ARCHITECTURE.md +23 -0
  41. package/docs/DECISIONS.md +209 -0
  42. package/docs/DESIGN.md +9 -0
  43. package/docs/THREAT_MODEL.md +75 -5
  44. package/package.json +1 -1
  45. package/src/annotate/types.ts +28 -3
  46. package/src/annotate/validate.ts +23 -2
  47. package/src/compile/policy.ts +14 -1
  48. package/src/config/schema.ts +11 -0
  49. package/src/config/validate.ts +23 -0
  50. package/src/displays/displays.ts +3 -1
  51. package/src/guide/limits.ts +1 -1
  52. package/src/host/create-host.ts +76 -4
  53. package/src/host/pass-on.ts +73 -0
  54. package/src/host/types.ts +20 -0
  55. package/src/index.ts +4 -1
  56. package/src/isolation/types.ts +12 -0
  57. package/src/platform/focus-guard.ts +258 -15
  58. package/src/platform/types.ts +8 -1
  59. package/src/platform/web.ts +81 -9
  60. package/src/presets/presets.ts +3 -0
  61. package/src/server/files.ts +19 -3
  62. package/src/server/index.ts +10 -0
  63. package/src/server/nested.ts +86 -0
  64. package/src/server/server.ts +65 -6
  65. package/src/shell/annotate.ts +157 -7
  66. package/src/shell/frame.ts +268 -0
  67. package/src/shell/plan.ts +2 -1
  68. package/src/shell/runtime.ts +49 -14
@@ -0,0 +1,268 @@
1
+ import { PAGE_SANDBOX_TOKENS } from "../compile/policy.ts";
2
+ import { hostOriginProblem } from "../isolation/served.ts";
3
+
4
+ /**
5
+ * Porthole's shell for a nested page (P111): the whole document at the address the host frames,
6
+ * with no content in it. It frames the page (the same origin's sealed files' address) in a child
7
+ * sandboxed without `allow-same-origin`, so the page's origin is opaque and nothing of the shell's
8
+ * realm is reachable from it: not its DOM, its globals, its storage, or the window the host hears.
9
+ *
10
+ * The shell:
11
+ * - passes every message between the host (its parent, from the host origins) and the page (its
12
+ * child's window, by `event.source` alone: the page's origin is "null") unchanged, and drops any
13
+ * the page sends in the shell's own namespace (`porthole/shell/`), so the shell's words to the
14
+ * host are only its own;
15
+ * - tells the host its own state (`porthole/shell/state`): whether its transient activation is on,
16
+ * and since when, and whether the page's focus is in it. A gesture in the page activates the
17
+ * page and its ancestors, the shell among them, and nothing on the host's page activates the
18
+ * shell (activation goes up, never down), so the shell's activation turning on is a press or
19
+ * key of the person's in this page. It is read at every animation frame, on its window's focus
20
+ * and blur, and before each message of the page's is passed on (a call made in a click's handler
21
+ * arrives after the state that says so). What it can't see: a gesture while its activation is
22
+ * already on (within 5 s of the last one) turns nothing on;
23
+ * - renews the page's sealed files' address before it ends, and gives the page the new one
24
+ * (`porthole/files-renewed`): the page's own loads aren't its origin's (an opaque origin's never
25
+ * are), so only the shell, on that origin, may renew it (P083);
26
+ * - passes its focus to the page (the host's own move into the frame, or a Tab).
27
+ *
28
+ * Plain ES2020, no imports and no build step, like the runtime. `__PLAN__` is its plan.
29
+ */
30
+ export const FRAME_SHELL_RUNTIME = String.raw`(() => {
31
+ "use strict";
32
+ const plan = __PLAN__;
33
+ const PREFIX = "porthole/shell/";
34
+ const own = location.origin;
35
+ let page = null;
36
+ try {
37
+ page = new URL(plan.page);
38
+ } catch (_) {}
39
+ // Only a page of this origin's sealed files: nothing else is ever framed here.
40
+ if (page === null || page.origin !== own || page.pathname.indexOf("/__porthole/f/") !== 0) {
41
+ return;
42
+ }
43
+ // Belt and braces: a plan that would give the page this origin frames nothing.
44
+ if (typeof plan.sandbox !== "string" || /allow-same-origin/i.test(plan.sandbox)) {
45
+ return;
46
+ }
47
+ const root = document.documentElement;
48
+ root.style.height = "100%";
49
+ document.body.style.margin = "0";
50
+ document.body.style.height = "100%";
51
+ document.body.style.background = "transparent";
52
+ const frame = document.createElement("iframe");
53
+ // Scripts only: never allow-same-origin (the page would have this origin, and so this realm).
54
+ frame.setAttribute("sandbox", plan.sandbox);
55
+ frame.setAttribute("allow", "clipboard-read 'none'; clipboard-write 'none'");
56
+ frame.setAttribute("referrerpolicy", "no-referrer");
57
+ frame.title = typeof plan.title === "string" ? plan.title : "Content";
58
+ frame.style.display = "block";
59
+ frame.style.border = "0";
60
+ frame.style.width = "100%";
61
+ frame.style.height = "100%";
62
+ frame.style.background = "transparent";
63
+ frame.src = page.href;
64
+ document.body.append(frame);
65
+
66
+ const framed = window.parent !== window;
67
+ const web = plan.origins.filter((origin) => origin.indexOf("https://") === 0 || origin.indexOf("http://") === 0);
68
+ // To the host: each web host origin (only the parent's is delivered); a desktop app's own scheme
69
+ // is no target a browser takes, so where one is listed and the parent isn't a known web origin,
70
+ // "*" (frame-ancestors holds the parent to the host origins; nothing said here is secret).
71
+ const toHost = (message) => {
72
+ if (!framed) {
73
+ return;
74
+ }
75
+ for (const origin of web) {
76
+ try {
77
+ window.parent.postMessage(message, origin);
78
+ } catch (_) {}
79
+ }
80
+ const parent = location.ancestorOrigins && location.ancestorOrigins.length > 0 ? location.ancestorOrigins[0] : null;
81
+ if (web.length < plan.origins.length && (parent === null || web.indexOf(parent) === -1)) {
82
+ try {
83
+ window.parent.postMessage(message, "*");
84
+ } catch (_) {}
85
+ }
86
+ };
87
+
88
+ // The shell's own state: its transient activation (and when it turned on), the page's focus.
89
+ // "since" is when the shell saw activation turn on, which a page that holds the thread the shell
90
+ // shares with it (WebKit, Firefox) can push later than the press: so the shell looks every 50 ms
91
+ // and beats as often, and each report says how long it went without a look before (gap). The
92
+ // host takes an activation the shell saw after a stall for uncertain (P111, after its review).
93
+ const activation = navigator.userActivation;
94
+ let active = false;
95
+ let since = 0;
96
+ let focused = false;
97
+ let lookedAt = performance.now();
98
+ let toldAt = 0;
99
+ const tell = (gap) => {
100
+ toldAt = performance.now();
101
+ toHost({
102
+ jsonrpc: "2.0",
103
+ method: PREFIX + "state",
104
+ params: {
105
+ supported: activation !== undefined,
106
+ active,
107
+ ago: active ? Math.max(0, Math.round(performance.now() - since)) : null,
108
+ focused,
109
+ gap: Math.max(0, Math.round(gap)),
110
+ },
111
+ });
112
+ };
113
+ const look = () => {
114
+ const now = performance.now();
115
+ const gap = now - lookedAt;
116
+ lookedAt = now;
117
+ const on = activation !== undefined && activation.isActive === true;
118
+ const inside = document.hasFocus();
119
+ let changed = false;
120
+ if (on !== active) {
121
+ active = on;
122
+ if (on) {
123
+ since = performance.now();
124
+ }
125
+ changed = true;
126
+ }
127
+ if (inside !== focused) {
128
+ focused = inside;
129
+ changed = true;
130
+ }
131
+ if (changed) {
132
+ tell(gap);
133
+ } else if (now - toldAt >= 50) {
134
+ // The heartbeat: the host knows the shell was looking, every 50 ms.
135
+ toldAt = now;
136
+ toHost({ jsonrpc: "2.0", method: PREFIX + "beat", params: { gap: Math.max(0, Math.round(gap)) } });
137
+ }
138
+ };
139
+ const tick = () => {
140
+ look();
141
+ requestAnimationFrame(tick);
142
+ };
143
+ requestAnimationFrame(tick);
144
+ setInterval(look, 50);
145
+ addEventListener("blur", look);
146
+ addEventListener("focus", (event) => {
147
+ // The host's move into the frame (or a Tab) lands here: on to the page.
148
+ if (event.target === window && document.activeElement !== frame) {
149
+ frame.focus();
150
+ }
151
+ look();
152
+ });
153
+ document.addEventListener("visibilitychange", look);
154
+ tell(0);
155
+
156
+ addEventListener("message", (event) => {
157
+ const source = event.source;
158
+ if (source !== null && source === frame.contentWindow) {
159
+ const data = event.data;
160
+ // Never the shell's words in the page's mouth.
161
+ if (data !== null && typeof data === "object" && typeof data.method === "string" && data.method.indexOf(PREFIX) === 0) {
162
+ return;
163
+ }
164
+ // What the shell knows now goes first: a call in a click's handler follows its activation.
165
+ look();
166
+ toHost(data);
167
+ return;
168
+ }
169
+ if (framed && source === window.parent && plan.origins.indexOf(event.origin) !== -1) {
170
+ const child = frame.contentWindow;
171
+ if (child !== null) {
172
+ child.postMessage(event.data, "*");
173
+ }
174
+ }
175
+ });
176
+
177
+ // The page's sealed address, renewed before it ends (P083): the page can't, the shell can.
178
+ if (plan.renew && typeof plan.renew.url === "string" && typeof plan.renew.expires === "number") {
179
+ const prefix = own + "/__porthole/f/";
180
+ let renewUrl = plan.renew.url;
181
+ const renewIn = (expires) => Math.max(30000, expires - Date.now() - 300000);
182
+ const renew = () => {
183
+ fetch(renewUrl, { credentials: "omit", cache: "no-store" })
184
+ .then((answer) => (answer.ok ? answer.json() : null))
185
+ .then((renewed) => {
186
+ if (!renewed || typeof renewed.base !== "string" || typeof renewed.expires !== "number" || renewed.base.indexOf(prefix) !== 0) {
187
+ setTimeout(renew, 60000);
188
+ return;
189
+ }
190
+ renewUrl = renewed.base + "__porthole/renew";
191
+ const child = frame.contentWindow;
192
+ if (child !== null) {
193
+ child.postMessage({ jsonrpc: "2.0", method: "porthole/files-renewed", params: { base: renewed.base } }, "*");
194
+ }
195
+ setTimeout(renew, renewIn(renewed.expires));
196
+ })
197
+ .catch(() => setTimeout(renew, 60000));
198
+ };
199
+ setTimeout(renew, renewIn(plan.renew.expires));
200
+ }
201
+ })();`;
202
+
203
+ export { SHELL_METHOD_PREFIX, SHELL_STATE_METHOD } from "../isolation/types.ts";
204
+
205
+ export interface FrameShellOptions {
206
+ /** The host origins: the shell takes messages from its parent only from these. */
207
+ readonly origins: readonly string[];
208
+ /** The page's address: absolute, on the shell's own origin, under `/__porthole/f/`. */
209
+ readonly page: string;
210
+ /** The page frame's accessible name. */
211
+ readonly title?: string;
212
+ /** Where the page's sealed address is renewed (`<base>__porthole/renew`), and when it ends. */
213
+ readonly renew?: { readonly url: string; readonly expires: number };
214
+ }
215
+
216
+ /** JSON that is safe inside a `<script>`. */
217
+ const scriptJson = (value: unknown): string =>
218
+ JSON.stringify(value)
219
+ .replace(/</gu, "\\u003c")
220
+ .replace(/\u2028/gu, "\\u2028")
221
+ .replace(/\u2029/gu, "\\u2029");
222
+
223
+ /** The shell's script for `options`: what its document runs, and what its CSP's hash names. */
224
+ export function frameShellScript(options: FrameShellOptions): string {
225
+ for (const origin of options.origins) {
226
+ const problem = hostOriginProblem(origin);
227
+ if (problem !== null) {
228
+ throw new TypeError(`frameShellScript: ${problem}`);
229
+ }
230
+ }
231
+ let page: URL;
232
+ try {
233
+ page = new URL(options.page);
234
+ } catch {
235
+ throw new TypeError("frameShellScript: page is an absolute URL");
236
+ }
237
+ if (!page.pathname.startsWith("/__porthole/f/") || page.hash !== "") {
238
+ throw new TypeError("frameShellScript: page is under /__porthole/f/, with no fragment");
239
+ }
240
+ return FRAME_SHELL_RUNTIME.replace("__PLAN__", () =>
241
+ scriptJson({
242
+ origins: options.origins,
243
+ page: page.href,
244
+ sandbox: PAGE_SANDBOX_TOKENS.join(" "),
245
+ ...(options.title === undefined ? {} : { title: options.title }),
246
+ ...(options.renew === undefined
247
+ ? {}
248
+ : { renew: { url: options.renew.url, expires: options.renew.expires } }),
249
+ }),
250
+ );
251
+ }
252
+
253
+ /** The shell's document around its script: nothing else (its CSP allows that script alone). */
254
+ export function frameShellDocument(script: string): string {
255
+ return `<!doctype html>
256
+ <html lang="en">
257
+ <head>
258
+ <meta charset="utf-8">
259
+ <meta name="color-scheme" content="light dark">
260
+ <meta name="referrer" content="no-referrer">
261
+ <title>Porthole</title>
262
+ </head>
263
+ <body>
264
+ <script>${script}</script>
265
+ </body>
266
+ </html>
267
+ `;
268
+ }
package/src/shell/plan.ts CHANGED
@@ -97,7 +97,8 @@ export function shellPlan(config: PortholeConfig): ShellPlan | null {
97
97
  themeVariables: Object.keys(config.theme.variables),
98
98
  themeEvent: config.theme.event,
99
99
  themeDocuments: config.theme.documents ?? "declared",
100
- storage: config.isolation === "opaque" ? "memory" : "native",
100
+ // An opaque origin has no storage: an opaque frame's, and a nested page's (P111).
101
+ storage: config.isolation === "opaque" || config.nested === true ? "memory" : "native",
101
102
  maxErrorReports: config.limits.maxErrorReports,
102
103
  maxPromptChars: config.limits.maxPromptChars,
103
104
  maxCopyChars: MAX_COPY_CHARS,
@@ -68,6 +68,9 @@ export const SHELL_RUNTIME = String.raw`(() => {
68
68
  }
69
69
  };
70
70
 
71
+ // Whether the page touched storage, told to the host once (P111): yes, never what it keeps.
72
+ let storageTouched = false;
73
+ let tellStorage = () => {};
71
74
  if (plan.storage === "memory") {
72
75
  // An opaque origin has no storage: a map stands in, for as long as the frame lives.
73
76
  const store = () => {
@@ -84,8 +87,18 @@ export const SHELL_RUNTIME = String.raw`(() => {
84
87
  };
85
88
  };
86
89
  for (const name of ["localStorage", "sessionStorage"]) {
90
+ const kept = store();
87
91
  try {
88
- Object.defineProperty(window, name, { configurable: true, value: store() });
92
+ Object.defineProperty(window, name, {
93
+ configurable: true,
94
+ get() {
95
+ if (!storageTouched) {
96
+ storageTouched = true;
97
+ tellStorage();
98
+ }
99
+ return kept;
100
+ },
101
+ });
89
102
  } catch (_) {}
90
103
  }
91
104
  }
@@ -115,6 +128,8 @@ export const SHELL_RUNTIME = String.raw`(() => {
115
128
  // takes the next load for that page, not for content that left). Anything else stays stopped:
116
129
  // another path of the origin, a file that isn't a page, another origin.
117
130
  let navigating = (_url) => {};
131
+ // A nested page's files' address renewed by its shell (P111), which the shell's message gives.
132
+ let adoptFiles = (_base) => false;
118
133
  let filesBase = plan.files && typeof plan.files.base === "string" ? plan.files.base : null;
119
134
  // Every address the page's files had (P083: a sealed one is renewed): what reports say as "./".
120
135
  const filesBases = filesBase === null ? [] : [filesBase];
@@ -880,6 +895,11 @@ export const SHELL_RUNTIME = String.raw`(() => {
880
895
  const filesSet = plan.files && plan.files.set && typeof plan.files.set === "object" ? { id: String(plan.files.set.id), version: String(plan.files.set.version) } : null;
881
896
  const notice = (url, more) => post({ method: "porthole/navigating", params: Object.assign({ url: ownAddresses(url).slice(0, 4096) }, filesSet === null ? {} : { set: filesSet }, more) });
882
897
  navigating = (url) => notice(url, {});
898
+ // The page used storage (P111): said once, as soon as the shell can speak; never keys or values.
899
+ tellStorage = () => post({ method: "porthole/storage-used", params: {} });
900
+ if (storageTouched) {
901
+ tellStorage();
902
+ }
883
903
  if (arrived) {
884
904
  notice(location.href, { arrived: true });
885
905
  }
@@ -887,28 +907,38 @@ export const SHELL_RUNTIME = String.raw`(() => {
887
907
  // it ends, the page moved to the new one in place (the same page, the same history entry). Its
888
908
  // modules keep the address they loaded from; the server serves their own loads till the session
889
909
  // ends.
910
+ const replace = History.prototype.replaceState;
911
+ // The files' origin, read from the address itself: a nested page's own origin is opaque (P111).
912
+ const filesOrigin = filesBase === null ? null : new NativeURL(filesBase).origin;
913
+ // A renewed sealed address (P083): one segment under the same origin's sealed prefix, checked
914
+ // through the platform's own functions (content may have replaced a prototype's, and the base
915
+ // decides where this page may go, ownPage); the page moves there in place.
916
+ const adopt = (base) => {
917
+ const prefix = filesOrigin + "/__porthole/f/";
918
+ if (filesBase === null || typeof base !== "string" || callOf(indexIn, base, [prefix]) !== 0 || callOf(indexIn, callOf(sliceOf, base, [prefix.length, -1]), ["/"]) !== -1 || callOf(sliceOf, base, [-1]) !== "/") {
919
+ return false;
920
+ }
921
+ const here = location.href;
922
+ if (here.indexOf(filesBase) === 0) {
923
+ try {
924
+ applyOf(replace, history, [history.state, "", base + here.slice(filesBase.length)]);
925
+ } catch (_) {}
926
+ }
927
+ filesBase = base;
928
+ filesBases.push(base);
929
+ return true;
930
+ };
931
+ adoptFiles = adopt;
890
932
  if (filesBase !== null && typeof plan.files.expires === "number") {
891
933
  const nativeFetch = window.fetch.bind(window);
892
- const replace = History.prototype.replaceState;
893
934
  const renewIn = (expires) => Math.max(30000, expires - Date.now() - 300000);
894
935
  const renew = () => {
895
936
  nativeFetch(filesBase + "__porthole/renew", { credentials: "omit", cache: "no-store" })
896
937
  .then((answer) => (answer.ok ? answer.json() : null))
897
938
  .then((renewed) => {
898
- // Checked through the platform's own functions: content may have replaced a prototype's,
899
- // and the base decides where this page may go (ownPage).
900
- const prefix = location.origin + "/__porthole/f/";
901
- if (!renewed || typeof renewed.base !== "string" || typeof renewed.expires !== "number" || callOf(indexIn, renewed.base, [prefix]) !== 0 || callOf(indexIn, callOf(sliceOf, renewed.base, [prefix.length, -1]), ["/"]) !== -1 || callOf(sliceOf, renewed.base, [-1]) !== "/") {
939
+ if (!renewed || typeof renewed.expires !== "number" || !adopt(renewed.base)) {
902
940
  return;
903
941
  }
904
- const here = location.href;
905
- if (here.indexOf(filesBase) === 0) {
906
- try {
907
- applyOf(replace, history, [history.state, "", renewed.base + here.slice(filesBase.length)]);
908
- } catch (_) {}
909
- }
910
- filesBase = renewed.base;
911
- filesBases.push(renewed.base);
912
942
  setTimeout(renew, renewIn(renewed.expires));
913
943
  })
914
944
  .catch(() => setTimeout(renew, 60000));
@@ -1465,6 +1495,11 @@ export const SHELL_RUNTIME = String.raw`(() => {
1465
1495
  case "ui/notifications/tool-input":
1466
1496
  void render(params.arguments);
1467
1497
  break;
1498
+ case "porthole/files-renewed":
1499
+ // From the page's parent only (the listener's check): a nested page's shell renewed its
1500
+ // files' sealed address, which this page can't (its loads aren't its origin's own, P111).
1501
+ adoptFiles(params.base);
1502
+ break;
1468
1503
  case "ui/notifications/host-context-changed":
1469
1504
  applyTheme(params);
1470
1505
  break;