@botiverse/porthole 0.6.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 +25 -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 +38 -5
- package/dist/bridge/bridge.js +469 -39
- 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.d.ts +5 -1
- package/dist/bridge/refusal.js +17 -0
- package/dist/bridge/types.d.ts +73 -5
- package/dist/check/check.d.ts +7 -0
- package/dist/check/check.js +1 -0
- package/dist/check/instrument.d.ts +3 -0
- package/dist/check/lint.js +4 -2
- package/dist/check/page-source.js +1 -1
- package/dist/check/page.js +16 -0
- package/dist/check/tools.js +2 -3
- package/dist/compile/compile.d.ts +6 -0
- package/dist/compile/compile.js +3 -1
- package/dist/compile/policy.d.ts +55 -0
- package/dist/compile/policy.js +157 -0
- package/dist/config/capabilities.d.ts +85 -7
- package/dist/config/capabilities.js +32 -2
- package/dist/config/schema.d.ts +35 -0
- package/dist/config/validate.d.ts +9 -3
- package/dist/config/validate.js +92 -8
- 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 +11 -0
- package/dist/conformance/index.js +2 -0
- package/dist/conformance/limits.js +38 -1
- package/dist/conformance/runtime.d.ts +58 -0
- package/dist/conformance/runtime.js +324 -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 +9 -0
- package/dist/guide/gates.js +31 -0
- package/dist/guide/guide.d.ts +2 -0
- package/dist/guide/guide.js +35 -33
- package/dist/guide/limits.js +4 -4
- package/dist/guide/runtime.d.ts +13 -0
- package/dist/guide/runtime.js +210 -0
- package/dist/host/create-host.js +73 -37
- package/dist/host/locate.js +8 -0
- package/dist/host/locator.js +7 -0
- 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 +26 -1
- package/dist/index.d.ts +11 -4
- package/dist/index.js +11 -2
- package/dist/isolation/types.d.ts +8 -0
- 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 +22 -8
- package/dist/presets/presets.js +40 -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.d.ts +28 -0
- package/dist/protocol/runtime-calls.js +116 -0
- package/dist/protocol/shell-adapter.js +64 -25
- package/dist/react/context-notice.d.ts +32 -0
- package/dist/react/context-notice.js +28 -0
- package/dist/react/dialog.js +91 -16
- package/dist/react/frame.d.ts +25 -1
- package/dist/react/frame.js +85 -2
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +2 -1
- package/dist/runtime/contract.d.ts +201 -0
- package/dist/runtime/contract.js +245 -0
- package/dist/runtime/db.d.ts +188 -0
- package/dist/runtime/db.js +502 -0
- package/dist/runtime/declaration.d.ts +100 -0
- package/dist/runtime/declaration.js +363 -0
- package/dist/runtime/levels.d.ts +33 -0
- package/dist/runtime/levels.js +41 -0
- package/dist/runtime/requests.d.ts +125 -0
- package/dist/runtime/requests.js +215 -0
- package/dist/runtime/segments.d.ts +8 -0
- package/dist/runtime/segments.js +14 -0
- package/dist/runtime/types.generated.d.ts +1 -0
- package/dist/runtime/types.generated.js +19 -0
- package/dist/server/files.d.ts +19 -1
- package/dist/server/files.js +115 -6
- package/dist/server/host.d.ts +24 -2
- package/dist/server/host.js +137 -2
- package/dist/server/index.d.ts +3 -2
- package/dist/server/index.js +2 -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/pseudonym.d.ts +9 -0
- package/dist/server/pseudonym.js +20 -0
- package/dist/server/server.d.ts +0 -5
- package/dist/server/server.js +318 -52
- package/dist/server/types.d.ts +74 -2
- package/dist/shell/document.d.ts +16 -0
- package/dist/shell/document.js +19 -2
- package/dist/shell/legacy.d.ts +21 -0
- package/dist/shell/legacy.js +63 -0
- package/dist/shell/plan.d.ts +17 -1
- package/dist/shell/plan.js +13 -2
- package/dist/shell/runtime.js +390 -56
- package/dist/shell/use.d.ts +15 -0
- package/dist/shell/use.js +235 -0
- package/docs/ADOPTING.md +205 -15
- package/docs/ARCHITECTURE.md +34 -5
- package/docs/DECISIONS.md +1867 -11
- package/docs/DESIGN.md +137 -17
- package/docs/THREAT_MODEL.md +616 -0
- package/package.json +16 -3
- package/src/bridge/bounded-text.ts +240 -0
- package/src/bridge/bridge.ts +601 -51
- package/src/bridge/json-data.ts +95 -0
- package/src/bridge/navigation.ts +10 -0
- package/src/bridge/refusal.ts +17 -0
- package/src/bridge/types.ts +76 -5
- package/src/check/check.ts +8 -0
- package/src/check/instrument.ts +3 -0
- package/src/check/lint.ts +4 -2
- package/src/check/page.ts +16 -0
- package/src/check/tools.ts +2 -3
- package/src/compile/compile.ts +9 -1
- package/src/compile/policy.ts +177 -0
- package/src/config/capabilities.ts +71 -5
- package/src/config/schema.ts +35 -0
- package/src/config/validate.ts +141 -8
- 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 +17 -0
- package/src/conformance/limits.ts +39 -1
- package/src/conformance/runtime.ts +594 -0
- package/src/conformance/served.ts +888 -4
- package/src/files/paths.ts +4 -0
- package/src/guide/gates.ts +37 -0
- package/src/guide/guide.ts +61 -55
- package/src/guide/limits.ts +4 -4
- package/src/guide/runtime.ts +253 -0
- package/src/host/create-host.ts +88 -36
- package/src/host/locate.ts +10 -0
- package/src/host/locator.ts +11 -0
- package/src/host/pages-of-set.ts +226 -0
- package/src/host/types.ts +33 -1
- package/src/index.ts +94 -0
- package/src/isolation/types.ts +8 -0
- package/src/mcp/adapter.ts +16 -20
- package/src/node/index.ts +507 -116
- package/src/presets/presets.ts +67 -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 +141 -0
- package/src/protocol/shell-adapter.ts +79 -26
- package/src/react/context-notice.tsx +95 -0
- package/src/react/dialog.tsx +108 -15
- package/src/react/frame.tsx +120 -0
- package/src/react/index.ts +8 -1
- package/src/runtime/contract.ts +339 -0
- package/src/runtime/db.ts +650 -0
- package/src/runtime/declaration.ts +516 -0
- package/src/runtime/levels.ts +69 -0
- package/src/runtime/requests.ts +355 -0
- package/src/runtime/segments.ts +17 -0
- package/src/runtime/types.generated.ts +19 -0
- package/src/server/files.ts +138 -7
- package/src/server/host.ts +170 -3
- package/src/server/index.ts +4 -0
- package/src/server/peer.ts +21 -0
- package/src/server/proxy.ts +13 -3
- package/src/server/pseudonym.ts +21 -0
- package/src/server/server.ts +370 -57
- package/src/server/types.ts +73 -2
- package/src/shell/document.ts +29 -4
- package/src/shell/legacy.ts +64 -0
- package/src/shell/plan.ts +37 -3
- package/src/shell/runtime.ts +390 -56
- package/src/shell/use.ts +235 -0
- package/types/runtime/v1/artifact.d.ts +50 -0
- package/types/runtime/v1/assets.d.ts +43 -0
- package/types/runtime/v1/chat.d.ts +13 -0
- package/types/runtime/v1/clipboard.d.ts +14 -0
- package/types/runtime/v1/comments.d.ts +24 -0
- package/types/runtime/v1/context.d.ts +27 -0
- package/types/runtime/v1/data.d.ts +29 -0
- package/types/runtime/v1/db.d.ts +110 -0
- package/types/runtime/v1/downloads.d.ts +22 -0
- package/types/runtime/v1/links.d.ts +13 -0
- package/types/runtime/v1/porthole.d.ts +44 -0
- package/types/runtime/v1/user.d.ts +19 -0
package/src/server/server.ts
CHANGED
|
@@ -13,14 +13,23 @@ import { randomBytes } from "./crypto.ts";
|
|
|
13
13
|
import { isHostLabel, reservedLabels } from "./naming.ts";
|
|
14
14
|
import { enterPage, pageScript, SESSION_PATH, statusPage } from "./pages.ts";
|
|
15
15
|
import { libraryFile } from "./libraries.ts";
|
|
16
|
-
import { ownLoad, serveFile } from "./files.ts";
|
|
17
|
-
import {
|
|
16
|
+
import { ownLoad, serveAsset, serveFile } from "./files.ts";
|
|
17
|
+
import { isPortholeCookie, setsPortholeCookie } from "./proxy.ts";
|
|
18
|
+
import {
|
|
19
|
+
assetCodeRuns,
|
|
20
|
+
assetCspDirectives,
|
|
21
|
+
INERT_FILE_CSP,
|
|
22
|
+
servedDocumentCsp,
|
|
23
|
+
servedPermissionsPolicy,
|
|
24
|
+
} from "../compile/policy.ts";
|
|
25
|
+
import { parseViewer, type ViewerInfo } from "../runtime/levels.ts";
|
|
18
26
|
import type { FrameBlock } from "../host/types.ts";
|
|
19
27
|
import { memoryTicketStore } from "./tickets.ts";
|
|
20
28
|
import type {
|
|
21
29
|
Access,
|
|
22
30
|
AuthorizeAnswer,
|
|
23
31
|
Grant,
|
|
32
|
+
StoredAsset,
|
|
24
33
|
PortholeServer,
|
|
25
34
|
ServerOptions,
|
|
26
35
|
StoredFiles,
|
|
@@ -42,6 +51,10 @@ const DOCUMENT_PATH = /^\/__porthole\/d\/([a-z2-7]{16})$/u;
|
|
|
42
51
|
*/
|
|
43
52
|
const FILES_PATH = /^\/__porthole\/f\/([A-Za-z0-9_-]{40,4096})(\/.*)?$/u;
|
|
44
53
|
const FILES_PREFIX = "/__porthole/f/";
|
|
54
|
+
/** An asset's address (P089): `/_blob/` and 32 lowercase hex characters (`ASSET_ID`). */
|
|
55
|
+
const ASSET_ROUTE = /^\/_blob\/([0-9a-f]{32})$/u;
|
|
56
|
+
/** The same, under a frame's sealed files' address (P091): `<base>__porthole/blob/<id>`. */
|
|
57
|
+
const SEALED_ASSET = /^\/__porthole\/blob\/([0-9a-f]{32})$/u;
|
|
45
58
|
/** How long a sealed files' address opens a page's files on its own (P083); the shell renews it. */
|
|
46
59
|
const FILES_ADDRESS_MS = 3_600_000;
|
|
47
60
|
/** Below a sealed address: where the page's shell renews it (no file has a `__porthole/` path). */
|
|
@@ -220,24 +233,55 @@ function seconds(value: number | undefined, fallback: number, max: number, what:
|
|
|
220
233
|
return value;
|
|
221
234
|
}
|
|
222
235
|
|
|
223
|
-
/**
|
|
236
|
+
/**
|
|
237
|
+
* The cookies a request carries, as name → values (a name may come twice: partitioned or not).
|
|
238
|
+
* Only spaces and tabs are trimmed, as browsers trim them (P101): a name a sibling set with a
|
|
239
|
+
* leading U+00A0 is no `__Host-` name, though JavaScript's `trim` would make it one.
|
|
240
|
+
*/
|
|
224
241
|
function cookiesOf(request: Request): [string, string][] {
|
|
225
242
|
const header = request.headers.get("cookie") ?? "";
|
|
226
243
|
const pairs: [string, string][] = [];
|
|
244
|
+
const blank = /^[ \t]+|[ \t]+$/gu;
|
|
227
245
|
for (const part of header.split(";")) {
|
|
228
246
|
const at = part.indexOf("=");
|
|
229
247
|
if (at > 0) {
|
|
230
|
-
pairs.push([part.slice(0, at).
|
|
248
|
+
pairs.push([part.slice(0, at).replace(blank, ""), part.slice(at + 1).replace(blank, "")]);
|
|
231
249
|
}
|
|
232
250
|
}
|
|
233
251
|
return pairs;
|
|
234
252
|
}
|
|
235
253
|
|
|
254
|
+
/** The artifact version a file set is, for its pages' notices to the host (P098). */
|
|
255
|
+
const versionOf = (instance: InstanceRef): { artifact?: { id: string; version: string } } =>
|
|
256
|
+
instance.document.kind === "artifact"
|
|
257
|
+
? { artifact: { id: instance.document.id, version: instance.document.version } }
|
|
258
|
+
: {};
|
|
259
|
+
|
|
236
260
|
/**
|
|
237
261
|
* Serving for domain, artifact and url modes, and the MCP Apps sandbox proxy (P016, P020): a
|
|
238
262
|
* fetch-style handler a host's backend mounts in front of its own routes, plus `issue`, which the
|
|
239
263
|
* host's authenticated API calls once it has decided a viewer may see an instance.
|
|
240
264
|
*/
|
|
265
|
+
/** A grant's viewer for the display's bridge (P087), checked; null without a level. */
|
|
266
|
+
function viewerOfGrant(grant: Grant): ViewerInfo | null {
|
|
267
|
+
if (grant.level === undefined) {
|
|
268
|
+
if (grant.user !== undefined) {
|
|
269
|
+
throw new TypeError("A grant's user comes with its level");
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
const viewer = parseViewer({
|
|
274
|
+
level: grant.level,
|
|
275
|
+
...(grant.user === undefined ? {} : { id: grant.user.id, name: grant.user.name }),
|
|
276
|
+
});
|
|
277
|
+
if (viewer === null) {
|
|
278
|
+
throw new TypeError(
|
|
279
|
+
"A grant's level is view, interact, admin or owner; its user's id and name 1 to 256 characters",
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
return viewer;
|
|
283
|
+
}
|
|
284
|
+
|
|
241
285
|
export function createServer(options: ServerOptions): PortholeServer {
|
|
242
286
|
const { content } = options;
|
|
243
287
|
const domain = content.domain;
|
|
@@ -473,10 +517,13 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
473
517
|
}
|
|
474
518
|
}
|
|
475
519
|
|
|
476
|
-
async function documentOf(
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
520
|
+
async function documentOf(instance: InstanceRef): Promise<
|
|
521
|
+
| {
|
|
522
|
+
readonly html: string;
|
|
523
|
+
readonly compiled: CompiledConfig;
|
|
524
|
+
/** Whether its CSP opens its artifact's asset library (P089): an artifact's page. */
|
|
525
|
+
readonly assets: boolean;
|
|
526
|
+
}
|
|
480
527
|
| { readonly files: StoredFiles; readonly compiled: CompiledConfig }
|
|
481
528
|
| null
|
|
482
529
|
> {
|
|
@@ -488,7 +535,11 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
488
535
|
const origins = options.hostOrigins;
|
|
489
536
|
switch (document.kind) {
|
|
490
537
|
case "shell":
|
|
491
|
-
return {
|
|
538
|
+
return {
|
|
539
|
+
html: config.shellDocument({ origins, cspMeta: false }),
|
|
540
|
+
compiled: config,
|
|
541
|
+
assets: false,
|
|
542
|
+
};
|
|
492
543
|
case "artifact": {
|
|
493
544
|
const stored = await options.artifacts?.get(document.id, document.version);
|
|
494
545
|
if (stored === null || stored === undefined) {
|
|
@@ -503,11 +554,12 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
503
554
|
}
|
|
504
555
|
return {
|
|
505
556
|
html: config.shellDocument({ origins, document: stored.html, cspMeta: false }),
|
|
506
|
-
|
|
557
|
+
compiled: config,
|
|
558
|
+
assets: true,
|
|
507
559
|
};
|
|
508
560
|
}
|
|
509
561
|
case "mcp-proxy":
|
|
510
|
-
return { html: sandboxProxyDocument(config, origins),
|
|
562
|
+
return { html: sandboxProxyDocument(config, origins), compiled: config, assets: false };
|
|
511
563
|
case "external":
|
|
512
564
|
return null;
|
|
513
565
|
}
|
|
@@ -515,8 +567,21 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
515
567
|
|
|
516
568
|
const cookieName = (key: string): string =>
|
|
517
569
|
secure ? `__Host-porthole-${key}` : `porthole-${key}`;
|
|
570
|
+
/**
|
|
571
|
+
* Whether a cookie is one this server set, by its name (P101). Over https only `__Host-` names:
|
|
572
|
+
* every label is same-site with every other (P047), and a sibling label (a url-mode app, an
|
|
573
|
+
* app's preview) can set any other name for the whole content domain (`Domain=`), never a
|
|
574
|
+
* `__Host-` one, which the browser keeps to the origin that set it. Over plain http (loopback
|
|
575
|
+
* dev only: `createServer` refuses http elsewhere) the names have no prefix, set and read:
|
|
576
|
+
* `__Host-` needs `Secure`, and WebKit keeps no `Secure` cookie on a tab of http://*.localhost
|
|
577
|
+
* (measured). A sibling there can set the very name, so a copy of one of this label's own
|
|
578
|
+
* sessions or nonces is read: someone must hold one to plant it, on a developer's own server.
|
|
579
|
+
* Another label's session is never this label's, either scheme (its signed scope). Case and
|
|
580
|
+
* every byte count: `__host-` or a name with anything before the prefix is not ours. What
|
|
581
|
+
* Porthole strips from an app's requests and answers is wider (`isPortholeCookie`).
|
|
582
|
+
*/
|
|
518
583
|
const ourCookie = (name: string): boolean =>
|
|
519
|
-
name.startsWith("porthole-"
|
|
584
|
+
name.startsWith(secure ? "__Host-porthole-" : "porthole-");
|
|
520
585
|
|
|
521
586
|
/**
|
|
522
587
|
* The cookie a taken ticket buys: this origin only (`__Host-` over https), never readable by the
|
|
@@ -559,8 +624,38 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
559
624
|
|
|
560
625
|
/** The cookie a redirect entry's browser holds while it is away (P068), by its nonce. */
|
|
561
626
|
const nonceCookie = (nonce: string): string => cookieName(`n-${nonce}`);
|
|
562
|
-
/**
|
|
563
|
-
|
|
627
|
+
/**
|
|
628
|
+
* What an entered path's nonce cookie says after its code was redeemed: the loop guard. Its
|
|
629
|
+
* label's and its path's (P101): a mark redeemed on one label, planted on another, is nothing.
|
|
630
|
+
*/
|
|
631
|
+
const enteredMark = (label: string, pathname: string): string =>
|
|
632
|
+
`e.${keys.instanceKey({ entered: pathname, label })}`;
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Whether a url-mode app's request may ride its cookie (P101): what the app's own origin asks
|
|
636
|
+
* (its `Origin`, or `Sec-Fetch-Site` `same-origin`; `none` is the person's own address bar),
|
|
637
|
+
* and, where `navigable`, a navigation that opens it (GET or HEAD: the host's frame, a link, a
|
|
638
|
+
* redirect back from sign-in). Another origin (a sibling label's app, another frame on the
|
|
639
|
+
* host's page, an opaque origin's `null`) gets no write, no subresource, no socket. A request
|
|
640
|
+
* with neither header is no browser's (a browser always sends `Origin` on a socket and on a
|
|
641
|
+
* write, and fetch metadata on everything, where it sends them at all).
|
|
642
|
+
*/
|
|
643
|
+
function fromItsOwnOrigin(request: Request, label: string, navigable: boolean): boolean {
|
|
644
|
+
const origin = request.headers.get("origin");
|
|
645
|
+
if (origin !== null && origin !== originOfLabel(label)) {
|
|
646
|
+
return false;
|
|
647
|
+
}
|
|
648
|
+
const site = request.headers.get("sec-fetch-site");
|
|
649
|
+
if (site === null || site === "same-origin" || site === "none") {
|
|
650
|
+
return true;
|
|
651
|
+
}
|
|
652
|
+
const method = request.method.toUpperCase();
|
|
653
|
+
return (
|
|
654
|
+
navigable &&
|
|
655
|
+
request.headers.get("sec-fetch-mode") === "navigate" &&
|
|
656
|
+
(method === "GET" || method === "HEAD")
|
|
657
|
+
);
|
|
658
|
+
}
|
|
564
659
|
|
|
565
660
|
/** The access a request's own cookies give on `label`'s origin, for `key`'s instance or the app. */
|
|
566
661
|
function accessOf(request: Request, label: string, key: string | null): Access | null {
|
|
@@ -712,10 +807,12 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
712
807
|
}
|
|
713
808
|
}
|
|
714
809
|
|
|
715
|
-
function documentHeaders(csp: string): Headers {
|
|
810
|
+
function documentHeaders(csp: string, permissions: string | null): Headers {
|
|
716
811
|
return new Headers({
|
|
717
812
|
"content-type": "text/html; charset=utf-8",
|
|
718
813
|
"content-security-policy": `${csp}; frame-ancestors ${frameAncestors}`,
|
|
814
|
+
// What the frame's `allow` doesn't grant, denied on a tab of its own too (P099).
|
|
815
|
+
...(permissions === null ? {} : { "permissions-policy": permissions }),
|
|
719
816
|
"cache-control": "no-store",
|
|
720
817
|
"referrer-policy": "no-referrer",
|
|
721
818
|
"x-content-type-options": "nosniff",
|
|
@@ -723,6 +820,46 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
723
820
|
});
|
|
724
821
|
}
|
|
725
822
|
|
|
823
|
+
/**
|
|
824
|
+
* The access a sealed files' address carries (P083), if it holds for this origin now: on its own
|
|
825
|
+
* for its hour (`x`), then only for the running page's own loads until the session ends (`e`).
|
|
826
|
+
*/
|
|
827
|
+
async function sealedAccess(
|
|
828
|
+
request: Request,
|
|
829
|
+
label: string,
|
|
830
|
+
token: string,
|
|
831
|
+
): Promise<Access | null> {
|
|
832
|
+
const session = await keys.unseal(token);
|
|
833
|
+
const now = Date.now();
|
|
834
|
+
if (
|
|
835
|
+
!isRecord(session) ||
|
|
836
|
+
typeof session.w !== "string" ||
|
|
837
|
+
typeof session.x !== "number" ||
|
|
838
|
+
typeof session.e !== "number" ||
|
|
839
|
+
(session.s !== undefined && typeof session.s !== "string")
|
|
840
|
+
) {
|
|
841
|
+
return null;
|
|
842
|
+
}
|
|
843
|
+
const open = session.x > now || (session.e > now && ownLoad(request));
|
|
844
|
+
let instance: InstanceRef;
|
|
845
|
+
try {
|
|
846
|
+
instance = cleanInstance(session.i);
|
|
847
|
+
} catch {
|
|
848
|
+
return null;
|
|
849
|
+
}
|
|
850
|
+
if (!open || instance.document.kind !== "artifact" || nameOf(instance.scope) !== label) {
|
|
851
|
+
return null;
|
|
852
|
+
}
|
|
853
|
+
return {
|
|
854
|
+
instance,
|
|
855
|
+
viewer: session.w,
|
|
856
|
+
origin: originOfLabel(label),
|
|
857
|
+
expiresAt: session.e,
|
|
858
|
+
...(session.a === 1 ? { anonymous: true } : {}),
|
|
859
|
+
...(typeof session.s === "string" ? { subject: session.s } : {}),
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
|
|
726
863
|
/**
|
|
727
864
|
* A file set's file in a frame (P083): the sealed access in its path opens it, once it holds
|
|
728
865
|
* for this origin and the host's check lets it in; every path under it is the same version's.
|
|
@@ -766,30 +903,8 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
766
903
|
headers: { location: `${origin}${base}${url.search}`, "cache-control": "no-store" },
|
|
767
904
|
});
|
|
768
905
|
}
|
|
769
|
-
const
|
|
770
|
-
|
|
771
|
-
let instance: InstanceRef | null = null;
|
|
772
|
-
let end = 0;
|
|
773
|
-
if (
|
|
774
|
-
isRecord(session) &&
|
|
775
|
-
typeof session.w === "string" &&
|
|
776
|
-
typeof session.x === "number" &&
|
|
777
|
-
typeof session.e === "number" &&
|
|
778
|
-
(session.s === undefined || typeof session.s === "string")
|
|
779
|
-
) {
|
|
780
|
-
end = session.e;
|
|
781
|
-
const open = session.x > now || (session.e > now && ownLoad(request));
|
|
782
|
-
try {
|
|
783
|
-
const clean = cleanInstance(session.i);
|
|
784
|
-
instance =
|
|
785
|
-
open && clean.document.kind === "artifact" && nameOf(clean.scope) === label
|
|
786
|
-
? clean
|
|
787
|
-
: null;
|
|
788
|
-
} catch {
|
|
789
|
-
instance = null;
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
if (instance === null || !isRecord(session)) {
|
|
906
|
+
const access = await sealedAccess(request, label, token);
|
|
907
|
+
if (access === null) {
|
|
793
908
|
return page(
|
|
794
909
|
401,
|
|
795
910
|
"This has ended",
|
|
@@ -798,14 +913,18 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
798
913
|
"session",
|
|
799
914
|
);
|
|
800
915
|
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
916
|
+
// An asset of this artifact's library, at the frame's own sealed address (P091): every engine
|
|
917
|
+
// sends a frame's path, where some send no Referer that names it.
|
|
918
|
+
const blob = SEALED_ASSET.exec(rest)?.[1];
|
|
919
|
+
if (blob !== undefined) {
|
|
920
|
+
if (options.assets === undefined) {
|
|
921
|
+
return page(404, "Not found", "This address names no content.", method);
|
|
922
|
+
}
|
|
923
|
+
return (
|
|
924
|
+
(await assetFor(request, label, access, blob)) ??
|
|
925
|
+
page(404, "Not found", "This address names no content.", method)
|
|
926
|
+
);
|
|
927
|
+
}
|
|
809
928
|
if (rest === RENEW_PATH) {
|
|
810
929
|
// Only the page itself renews its address, while its session lasts and the host lets it.
|
|
811
930
|
if (method !== "GET" || !ownLoad(request)) {
|
|
@@ -916,6 +1035,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
916
1035
|
compiled: served.compiled,
|
|
917
1036
|
hostOrigins: options.hostOrigins,
|
|
918
1037
|
frameAncestors,
|
|
1038
|
+
...versionOf(access.instance),
|
|
919
1039
|
});
|
|
920
1040
|
}
|
|
921
1041
|
// A frame's document (a ticket's): its files carry its access in their path (P083). The
|
|
@@ -935,19 +1055,185 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
935
1055
|
hostOrigins: options.hostOrigins,
|
|
936
1056
|
frameAncestors,
|
|
937
1057
|
ticket: { expires: sealed.expires },
|
|
1058
|
+
...versionOf(access.instance),
|
|
938
1059
|
});
|
|
939
1060
|
}
|
|
940
1061
|
// One page at a file set's base (P081): its base is the page, nothing under it is.
|
|
941
1062
|
if (at !== undefined && at.rest !== "") {
|
|
942
1063
|
return page(404, "Not found", "This page, or this version of it, doesn't exist.", method);
|
|
943
1064
|
}
|
|
944
|
-
|
|
1065
|
+
// A page that may use its artifact's assets loads them from its own origin (P089); every
|
|
1066
|
+
// served page carries its frame's sandbox, a tab of its own included (P099).
|
|
1067
|
+
const { config, cspDirectives } = served.compiled;
|
|
1068
|
+
const headers = documentHeaders(
|
|
1069
|
+
servedDocumentCsp(
|
|
1070
|
+
config,
|
|
1071
|
+
served.assets ? assetCspDirectives(config, cspDirectives, access.origin) : cspDirectives,
|
|
1072
|
+
),
|
|
1073
|
+
servedPermissionsPolicy(config),
|
|
1074
|
+
);
|
|
945
1075
|
if (cookie !== null) {
|
|
946
1076
|
headers.set("set-cookie", cookie);
|
|
947
1077
|
}
|
|
948
1078
|
return new Response(method === "HEAD" ? null : served.html, { status: 200, headers });
|
|
949
1079
|
}
|
|
950
1080
|
|
|
1081
|
+
/**
|
|
1082
|
+
* An asset at `/_blob/<id>` (P089, after the review): a request carrying an access to an
|
|
1083
|
+
* artifact of this origin (its page's sealed files' address, as its `Referer` says, P091; or a
|
|
1084
|
+
* session cookie), then the host's check for that access, then the host's store asked with it;
|
|
1085
|
+
* served only when the asset is that artifact's, on its scope's origin. Never an entry. By id
|
|
1086
|
+
* alone only where the host opted into it (`assets.bearer`).
|
|
1087
|
+
*/
|
|
1088
|
+
async function serveAssetAt(request: Request, label: string, id: string): Promise<Response> {
|
|
1089
|
+
const method = request.method.toUpperCase();
|
|
1090
|
+
const store = options.assets;
|
|
1091
|
+
const none = () => page(404, "Not found", "This address names no content.", method);
|
|
1092
|
+
if (store === undefined) {
|
|
1093
|
+
return none();
|
|
1094
|
+
}
|
|
1095
|
+
const scoped = (asset: StoredAsset | null): asset is StoredAsset => {
|
|
1096
|
+
try {
|
|
1097
|
+
return asset !== null && nameOf(asset.scope) === label;
|
|
1098
|
+
} catch {
|
|
1099
|
+
return false;
|
|
1100
|
+
}
|
|
1101
|
+
};
|
|
1102
|
+
if (store.bearer === true) {
|
|
1103
|
+
const asset = await store.get(id, null);
|
|
1104
|
+
return scoped(asset)
|
|
1105
|
+
? serveAsset({ request, asset, frameAncestors, code: runsAsCode(asset, null) })
|
|
1106
|
+
: none();
|
|
1107
|
+
}
|
|
1108
|
+
const accesses = await assetAccessesOf(request, label);
|
|
1109
|
+
if (accesses.length === 0) {
|
|
1110
|
+
return new Response(null, { status: 401, headers: { "cache-control": "no-store" } });
|
|
1111
|
+
}
|
|
1112
|
+
for (const access of accesses) {
|
|
1113
|
+
const answer = await assetFor(request, label, access, id);
|
|
1114
|
+
if (answer !== null) {
|
|
1115
|
+
return answer;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
return none();
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
/**
|
|
1122
|
+
* An asset for one access (P089, P091): the store asked with it, served only when the asset is
|
|
1123
|
+
* its artifact's on its scope's origin, after the host's check; its code only where it was
|
|
1124
|
+
* trusted and the artifact's config still lets uploaded code run. Null: not this access's.
|
|
1125
|
+
*/
|
|
1126
|
+
async function assetFor(
|
|
1127
|
+
request: Request,
|
|
1128
|
+
label: string,
|
|
1129
|
+
access: Access,
|
|
1130
|
+
id: string,
|
|
1131
|
+
): Promise<Response | null> {
|
|
1132
|
+
const store = options.assets;
|
|
1133
|
+
if (store === undefined || access.instance.document.kind !== "artifact") {
|
|
1134
|
+
return null;
|
|
1135
|
+
}
|
|
1136
|
+
const asset = await store.get(id, access);
|
|
1137
|
+
let named: string | null = null;
|
|
1138
|
+
try {
|
|
1139
|
+
named = asset === null ? null : nameOf(asset.scope);
|
|
1140
|
+
} catch {
|
|
1141
|
+
named = null;
|
|
1142
|
+
}
|
|
1143
|
+
if (asset === null || named !== label || asset.artifact !== access.instance.document.id) {
|
|
1144
|
+
return null;
|
|
1145
|
+
}
|
|
1146
|
+
const refusal = await denied(request, access);
|
|
1147
|
+
if (refusal !== null) {
|
|
1148
|
+
return refusal;
|
|
1149
|
+
}
|
|
1150
|
+
return serveAsset({ request, asset, frameAncestors, code: runsAsCode(asset, access) });
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
/**
|
|
1154
|
+
* Whether a code-typed asset goes out as code (P089, P103): only where it was trusted when it
|
|
1155
|
+
* was uploaded and the config its artifact's page is served under still lets uploaded code
|
|
1156
|
+
* run, asked at every request, so a stricter config wins at once. An access names that config.
|
|
1157
|
+
* By id alone (bearer mode) the server can't tell which of its artifact configs the page is
|
|
1158
|
+
* under: the strictest it might be decides (every one must let uploaded code run).
|
|
1159
|
+
*/
|
|
1160
|
+
function runsAsCode(asset: StoredAsset, access: Access | null): boolean {
|
|
1161
|
+
if (asset.trusted !== true) {
|
|
1162
|
+
return false;
|
|
1163
|
+
}
|
|
1164
|
+
if (access !== null) {
|
|
1165
|
+
const config = compiledFor(access.instance)?.config;
|
|
1166
|
+
return config !== undefined && assetCodeRuns(config);
|
|
1167
|
+
}
|
|
1168
|
+
const artifacts = [...compiled.values()]
|
|
1169
|
+
.map((each) => each.config)
|
|
1170
|
+
.filter((config) => config.mode === "artifact" && config.isolation === "domain");
|
|
1171
|
+
return artifacts.length > 0 && artifacts.every((config) => assetCodeRuns(config));
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/**
|
|
1175
|
+
* The accesses a request carries to artifacts of `label`'s origin, most specific first: the
|
|
1176
|
+
* sealed files' address its page is at (a frame's, which keeps no cookie, P083), as its
|
|
1177
|
+
* same-origin `Referer` names it; then its session cookies.
|
|
1178
|
+
*/
|
|
1179
|
+
async function assetAccessesOf(request: Request, label: string): Promise<Access[]> {
|
|
1180
|
+
const found: Access[] = [];
|
|
1181
|
+
const referer = request.headers.get("referer");
|
|
1182
|
+
if (referer !== null) {
|
|
1183
|
+
try {
|
|
1184
|
+
const from = new URL(referer);
|
|
1185
|
+
const token = FILES_PATH.exec(from.pathname)?.[1];
|
|
1186
|
+
if (from.origin === originOfLabel(label) && token !== undefined) {
|
|
1187
|
+
const sealed = await sealedAccess(request, label, token);
|
|
1188
|
+
if (sealed !== null) {
|
|
1189
|
+
found.push(sealed);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
} catch {
|
|
1193
|
+
// Not an address: no access from it.
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
return [...found, ...artifactAccessesOf(request, label)];
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
/** The accesses a request's cookies give to artifacts of `label`'s origin, latest first. */
|
|
1200
|
+
function artifactAccessesOf(request: Request, label: string): Access[] {
|
|
1201
|
+
const found: Access[] = [];
|
|
1202
|
+
for (const [name, value] of cookiesOf(request)) {
|
|
1203
|
+
if (!ourCookie(name)) {
|
|
1204
|
+
continue;
|
|
1205
|
+
}
|
|
1206
|
+
const session = keys.verify(value);
|
|
1207
|
+
if (
|
|
1208
|
+
!isRecord(session) ||
|
|
1209
|
+
typeof session.w !== "string" ||
|
|
1210
|
+
typeof session.x !== "number" ||
|
|
1211
|
+
session.x <= Date.now() ||
|
|
1212
|
+
(session.s !== undefined && typeof session.s !== "string")
|
|
1213
|
+
) {
|
|
1214
|
+
continue;
|
|
1215
|
+
}
|
|
1216
|
+
let instance: InstanceRef;
|
|
1217
|
+
try {
|
|
1218
|
+
instance = cleanInstance(session.i);
|
|
1219
|
+
if (instance.document.kind !== "artifact" || nameOf(instance.scope) !== label) {
|
|
1220
|
+
continue;
|
|
1221
|
+
}
|
|
1222
|
+
} catch {
|
|
1223
|
+
continue;
|
|
1224
|
+
}
|
|
1225
|
+
found.push({
|
|
1226
|
+
instance,
|
|
1227
|
+
viewer: session.w,
|
|
1228
|
+
origin: originOfLabel(label),
|
|
1229
|
+
expiresAt: session.x,
|
|
1230
|
+
...(session.a === 1 ? { anonymous: true } : {}),
|
|
1231
|
+
...(typeof session.s === "string" ? { subject: session.s } : {}),
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
return found.sort((a, b) => b.expiresAt - a.expiresAt);
|
|
1235
|
+
}
|
|
1236
|
+
|
|
951
1237
|
/**
|
|
952
1238
|
* A clean address without a cookie for it (P068): off to the host's authorize route with a
|
|
953
1239
|
* signed state, the browser holding the state's nonce in a cookie of this origin. A path just
|
|
@@ -993,8 +1279,9 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
993
1279
|
if (path.length > MAX_PATH) {
|
|
994
1280
|
return page(414, "Address too long", "This address is too long to open.", method);
|
|
995
1281
|
}
|
|
996
|
-
const mark = enteredMark(url.pathname);
|
|
997
|
-
|
|
1282
|
+
const mark = enteredMark(label, url.pathname);
|
|
1283
|
+
const marked = cookieName("n-");
|
|
1284
|
+
if (cookiesOf(request).some(([name, value]) => name.startsWith(marked) && value === mark)) {
|
|
998
1285
|
return page(
|
|
999
1286
|
401,
|
|
1000
1287
|
"Open this in a new tab",
|
|
@@ -1102,7 +1389,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1102
1389
|
headers.append(
|
|
1103
1390
|
"set-cookie",
|
|
1104
1391
|
[
|
|
1105
|
-
`${named}=${enteredMark(target.pathname)}`,
|
|
1392
|
+
`${named}=${enteredMark(label, target.pathname)}`,
|
|
1106
1393
|
"Path=/",
|
|
1107
1394
|
"HttpOnly",
|
|
1108
1395
|
`Max-Age=${ENTERED_SECONDS}`,
|
|
@@ -1165,7 +1452,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1165
1452
|
function forUpstream(request: Request): Request {
|
|
1166
1453
|
const headers = new Headers(request.headers);
|
|
1167
1454
|
const kept = cookiesOf(request)
|
|
1168
|
-
.filter(([name]) => !
|
|
1455
|
+
.filter(([name]) => !isPortholeCookie(name))
|
|
1169
1456
|
.map(([name, value]) => `${name}=${value}`);
|
|
1170
1457
|
if (kept.length > 0) {
|
|
1171
1458
|
headers.set("cookie", kept.join("; "));
|
|
@@ -1180,8 +1467,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1180
1467
|
const headers = new Headers(response.headers);
|
|
1181
1468
|
headers.delete("set-cookie");
|
|
1182
1469
|
for (const cookie of response.headers.getSetCookie()) {
|
|
1183
|
-
|
|
1184
|
-
if (!ourCookie(name)) {
|
|
1470
|
+
if (!setsPortholeCookie(cookie)) {
|
|
1185
1471
|
headers.append("set-cookie", cookie);
|
|
1186
1472
|
}
|
|
1187
1473
|
}
|
|
@@ -1196,6 +1482,15 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1196
1482
|
|
|
1197
1483
|
async function serveApp(request: Request, label: string): Promise<Response> {
|
|
1198
1484
|
const method = request.method.toUpperCase();
|
|
1485
|
+
// Before the cookie is read: another origin's request is refused whatever it carries.
|
|
1486
|
+
if (!fromItsOwnOrigin(request, label, true)) {
|
|
1487
|
+
return page(
|
|
1488
|
+
403,
|
|
1489
|
+
"Not from this app",
|
|
1490
|
+
"A request from another site doesn't ride this app's sign-in.",
|
|
1491
|
+
method,
|
|
1492
|
+
);
|
|
1493
|
+
}
|
|
1199
1494
|
const access = accessOf(request, label, null);
|
|
1200
1495
|
if (access === null) {
|
|
1201
1496
|
return page(
|
|
@@ -1344,7 +1639,9 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1344
1639
|
clean.document.kind === "external"
|
|
1345
1640
|
? `${ENTER_PATH}?ticket=${ticket}`
|
|
1346
1641
|
: `${PREFIX}d/${key}?ticket=${ticket}`;
|
|
1347
|
-
|
|
1642
|
+
// The viewer's level, as the host's access decided it now (P087): for the display's bridge.
|
|
1643
|
+
const viewer = viewerOfGrant(grant);
|
|
1644
|
+
return { url: `${origin}${path}`, origin, reports: true, ...(viewer ? { viewer } : {}) };
|
|
1348
1645
|
},
|
|
1349
1646
|
|
|
1350
1647
|
forget(filter = {}) {
|
|
@@ -1444,7 +1741,13 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1444
1741
|
|
|
1445
1742
|
async access(request) {
|
|
1446
1743
|
const at = labelOf(request);
|
|
1447
|
-
|
|
1744
|
+
// A socket is never a navigation: the app's own origin only (P101).
|
|
1745
|
+
if (
|
|
1746
|
+
at === null ||
|
|
1747
|
+
at === "mismatch" ||
|
|
1748
|
+
!ours(at.label) ||
|
|
1749
|
+
!fromItsOwnOrigin(request, at.label, false)
|
|
1750
|
+
) {
|
|
1448
1751
|
return null;
|
|
1449
1752
|
}
|
|
1450
1753
|
const access = accessOf(request, at.label, null);
|
|
@@ -1481,12 +1784,22 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1481
1784
|
return script;
|
|
1482
1785
|
}
|
|
1483
1786
|
if (url.pathname === SESSION_PATH) {
|
|
1484
|
-
// Whether this request's cookie opens its origin's app: the entry page asks
|
|
1787
|
+
// Whether this request's cookie opens its origin's app: the entry page asks, from the
|
|
1788
|
+
// app's origin (P101).
|
|
1789
|
+
if (!fromItsOwnOrigin(request, label, false)) {
|
|
1790
|
+
return new Response(null, { status: 403, headers: { "cache-control": "no-store" } });
|
|
1791
|
+
}
|
|
1485
1792
|
const access = accessOf(request, label, null);
|
|
1486
1793
|
const status =
|
|
1487
1794
|
access === null ? 401 : (await denied(request, access)) === null ? 204 : 403;
|
|
1488
1795
|
return new Response(null, { status, headers: { "cache-control": "no-store" } });
|
|
1489
1796
|
}
|
|
1797
|
+
// An artifact's asset library (P089), outside every version: `/_blob/<id>` only; any
|
|
1798
|
+
// other path there is a clean address as before (after the review).
|
|
1799
|
+
const asset = options.assets === undefined ? null : ASSET_ROUTE.exec(url.pathname);
|
|
1800
|
+
if (asset?.[1] !== undefined) {
|
|
1801
|
+
return await serveAssetAt(request, label, asset[1]);
|
|
1802
|
+
}
|
|
1490
1803
|
const document = DOCUMENT_PATH.exec(url.pathname);
|
|
1491
1804
|
if (document?.[1] !== undefined) {
|
|
1492
1805
|
return await serveDocument(request, url, label, document[1]);
|