@botiverse/porthole 0.5.1 → 0.7.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 +41 -0
- package/dist/bridge/bridge.d.ts +13 -0
- package/dist/bridge/bridge.js +208 -13
- package/dist/bridge/navigation.d.ts +1 -6
- package/dist/bridge/navigation.js +11 -0
- package/dist/bridge/refusal.d.ts +5 -1
- package/dist/bridge/refusal.js +16 -0
- package/dist/bridge/types.d.ts +41 -4
- package/dist/check/check.d.ts +19 -3
- package/dist/check/check.js +55 -8
- package/dist/check/files.d.ts +26 -0
- package/dist/check/files.js +307 -0
- package/dist/check/instrument.d.ts +3 -0
- package/dist/check/lint.js +31 -12
- package/dist/check/page-source.js +1 -1
- package/dist/check/page.js +14 -0
- package/dist/check/tools.d.ts +23 -0
- package/dist/check/tools.js +27 -3
- package/dist/compile/policy.d.ts +34 -0
- package/dist/compile/policy.js +74 -0
- package/dist/config/capabilities.d.ts +62 -3
- package/dist/config/capabilities.js +20 -0
- package/dist/config/schema.d.ts +32 -0
- package/dist/config/validate.d.ts +1 -1
- package/dist/config/validate.js +74 -5
- package/dist/conformance/harness-source.js +1 -1
- package/dist/conformance/index.d.ts +1 -0
- package/dist/conformance/index.js +2 -0
- package/dist/conformance/runtime.d.ts +58 -0
- package/dist/conformance/runtime.js +314 -0
- package/dist/files/paths.d.ts +99 -0
- package/dist/files/paths.js +263 -0
- package/dist/files/types.d.ts +24 -0
- package/dist/files/types.js +83 -0
- package/dist/guide/gates.d.ts +3 -0
- package/dist/guide/gates.js +19 -0
- package/dist/guide/guide.d.ts +2 -0
- package/dist/guide/guide.js +44 -32
- package/dist/guide/limits.js +4 -4
- package/dist/guide/runtime.d.ts +13 -0
- package/dist/guide/runtime.js +202 -0
- package/dist/host/create-host.js +45 -0
- package/dist/host/locate.js +8 -0
- package/dist/host/locator.js +7 -0
- package/dist/host/types.d.ts +18 -0
- package/dist/index.d.ts +13 -4
- package/dist/index.js +16 -3
- package/dist/isolation/types.d.ts +8 -0
- package/dist/presets/presets.d.ts +6 -0
- package/dist/presets/presets.js +1 -0
- package/dist/protocol/adapter.d.ts +4 -0
- package/dist/protocol/mcp-apps.d.ts +6 -0
- package/dist/protocol/mcp-apps.js +6 -0
- package/dist/protocol/runtime-calls.d.ts +28 -0
- package/dist/protocol/runtime-calls.js +113 -0
- package/dist/protocol/shell-adapter.js +52 -3
- package/dist/react/dialog.js +31 -0
- package/dist/react/frame.d.ts +13 -0
- package/dist/react/frame.js +6 -0
- package/dist/runtime/contract.d.ts +193 -0
- package/dist/runtime/contract.js +216 -0
- package/dist/runtime/db.d.ts +153 -0
- package/dist/runtime/db.js +398 -0
- package/dist/runtime/declaration.d.ts +86 -0
- package/dist/runtime/declaration.js +266 -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 +199 -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 +57 -0
- package/dist/server/files.js +338 -0
- package/dist/server/host.d.ts +44 -2
- package/dist/server/host.js +42 -2
- package/dist/server/index.d.ts +5 -2
- package/dist/server/index.js +5 -1
- package/dist/server/keys.d.ts +8 -0
- package/dist/server/keys.js +34 -0
- 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 +468 -24
- package/dist/server/types.d.ts +118 -5
- package/dist/shell/document.d.ts +27 -0
- package/dist/shell/document.js +46 -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.d.ts +2 -1
- package/dist/shell/runtime.js +114 -46
- package/dist/shell/use.d.ts +15 -0
- package/dist/shell/use.js +235 -0
- package/docs/ADOPTING.md +142 -5
- package/docs/ARCHITECTURE.md +34 -5
- package/docs/DECISIONS.md +716 -7
- package/docs/DESIGN.md +97 -3
- package/package.json +3 -1
- package/src/bridge/bridge.ts +281 -17
- package/src/bridge/navigation.ts +12 -0
- package/src/bridge/refusal.ts +16 -0
- package/src/bridge/types.ts +46 -4
- package/src/check/check.ts +100 -14
- package/src/check/files.ts +384 -0
- package/src/check/instrument.ts +3 -0
- package/src/check/lint.ts +33 -12
- package/src/check/page.ts +14 -0
- package/src/check/tools.ts +52 -3
- package/src/compile/policy.ts +92 -0
- package/src/config/capabilities.ts +49 -2
- package/src/config/schema.ts +32 -0
- package/src/config/validate.ts +107 -5
- package/src/conformance/index.ts +7 -0
- package/src/conformance/runtime.ts +573 -0
- package/src/files/paths.ts +323 -0
- package/src/files/types.ts +101 -0
- package/src/guide/gates.ts +25 -0
- package/src/guide/guide.ts +78 -52
- package/src/guide/limits.ts +4 -4
- package/src/guide/runtime.ts +244 -0
- package/src/host/create-host.ts +55 -0
- package/src/host/locate.ts +10 -0
- package/src/host/locator.ts +11 -0
- package/src/host/types.ts +18 -0
- package/src/index.ts +121 -0
- package/src/isolation/types.ts +8 -0
- package/src/presets/presets.ts +7 -0
- package/src/protocol/adapter.ts +3 -1
- package/src/protocol/mcp-apps.ts +7 -0
- package/src/protocol/runtime-calls.ts +138 -0
- package/src/protocol/shell-adapter.ts +56 -2
- package/src/react/dialog.tsx +33 -0
- package/src/react/frame.tsx +19 -0
- package/src/runtime/contract.ts +305 -0
- package/src/runtime/db.ts +530 -0
- package/src/runtime/declaration.ts +378 -0
- package/src/runtime/levels.ts +69 -0
- package/src/runtime/requests.ts +339 -0
- package/src/runtime/segments.ts +17 -0
- package/src/runtime/types.generated.ts +19 -0
- package/src/server/files.ts +425 -0
- package/src/server/host.ts +84 -3
- package/src/server/index.ts +22 -0
- package/src/server/keys.ts +53 -0
- package/src/server/pseudonym.ts +21 -0
- package/src/server/server.ts +565 -26
- package/src/server/types.ts +124 -4
- package/src/shell/document.ts +74 -4
- package/src/shell/legacy.ts +64 -0
- package/src/shell/plan.ts +37 -3
- package/src/shell/runtime.ts +114 -46
- 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 +16 -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/dist/server/server.js
CHANGED
|
@@ -6,6 +6,9 @@ import { randomBytes } from "./crypto.js";
|
|
|
6
6
|
import { isHostLabel, reservedLabels } from "./naming.js";
|
|
7
7
|
import { enterPage, pageScript, SESSION_PATH, statusPage } from "./pages.js";
|
|
8
8
|
import { libraryFile } from "./libraries.js";
|
|
9
|
+
import { ownLoad, serveAsset, serveFile } from "./files.js";
|
|
10
|
+
import { assetCodeRuns, assetCspDirectives, INERT_FILE_CSP, serializeCsp, } from "../compile/policy.js";
|
|
11
|
+
import { parseViewer } from "../runtime/levels.js";
|
|
9
12
|
import { memoryTicketStore } from "./tickets.js";
|
|
10
13
|
/**
|
|
11
14
|
* Paths on a content origin. Porthole's own live under `/__porthole/`; on a url-mode app's origin
|
|
@@ -14,6 +17,25 @@ import { memoryTicketStore } from "./tickets.js";
|
|
|
14
17
|
const PREFIX = "/__porthole/";
|
|
15
18
|
/** A served document (shell, artifact page, sandbox proxy), by its instance's key. */
|
|
16
19
|
const DOCUMENT_PATH = /^\/__porthole\/d\/([a-z2-7]{16})$/u;
|
|
20
|
+
/**
|
|
21
|
+
* A file set's files in a frame (P083): `/__porthole/f/<sealed access>/<path>`. The access is
|
|
22
|
+
* in the path, so a frame whose engine keeps no cookie (WebKit, P025) loads them too.
|
|
23
|
+
*/
|
|
24
|
+
const FILES_PATH = /^\/__porthole\/f\/([A-Za-z0-9_-]{40,4096})(\/.*)?$/u;
|
|
25
|
+
const FILES_PREFIX = "/__porthole/f/";
|
|
26
|
+
/** An asset's address (P089): `/_blob/` and 32 lowercase hex characters (`ASSET_ID`). */
|
|
27
|
+
const ASSET_ROUTE = /^\/_blob\/([0-9a-f]{32})$/u;
|
|
28
|
+
/** The same, under a frame's sealed files' address (P091): `<base>__porthole/blob/<id>`. */
|
|
29
|
+
const SEALED_ASSET = /^\/__porthole\/blob\/([0-9a-f]{32})$/u;
|
|
30
|
+
/** How long a sealed files' address opens a page's files on its own (P083); the shell renews it. */
|
|
31
|
+
const FILES_ADDRESS_MS = 3_600_000;
|
|
32
|
+
/** Below a sealed address: where the page's shell renews it (no file has a `__porthole/` path). */
|
|
33
|
+
const RENEW_PATH = "/__porthole/renew";
|
|
34
|
+
/**
|
|
35
|
+
* A clean address's base (P081): a path ending in `/`, each segment of letters, digits and
|
|
36
|
+
* `. _ : ~ -` (nothing a CSP source or a header would read otherwise), never `.` or `..`.
|
|
37
|
+
*/
|
|
38
|
+
const BASE = /^\/(?:(?!\.{1,2}\/)[A-Za-z0-9._:~-]{1,200}\/){0,16}$/u;
|
|
17
39
|
/** Where a url-mode app's ticket is taken, before the app's own landing path. */
|
|
18
40
|
const ENTER_PATH = "/__porthole/enter";
|
|
19
41
|
/** Where a redirect entry's code is redeemed, before its clean address (P068). */
|
|
@@ -49,6 +71,9 @@ function normalHost(value, protocol) {
|
|
|
49
71
|
: host;
|
|
50
72
|
}
|
|
51
73
|
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
74
|
+
const isStoredFiles = (stored) => isRecord(stored) &&
|
|
75
|
+
isRecord(stored.files) &&
|
|
76
|
+
typeof stored.read === "function";
|
|
52
77
|
/** Which configs a kind of served document may be served under. */
|
|
53
78
|
const MODES = {
|
|
54
79
|
shell: ["inline", "artifact"],
|
|
@@ -156,6 +181,23 @@ function cookiesOf(request) {
|
|
|
156
181
|
* fetch-style handler a host's backend mounts in front of its own routes, plus `issue`, which the
|
|
157
182
|
* host's authenticated API calls once it has decided a viewer may see an instance.
|
|
158
183
|
*/
|
|
184
|
+
/** A grant's viewer for the display's bridge (P087), checked; null without a level. */
|
|
185
|
+
function viewerOfGrant(grant) {
|
|
186
|
+
if (grant.level === undefined) {
|
|
187
|
+
if (grant.user !== undefined) {
|
|
188
|
+
throw new TypeError("A grant's user comes with its level");
|
|
189
|
+
}
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
const viewer = parseViewer({
|
|
193
|
+
level: grant.level,
|
|
194
|
+
...(grant.user === undefined ? {} : { id: grant.user.id, name: grant.user.name }),
|
|
195
|
+
});
|
|
196
|
+
if (viewer === null) {
|
|
197
|
+
throw new TypeError("A grant's level is view, interact, admin or owner; its user's id and name 1 to 256 characters");
|
|
198
|
+
}
|
|
199
|
+
return viewer;
|
|
200
|
+
}
|
|
159
201
|
export function createServer(options) {
|
|
160
202
|
const { content } = options;
|
|
161
203
|
const domain = content.domain;
|
|
@@ -357,12 +399,20 @@ export function createServer(options) {
|
|
|
357
399
|
return { html: config.shellDocument({ origins, cspMeta: false }), csp: config.csp };
|
|
358
400
|
case "artifact": {
|
|
359
401
|
const stored = await options.artifacts?.get(document.id, document.version);
|
|
360
|
-
if (stored === null || stored === undefined
|
|
402
|
+
if (stored === null || stored === undefined) {
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
// A version made of files (P081): served file by file under its base.
|
|
406
|
+
if (isStoredFiles(stored)) {
|
|
407
|
+
return { files: stored, compiled: config };
|
|
408
|
+
}
|
|
409
|
+
if (typeof stored.html !== "string") {
|
|
361
410
|
return null;
|
|
362
411
|
}
|
|
363
412
|
return {
|
|
364
413
|
html: config.shellDocument({ origins, document: stored.html, cspMeta: false }),
|
|
365
414
|
csp: config.csp,
|
|
415
|
+
compiled: config,
|
|
366
416
|
};
|
|
367
417
|
}
|
|
368
418
|
case "mcp-proxy":
|
|
@@ -379,7 +429,7 @@ export function createServer(options) {
|
|
|
379
429
|
* page that frames it; on a tab of its own, `Lax`. (Engines that keep no cookie in a cross-site
|
|
380
430
|
* frame, measured: WebKit, still get the document, which comes with the ticket's answer.)
|
|
381
431
|
*/
|
|
382
|
-
function sessionCookie(request, record,
|
|
432
|
+
function sessionCookie(request, record, at = {}) {
|
|
383
433
|
const sessionSeconds = record.sessionSeconds ?? DEFAULT_SESSION_SECONDS;
|
|
384
434
|
const session = {
|
|
385
435
|
i: record.instance,
|
|
@@ -387,7 +437,9 @@ export function createServer(options) {
|
|
|
387
437
|
x: Date.now() + sessionSeconds * 1_000,
|
|
388
438
|
...(record.anonymous === true ? { a: 1 } : {}),
|
|
389
439
|
...(record.subject === undefined ? {} : { s: record.subject }),
|
|
390
|
-
...(
|
|
440
|
+
...(at.p === undefined ? {} : { p: at.p }),
|
|
441
|
+
...(at.b === undefined ? {} : { b: at.b }),
|
|
442
|
+
...(at.k === undefined ? {} : { k: at.k }),
|
|
391
443
|
};
|
|
392
444
|
return [
|
|
393
445
|
`${cookieName(keys.instanceKey(record.instance))}=${keys.sign(session)}`,
|
|
@@ -454,16 +506,26 @@ export function createServer(options) {
|
|
|
454
506
|
}
|
|
455
507
|
return best;
|
|
456
508
|
}
|
|
457
|
-
/**
|
|
458
|
-
|
|
509
|
+
/**
|
|
510
|
+
* The access a request's cookies give to the document entered at `pathname` (P068), or to a
|
|
511
|
+
* file set's path under the base it was entered at (P081): with where the path falls under the
|
|
512
|
+
* base, or the base itself to go to (`/v/3` → `/v/3/`).
|
|
513
|
+
*/
|
|
514
|
+
function accessAtPath(request, label, url) {
|
|
515
|
+
const { pathname } = url;
|
|
459
516
|
let best = null;
|
|
460
517
|
for (const [name, value] of cookiesOf(request)) {
|
|
461
518
|
if (!ourCookie(name)) {
|
|
462
519
|
continue;
|
|
463
520
|
}
|
|
464
521
|
const session = keys.verify(value);
|
|
465
|
-
if (!isRecord(session)
|
|
466
|
-
|
|
522
|
+
if (!isRecord(session)) {
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
const base = typeof session.b === "string" && BASE.test(session.b) ? session.b : null;
|
|
526
|
+
const under = base !== null && pathname.startsWith(base);
|
|
527
|
+
const atBase = base !== null && `${pathname}/` === base;
|
|
528
|
+
if ((session.p !== pathname && !under && !atBase) ||
|
|
467
529
|
typeof session.w !== "string" ||
|
|
468
530
|
typeof session.x !== "number" ||
|
|
469
531
|
session.x <= Date.now() ||
|
|
@@ -480,14 +542,37 @@ export function createServer(options) {
|
|
|
480
542
|
catch {
|
|
481
543
|
continue;
|
|
482
544
|
}
|
|
483
|
-
|
|
545
|
+
// The most specific first: the path entered itself, then the longest base, then the
|
|
546
|
+
// latest to end.
|
|
547
|
+
const rank = session.p === pathname ? Number.MAX_SAFE_INTEGER : (base?.length ?? 0);
|
|
548
|
+
if (best === null ||
|
|
549
|
+
rank > best.rank ||
|
|
550
|
+
(rank === best.rank && session.x > best.access.expiresAt)) {
|
|
551
|
+
const origin = originOfLabel(label);
|
|
484
552
|
best = {
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
553
|
+
rank,
|
|
554
|
+
access: {
|
|
555
|
+
instance,
|
|
556
|
+
viewer: session.w,
|
|
557
|
+
origin,
|
|
558
|
+
expiresAt: session.x,
|
|
559
|
+
...(session.a === 1 ? { anonymous: true } : {}),
|
|
560
|
+
...(typeof session.s === "string" ? { subject: session.s } : {}),
|
|
561
|
+
},
|
|
562
|
+
...(base !== null && under
|
|
563
|
+
? {
|
|
564
|
+
at: {
|
|
565
|
+
base,
|
|
566
|
+
rest: pathname.slice(base.length),
|
|
567
|
+
search: url.search,
|
|
568
|
+
pinned: session.k === 1,
|
|
569
|
+
source: new URL(base, origin).href,
|
|
570
|
+
},
|
|
571
|
+
}
|
|
572
|
+
: {}),
|
|
573
|
+
...(base !== null && atBase && session.p !== pathname
|
|
574
|
+
? { redirect: `${origin}${base}${url.search}` }
|
|
575
|
+
: {}),
|
|
491
576
|
};
|
|
492
577
|
}
|
|
493
578
|
}
|
|
@@ -520,6 +605,118 @@ export function createServer(options) {
|
|
|
520
605
|
"x-robots-tag": "noindex, nofollow",
|
|
521
606
|
});
|
|
522
607
|
}
|
|
608
|
+
/**
|
|
609
|
+
* The access a sealed files' address carries (P083), if it holds for this origin now: on its own
|
|
610
|
+
* for its hour (`x`), then only for the running page's own loads until the session ends (`e`).
|
|
611
|
+
*/
|
|
612
|
+
async function sealedAccess(request, label, token) {
|
|
613
|
+
const session = await keys.unseal(token);
|
|
614
|
+
const now = Date.now();
|
|
615
|
+
if (!isRecord(session) ||
|
|
616
|
+
typeof session.w !== "string" ||
|
|
617
|
+
typeof session.x !== "number" ||
|
|
618
|
+
typeof session.e !== "number" ||
|
|
619
|
+
(session.s !== undefined && typeof session.s !== "string")) {
|
|
620
|
+
return null;
|
|
621
|
+
}
|
|
622
|
+
const open = session.x > now || (session.e > now && ownLoad(request));
|
|
623
|
+
let instance;
|
|
624
|
+
try {
|
|
625
|
+
instance = cleanInstance(session.i);
|
|
626
|
+
}
|
|
627
|
+
catch {
|
|
628
|
+
return null;
|
|
629
|
+
}
|
|
630
|
+
if (!open || instance.document.kind !== "artifact" || nameOf(instance.scope) !== label) {
|
|
631
|
+
return null;
|
|
632
|
+
}
|
|
633
|
+
return {
|
|
634
|
+
instance,
|
|
635
|
+
viewer: session.w,
|
|
636
|
+
origin: originOfLabel(label),
|
|
637
|
+
expiresAt: session.e,
|
|
638
|
+
...(session.a === 1 ? { anonymous: true } : {}),
|
|
639
|
+
...(typeof session.s === "string" ? { subject: session.s } : {}),
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* A file set's file in a frame (P083): the sealed access in its path opens it, once it holds
|
|
644
|
+
* for this origin and the host's check lets it in; every path under it is the same version's.
|
|
645
|
+
*/
|
|
646
|
+
/** A files' address for an access (P083): sealed, its own end within the session's. */
|
|
647
|
+
async function sealFiles(access) {
|
|
648
|
+
const expires = Math.min(access.expiresAt, Date.now() + FILES_ADDRESS_MS);
|
|
649
|
+
const token = await keys.seal({
|
|
650
|
+
i: access.instance,
|
|
651
|
+
w: access.viewer,
|
|
652
|
+
x: expires,
|
|
653
|
+
e: access.expiresAt,
|
|
654
|
+
...(access.anonymous === true ? { a: 1 } : {}),
|
|
655
|
+
...(access.subject === undefined ? {} : { s: access.subject }),
|
|
656
|
+
});
|
|
657
|
+
return { base: `${FILES_PREFIX}${token}/`, expires };
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* A file set's file in a frame (P083): the sealed access in its path opens it, once it holds
|
|
661
|
+
* for this origin and the host's check lets it in; every path under it is the same version's.
|
|
662
|
+
* The address opens on its own for an hour (its `x`); after that, until the session's end (its
|
|
663
|
+
* `e`), only the running page's own loads (same-origin, not a navigation), from addresses it
|
|
664
|
+
* already holds, while its shell renews the address (`__porthole/renew`).
|
|
665
|
+
*/
|
|
666
|
+
async function serveSealed(request, url, label, token, rest) {
|
|
667
|
+
const method = request.method.toUpperCase();
|
|
668
|
+
const origin = originOfLabel(label);
|
|
669
|
+
const base = `${FILES_PREFIX}${token}/`;
|
|
670
|
+
if (rest === undefined) {
|
|
671
|
+
return new Response(null, {
|
|
672
|
+
status: 307,
|
|
673
|
+
headers: { location: `${origin}${base}${url.search}`, "cache-control": "no-store" },
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
const access = await sealedAccess(request, label, token);
|
|
677
|
+
if (access === null) {
|
|
678
|
+
return page(401, "This has ended", "Content here opens for a while. Open it again from the app that showed it.", method, "session");
|
|
679
|
+
}
|
|
680
|
+
// An asset of this artifact's library, at the frame's own sealed address (P091): every engine
|
|
681
|
+
// sends a frame's path, where some send no Referer that names it.
|
|
682
|
+
const blob = SEALED_ASSET.exec(rest)?.[1];
|
|
683
|
+
if (blob !== undefined) {
|
|
684
|
+
if (options.assets === undefined) {
|
|
685
|
+
return page(404, "Not found", "This address names no content.", method);
|
|
686
|
+
}
|
|
687
|
+
return ((await assetFor(request, label, access, blob)) ??
|
|
688
|
+
page(404, "Not found", "This address names no content.", method));
|
|
689
|
+
}
|
|
690
|
+
if (rest === RENEW_PATH) {
|
|
691
|
+
// Only the page itself renews its address, while its session lasts and the host lets it.
|
|
692
|
+
if (method !== "GET" || !ownLoad(request)) {
|
|
693
|
+
return page(403, "Not allowed", "Only the page renews its own address.", method);
|
|
694
|
+
}
|
|
695
|
+
const refusal = await denied(request, access);
|
|
696
|
+
if (refusal !== null) {
|
|
697
|
+
return refusal;
|
|
698
|
+
}
|
|
699
|
+
const renewed = await sealFiles(access);
|
|
700
|
+
return Response.json({ base: `${origin}${renewed.base}`, expires: renewed.expires }, {
|
|
701
|
+
headers: {
|
|
702
|
+
"cache-control": "no-store",
|
|
703
|
+
"x-content-type-options": "nosniff",
|
|
704
|
+
"cross-origin-resource-policy": "same-origin",
|
|
705
|
+
"content-security-policy": `${INERT_FILE_CSP}; frame-ancestors ${frameAncestors}`,
|
|
706
|
+
"referrer-policy": "no-referrer",
|
|
707
|
+
},
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
return serveAccess(request, access, null, {
|
|
711
|
+
base,
|
|
712
|
+
rest: rest.slice(1),
|
|
713
|
+
search: url.search,
|
|
714
|
+
pinned: true,
|
|
715
|
+
// Every frame's files on this origin share the prefix (the scope's, P082): a page knows
|
|
716
|
+
// only its own access.
|
|
717
|
+
source: `${origin}${FILES_PREFIX}`,
|
|
718
|
+
});
|
|
719
|
+
}
|
|
523
720
|
async function serveDocument(request, url, label, key) {
|
|
524
721
|
const method = request.method.toUpperCase();
|
|
525
722
|
if (method !== "GET" && method !== "HEAD") {
|
|
@@ -549,8 +746,11 @@ export function createServer(options) {
|
|
|
549
746
|
}
|
|
550
747
|
return serveAccess(request, access, cookie);
|
|
551
748
|
}
|
|
552
|
-
/**
|
|
553
|
-
|
|
749
|
+
/**
|
|
750
|
+
* The document an access opens, once the host's check lets it in: at its document's path, or
|
|
751
|
+
* (`at`) a path under a file set's base (P081).
|
|
752
|
+
*/
|
|
753
|
+
async function serveAccess(request, access, cookie, at) {
|
|
554
754
|
const method = request.method.toUpperCase();
|
|
555
755
|
const refusal = await denied(request, access);
|
|
556
756
|
if (refusal !== null) {
|
|
@@ -560,12 +760,184 @@ export function createServer(options) {
|
|
|
560
760
|
if (served === null) {
|
|
561
761
|
return page(404, "Not found", "This page, or this version of it, doesn't exist.", method);
|
|
562
762
|
}
|
|
563
|
-
|
|
763
|
+
if ("files" in served) {
|
|
764
|
+
if (at !== undefined) {
|
|
765
|
+
return serveFile({
|
|
766
|
+
request,
|
|
767
|
+
set: served.files,
|
|
768
|
+
rest: at.rest,
|
|
769
|
+
search: at.search,
|
|
770
|
+
base: at.base,
|
|
771
|
+
origin: access.origin,
|
|
772
|
+
pinned: at.pinned,
|
|
773
|
+
source: at.source,
|
|
774
|
+
compiled: served.compiled,
|
|
775
|
+
hostOrigins: options.hostOrigins,
|
|
776
|
+
frameAncestors,
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
// A frame's document (a ticket's): its files carry its access in their path (P083). The
|
|
780
|
+
// entry comes in the ticket's own answer (P025), its shell moving the frame to its address
|
|
781
|
+
// there before anything loads, and renewing it before it ends.
|
|
782
|
+
const sealed = await sealFiles(access);
|
|
783
|
+
return serveFile({
|
|
784
|
+
request,
|
|
785
|
+
set: served.files,
|
|
786
|
+
rest: "",
|
|
787
|
+
search: "",
|
|
788
|
+
base: sealed.base,
|
|
789
|
+
origin: access.origin,
|
|
790
|
+
pinned: true,
|
|
791
|
+
source: `${access.origin}${FILES_PREFIX}`,
|
|
792
|
+
compiled: served.compiled,
|
|
793
|
+
hostOrigins: options.hostOrigins,
|
|
794
|
+
frameAncestors,
|
|
795
|
+
ticket: { expires: sealed.expires },
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
// One page at a file set's base (P081): its base is the page, nothing under it is.
|
|
799
|
+
if (at !== undefined && at.rest !== "") {
|
|
800
|
+
return page(404, "Not found", "This page, or this version of it, doesn't exist.", method);
|
|
801
|
+
}
|
|
802
|
+
// A page that may use its artifact's assets loads them from its own origin (P089).
|
|
803
|
+
const headers = documentHeaders(served.compiled === undefined
|
|
804
|
+
? served.csp
|
|
805
|
+
: serializeCsp(assetCspDirectives(served.compiled.config, served.compiled.cspDirectives, access.origin)));
|
|
564
806
|
if (cookie !== null) {
|
|
565
807
|
headers.set("set-cookie", cookie);
|
|
566
808
|
}
|
|
567
809
|
return new Response(method === "HEAD" ? null : served.html, { status: 200, headers });
|
|
568
810
|
}
|
|
811
|
+
/**
|
|
812
|
+
* An asset at `/_blob/<id>` (P089, after the review): a request carrying an access to an
|
|
813
|
+
* artifact of this origin (its page's sealed files' address, as its `Referer` says, P091; or a
|
|
814
|
+
* session cookie), then the host's check for that access, then the host's store asked with it;
|
|
815
|
+
* served only when the asset is that artifact's, on its scope's origin. Never an entry. By id
|
|
816
|
+
* alone only where the host opted into it (`assets.bearer`).
|
|
817
|
+
*/
|
|
818
|
+
async function serveAssetAt(request, label, id) {
|
|
819
|
+
const method = request.method.toUpperCase();
|
|
820
|
+
const store = options.assets;
|
|
821
|
+
const none = () => page(404, "Not found", "This address names no content.", method);
|
|
822
|
+
if (store === undefined) {
|
|
823
|
+
return none();
|
|
824
|
+
}
|
|
825
|
+
const scoped = (asset) => {
|
|
826
|
+
try {
|
|
827
|
+
return asset !== null && nameOf(asset.scope) === label;
|
|
828
|
+
}
|
|
829
|
+
catch {
|
|
830
|
+
return false;
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
if (store.bearer === true) {
|
|
834
|
+
const asset = await store.get(id, null);
|
|
835
|
+
return scoped(asset)
|
|
836
|
+
? serveAsset({ request, asset, frameAncestors, code: asset.trusted === true })
|
|
837
|
+
: none();
|
|
838
|
+
}
|
|
839
|
+
const accesses = await assetAccessesOf(request, label);
|
|
840
|
+
if (accesses.length === 0) {
|
|
841
|
+
return new Response(null, { status: 401, headers: { "cache-control": "no-store" } });
|
|
842
|
+
}
|
|
843
|
+
for (const access of accesses) {
|
|
844
|
+
const answer = await assetFor(request, label, access, id);
|
|
845
|
+
if (answer !== null) {
|
|
846
|
+
return answer;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
return none();
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* An asset for one access (P089, P091): the store asked with it, served only when the asset is
|
|
853
|
+
* its artifact's on its scope's origin, after the host's check; its code only where it was
|
|
854
|
+
* trusted and the artifact's config still lets uploaded code run. Null: not this access's.
|
|
855
|
+
*/
|
|
856
|
+
async function assetFor(request, label, access, id) {
|
|
857
|
+
const store = options.assets;
|
|
858
|
+
if (store === undefined || access.instance.document.kind !== "artifact") {
|
|
859
|
+
return null;
|
|
860
|
+
}
|
|
861
|
+
const asset = await store.get(id, access);
|
|
862
|
+
let named = null;
|
|
863
|
+
try {
|
|
864
|
+
named = asset === null ? null : nameOf(asset.scope);
|
|
865
|
+
}
|
|
866
|
+
catch {
|
|
867
|
+
named = null;
|
|
868
|
+
}
|
|
869
|
+
if (asset === null || named !== label || asset.artifact !== access.instance.document.id) {
|
|
870
|
+
return null;
|
|
871
|
+
}
|
|
872
|
+
const refusal = await denied(request, access);
|
|
873
|
+
if (refusal !== null) {
|
|
874
|
+
return refusal;
|
|
875
|
+
}
|
|
876
|
+
const config = compiledFor(access.instance)?.config;
|
|
877
|
+
const code = asset.trusted === true && config !== undefined && assetCodeRuns(config);
|
|
878
|
+
return serveAsset({ request, asset, frameAncestors, code });
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* The accesses a request carries to artifacts of `label`'s origin, most specific first: the
|
|
882
|
+
* sealed files' address its page is at (a frame's, which keeps no cookie, P083), as its
|
|
883
|
+
* same-origin `Referer` names it; then its session cookies.
|
|
884
|
+
*/
|
|
885
|
+
async function assetAccessesOf(request, label) {
|
|
886
|
+
const found = [];
|
|
887
|
+
const referer = request.headers.get("referer");
|
|
888
|
+
if (referer !== null) {
|
|
889
|
+
try {
|
|
890
|
+
const from = new URL(referer);
|
|
891
|
+
const token = FILES_PATH.exec(from.pathname)?.[1];
|
|
892
|
+
if (from.origin === originOfLabel(label) && token !== undefined) {
|
|
893
|
+
const sealed = await sealedAccess(request, label, token);
|
|
894
|
+
if (sealed !== null) {
|
|
895
|
+
found.push(sealed);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
catch {
|
|
900
|
+
// Not an address: no access from it.
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
return [...found, ...artifactAccessesOf(request, label)];
|
|
904
|
+
}
|
|
905
|
+
/** The accesses a request's cookies give to artifacts of `label`'s origin, latest first. */
|
|
906
|
+
function artifactAccessesOf(request, label) {
|
|
907
|
+
const found = [];
|
|
908
|
+
for (const [name, value] of cookiesOf(request)) {
|
|
909
|
+
if (!ourCookie(name)) {
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
const session = keys.verify(value);
|
|
913
|
+
if (!isRecord(session) ||
|
|
914
|
+
typeof session.w !== "string" ||
|
|
915
|
+
typeof session.x !== "number" ||
|
|
916
|
+
session.x <= Date.now() ||
|
|
917
|
+
(session.s !== undefined && typeof session.s !== "string")) {
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
let instance;
|
|
921
|
+
try {
|
|
922
|
+
instance = cleanInstance(session.i);
|
|
923
|
+
if (instance.document.kind !== "artifact" || nameOf(instance.scope) !== label) {
|
|
924
|
+
continue;
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
catch {
|
|
928
|
+
continue;
|
|
929
|
+
}
|
|
930
|
+
found.push({
|
|
931
|
+
instance,
|
|
932
|
+
viewer: session.w,
|
|
933
|
+
origin: originOfLabel(label),
|
|
934
|
+
expiresAt: session.x,
|
|
935
|
+
...(session.a === 1 ? { anonymous: true } : {}),
|
|
936
|
+
...(typeof session.s === "string" ? { subject: session.s } : {}),
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
return found.sort((a, b) => b.expiresAt - a.expiresAt);
|
|
940
|
+
}
|
|
569
941
|
/**
|
|
570
942
|
* A clean address without a cookie for it (P068): off to the host's authorize route with a
|
|
571
943
|
* signed state, the browser holding the state's nonce in a cookie of this origin. A path just
|
|
@@ -580,6 +952,22 @@ export function createServer(options) {
|
|
|
580
952
|
(named !== undefined && !(await named(label, url.pathname, request)))) {
|
|
581
953
|
return page(404, "Nothing here", "This address names no content.", method);
|
|
582
954
|
}
|
|
955
|
+
// Only a page opening is sent to sign in (a browser's navigation; a client that sends no fetch
|
|
956
|
+
// metadata may be one): a browser's subresource without a session is refused here, with no
|
|
957
|
+
// state of its own (no nonce cookie, nothing cached).
|
|
958
|
+
const mode = request.headers.get("sec-fetch-mode");
|
|
959
|
+
const dest = request.headers.get("sec-fetch-dest") ?? "";
|
|
960
|
+
if (mode !== null && mode !== "navigate" && !["document", "iframe", "frame"].includes(dest)) {
|
|
961
|
+
return new Response(method === "HEAD" ? null : "Open this address in a browser.", {
|
|
962
|
+
status: 401,
|
|
963
|
+
headers: {
|
|
964
|
+
"content-type": "text/plain; charset=utf-8",
|
|
965
|
+
"cache-control": "no-store",
|
|
966
|
+
"x-content-type-options": "nosniff",
|
|
967
|
+
"x-robots-tag": "noindex, nofollow",
|
|
968
|
+
},
|
|
969
|
+
});
|
|
970
|
+
}
|
|
583
971
|
if (method !== "GET" && method !== "HEAD") {
|
|
584
972
|
return page(401, "Open this from the app", "Open this address in a browser.", method, "session");
|
|
585
973
|
}
|
|
@@ -634,7 +1022,7 @@ export function createServer(options) {
|
|
|
634
1022
|
if (record?.entry === undefined) {
|
|
635
1023
|
return page(403, "This sign-in link has been used", "It works once, for a short time. Open the address again.", method, "session");
|
|
636
1024
|
}
|
|
637
|
-
const { nonce, path } = record.entry;
|
|
1025
|
+
const { nonce, path, base, pinned } = record.entry;
|
|
638
1026
|
const named = nonceCookie(nonce);
|
|
639
1027
|
if (!cookiesOf(request).some(([name, value]) => name === named && value === "1")) {
|
|
640
1028
|
return page(401, "Open this in a new tab", "This browser keeps no sign-in for it inside a frame.", method, "cookies");
|
|
@@ -660,7 +1048,13 @@ export function createServer(options) {
|
|
|
660
1048
|
"referrer-policy": "no-referrer",
|
|
661
1049
|
"x-robots-tag": "noindex, nofollow",
|
|
662
1050
|
});
|
|
663
|
-
|
|
1051
|
+
// A file set's address (P081): the session serves every path under its base, the version's
|
|
1052
|
+
// own address. Else the path it was entered at, alone (P068).
|
|
1053
|
+
headers.append("set-cookie", sessionCookie(request, record, external
|
|
1054
|
+
? {}
|
|
1055
|
+
: base !== undefined
|
|
1056
|
+
? { b: base, ...(pinned === true ? { k: 1 } : {}) }
|
|
1057
|
+
: { p: target.pathname }));
|
|
664
1058
|
headers.append("set-cookie", [
|
|
665
1059
|
`${named}=${enteredMark(target.pathname)}`,
|
|
666
1060
|
"Path=/",
|
|
@@ -792,6 +1186,9 @@ export function createServer(options) {
|
|
|
792
1186
|
...(anonymous ? { a: 1 } : {}),
|
|
793
1187
|
...(grant.subject === undefined ? {} : { s: grant.subject }),
|
|
794
1188
|
...(entry === undefined ? {} : { p: entry.path }),
|
|
1189
|
+
...(entry?.base === undefined
|
|
1190
|
+
? {}
|
|
1191
|
+
: { b: entry.base, ...(entry.pinned === true ? { k: 1 } : {}) }),
|
|
795
1192
|
});
|
|
796
1193
|
if (cookieName(key).length + probe.length + 120 > MAX_COOKIE_BYTES) {
|
|
797
1194
|
throw new TypeError("This instance is too large to carry in a cookie (fewer origins?)");
|
|
@@ -859,7 +1256,9 @@ export function createServer(options) {
|
|
|
859
1256
|
const path = clean.document.kind === "external"
|
|
860
1257
|
? `${ENTER_PATH}?ticket=${ticket}`
|
|
861
1258
|
: `${PREFIX}d/${key}?ticket=${ticket}`;
|
|
862
|
-
|
|
1259
|
+
// The viewer's level, as the host's access decided it now (P087): for the display's bridge.
|
|
1260
|
+
const viewer = viewerOfGrant(grant);
|
|
1261
|
+
return { url: `${origin}${path}`, origin, reports: true, ...(viewer ? { viewer } : {}) };
|
|
863
1262
|
},
|
|
864
1263
|
forget(filter = {}) {
|
|
865
1264
|
for (const [key, each] of answers) {
|
|
@@ -897,7 +1296,7 @@ export function createServer(options) {
|
|
|
897
1296
|
expired: state.x <= Date.now(),
|
|
898
1297
|
};
|
|
899
1298
|
},
|
|
900
|
-
async enterBy(state, instance, grant) {
|
|
1299
|
+
async enterBy(state, instance, grant, address) {
|
|
901
1300
|
if (state.expired) {
|
|
902
1301
|
throw new TypeError("This entry's state has expired: start again from the clean address");
|
|
903
1302
|
}
|
|
@@ -905,9 +1304,38 @@ export function createServer(options) {
|
|
|
905
1304
|
if (nameOf(clean.scope) !== state.label) {
|
|
906
1305
|
throw new TypeError("The instance isn't served on the address the entry began at");
|
|
907
1306
|
}
|
|
1307
|
+
let path = state.path;
|
|
1308
|
+
if (address !== undefined) {
|
|
1309
|
+
// A file set's own address (P081): a path ending in /, not Porthole's own. An address
|
|
1310
|
+
// that isn't under it (the latest's) goes to the same file under it, which must stay
|
|
1311
|
+
// under it: no dot segment, no backslash, nothing the URL parser would resolve away.
|
|
1312
|
+
const { base, file = "" } = address;
|
|
1313
|
+
const segments = typeof file === "string" ? file.split("/") : [];
|
|
1314
|
+
if (typeof base !== "string" ||
|
|
1315
|
+
!BASE.test(base) ||
|
|
1316
|
+
base.startsWith(PREFIX) ||
|
|
1317
|
+
typeof file !== "string" ||
|
|
1318
|
+
file.length > MAX_PATH ||
|
|
1319
|
+
file.startsWith("/") ||
|
|
1320
|
+
// oxlint-disable-next-line no-control-regex -- control characters are what it refuses
|
|
1321
|
+
/[?#\\\u0000-\u001f\u007f]/u.test(file) ||
|
|
1322
|
+
segments.some((each) => /^(?:\.|%2e){1,2}$/iu.test(each))) {
|
|
1323
|
+
throw new TypeError("An entry's base is a path ending in / (not Porthole's own), its file a relative path below it");
|
|
1324
|
+
}
|
|
1325
|
+
const asked = new URL(state.path, "https://x.invalid");
|
|
1326
|
+
if (!asked.pathname.startsWith(base)) {
|
|
1327
|
+
path = `${base}${file}${asked.search}`;
|
|
1328
|
+
}
|
|
1329
|
+
if (!new URL(path, "https://x.invalid").pathname.startsWith(base)) {
|
|
1330
|
+
throw new TypeError("An entry's file must stay below its base");
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
908
1333
|
const { ticket } = await mint(clean, { ...grant, ttlSeconds: ENTRY_CODE_SECONDS }, {
|
|
909
|
-
path
|
|
1334
|
+
path,
|
|
910
1335
|
nonce: state.nonce,
|
|
1336
|
+
...(address === undefined
|
|
1337
|
+
? {}
|
|
1338
|
+
: { base: address.base, ...(address.pinned === true ? { pinned: true } : {}) }),
|
|
911
1339
|
});
|
|
912
1340
|
return `${originOfLabel(state.label)}${ENTRY_PATH}?code=${ticket}`;
|
|
913
1341
|
},
|
|
@@ -949,18 +1377,34 @@ export function createServer(options) {
|
|
|
949
1377
|
const status = access === null ? 401 : (await denied(request, access)) === null ? 204 : 403;
|
|
950
1378
|
return new Response(null, { status, headers: { "cache-control": "no-store" } });
|
|
951
1379
|
}
|
|
1380
|
+
// An artifact's asset library (P089), outside every version: `/_blob/<id>` only; any
|
|
1381
|
+
// other path there is a clean address as before (after the review).
|
|
1382
|
+
const asset = options.assets === undefined ? null : ASSET_ROUTE.exec(url.pathname);
|
|
1383
|
+
if (asset?.[1] !== undefined) {
|
|
1384
|
+
return await serveAssetAt(request, label, asset[1]);
|
|
1385
|
+
}
|
|
952
1386
|
const document = DOCUMENT_PATH.exec(url.pathname);
|
|
953
1387
|
if (document?.[1] !== undefined) {
|
|
954
1388
|
return await serveDocument(request, url, label, document[1]);
|
|
955
1389
|
}
|
|
1390
|
+
const sealed = FILES_PATH.exec(url.pathname);
|
|
1391
|
+
if (sealed?.[1] !== undefined) {
|
|
1392
|
+
return await serveSealed(request, url, label, sealed[1], sealed[2]);
|
|
1393
|
+
}
|
|
956
1394
|
if (url.pathname.startsWith(PREFIX)) {
|
|
957
1395
|
return page(404, "Nothing here", "This address names no content.", method);
|
|
958
1396
|
}
|
|
959
1397
|
// A clean address (P068): the document entered here, else the app, else entry.
|
|
960
1398
|
if (authorizeUrl !== null) {
|
|
961
|
-
const entered = accessAtPath(request, label, url
|
|
1399
|
+
const entered = accessAtPath(request, label, url);
|
|
1400
|
+
if (entered?.redirect !== undefined) {
|
|
1401
|
+
return new Response(null, {
|
|
1402
|
+
status: 307,
|
|
1403
|
+
headers: { location: entered.redirect, "cache-control": "no-store" },
|
|
1404
|
+
});
|
|
1405
|
+
}
|
|
962
1406
|
if (entered !== null) {
|
|
963
|
-
return await serveAccess(request, entered, null);
|
|
1407
|
+
return await serveAccess(request, entered.access, null, entered.at);
|
|
964
1408
|
}
|
|
965
1409
|
if (options.upstream === undefined || accessOf(request, label, null) === null) {
|
|
966
1410
|
return await beginEntry(request, url, label, authorizeUrl);
|