@botiverse/porthole 0.14.4 → 0.14.6

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 (78) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/dist/check/page-source.js +1 -1
  3. package/dist/check/page.js +5 -1
  4. package/dist/compile/policy.d.ts +6 -0
  5. package/dist/compile/policy.js +6 -0
  6. package/dist/conformance/entry.d.ts +25 -6
  7. package/dist/conformance/entry.js +69 -18
  8. package/dist/conformance/harness/page.d.ts +12 -0
  9. package/dist/conformance/harness/page.js +95 -51
  10. package/dist/conformance/harness-source.js +1 -1
  11. package/dist/conformance/host-kit.js +39 -13
  12. package/dist/conformance/served.js +140 -0
  13. package/dist/files/paths.d.ts +2 -2
  14. package/dist/files/paths.js +12 -2
  15. package/dist/host/client.d.ts +8 -0
  16. package/dist/host/client.js +47 -17
  17. package/dist/host/create-host.d.ts +10 -0
  18. package/dist/host/create-host.js +126 -10
  19. package/dist/host/door.d.ts +41 -0
  20. package/dist/host/door.js +55 -0
  21. package/dist/host/locate.js +2 -0
  22. package/dist/host/locator.d.ts +55 -3
  23. package/dist/host/locator.js +152 -42
  24. package/dist/host/outcome.d.ts +58 -1
  25. package/dist/host/outcome.js +74 -6
  26. package/dist/host/types.d.ts +20 -4
  27. package/dist/isolation/served.d.ts +1 -1
  28. package/dist/isolation/served.js +35 -0
  29. package/dist/isolation/types.d.ts +20 -2
  30. package/dist/isolation/types.js +2 -0
  31. package/dist/platform/types.d.ts +22 -0
  32. package/dist/platform/web.js +55 -12
  33. package/dist/react/frame.d.ts +2 -1
  34. package/dist/react/view.d.ts +13 -0
  35. package/dist/react/view.js +101 -2
  36. package/dist/server/files.d.ts +11 -0
  37. package/dist/server/files.js +26 -4
  38. package/dist/server/host.js +106 -28
  39. package/dist/server/index.d.ts +2 -1
  40. package/dist/server/index.js +6 -1
  41. package/dist/server/pages.d.ts +32 -0
  42. package/dist/server/pages.js +82 -6
  43. package/dist/server/server.js +140 -11
  44. package/dist/server/types.d.ts +15 -1
  45. package/dist/transport/post-message.d.ts +1 -9
  46. package/dist/transport/post-message.js +2 -8
  47. package/docs/ADOPTING.md +58 -0
  48. package/docs/DECISIONS.md +290 -0
  49. package/docs/DESIGN.md +24 -16
  50. package/docs/THREAT_MODEL.md +98 -3
  51. package/package.json +1 -1
  52. package/src/check/page.ts +6 -1
  53. package/src/compile/policy.ts +7 -0
  54. package/src/conformance/entry.ts +129 -24
  55. package/src/conformance/harness/page.ts +112 -49
  56. package/src/conformance/host-kit.ts +53 -16
  57. package/src/conformance/served.ts +175 -0
  58. package/src/files/paths.ts +11 -2
  59. package/src/host/client.ts +59 -22
  60. package/src/host/create-host.ts +146 -18
  61. package/src/host/door.ts +97 -0
  62. package/src/host/locate.ts +2 -0
  63. package/src/host/locator.ts +208 -57
  64. package/src/host/outcome.ts +88 -7
  65. package/src/host/types.ts +20 -4
  66. package/src/isolation/served.ts +40 -1
  67. package/src/isolation/types.ts +19 -2
  68. package/src/platform/types.ts +21 -0
  69. package/src/platform/web.ts +55 -20
  70. package/src/react/frame.tsx +2 -1
  71. package/src/react/view.tsx +149 -2
  72. package/src/server/files.ts +37 -5
  73. package/src/server/host.ts +131 -46
  74. package/src/server/index.ts +6 -1
  75. package/src/server/pages.ts +101 -6
  76. package/src/server/server.ts +186 -19
  77. package/src/server/types.ts +16 -1
  78. package/src/transport/post-message.ts +1 -9
@@ -1,6 +1,7 @@
1
1
  import { parseViewer } from "../runtime/levels.ts";
2
2
  import type { PortholeConfig } from "../config/schema.ts";
