@botiverse/porthole 0.14.16 → 0.14.18
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/CHANGELOG.md +29 -0
- package/dist/check/page-source.js +1 -1
- package/dist/conformance/harness-source.js +1 -1
- package/dist/host/pages-of-set.d.ts +3 -1
- package/dist/host/pages-of-set.js +17 -3
- package/dist/isolation/served.d.ts +8 -0
- package/dist/isolation/served.js +16 -0
- package/dist/protocol/mcp-apps.d.ts +5 -3
- package/dist/protocol/mcp-apps.js +5 -3
- package/dist/protocol/shell-adapter.js +5 -0
- package/dist/server/host.d.ts +14 -1
- package/dist/server/host.js +8 -3
- package/dist/server/nested.d.ts +5 -4
- package/dist/server/nested.js +8 -6
- package/dist/server/pages.d.ts +16 -2
- package/dist/server/pages.js +51 -16
- package/dist/server/server.js +26 -2
- package/dist/server/types.d.ts +8 -0
- package/dist/shell/frame.d.ts +6 -0
- package/dist/shell/frame.js +16 -3
- package/dist/shell/runtime.js +13 -3
- package/docs/ADOPTING.md +12 -3
- package/docs/DECISIONS.md +210 -4
- package/docs/DESIGN.md +2 -1
- package/docs/THREAT_MODEL.md +21 -4
- package/package.json +1 -1
- package/src/host/pages-of-set.ts +18 -3
- package/src/isolation/served.ts +21 -0
- package/src/protocol/mcp-apps.ts +5 -3
- package/src/protocol/shell-adapter.ts +5 -0
- package/src/server/host.ts +22 -4
- package/src/server/nested.ts +13 -5
- package/src/server/pages.ts +72 -18
- package/src/server/server.ts +32 -1
- package/src/server/types.ts +8 -0
- package/src/shell/frame.ts +22 -3
- package/src/shell/runtime.ts +13 -3
package/src/server/nested.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import { frameShellDocument, frameShellScript, type FrameShellOptions } from "../shell/frame.ts";
|
|
10
10
|
import { onNestedLabel, PORTHOLE_LABEL_PREFIX } from "../isolation/served.ts";
|
|
11
11
|
import { sha256, utf8 } from "./crypto.ts";
|
|
12
|
+
import { ancestorsOf } from "./pages.ts";
|
|
12
13
|
import { base32 } from "./keys.ts";
|
|
13
14
|
|
|
14
15
|
export { isNestedLabel, PORTHOLE_LABEL_PREFIX } from "../isolation/served.ts";
|
|
@@ -46,13 +47,14 @@ const base64 = (bytes: Uint8Array): string => btoa(String.fromCharCode(...bytes)
|
|
|
46
47
|
/**
|
|
47
48
|
* The shell's CSP (a header): its own script alone runs (by its hash), it frames and fetches
|
|
48
49
|
* nothing but its origin's sealed files (the page, the renewal), and only the host origins frame
|
|
49
|
-
* it. Nothing of the page's runs here: the page is a
|
|
50
|
-
* this (a framed URL's document never does).
|
|
50
|
+
* it (with, above them, a host's `framedBy`, P141). Nothing of the page's runs here: the page is a
|
|
51
|
+
* document of its own, which inherits none of this (a framed URL's document never does).
|
|
51
52
|
*/
|
|
52
53
|
export function nestedShellCsp(
|
|
53
54
|
script: string,
|
|
54
55
|
origin: string,
|
|
55
56
|
hostOrigins: readonly string[],
|
|
57
|
+
framedBy: readonly string[] = [],
|
|
56
58
|
): string {
|
|
57
59
|
const files = `${origin}${NESTED_FILES_PREFIX}`;
|
|
58
60
|
const directives: CspDirective[] = [
|
|
@@ -62,7 +64,7 @@ export function nestedShellCsp(
|
|
|
62
64
|
["connect-src", files],
|
|
63
65
|
["base-uri", "'none'"],
|
|
64
66
|
["form-action", "'none'"],
|
|
65
|
-
["frame-ancestors", ...hostOrigins],
|
|
67
|
+
["frame-ancestors", ...ancestorsOf(hostOrigins, framedBy).split(" ")],
|
|
66
68
|
];
|
|
67
69
|
return serializeCsp(directives);
|
|
68
70
|
}
|
|
@@ -88,7 +90,12 @@ export function nestedShellResponse(
|
|
|
88
90
|
const script = frameShellScript(options);
|
|
89
91
|
const headers = new Headers({
|
|
90
92
|
"content-type": "text/html; charset=utf-8",
|
|
91
|
-
"content-security-policy": nestedShellCsp(
|
|
93
|
+
"content-security-policy": nestedShellCsp(
|
|
94
|
+
script,
|
|
95
|
+
options.origin,
|
|
96
|
+
options.origins,
|
|
97
|
+
options.framedBy,
|
|
98
|
+
),
|
|
92
99
|
"cache-control": "no-store",
|
|
93
100
|
"referrer-policy": "no-referrer",
|
|
94
101
|
"x-content-type-options": "nosniff",
|
|
@@ -107,7 +114,8 @@ export function nestedShellResponse(
|
|
|
107
114
|
|
|
108
115
|
/**
|
|
109
116
|
* Who may frame a nested page and its files' answers: its shell (the shell's own origin, P114) and,
|
|
110
|
-
* above it, the host origins (`frame-ancestors` names every ancestor)
|
|
117
|
+
* above it, the host origins and a host's `framedBy` (`frame-ancestors` names every ancestor; P141):
|
|
118
|
+
* `frameAncestors` is the server's whole list.
|
|
111
119
|
*/
|
|
112
120
|
export const nestedAncestors = (origin: string, frameAncestors: string): string =>
|
|
113
121
|
`${origin} ${frameAncestors}`;
|
package/src/server/pages.ts
CHANGED
|
@@ -1,16 +1,55 @@
|
|
|
1
1
|
import { DOOR_SANDBOX_TOKENS } from "../compile/policy.ts";
|
|
2
|
+
import { framedByProblem } from "../isolation/served.ts";
|
|
2
3
|
import { FRAME_STATUS_METHOD } from "../isolation/types.ts";
|
|
3
4
|
import type { FrameBlock } from "../host/types.ts";
|
|
4
5
|
import { base64url, sha256, utf8 } from "./crypto.ts";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* The server's own pages on a content origin: why it shows nothing of the content, and the page a
|
|
8
|
-
* framed app is entered through. Plain, framable only by the host origins,
|
|
9
|
-
* the page that frames it how loading went
|
|
10
|
-
* the host origins only, so a host can show
|
|
11
|
-
* the host-agnostic mechanism's; a host that
|
|
9
|
+
* framed app is entered through. Plain, framable only by the host origins (and, above them, a
|
|
10
|
+
* host's `framedBy`, P141), never cached. Each tells the page that frames it how loading went
|
|
11
|
+
* (`porthole/notifications/frame-status`, P040), posted to the host origins only, so a host can show
|
|
12
|
+
* its own fallback ("Open in a new tab"). Their words are the host-agnostic mechanism's; a host that
|
|
13
|
+
* wants its own wording serves its own page in front.
|
|
12
14
|
*/
|
|
13
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Whether a page's `frame-ancestors` holds its parent to its host origins: it names no one else.
|
|
18
|
+
* Where it does (a host's `framedBy`, P141), its parent may be an outer page, and the page says
|
|
19
|
+
* `*` only to a parent the engine names as a host origin (`strictParent`, below).
|
|
20
|
+
*/
|
|
21
|
+
const heldToHosts = (frameAncestors: string, hostOrigins: readonly string[]): boolean =>
|
|
22
|
+
frameAncestors
|
|
23
|
+
.split(" ")
|
|
24
|
+
.filter((each) => each !== "")
|
|
25
|
+
.every((each) => hostOrigins.includes(each));
|
|
26
|
+
|
|
27
|
+
/** The attribute that says so, on a page's `<main>`: none where its parent is held to its hosts. */
|
|
28
|
+
const strictParent = (frameAncestors: string, hostOrigins: readonly string[]): string =>
|
|
29
|
+
heldToHosts(frameAncestors, hostOrigins) ? "" : ' data-porthole-strict-parent=""';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Who may frame a page: the host origins, then a host's `framedBy` (P141), each checked (they go
|
|
33
|
+
* into a header).
|
|
34
|
+
*/
|
|
35
|
+
export function ancestorsOf(
|
|
36
|
+
hostOrigins: readonly string[],
|
|
37
|
+
framedBy: readonly string[] = [],
|
|
38
|
+
): string {
|
|
39
|
+
for (const origin of framedBy) {
|
|
40
|
+
const problem = framedByProblem(origin);
|
|
41
|
+
if (problem !== null) {
|
|
42
|
+
throw new TypeError(`framedBy: ${problem}`);
|
|
43
|
+
}
|
|
44
|
+
if (hostOrigins.includes(origin)) {
|
|
45
|
+
throw new TypeError(`framedBy: ${origin} is a host origin already`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return [...hostOrigins, ...framedBy.filter((each, at) => framedBy.indexOf(each) === at)].join(
|
|
49
|
+
" ",
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
14
53
|
const escape = (text: string): string =>
|
|
15
54
|
text.replace(/[&<>"']/gu, (char) => `&#${char.charCodeAt(0)};`);
|
|
16
55
|
|
|
@@ -27,17 +66,21 @@ export const SESSION_PATH = "/__porthole/session";
|
|
|
27
66
|
* Tells the page that frames this one how loading went, at each web host origin. A desktop app's
|
|
28
67
|
* own scheme (`ferry://app`) is no target origin a browser takes (postMessage throws), so where
|
|
29
68
|
* the host origins hold one, and the parent isn't known to be a web host origin, the word goes to
|
|
30
|
-
* the parent as `*`: these pages' `frame-ancestors`
|
|
31
|
-
*
|
|
69
|
+
* the parent as `*`: these pages' `frame-ancestors` hold their parent to the host origins, and what
|
|
70
|
+
* they say is no secret (P070). Where it names an outer page too (`framedBy`, P141), the parent may be
|
|
71
|
+
* the outer page: then `*` goes only to a parent the engine names (`location.ancestorOrigins`,
|
|
72
|
+
* not masked as "null") as one of the host origins, so an outer page is never told. One origin
|
|
73
|
+
* failing never stops the page.
|
|
32
74
|
*/
|
|
33
75
|
const say = (): string =>
|
|
34
|
-
`const
|
|
76
|
+
`const strict = main.dataset.portholeStrictParent !== undefined;
|
|
77
|
+
const say = (params) => {
|
|
35
78
|
if (window.parent === window) return;
|
|
36
79
|
const message = { jsonrpc: "2.0", method: ${JSON.stringify(FRAME_STATUS_METHOD)}, params };
|
|
37
80
|
const web = hosts.filter((origin) => origin.startsWith("https://") || origin.startsWith("http://"));
|
|
38
81
|
for (const origin of web) { try { window.parent.postMessage(message, origin); } catch (_) {} }
|
|
39
82
|
const parent = location.ancestorOrigins && location.ancestorOrigins.length > 0 ? location.ancestorOrigins[0] : null;
|
|
40
|
-
if (web.length < hosts.length && (parent === null || !web.includes(parent))) { try { window.parent.postMessage(message, "*"); } catch (_) {} }
|
|
83
|
+
if (web.length < hosts.length && (parent === null || !web.includes(parent)) && (!strict || (parent !== null && hosts.includes(parent)))) { try { window.parent.postMessage(message, "*"); } catch (_) {} }
|
|
41
84
|
};`;
|
|
42
85
|
|
|
43
86
|
/** Tells the host the page is blocked, and why (from the page's data attributes). */
|
|
@@ -123,7 +166,7 @@ export function statusPage(
|
|
|
123
166
|
hostOrigins: readonly string[] = [],
|
|
124
167
|
code?: string,
|
|
125
168
|
): Response {
|
|
126
|
-
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escape(title)}</title><style>${STYLE}</style></head><body><main data-porthole-status="${status}" data-porthole-reason="${escape(reason)}"${code === undefined ? "" : ` data-porthole-code="${escape(code)}"`} data-porthole-hosts="${escape(JSON.stringify(hostOrigins))}"><h1>${escape(title)}</h1><p>${escape(detail)}</p></main><script src="${STATUS_SCRIPT_PATH}"></script></body></html>`;
|
|
169
|
+
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escape(title)}</title><style>${STYLE}</style></head><body><main data-porthole-status="${status}" data-porthole-reason="${escape(reason)}"${code === undefined ? "" : ` data-porthole-code="${escape(code)}"`} data-porthole-hosts="${escape(JSON.stringify(hostOrigins))}"${strictParent(frameAncestors, hostOrigins)}><h1>${escape(title)}</h1><p>${escape(detail)}</p></main><script src="${STATUS_SCRIPT_PATH}"></script></body></html>`;
|
|
127
170
|
return new Response(method === "HEAD" ? null : body, {
|
|
128
171
|
status,
|
|
129
172
|
headers: pageHeaders(frameAncestors, false),
|
|
@@ -137,7 +180,7 @@ export function enterPage(
|
|
|
137
180
|
hostOrigins: readonly string[],
|
|
138
181
|
cookie: string,
|
|
139
182
|
): Response {
|
|
140
|
-
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Opening the app</title><style>${STYLE}</style></head><body><main data-porthole-enter="" data-porthole-target="${escape(target)}" data-porthole-hosts="${escape(JSON.stringify(hostOrigins))}"><h1>Opening the app…</h1><p></p></main><script src="${ENTER_SCRIPT_PATH}"></script></body></html>`;
|
|
183
|
+
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Opening the app</title><style>${STYLE}</style></head><body><main data-porthole-enter="" data-porthole-target="${escape(target)}" data-porthole-hosts="${escape(JSON.stringify(hostOrigins))}"${strictParent(frameAncestors, hostOrigins)}><h1>Opening the app…</h1><p></p></main><script src="${ENTER_SCRIPT_PATH}"></script></body></html>`;
|
|
141
184
|
const headers = new Headers(pageHeaders(frameAncestors, true));
|
|
142
185
|
headers.set("set-cookie", cookie);
|
|
143
186
|
return new Response(body, { status: 200, headers });
|
|
@@ -173,7 +216,7 @@ export function appStatusPage(
|
|
|
173
216
|
link === undefined
|
|
174
217
|
? ""
|
|
175
218
|
: `<p><a href="${escape(link.href)}" target="_top" rel="noopener">${escape(link.text)}</a></p>`;
|
|
176
|
-
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escape(title)}</title><style>${STYLE}a{color:inherit}</style></head><body><main data-porthole-status="${status}" data-porthole-reason="${escape(reason)}" data-porthole-hosts="${escape(JSON.stringify(hostOrigins))}"><h1>${escape(title)}</h1><p>${escape(detail)}</p>${more}</main><script>${STATUS_SCRIPT}</script></body></html>`;
|
|
219
|
+
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escape(title)}</title><style>${STYLE}a{color:inherit}</style></head><body><main data-porthole-status="${status}" data-porthole-reason="${escape(reason)}" data-porthole-hosts="${escape(JSON.stringify(hostOrigins))}"${strictParent(frameAncestors, hostOrigins)}><h1>${escape(title)}</h1><p>${escape(detail)}</p>${more}</main><script>${STATUS_SCRIPT}</script></body></html>`;
|
|
177
220
|
return new Response(method === "HEAD" ? null : body, {
|
|
178
221
|
status,
|
|
179
222
|
headers: {
|
|
@@ -198,7 +241,12 @@ export function statusPageOf(options: {
|
|
|
198
241
|
readonly detail: string;
|
|
199
242
|
readonly reason?: FrameBlock;
|
|
200
243
|
readonly hostOrigins: readonly string[];
|
|
201
|
-
/**
|
|
244
|
+
/**
|
|
245
|
+
* Origins of an outer page that may be an ancestor above a host origin's page (P141): added to its
|
|
246
|
+
* `frame-ancestors` after the host origins, never told anything. Default none.
|
|
247
|
+
*/
|
|
248
|
+
readonly framedBy?: readonly string[];
|
|
249
|
+
/** Who may frame it (`frame-ancestors`): default, the host origins, then `framedBy`. */
|
|
202
250
|
readonly frameAncestors?: string;
|
|
203
251
|
readonly link?: { readonly href: string; readonly text: string };
|
|
204
252
|
readonly method?: string;
|
|
@@ -207,7 +255,7 @@ export function statusPageOf(options: {
|
|
|
207
255
|
options.status,
|
|
208
256
|
options.title,
|
|
209
257
|
options.detail,
|
|
210
|
-
options.frameAncestors ?? options.hostOrigins.
|
|
258
|
+
options.frameAncestors ?? ancestorsOf(options.hostOrigins, options.framedBy),
|
|
211
259
|
options.reason ?? "error",
|
|
212
260
|
options.hostOrigins,
|
|
213
261
|
options.link,
|
|
@@ -242,9 +290,13 @@ const doorHeaders = (script: string, frameAncestors: string): Headers =>
|
|
|
242
290
|
"x-robots-tag": "noindex, nofollow",
|
|
243
291
|
});
|
|
244
292
|
|
|
245
|
-
/**
|
|
293
|
+
/**
|
|
294
|
+
* What a door is given: the host origins it tells, and who may frame it (default, those and then
|
|
295
|
+
* `framedBy`, P141: an outer page's origins above a host origin's page, never told anything).
|
|
296
|
+
*/
|
|
246
297
|
export interface DoorOptions {
|
|
247
298
|
readonly hostOrigins: readonly string[];
|
|
299
|
+
readonly framedBy?: readonly string[];
|
|
248
300
|
readonly frameAncestors?: string;
|
|
249
301
|
readonly method?: string;
|
|
250
302
|
}
|
|
@@ -257,10 +309,11 @@ export interface DoorOptions {
|
|
|
257
309
|
*/
|
|
258
310
|
export function doorResponse(options: DoorOptions): Response {
|
|
259
311
|
const hosts = escape(JSON.stringify(options.hostOrigins));
|
|
260
|
-
const
|
|
312
|
+
const ancestors = options.frameAncestors ?? ancestorsOf(options.hostOrigins, options.framedBy);
|
|
313
|
+
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Opening</title></head><body><main data-porthole-door="" data-porthole-hosts="${hosts}"${strictParent(ancestors, options.hostOrigins)}></main><script>${DOOR_SCRIPT}</script></body></html>`;
|
|
261
314
|
return new Response(options.method?.toUpperCase() === "HEAD" ? null : body, {
|
|
262
315
|
status: 200,
|
|
263
|
-
headers: doorHeaders(hashOfDoor(),
|
|
316
|
+
headers: doorHeaders(hashOfDoor(), ancestors),
|
|
264
317
|
});
|
|
265
318
|
}
|
|
266
319
|
|
|
@@ -279,10 +332,11 @@ export function doorStatusResponse(
|
|
|
279
332
|
): Response {
|
|
280
333
|
const { status, reason, code } = options;
|
|
281
334
|
const hosts = escape(JSON.stringify(options.hostOrigins));
|
|
282
|
-
const
|
|
335
|
+
const ancestors = options.frameAncestors ?? ancestorsOf(options.hostOrigins, options.framedBy);
|
|
336
|
+
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Not shown</title></head><body><main data-porthole-status="${status}" data-porthole-reason="${escape(reason)}"${code === undefined ? "" : ` data-porthole-code="${escape(code)}"`} data-porthole-hosts="${hosts}"${strictParent(ancestors, options.hostOrigins)}></main><script>${STATUS_SCRIPT}</script></body></html>`;
|
|
283
337
|
return new Response(options.method?.toUpperCase() === "HEAD" ? null : body, {
|
|
284
338
|
status,
|
|
285
|
-
headers: doorHeaders(hashOfStatus(),
|
|
339
|
+
headers: doorHeaders(hashOfStatus(), ancestors),
|
|
286
340
|
});
|
|
287
341
|
}
|
|
288
342
|
|
package/src/server/server.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { compile, type CompiledConfig } from "../compile/compile.ts";
|
|
2
2
|
import type { DisplayMode, EgressConfig } from "../config/schema.ts";
|
|
3
3
|
import {
|
|
4
|
+
framedByProblem,
|
|
4
5
|
hostOriginProblem,
|
|
5
6
|
maybeSameSite,
|
|
6
7
|
originMatchesSource,
|
|
@@ -438,7 +439,36 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
438
439
|
);
|
|
439
440
|
}
|
|
440
441
|
}
|
|
441
|
-
|
|
442
|
+
// An outer page's origins (P141): ancestors above a host origin's page, in `frame-ancestors` alone.
|
|
443
|
+
const framedBy: string[] = [];
|
|
444
|
+
for (const origin of options.framedBy ?? []) {
|
|
445
|
+
const problem = framedByProblem(origin);
|
|
446
|
+
if (problem !== null) {
|
|
447
|
+
throw new TypeError(`framedBy: ${problem}`);
|
|
448
|
+
}
|
|
449
|
+
if (options.hostOrigins.includes(origin)) {
|
|
450
|
+
throw new TypeError(
|
|
451
|
+
`framedBy: ${origin} is a host origin already: framedBy names an ancestor that never talks to a frame`,
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
const parsed = parseOrigin(origin);
|
|
455
|
+
if (
|
|
456
|
+
parsed === null ||
|
|
457
|
+
parsed.host === domain ||
|
|
458
|
+
parsed.host.endsWith(`.${domain}`) ||
|
|
459
|
+
maybeSameSite(parsed.host, sampleHost)
|
|
460
|
+
) {
|
|
461
|
+
throw new TypeError(
|
|
462
|
+
`framedBy: ${origin} may be the same site as the content domain ${domain}: serve content from a registrable domain the host uses for nothing else`,
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
if (!framedBy.includes(origin)) {
|
|
466
|
+
framedBy.push(origin);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
// Who may be an ancestor of a frame served here: the host origins, then the outer pages' (P141).
|
|
470
|
+
// Every frame's `frame-ancestors` is this; whom a frame hears and tells is `hostOrigins` alone.
|
|
471
|
+
const frameAncestors = [...options.hostOrigins, ...framedBy].join(" ");
|
|
442
472
|
let authorizeUrl: URL | null = null;
|
|
443
473
|
if (options.entry !== undefined) {
|
|
444
474
|
try {
|
|
@@ -1290,6 +1320,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1290
1320
|
const shell = nestedShellResponse(served.compiled, {
|
|
1291
1321
|
origin: access.origin,
|
|
1292
1322
|
origins: options.hostOrigins,
|
|
1323
|
+
framedBy,
|
|
1293
1324
|
page: `${access.origin}${sealed.base}${rest}`,
|
|
1294
1325
|
renew: { url: `${access.origin}${sealed.base}__porthole/renew`, expires: sealed.expires },
|
|
1295
1326
|
method,
|
package/src/server/types.ts
CHANGED
|
@@ -273,6 +273,14 @@ export interface ServerOptions {
|
|
|
273
273
|
readonly secret: Uint8Array;
|
|
274
274
|
/** The host origins that may embed content (`frame-ancestors`) and talk to a served shell. */
|
|
275
275
|
readonly hostOrigins: readonly string[];
|
|
276
|
+
/**
|
|
277
|
+
* Further origins that may be an ancestor of every frame served here, above a host origin's
|
|
278
|
+
* page (P141): an outer page that frames the host's app. Added to each `frame-ancestors` after
|
|
279
|
+
* the host origins, and nowhere else: never an origin a shell, a door or a page takes a message
|
|
280
|
+
* from or says anything to, never a host origin for `entry` or the handoff. Exact https origins
|
|
281
|
+
* (`http://` on loopback), none a host origin, none the content domain's site. Default none.
|
|
282
|
+
*/
|
|
283
|
+
readonly framedBy?: readonly string[];
|
|
276
284
|
/**
|
|
277
285
|
* The configs a served shell or page is served under, by the name a `ServedDocument` gives
|
|
278
286
|
* (an artifact defaults to `artifact`, the MCP Apps sandbox proxy to `mcpApp`).
|
package/src/shell/frame.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PAGE_SANDBOX_TOKENS } from "../compile/policy.ts";
|
|
2
|
-
import { hostOriginProblem, onNestedLabel } from "../isolation/served.ts";
|
|
2
|
+
import { framedByProblem, hostOriginProblem, onNestedLabel } from "../isolation/served.ts";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Porthole's shell for a nested page (P111): the whole document at the address the host frames,
|
|
@@ -81,7 +81,9 @@ export const FRAME_SHELL_RUNTIME = String.raw`(() => {
|
|
|
81
81
|
const web = plan.origins.filter((origin) => origin.indexOf("https://") === 0 || origin.indexOf("http://") === 0);
|
|
82
82
|
// To the host: each web host origin (only the parent's is delivered); a desktop app's own scheme
|
|
83
83
|
// is no target a browser takes, so where one is listed and the parent isn't a known web origin,
|
|
84
|
-
// "*" (frame-ancestors holds the parent to the host origins; nothing said here is secret).
|
|
84
|
+
// "*" (frame-ancestors holds the parent to the host origins; nothing said here is secret). Where
|
|
85
|
+
// it names an outer page above them too (the host's framedBy, P141: plan.strictParent), the parent
|
|
86
|
+
// may be the outer page: "*" then goes only to a parent the engine names as a host origin.
|
|
85
87
|
const toHost = (message) => {
|
|
86
88
|
if (!framed) {
|
|
87
89
|
return;
|
|
@@ -92,7 +94,11 @@ export const FRAME_SHELL_RUNTIME = String.raw`(() => {
|
|
|
92
94
|
} catch (_) {}
|
|
93
95
|
}
|
|
94
96
|
const parent = location.ancestorOrigins && location.ancestorOrigins.length > 0 ? location.ancestorOrigins[0] : null;
|
|
95
|
-
if (
|
|
97
|
+
if (
|
|
98
|
+
web.length < plan.origins.length &&
|
|
99
|
+
(parent === null || web.indexOf(parent) === -1) &&
|
|
100
|
+
(plan.strictParent !== true || (parent !== null && plan.origins.indexOf(parent) !== -1))
|
|
101
|
+
) {
|
|
96
102
|
try {
|
|
97
103
|
window.parent.postMessage(message, "*");
|
|
98
104
|
} catch (_) {}
|
|
@@ -264,6 +270,12 @@ export { SHELL_METHOD_PREFIX, SHELL_STATE_METHOD } from "../isolation/types.ts";
|
|
|
264
270
|
export interface FrameShellOptions {
|
|
265
271
|
/** The host origins: the shell takes messages from its parent only from these. */
|
|
266
272
|
readonly origins: readonly string[];
|
|
273
|
+
/**
|
|
274
|
+
* Origins of an outer page that may be an ancestor above a host origin's page (P141): in the
|
|
275
|
+
* shell's `frame-ancestors` after `origins` (`nestedShellResponse`), never heard or told. Where
|
|
276
|
+
* there are any, the shell says `*` only to a parent the engine names as a host origin.
|
|
277
|
+
*/
|
|
278
|
+
readonly framedBy?: readonly string[];
|
|
267
279
|
/** The page's address: absolute, on the shell's own origin, under `/__porthole/f/`. */
|
|
268
280
|
readonly page: string;
|
|
269
281
|
/** The page frame's accessible name. */
|
|
@@ -287,6 +299,12 @@ export function frameShellScript(options: FrameShellOptions): string {
|
|
|
287
299
|
throw new TypeError(`frameShellScript: ${problem}`);
|
|
288
300
|
}
|
|
289
301
|
}
|
|
302
|
+
for (const origin of options.framedBy ?? []) {
|
|
303
|
+
const problem = framedByProblem(origin);
|
|
304
|
+
if (problem !== null || options.origins.includes(origin)) {
|
|
305
|
+
throw new TypeError(`frameShellScript: framedBy: ${problem ?? `${origin} is a host origin`}`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
290
308
|
let page: URL;
|
|
291
309
|
try {
|
|
292
310
|
page = new URL(options.page);
|
|
@@ -305,6 +323,7 @@ export function frameShellScript(options: FrameShellOptions): string {
|
|
|
305
323
|
return FRAME_SHELL_RUNTIME.replace("__PLAN__", () =>
|
|
306
324
|
scriptJson({
|
|
307
325
|
origins: options.origins,
|
|
326
|
+
...((options.framedBy ?? []).length === 0 ? {} : { strictParent: true }),
|
|
308
327
|
page: page.href,
|
|
309
328
|
sandbox: PAGE_SANDBOX_TOKENS.join(" "),
|
|
310
329
|
...(options.title === undefined ? {} : { title: options.title }),
|
package/src/shell/runtime.ts
CHANGED
|
@@ -323,10 +323,11 @@ export const SHELL_RUNTIME = String.raw`(() => {
|
|
|
323
323
|
// A page of a set that a navigation opened (not its ticket's answer) says so as it arrives
|
|
324
324
|
// (P098), below, once the shell can speak: the notice its last page sent as it left can reach
|
|
325
325
|
// the host after this page's load, or never (Chromium forwards a cross-process message from a
|
|
326
|
-
// task of the sending document's, which a navigation that commits first drops).
|
|
327
|
-
|
|
326
|
+
// task of the sending document's, which a navigation that commits first drops). With or without
|
|
327
|
+
// the Navigation API (P142): where there is none, the leaving page sends nothing, and this is the
|
|
328
|
+
// page's only word.
|
|
329
|
+
const arrived = filesBase !== null && typeof plan.files.address !== "string";
|
|
328
330
|
if (navigationApi && typeof navigationApi.addEventListener === "function" && destinationOf !== null && sameDocumentOf !== null && navigationTypeOf !== null) {
|
|
329
|
-
arrived = filesBase !== null && typeof plan.files.address !== "string";
|
|
330
331
|
navigationApi.addEventListener("navigate", (event) => {
|
|
331
332
|
const destination = applyOf(destinationOf, event, []);
|
|
332
333
|
if (!applyOf(sameDocumentOf, destination, []) && applyOf(navigationTypeOf, event, []) !== "reload") {
|
|
@@ -1637,6 +1638,15 @@ export const SHELL_RUNTIME = String.raw`(() => {
|
|
|
1637
1638
|
if (arrived) {
|
|
1638
1639
|
notice(location.href, { arrived: true });
|
|
1639
1640
|
}
|
|
1641
|
+
// A navigation within the document (a fragment, history between its entries) says the page
|
|
1642
|
+
// stayed (P142): WebKit fires the frame's load for one (measured, 18.2 and 26.6: a link to
|
|
1643
|
+
// #bottom, the first in a document), which the host would otherwise take for content that left.
|
|
1644
|
+
// Listened for now, before any content runs; each is the content's word, as every notice is.
|
|
1645
|
+
if (filesSet !== null && filesBase !== null) {
|
|
1646
|
+
const stayed = () => notice(location.href, { same: true });
|
|
1647
|
+
window.addEventListener("popstate", stayed, true);
|
|
1648
|
+
window.addEventListener("hashchange", stayed, true);
|
|
1649
|
+
}
|
|
1640
1650
|
// A sealed files' address opens the page's files on its own for a while (P083): renewed before
|
|
1641
1651
|
// it ends, the page moved to the new one in place (the same page, the same history entry). Its
|
|
1642
1652
|
// modules keep the address they loaded from; the server serves their own loads till the session
|