3
- import type { FrameLocation, InstanceLocator } from "../isolation/types.ts";
3
+ import type { FrameLocation, InstanceLocator, InstanceRef } from "../isolation/types.ts";
4
+ import type { FrameOutcome } from "./types.ts";
4
5
 
5
6
  /**
6
7
  * The client half of the host kit (P066): a locator that asks the app's issue endpoint
@@ -9,17 +10,56 @@ import type { FrameLocation, InstanceLocator } from "../isolation/types.ts";
9
10
  * alone chooses the origin.
10
11
  */
11
12
 
12
- /** Why the issue endpoint gave no location: its status and code (`not-served`, `forbidden`, …). */
13
+ /**
14
+ * Why the issue endpoint gave no location: its status and code (`not-served`, `forbidden`, …), and
15
+ * for a refusal the host's own reason code where its answer gave one (`error.reason`, P125).
16
+ */
13
17
  export class LocateError extends Error {
14
18
  readonly status: number;
15
19
  readonly code: string;
20
+ /** The host's reason code from the issue answer's `error.reason` (a short lowercase token). */
21
+ readonly reason?: string;
16
22
 
17
- constructor(message: string, status: number, code: string) {
23
+ constructor(message: string, status: number, code: string, reason?: string) {
18
24
  super(message);
19
25
  this.name = "LocateError";
20
26
  this.status = status;
21
27
  this.code = code;
28
+ if (reason !== undefined) {
29
+ this.reason = reason;
30
+ }
31
+ }
32
+ }
33
+
34
+ /** A host's reason code, as an outcome's `code` takes it (`FrameOutcome.code`). */
35
+ const REASON_CODE = /^[a-z0-9-]{1,40}$/u;
36
+
37
+ /**
38
+ * What a display shows when the host's own issue endpoint refused it (P125): the host asked before
39
+ * it built any frame, so this is the host's word, never a frame's. Only a refusal maps (401: sign
40
+ * in, 403: denied, with the host's code if its answer gave one, 404: not found); anything else
41
+ * (5xx, a network failure, a malformed answer) is null, the host's own failure (`locate`), never
42
+ * `denied`.
43
+ */
44
+ export function outcomeOfRefusal(error: unknown): FrameOutcome | null {
45
+ if (!(error instanceof LocateError)) {
46
+ return null;
47
+ }
48
+ const code =
49
+ error.reason !== undefined && REASON_CODE.test(error.reason) ? { code: error.reason } : {};
50
+ // Only the issue endpoint's own refusal, by its shape (`{ error: { code } }` with the code that
51
+ // goes with its status): a bare 401, 403 or 404 from a router or a firewall in front of it is no
52
+ // answer of the host's, so it stays the host's own failure (`locate`).
53
+ if (error.status === 401 && error.code === "unauthenticated") {
54
+ return { state: "blocked", reason: "sign-in", status: 401 };
55
+ }
56
+ if (error.status === 403 && error.code === "forbidden") {
57
+ return { state: "blocked", reason: "denied", status: 403, ...code };
58
+ }
59
+ if (error.status === 404 && error.code === "not-found") {
60
+ return { state: "blocked", reason: "not-found", status: 404 };
22
61
  }
62
+ return null;
23
63
  }
24
64
 
25
65
  export interface HttpLocatorOptions {
@@ -35,68 +75,179 @@ export interface HttpLocatorOptions {
35
75
  const isRecord = (value: unknown): value is Record<string, unknown> =>
36
76
  value !== null && typeof value === "object" && !Array.isArray(value);
37
77
 
78
+ /**
79
+ * A location's door as an issue endpoint answered it (P125): passed on as it came, checked with the
80
+ * location before any frame is made (`checkFrameLocation`); null for none, "bad" for one that
81
+ * isn't `{ url: string }`.
82
+ */
83
+ export function doorOf(data: Record<string, unknown>): { readonly url: string } | null | "bad" {
84
+ if (data.door === undefined) {
85
+ return null;
86
+ }
87
+ return isRecord(data.door) && typeof data.door.url === "string" ? { url: data.door.url } : "bad";
88
+ }
89
+
90
+ /** The refusal an issue endpoint answered: its message, status, code and the host's reason. */
91
+ export function refusalOf(response: { readonly status: number }, data: unknown): LocateError {
92
+ const error = isRecord(data) && isRecord(data.error) ? data.error : {};
93
+ const code = typeof error.code === "string" ? error.code : "failed";
94
+ const reason =
95
+ typeof error.reason === "string" && REASON_CODE.test(error.reason) ? error.reason : undefined;
96
+ return new LocateError(
97
+ typeof error.message === "string"
98
+ ? error.message
99
+ : `The app's issue endpoint answered ${response.status}`,
100
+ response.status,
101
+ code,
102
+ reason,
103
+ );
104
+ }
105
+
106
+ /** A frame location as an issue endpoint answered it, checked in shape; throws a `LocateError`. */
107
+ export function locationOf(status: number, data: unknown): FrameLocation {
108
+ if (
109
+ !isRecord(data) ||
110
+ typeof data.url !== "string" ||
111
+ typeof data.origin !== "string" ||
112
+ (data.reports !== undefined && typeof data.reports !== "boolean")
113
+ ) {
114
+ throw new LocateError(
115
+ "The app's issue endpoint gave no frame location",
116
+ status,
117
+ "bad-location",
118
+ );
119
+ }
120
+ // Who is viewing, as the app's access decided (P087); one that doesn't parse is an error.
121
+ const viewer = data.viewer === undefined ? null : parseViewer(data.viewer);
122
+ if (data.viewer !== undefined && viewer === null) {
123
+ throw new LocateError(
124
+ "The app's issue endpoint named a viewer that isn't one",
125
+ status,
126
+ "bad-location",
127
+ );
128
+ }
129
+ const door = doorOf(data);
130
+ if (door === "bad") {
131
+ throw new LocateError(
132
+ "The app's issue endpoint named a door that isn't one",
133
+ status,
134
+ "bad-location",
135
+ );
136
+ }
137
+ return {
138
+ url: data.url,
139
+ origin: data.origin,
140
+ ...(typeof data.reports === "boolean" ? { reports: data.reports } : {}),
141
+ ...(viewer === null ? {} : { viewer }),
142
+ ...(door === null ? {} : { door }),
143
+ } satisfies FrameLocation;
144
+ }
145
+
146
+ /** POSTs `body` to the app's issue endpoint at `url`: its answer's JSON, or the refusal thrown. */
147
+ async function askIssue(
148
+ url: string,
149
+ body: Record<string, unknown>,
150
+ options: HttpLocatorOptions,
151
+ signal?: AbortSignal,
152
+ ): Promise<{ readonly status: number; readonly data: unknown }> {
153
+ const extra = typeof options.headers === "function" ? options.headers() : options.headers;
154
+ const headers = new Headers(extra);
155
+ headers.set("content-type", "application/json");
156
+ headers.set("accept", "application/json");
157
+ const response = await (options.fetch ?? fetch)(url, {
158
+ method: "POST",
159
+ headers,
160
+ credentials: options.credentials ?? "same-origin",
161
+ redirect: "error",
162
+ body: JSON.stringify(body),
163
+ ...(signal === undefined ? {} : { signal }),
164
+ });
165
+ const data: unknown = await response.json().catch(() => null);
166
+ if (!response.ok) {
167
+ const refused = refusalOf(response, data);
168
+ if (refused.code === "not-served") {
169
+ options.onNotServed?.();
170
+ }
171
+ throw refused;
172
+ }
173
+ return { status: response.status, data };
174
+ }
175
+
38
176
  /** A locator that POSTs `{ instance }` to the app's issue endpoint at `url`. */
39
177
  export function httpLocator(url: string, options: HttpLocatorOptions = {}): InstanceLocator {
40
178
  return {
41
179
  async locate(instance, signal) {
42
- const extra = typeof options.headers === "function" ? options.headers() : options.headers;
43
- const headers = new Headers(extra);
44
- headers.set("content-type", "application/json");
45
- headers.set("accept", "application/json");
46
- const response = await (options.fetch ?? fetch)(url, {
47
- method: "POST",
48
- headers,
49
- credentials: options.credentials ?? "same-origin",
50
- redirect: "error",
51
- body: JSON.stringify({ instance }),
52
- ...(signal === undefined ? {} : { signal }),
53
- });
54
- const data: unknown = await response.json().catch(() => null);
55
- if (!response.ok) {
56
- const error = isRecord(data) && isRecord(data.error) ? data.error : {};
57
- const code = typeof error.code === "string" ? error.code : "failed";
58
- if (code === "not-served") {
59
- options.onNotServed?.();
60
- }
61
- throw new LocateError(
62
- typeof error.message === "string"
63
- ? error.message
64
- : `The app's issue endpoint answered ${response.status}`,
65
- response.status,
66
- code,
67
- );
68
- }
69
- if (
70
- !isRecord(data) ||
71
- typeof data.url !== "string" ||
72
- typeof data.origin !== "string" ||
73
- (data.reports !== undefined && typeof data.reports !== "boolean")
74
- ) {
75
- throw new LocateError(
76
- "The app's issue endpoint gave no frame location",
77
- response.status,
78
- "bad-location",
79
- );
80
- }
81
- // Who is viewing, as the app's access decided (P087); one that doesn't parse is an error.
82
- const viewer = data.viewer === undefined ? null : parseViewer(data.viewer);
83
- if (data.viewer !== undefined && viewer === null) {
84
- throw new LocateError(
85
- "The app's issue endpoint named a viewer that isn't one",
86
- response.status,
87
- "bad-location",
88
- );
89
- }
90
- return {
91
- url: data.url,
92
- origin: data.origin,
93
- ...(typeof data.reports === "boolean" ? { reports: data.reports } : {}),
94
- ...(viewer === null ? {} : { viewer }),
95
- } satisfies FrameLocation;
180
+ const { status, data } = await askIssue(url, { instance }, options, signal);
181
+ return locationOf(status, data);
96
182
  },
97
183
  };
98
184
  }
99
185
 
186
+ /**
187
+ * What a clean address resolves to (P125): its instance and an issued location for it (a ticket,
188
+ * a door), or the outcome the host's issue endpoint answered instead (signed out, denied, not
189
+ * found), before any frame is built. A clean address is for a tab; a frame shows what it names
190
+ * through `issue`.
191
+ */
192
+ export type Resolved =
193
+ | { readonly instance: InstanceRef; readonly location: FrameLocation }
194
+ | { readonly outcome: FrameOutcome };
195
+
196
+ /**
197
+ * A locator whose first call answers `location` (one a `resolve` got), once, and every later one
198
+ * asks `then` (P125): bound to the one display that resolved it, so no other view of the same
199
+ * instance can take its tickets, and every run after the first issues afresh.
200
+ */
201
+ export function oneShot(location: FrameLocation, then: InstanceLocator): InstanceLocator {
202
+ let first: FrameLocation | null = location;
203
+ return {
204
+ locate(instance, signal) {
205
+ const given = first;
206
+ first = null;
207
+ return given === null ? then.locate(instance, signal) : Promise.resolve(given);
208
+ },
209
+ };
210
+ }
211
+
212
+ /** Whether a value is an instance as an issue endpoint gives it back: `{ scope, document }`. */
213
+ const isInstance = (value: unknown): value is InstanceRef =>
214
+ isRecord(value) &&
215
+ typeof value.scope === "string" &&
216
+ isRecord(value.document) &&
217
+ typeof value.document.kind === "string";
218
+
219
+ /**
220
+ * Asks the app's issue endpoint at `url` what the clean address `address` names, for this viewer
221
+ * (`{ address }`, P125). A refusal (401, 403, 404) is an outcome; anything else that isn't a
222
+ * location is thrown, the host's own failure.
223
+ */
224
+ export async function resolveAddress(
225
+ url: string,
226
+ address: string,
227
+ options: HttpLocatorOptions = {},
228
+ signal?: AbortSignal,
229
+ ): Promise<Resolved> {
230
+ let answer: { readonly status: number; readonly data: unknown };
231
+ try {
232
+ answer = await askIssue(url, { address }, options, signal);
233
+ } catch (error) {
234
+ const outcome = outcomeOfRefusal(error);
235
+ if (outcome !== null) {
236
+ return { outcome };
237
+ }
238
+ throw error;
239
+ }
240
+ const { status, data } = answer;
241
+ if (!isRecord(data) || !isInstance(data.instance)) {
242
+ throw new LocateError(
243
+ "The app's issue endpoint resolved the address to no instance",
244
+ status,
245
+ "bad-location",
246
+ );
247
+ }
248
+ return { instance: data.instance, location: locationOf(status, data) };
249
+ }
250
+
100
251
  /** What the page knows of content (`ContentHost.meta`, as the app's own meta carries it). */
101
252
  export interface ServedMeta {
102
253
  readonly served: boolean;
@@ -15,10 +15,40 @@ import type { FrameBlock, FrameOutcome } from "./types.ts";
15
15
  * - an app from a host's own gateway is loaded on its first load, unless its page says it is
16
16
  * blocked (the gateway may send the same message);
17
17
  * - nothing settled in `timeoutMs`: a timeout.
18
+ *
19
+ * Who may say how loading went (P124). A frame status is taken only from a document of Porthole's
20
+ * server, a host's gateway or the host's own pages, never from content: its words draw the host's
21
+ * fallback, in Porthole's wording, with a link. Nothing in a message tells who sent it (a page
22
+ * that shares the frame's window and origin can send the same bytes), so the host goes by what
23
+ * content can't change: where the frame's documents come from, and in what order they speak.
24
+ *
25
+ * - An opaque frame's only document is the host's own shell, with the content in it: no page of
26
+ * Porthole's server ever loads there, so its every frame status is the content's.
27
+ * - A frame that speaks (a served shell, a view through the sandbox proxy, a nested page's shell)
28
+ * says Porthole's own word first in every document that holds content, before any of the
29
+ * content's code runs: the shell's hello, the proxy's ready (a nested page's own runtime's hello
30
+ * reaches the host before anything its page says, through its shell). A status page says
31
+ * nothing but its frame status. So a frame status is taken only before anything else was heard
32
+ * from the frame in this run: from its first document, which then was a page of Porthole's.
33
+ * After that, it came from the content (or from a later document, which in a display without
34
+ * files starts the display again, whose first document then says how it went).
35
+ * That holds against the display's own content, not against another display's (P125): a
36
+ * scope's displays share its origin, and one that runs reaches a new document of another's
37
+ * before that document's first script, where it can speak first, or stop or rewrite it so it
38
+ * says nothing. So the rule holds only where no content runs on the frame's origin: a nested
39
+ * page's shell, on a label of its own (P114).
40
+ * - A served display that isn't nested (a served shell, a page, a view through the sandbox proxy)
41
+ * takes no frame status from its frame at all, and infers nothing from its silence (a load and
42
+ * no word is a timeout, never `framing`): its door said how loading went before the frame was
43
+ * made (P125, `watchDoor`), from an opaque frame of the host's that no content can reach.
44
+ * - A url-mode app has no shell to speak first, and shares its origin with the entry page and the
45
+ * status pages: its frame status is taken as before, the app's word (THREAT_MODEL S6).
46
+ *
47
+ * A frame status not taken is dropped, and told once a run to the host (`refused`).
18
48
  */
19
49
 
20
50
  /** How long after a load the frame's word may still arrive. */
21
- const SETTLE_MS = 400;
51
+ export const SETTLE_MS = 400;
22
52
 
23
53
  const BLOCKS: readonly FrameBlock[] = [
24
54
  "framing",
@@ -32,7 +62,8 @@ const BLOCKS: readonly FrameBlock[] = [
32
62
  ];
33
63
 
34
64
  interface FrameStatus {
35
- readonly state: "entering" | "blocked";
65
+ /** `open` is a door's word alone (P125): its display may be framed. */
66
+ readonly state: "entering" | "open" | "blocked";
36
67
  readonly reason?: FrameBlock;
37
68
  readonly status?: number;
38
69
  readonly code?: string;
@@ -56,8 +87,8 @@ export function parseFrameStatus(data: unknown): FrameStatus | null {
56
87
  if (params === null || typeof params !== "object") {
57
88
  return null;
58
89
  }
59
- if (params.state === "entering") {
60
- return { state: "entering" };
90
+ if (params.state === "entering" || params.state === "open") {
91
+ return { state: params.state };
61
92
  }
62
93
  if (params.state !== "blocked") {
63
94
  return null;
@@ -77,6 +108,16 @@ export function parseFrameStatus(data: unknown): FrameStatus | null {
77
108
  };
78
109
  }
79
110
 
111
+ /** Whether a message names the frame-status method, well formed or not. */
112
+ const namesFrameStatus = (data: unknown): boolean =>
113
+ data !== null &&
114
+ typeof data === "object" &&
115
+ (data as { method?: unknown }).method === FRAME_STATUS_METHOD;
116
+
117
+ /** What the host is told of a frame status it didn't take (P124). */
118
+ export const UNTRUSTED_FRAME_STATUS =
119
+ "A frame status (porthole/notifications/frame-status) came from the frame's content, not from a page of Porthole's server: not taken (P124)";
120
+
80
121
  export interface OutcomeWatch {
81
122
  /** The frame finished its handshake. */
82
123
  initialized(): void;
@@ -88,14 +129,26 @@ export function watchOutcome(options: {
88
129
  readonly source: FrameSource;
89
130
  /** Whether the frame speaks for itself (a shell or a view): then its handshake is the proof. */
90
131
  readonly speaks: boolean;
132
+ /**
133
+ * The config nests its pages (P111): the frame holds Porthole's shell, on a label no content
134
+ * runs on, whose first word may be a frame status. Any other served frame that speaks takes
135
+ * none: its door told (P125).
136
+ */
137
+ readonly nested?: boolean;
91
138
  readonly timeoutMs: number;
92
139
  readonly onOutcome: (outcome: FrameOutcome) => void;
140
+ /** A frame status that wasn't taken, once a run (P124): the host's to hear, never shown. */
141
+ readonly refused?: (message: string) => void;
93
142
  }): OutcomeWatch {
94
143
  const { surface, source, speaks } = options;
144
+ const nested = options.nested === true;
95
145
  const reports = source.kind === "url" && source.reports === true;
96
146
  // The first outcome; after a load, only a later block (a status page, access revoked) is told.
97
147
  let decided: FrameOutcome["state"] | null = null;
98
148
  let heard = false;
149
+ // Whether the frame said anything but a frame status in this run (P124).
150
+ let spoke = false;
151
+ let told = false;
99
152
  let entering = false;
100
153
  let loads = 0;
101
154
  const timers = new Set<ReturnType<typeof setTimeout>>();
@@ -127,9 +180,23 @@ export function watchOutcome(options: {
127
180
  options.onOutcome(outcome);
128
181
  }
129
182
  };
183
+ // Whether a frame status may be the word of a page of Porthole's (P124, P125, above).
184
+ const mayBePorthole = () => source.kind === "url" && (!speaks || (nested && !spoke));
130
185
  const unlisten = surface.transport.listen((data) => {
131
186
  heard = true;
187
+ if (!namesFrameStatus(data)) {
188
+ spoke = true;
189
+ return;
190
+ }
191
+ if (!mayBePorthole()) {
192
+ if (!told) {
193
+ told = true;
194
+ options.refused?.(UNTRUSTED_FRAME_STATUS);
195
+ }
196
+ return;
197
+ }
132
198
  const status = parseFrameStatus(data);
199
+ // `open` is a door's word (P125), never a frame's: nothing here.
133
200
  if (status?.state === "entering") {
134
201
  entering = true;
135
202
  } else if (status?.state === "blocked") {
@@ -147,16 +214,30 @@ export function watchOutcome(options: {
147
214
  decide({ state: "blocked", reason: "framing" });
148
215
  }
149
216
  });
217
+ // A served frame whose door told (P125): a load from which nothing spoke shows nothing, and
218
+ // no one can say why (another display of its scope may have stopped or rewritten it): a timeout,
219
+ // without waiting the whole of it, never `framing`.
220
+ const silent = () =>
221
+ later(SETTLE_MS, () => {
222
+ if (!spoke) {
223
+ decide({ state: "timeout" });
224
+ }
225
+ });
150
226
  const unload = surface.onLoad(() => {
151
227
  loads += 1;
152
228
  if (decided !== null) {
153
229
  return;
154
230
  }
155
231
  if (speaks) {
156
- // A served shell or view says something before its first load ends; one that never did
157
- // never ran. (An opaque srcdoc frame always runs: its handshake decides.)
232
+ // A served shell or view says something before its first load ends; a nested page's shell
233
+ // that never did never ran. Any other served frame's silence tells nothing (P125). (An
234
+ // opaque srcdoc frame always runs: its handshake decides.)
158
235
  if (source.kind === "url") {
159
- neverRan();
236
+ if (nested) {
237
+ neverRan();
238
+ } else {
239
+ silent();
240
+ }
160
241
  }
161
242
  return;
162
243
  }
package/src/host/types.ts CHANGED
@@ -125,7 +125,21 @@ export type HostErrorKind =
125
125
  /** A display couldn't be made (the React binding: a bad config, a missing handler). */
126
126
  | "mount"
127
127
  /** An annotation call was refused (an invalid mark, an unknown id). */
128
- | "annotations";
128
+ | "annotations"
129
+ /**
130
+ * A frame status (`porthole/notifications/frame-status`) the host didn't take, since it came
131
+ * from the frame's content, not from a page of Porthole's server (P124): never shown, never an
132
+ * outcome. Told once a run. A served display that isn't nested takes none from its frame at
133
+ * all: its door says how loading went (P125).
134
+ */
135
+ | "frame-status"
136
+ /**
137
+ * A served display that has a door (P125: a served shell, a page that isn't nested, the sandbox
138
+ * proxy) was located without one: the server is older than the host (upgrade it first), or a
139
+ * locator dropped `FrameLocation.door`. The display shows, but no frame status is taken, so a
140
+ * block is told as a timeout. Told once a mount.
141
+ */
142
+ | "door";
129
143
 
130
144
  /** What a display reports, by event name. */
131
145
  /** Why a display's frame showed nothing of its content (`FrameOutcome`). */
@@ -150,9 +164,11 @@ export type FrameBlock =
150
164
  /**
151
165
  * How a display's document went: it loaded (a shell or view finished its handshake; a url-mode
152
166
  * app's document loaded), it was blocked (and why), or nothing settled in
153
- * `limits.loadTimeoutMs`. Once per run, plus a later `blocked` if the frame shows a status page
154
- * after it loaded (access revoked mid-session). A host shows its own fallback ("Open in a new
155
- * tab") on anything but loaded.
167
+ * `limits.loadTimeoutMs`. Once per run, plus a later `blocked` if a url-mode app's frame shows a
168
+ * status page after it loaded (access revoked mid-session). A host shows its own fallback ("Open in
169
+ * a new tab") on anything but loaded. A `blocked` is the word of a page of Porthole's server (or a
170
+ * host's gateway, or its own pages), never of a frame's content, except a url-mode app's (P124):
171
+ * in a frame that speaks, only its run's first document says it; an opaque frame never does.
156
172
  */
157
173
  export type FrameOutcome =
158
174
  | { readonly state: "loaded" }
@@ -1,4 +1,4 @@
1
- import type { FrameLocation } from "./types.ts";
1
+ import { DOOR_PATH, type FrameLocation } from "./types.ts";
2
2
 
3
3
  /**
4
4
  * Checks for served frames (domain and external isolation), shared by the host's mount and by
@@ -172,6 +172,9 @@ export function checkFrameLocation(
172
172
  `The located origin ${location.origin} isn't a nested page's own: its shell is served on a label of its own alone (${PORTHOLE_LABEL_PREFIX}<24 base32>, nestedLabelOf(label), P114), never beside other content`,
173
173
  );
174
174
  }
175
+ if (location.door !== undefined) {
176
+ checkDoor(location.door, location.origin, nested);
177
+ }
175
178
  const host = parseOrigin(hostOrigin);
176
179
  if (host !== null && maybeSameSite(origin.host, host.host)) {
177
180
  throw new TypeError(
@@ -180,6 +183,42 @@ export function checkFrameLocation(
180
183
  }
181
184
  }
182
185
 
186
+ /**
187
+ * A location's door (P125): on the location's own origin, at Porthole's door path, never for a
188
+ * nested page (its shell's label serves no door: its first document is Porthole's own, P114).
189
+ * Named by its origin only in what is thrown: the door's address carries a ticket.
190
+ */
191
+ function checkDoor(door: unknown, origin: string, nested: boolean): void {
192
+ const url =
193
+ door !== null && typeof door === "object" ? (door as { url?: unknown }).url : undefined;
194
+ if (typeof url !== "string") {
195
+ throw new TypeError("The located door is not { url }");
196
+ }
197
+ if (nested) {
198
+ throw new TypeError(
199
+ "A nested page is located with no door: its shell is Porthole's own first document (P125)",
200
+ );
201
+ }
202
+ let parsed: URL;
203
+ try {
204
+ parsed = new URL(url);
205
+ } catch {
206
+ throw new TypeError("The located door is not a URL");
207
+ }
208
+ // The load-bearing check (P125): the door is on the display's own origin, the one its issue
209
+ // located. Its path is pinned below too, for now; a later relaxation of the path (a content
210
+ // Worker's own door route) must keep this check as it is, apart from the path's.
211
+ if (parsed.origin !== origin) {
212
+ throw new TypeError(
213
+ `The located door is not on its location's origin ${JSON.stringify(origin)} (it is on ${JSON.stringify(safeOrigin(url))})`,
214
+ );
215
+ }
216
+ // Porthole's door route (P125): pinned for now, not what makes the door the display's.
217
+ if (!parsed.pathname.startsWith(DOOR_PATH) || parsed.hash !== "") {
218
+ throw new TypeError(`The located door is not at ${DOOR_PATH}<key> on its origin`);
219
+ }
220
+ }
221
+
183
222
  /** A URL's origin, for a message: never its path or query (a ticket). */
184
223
  function safeOrigin(url: string): string {
185
224
  try {
@@ -69,8 +69,22 @@ export interface FrameLocation {
69
69
  * the display's bridge checks each runtime method's level against it.
70
70
  */
71
71
  readonly viewer?: ViewerInfo;
72
+ /**
73
+ * The display's door (P125): a page of Porthole's on the same origin, at
74
+ * `/__porthole/door/<instance key>?ticket=<its own ticket>`, that says how loading went before
75
+ * any content is framed. The host frames it opaque and hidden, and takes its one frame status
76
+ * (`open`, or `blocked` and why); then frames `url`. Given for a served shell, a page that isn't
77
+ * nested and the sandbox proxy: content of the scope runs on their origin, so no frame status
78
+ * from their own frame is ever Porthole's. A host that is given none for one of them takes no
79
+ * frame status at all (a load that shows nothing is a timeout) and says so (`hostError` kind
80
+ * `door`). A custom locator passes it on as it came.
81
+ */
82
+ readonly door?: { readonly url: string };
72
83
  }
73
84
 
85
+ /** Where a served display's door is (P125): `<origin>/__porthole/door/<instance key>`. */
86
+ export const DOOR_PATH = "/__porthole/door/";
87
+
74
88
  /** The message a served page sends its host about loading (own-prefixed, P040). */
75
89
  export const FRAME_STATUS_METHOD = "porthole/notifications/frame-status";
76
90
 
@@ -94,8 +108,9 @@ export const STORAGE_USED_METHOD = "porthole/storage-used";
94
108
 
95
109
  /**
96
110
  * Turns an instance into the URL its frame loads. Implemented by the host, usually by asking its
97
- * own backend (which authenticates the viewer and calls `PortholeServer.issue`). Called on every
98
- * mount: a ticket is good once.
111
+ * own backend (which authenticates the viewer and calls `PortholeServer.issue`). Called for every
112
+ * run (a mount, a reload, a restart after the content navigated away): answer each with a fresh
113
+ * issue, since a ticket and a door ticket are each good once (P125).
99
114
  */
100
115
  export interface InstanceLocator {
101
116
  locate(instance: InstanceRef, signal?: AbortSignal): Promise<FrameLocation>;
@@ -114,4 +129,6 @@ export type FrameSource =
114
129
  readonly reports?: boolean;
115
130
  /** The issue's viewer (`FrameLocation.viewer`), for the display's bridge. */
116
131
  readonly viewer?: ViewerInfo;
132
+ /** The display's door (`FrameLocation.door`, P125): framed before `url`. */
133
+ readonly door?: string;
117
134
  };
@@ -80,8 +80,29 @@ export interface Surface {
80
80
  destroy(): void;
81
81
  }
82
82
 
83
+ /**
84
+ * A served display's door (P125): a hidden frame of the host's own at the door's address,
85
+ * sandboxed with scripts alone (an opaque origin no content shares), with no `allow`, no name, out
86
+ * of focus and of the accessibility tree. Its one frame status is heard from its window and the
87
+ * opaque origin (`"null"`) alone.
88
+ */
89
+ export interface DoorSurface {
90
+ readonly transport: Transport;
91
+ /** The platform's own element for the door (an `HTMLIFrameElement` on the web). */
92
+ readonly element: unknown;
93
+ /** Every load of the door's document. */
94
+ onLoad(listener: () => void): () => void;
95
+ destroy(): void;
96
+ }
97
+
83
98
  export interface PlatformHost<Container = HTMLElement> {
84
99
  readonly name: string;
85
100
  /** Creates an isolated frame in `container`, its policy set before its document loads. */
86
101
  createSurface(container: Container, spec: SurfaceSpec): Surface;
102
+ /**
103
+ * Creates a served display's door in `container` (P125), loading `url`. A platform without one
104
+ * shows served displays that have a door without it: no frame status is taken from them, and
105
+ * the host hears a `door` host error.
106
+ */
107
+ createDoor?(container: Container, spec: { readonly url: string }): DoorSurface;
87
108
  }