@botiverse/porthole 0.6.0 → 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 +16 -0
- package/dist/bridge/bridge.d.ts +13 -0
- package/dist/bridge/bridge.js +200 -8
- 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 +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 +14 -0
- package/dist/check/tools.js +2 -3
- package/dist/compile/policy.d.ts +19 -0
- package/dist/compile/policy.js +42 -0
- package/dist/config/capabilities.d.ts +62 -3
- package/dist/config/capabilities.js +20 -0
- package/dist/config/schema.d.ts +24 -0
- package/dist/config/validate.d.ts +1 -1
- package/dist/config/validate.js +66 -4
- 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/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 +34 -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 +24 -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 +8 -2
- package/dist/index.js +9 -1
- 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/runtime-calls.d.ts +28 -0
- package/dist/protocol/runtime-calls.js +113 -0
- package/dist/protocol/shell-adapter.js +48 -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 +14 -1
- package/dist/server/files.js +97 -3
- package/dist/server/host.d.ts +6 -1
- package/dist/server/host.js +5 -0
- package/dist/server/index.d.ts +2 -1
- package/dist/server/index.js +1 -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 +214 -35
- package/dist/server/types.d.ts +69 -0
- package/dist/shell/document.d.ts +8 -0
- package/dist/shell/document.js +16 -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 +29 -42
- package/dist/shell/use.d.ts +15 -0
- package/dist/shell/use.js +235 -0
- package/docs/ADOPTING.md +91 -6
- package/docs/ARCHITECTURE.md +22 -3
- package/docs/DECISIONS.md +415 -6
- package/docs/DESIGN.md +66 -0
- package/package.json +3 -1
- package/src/bridge/bridge.ts +273 -12
- package/src/bridge/refusal.ts +16 -0
- package/src/bridge/types.ts +46 -4
- 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 +14 -0
- package/src/check/tools.ts +2 -3
- package/src/compile/policy.ts +54 -0
- package/src/config/capabilities.ts +49 -2
- package/src/config/schema.ts +24 -0
- package/src/config/validate.ts +99 -4
- package/src/conformance/index.ts +7 -0
- package/src/conformance/runtime.ts +573 -0
- package/src/guide/gates.ts +25 -0
- package/src/guide/guide.ts +59 -53
- package/src/guide/limits.ts +4 -4
- package/src/guide/runtime.ts +244 -0
- package/src/host/create-host.ts +34 -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 +89 -0
- package/src/isolation/types.ts +8 -0
- package/src/presets/presets.ts +7 -0
- package/src/protocol/runtime-calls.ts +138 -0
- package/src/protocol/shell-adapter.ts +52 -3
- 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 +117 -4
- package/src/server/host.ts +12 -0
- package/src/server/index.ts +3 -0
- package/src/server/pseudonym.ts +21 -0
- package/src/server/server.ts +247 -37
- package/src/server/types.ts +68 -0
- package/src/shell/document.ts +21 -4
- package/src/shell/legacy.ts +64 -0
- package/src/shell/plan.ts +37 -3
- package/src/shell/runtime.ts +29 -42
- 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/src/server/server.ts
CHANGED
|
@@ -13,14 +13,21 @@ 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 {
|
|
18
|
+
assetCodeRuns,
|
|
19
|
+
assetCspDirectives,
|
|
20
|
+
INERT_FILE_CSP,
|
|
21
|
+
serializeCsp,
|
|
22
|
+
} from "../compile/policy.ts";
|
|
23
|
+
import { parseViewer, type ViewerInfo } from "../runtime/levels.ts";
|
|
18
24
|
import type { FrameBlock } from "../host/types.ts";
|
|
19
25
|
import { memoryTicketStore } from "./tickets.ts";
|
|
20
26
|
import type {
|
|
21
27
|
Access,
|
|
22
28
|
AuthorizeAnswer,
|
|
23
29
|
Grant,
|
|
30
|
+
StoredAsset,
|
|
24
31
|
PortholeServer,
|
|
25
32
|
ServerOptions,
|
|
26
33
|
StoredFiles,
|
|
@@ -42,6 +49,10 @@ const DOCUMENT_PATH = /^\/__porthole\/d\/([a-z2-7]{16})$/u;
|
|
|
42
49
|
*/
|
|
43
50
|
const FILES_PATH = /^\/__porthole\/f\/([A-Za-z0-9_-]{40,4096})(\/.*)?$/u;
|
|
44
51
|
const FILES_PREFIX = "/__porthole/f/";
|
|
52
|
+
/** An asset's address (P089): `/_blob/` and 32 lowercase hex characters (`ASSET_ID`). */
|
|
53
|
+
const ASSET_ROUTE = /^\/_blob\/([0-9a-f]{32})$/u;
|
|
54
|
+
/** The same, under a frame's sealed files' address (P091): `<base>__porthole/blob/<id>`. */
|
|
55
|
+
const SEALED_ASSET = /^\/__porthole\/blob\/([0-9a-f]{32})$/u;
|
|
45
56
|
/** How long a sealed files' address opens a page's files on its own (P083); the shell renews it. */
|
|
46
57
|
const FILES_ADDRESS_MS = 3_600_000;
|
|
47
58
|
/** Below a sealed address: where the page's shell renews it (no file has a `__porthole/` path). */
|
|
@@ -238,6 +249,26 @@ function cookiesOf(request: Request): [string, string][] {
|
|
|
238
249
|
* fetch-style handler a host's backend mounts in front of its own routes, plus `issue`, which the
|
|
239
250
|
* host's authenticated API calls once it has decided a viewer may see an instance.
|
|
240
251
|
*/
|
|
252
|
+
/** A grant's viewer for the display's bridge (P087), checked; null without a level. */
|
|
253
|
+
function viewerOfGrant(grant: Grant): ViewerInfo | null {
|
|
254
|
+
if (grant.level === undefined) {
|
|
255
|
+
if (grant.user !== undefined) {
|
|
256
|
+
throw new TypeError("A grant's user comes with its level");
|
|
257
|
+
}
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
const viewer = parseViewer({
|
|
261
|
+
level: grant.level,
|
|
262
|
+
...(grant.user === undefined ? {} : { id: grant.user.id, name: grant.user.name }),
|
|
263
|
+
});
|
|
264
|
+
if (viewer === null) {
|
|
265
|
+
throw new TypeError(
|
|
266
|
+
"A grant's level is view, interact, admin or owner; its user's id and name 1 to 256 characters",
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
return viewer;
|
|
270
|
+
}
|
|
271
|
+
|
|
241
272
|
export function createServer(options: ServerOptions): PortholeServer {
|
|
242
273
|
const { content } = options;
|
|
243
274
|
const domain = content.domain;
|
|
@@ -476,7 +507,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
476
507
|
async function documentOf(
|
|
477
508
|
instance: InstanceRef,
|
|
478
509
|
): Promise<
|
|
479
|
-
| { readonly html: string; readonly csp: string }
|
|
510
|
+
| { readonly html: string; readonly csp: string; readonly compiled?: CompiledConfig }
|
|
480
511
|
| { readonly files: StoredFiles; readonly compiled: CompiledConfig }
|
|
481
512
|
| null
|
|
482
513
|
> {
|
|
@@ -504,6 +535,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
504
535
|
return {
|
|
505
536
|
html: config.shellDocument({ origins, document: stored.html, cspMeta: false }),
|
|
506
537
|
csp: config.csp,
|
|
538
|
+
compiled: config,
|
|
507
539
|
};
|
|
508
540
|
}
|
|
509
541
|
case "mcp-proxy":
|
|
@@ -723,6 +755,46 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
723
755
|
});
|
|
724
756
|
}
|
|
725
757
|
|
|
758
|
+
/**
|
|
759
|
+
* The access a sealed files' address carries (P083), if it holds for this origin now: on its own
|
|
760
|
+
* for its hour (`x`), then only for the running page's own loads until the session ends (`e`).
|
|
761
|
+
*/
|
|
762
|
+
async function sealedAccess(
|
|
763
|
+
request: Request,
|
|
764
|
+
label: string,
|
|
765
|
+
token: string,
|
|
766
|
+
): Promise<Access | null> {
|
|
767
|
+
const session = await keys.unseal(token);
|
|
768
|
+
const now = Date.now();
|
|
769
|
+
if (
|
|
770
|
+
!isRecord(session) ||
|
|
771
|
+
typeof session.w !== "string" ||
|
|
772
|
+
typeof session.x !== "number" ||
|
|
773
|
+
typeof session.e !== "number" ||
|
|
774
|
+
(session.s !== undefined && typeof session.s !== "string")
|
|
775
|
+
) {
|
|
776
|
+
return null;
|
|
777
|
+
}
|
|
778
|
+
const open = session.x > now || (session.e > now && ownLoad(request));
|
|
779
|
+
let instance: InstanceRef;
|
|
780
|
+
try {
|
|
781
|
+
instance = cleanInstance(session.i);
|
|
782
|
+
} catch {
|
|
783
|
+
return null;
|
|
784
|
+
}
|
|
785
|
+
if (!open || instance.document.kind !== "artifact" || nameOf(instance.scope) !== label) {
|
|
786
|
+
return null;
|
|
787
|
+
}
|
|
788
|
+
return {
|
|
789
|
+
instance,
|
|
790
|
+
viewer: session.w,
|
|
791
|
+
origin: originOfLabel(label),
|
|
792
|
+
expiresAt: session.e,
|
|
793
|
+
...(session.a === 1 ? { anonymous: true } : {}),
|
|
794
|
+
...(typeof session.s === "string" ? { subject: session.s } : {}),
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
|
|
726
798
|
/**
|
|
727
799
|
* A file set's file in a frame (P083): the sealed access in its path opens it, once it holds
|
|
728
800
|
* for this origin and the host's check lets it in; every path under it is the same version's.
|
|
@@ -766,30 +838,8 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
766
838
|
headers: { location: `${origin}${base}${url.search}`, "cache-control": "no-store" },
|
|
767
839
|
});
|
|
768
840
|
}
|
|
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)) {
|
|
841
|
+
const access = await sealedAccess(request, label, token);
|
|
842
|
+
if (access === null) {
|
|
793
843
|
return page(
|
|
794
844
|
401,
|
|
795
845
|
"This has ended",
|
|
@@ -798,14 +848,18 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
798
848
|
"session",
|
|
799
849
|
);
|
|
800
850
|
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
851
|
+
// An asset of this artifact's library, at the frame's own sealed address (P091): every engine
|
|
852
|
+
// sends a frame's path, where some send no Referer that names it.
|
|
853
|
+
const blob = SEALED_ASSET.exec(rest)?.[1];
|
|
854
|
+
if (blob !== undefined) {
|
|
855
|
+
if (options.assets === undefined) {
|
|
856
|
+
return page(404, "Not found", "This address names no content.", method);
|
|
857
|
+
}
|
|
858
|
+
return (
|
|
859
|
+
(await assetFor(request, label, access, blob)) ??
|
|
860
|
+
page(404, "Not found", "This address names no content.", method)
|
|
861
|
+
);
|
|
862
|
+
}
|
|
809
863
|
if (rest === RENEW_PATH) {
|
|
810
864
|
// Only the page itself renews its address, while its session lasts and the host lets it.
|
|
811
865
|
if (method !== "GET" || !ownLoad(request)) {
|
|
@@ -941,13 +995,161 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
941
995
|
if (at !== undefined && at.rest !== "") {
|
|
942
996
|
return page(404, "Not found", "This page, or this version of it, doesn't exist.", method);
|
|
943
997
|
}
|
|
944
|
-
|
|
998
|
+
// A page that may use its artifact's assets loads them from its own origin (P089).
|
|
999
|
+
const headers = documentHeaders(
|
|
1000
|
+
served.compiled === undefined
|
|
1001
|
+
? served.csp
|
|
1002
|
+
: serializeCsp(
|
|
1003
|
+
assetCspDirectives(
|
|
1004
|
+
served.compiled.config,
|
|
1005
|
+
served.compiled.cspDirectives,
|
|
1006
|
+
access.origin,
|
|
1007
|
+
),
|
|
1008
|
+
),
|
|
1009
|
+
);
|
|
945
1010
|
if (cookie !== null) {
|
|
946
1011
|
headers.set("set-cookie", cookie);
|
|
947
1012
|
}
|
|
948
1013
|
return new Response(method === "HEAD" ? null : served.html, { status: 200, headers });
|
|
949
1014
|
}
|
|
950
1015
|
|
|
1016
|
+
/**
|
|
1017
|
+
* An asset at `/_blob/<id>` (P089, after the review): a request carrying an access to an
|
|
1018
|
+
* artifact of this origin (its page's sealed files' address, as its `Referer` says, P091; or a
|
|
1019
|
+
* session cookie), then the host's check for that access, then the host's store asked with it;
|
|
1020
|
+
* served only when the asset is that artifact's, on its scope's origin. Never an entry. By id
|
|
1021
|
+
* alone only where the host opted into it (`assets.bearer`).
|
|
1022
|
+
*/
|
|
1023
|
+
async function serveAssetAt(request: Request, label: string, id: string): Promise<Response> {
|
|
1024
|
+
const method = request.method.toUpperCase();
|
|
1025
|
+
const store = options.assets;
|
|
1026
|
+
const none = () => page(404, "Not found", "This address names no content.", method);
|
|
1027
|
+
if (store === undefined) {
|
|
1028
|
+
return none();
|
|
1029
|
+
}
|
|
1030
|
+
const scoped = (asset: StoredAsset | null): asset is StoredAsset => {
|
|
1031
|
+
try {
|
|
1032
|
+
return asset !== null && nameOf(asset.scope) === label;
|
|
1033
|
+
} catch {
|
|
1034
|
+
return false;
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
if (store.bearer === true) {
|
|
1038
|
+
const asset = await store.get(id, null);
|
|
1039
|
+
return scoped(asset)
|
|
1040
|
+
? serveAsset({ request, asset, frameAncestors, code: asset.trusted === true })
|
|
1041
|
+
: none();
|
|
1042
|
+
}
|
|
1043
|
+
const accesses = await assetAccessesOf(request, label);
|
|
1044
|
+
if (accesses.length === 0) {
|
|
1045
|
+
return new Response(null, { status: 401, headers: { "cache-control": "no-store" } });
|
|
1046
|
+
}
|
|
1047
|
+
for (const access of accesses) {
|
|
1048
|
+
const answer = await assetFor(request, label, access, id);
|
|
1049
|
+
if (answer !== null) {
|
|
1050
|
+
return answer;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
return none();
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* An asset for one access (P089, P091): the store asked with it, served only when the asset is
|
|
1058
|
+
* its artifact's on its scope's origin, after the host's check; its code only where it was
|
|
1059
|
+
* trusted and the artifact's config still lets uploaded code run. Null: not this access's.
|
|
1060
|
+
*/
|
|
1061
|
+
async function assetFor(
|
|
1062
|
+
request: Request,
|
|
1063
|
+
label: string,
|
|
1064
|
+
access: Access,
|
|
1065
|
+
id: string,
|
|
1066
|
+
): Promise<Response | null> {
|
|
1067
|
+
const store = options.assets;
|
|
1068
|
+
if (store === undefined || access.instance.document.kind !== "artifact") {
|
|
1069
|
+
return null;
|
|
1070
|
+
}
|
|
1071
|
+
const asset = await store.get(id, access);
|
|
1072
|
+
let named: string | null = null;
|
|
1073
|
+
try {
|
|
1074
|
+
named = asset === null ? null : nameOf(asset.scope);
|
|
1075
|
+
} catch {
|
|
1076
|
+
named = null;
|
|
1077
|
+
}
|
|
1078
|
+
if (asset === null || named !== label || asset.artifact !== access.instance.document.id) {
|
|
1079
|
+
return null;
|
|
1080
|
+
}
|
|
1081
|
+
const refusal = await denied(request, access);
|
|
1082
|
+
if (refusal !== null) {
|
|
1083
|
+
return refusal;
|
|
1084
|
+
}
|
|
1085
|
+
const config = compiledFor(access.instance)?.config;
|
|
1086
|
+
const code = asset.trusted === true && config !== undefined && assetCodeRuns(config);
|
|
1087
|
+
return serveAsset({ request, asset, frameAncestors, code });
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
/**
|
|
1091
|
+
* The accesses a request carries to artifacts of `label`'s origin, most specific first: the
|
|
1092
|
+
* sealed files' address its page is at (a frame's, which keeps no cookie, P083), as its
|
|
1093
|
+
* same-origin `Referer` names it; then its session cookies.
|
|
1094
|
+
*/
|
|
1095
|
+
async function assetAccessesOf(request: Request, label: string): Promise<Access[]> {
|
|
1096
|
+
const found: Access[] = [];
|
|
1097
|
+
const referer = request.headers.get("referer");
|
|
1098
|
+
if (referer !== null) {
|
|
1099
|
+
try {
|
|
1100
|
+
const from = new URL(referer);
|
|
1101
|
+
const token = FILES_PATH.exec(from.pathname)?.[1];
|
|
1102
|
+
if (from.origin === originOfLabel(label) && token !== undefined) {
|
|
1103
|
+
const sealed = await sealedAccess(request, label, token);
|
|
1104
|
+
if (sealed !== null) {
|
|
1105
|
+
found.push(sealed);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
} catch {
|
|
1109
|
+
// Not an address: no access from it.
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
return [...found, ...artifactAccessesOf(request, label)];
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
/** The accesses a request's cookies give to artifacts of `label`'s origin, latest first. */
|
|
1116
|
+
function artifactAccessesOf(request: Request, label: string): Access[] {
|
|
1117
|
+
const found: Access[] = [];
|
|
1118
|
+
for (const [name, value] of cookiesOf(request)) {
|
|
1119
|
+
if (!ourCookie(name)) {
|
|
1120
|
+
continue;
|
|
1121
|
+
}
|
|
1122
|
+
const session = keys.verify(value);
|
|
1123
|
+
if (
|
|
1124
|
+
!isRecord(session) ||
|
|
1125
|
+
typeof session.w !== "string" ||
|
|
1126
|
+
typeof session.x !== "number" ||
|
|
1127
|
+
session.x <= Date.now() ||
|
|
1128
|
+
(session.s !== undefined && typeof session.s !== "string")
|
|
1129
|
+
) {
|
|
1130
|
+
continue;
|
|
1131
|
+
}
|
|
1132
|
+
let instance: InstanceRef;
|
|
1133
|
+
try {
|
|
1134
|
+
instance = cleanInstance(session.i);
|
|
1135
|
+
if (instance.document.kind !== "artifact" || nameOf(instance.scope) !== label) {
|
|
1136
|
+
continue;
|
|
1137
|
+
}
|
|
1138
|
+
} catch {
|
|
1139
|
+
continue;
|
|
1140
|
+
}
|
|
1141
|
+
found.push({
|
|
1142
|
+
instance,
|
|
1143
|
+
viewer: session.w,
|
|
1144
|
+
origin: originOfLabel(label),
|
|
1145
|
+
expiresAt: session.x,
|
|
1146
|
+
...(session.a === 1 ? { anonymous: true } : {}),
|
|
1147
|
+
...(typeof session.s === "string" ? { subject: session.s } : {}),
|
|
1148
|
+
});
|
|
1149
|
+
}
|
|
1150
|
+
return found.sort((a, b) => b.expiresAt - a.expiresAt);
|
|
1151
|
+
}
|
|
1152
|
+
|
|
951
1153
|
/**
|
|
952
1154
|
* A clean address without a cookie for it (P068): off to the host's authorize route with a
|
|
953
1155
|
* signed state, the browser holding the state's nonce in a cookie of this origin. A path just
|
|
@@ -1344,7 +1546,9 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1344
1546
|
clean.document.kind === "external"
|
|
1345
1547
|
? `${ENTER_PATH}?ticket=${ticket}`
|
|
1346
1548
|
: `${PREFIX}d/${key}?ticket=${ticket}`;
|
|
1347
|
-
|
|
1549
|
+
// The viewer's level, as the host's access decided it now (P087): for the display's bridge.
|
|
1550
|
+
const viewer = viewerOfGrant(grant);
|
|
1551
|
+
return { url: `${origin}${path}`, origin, reports: true, ...(viewer ? { viewer } : {}) };
|
|
1348
1552
|
},
|
|
1349
1553
|
|
|
1350
1554
|
forget(filter = {}) {
|
|
@@ -1487,6 +1691,12 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1487
1691
|
access === null ? 401 : (await denied(request, access)) === null ? 204 : 403;
|
|
1488
1692
|
return new Response(null, { status, headers: { "cache-control": "no-store" } });
|
|
1489
1693
|
}
|
|
1694
|
+
// An artifact's asset library (P089), outside every version: `/_blob/<id>` only; any
|
|
1695
|
+
// other path there is a clean address as before (after the review).
|
|
1696
|
+
const asset = options.assets === undefined ? null : ASSET_ROUTE.exec(url.pathname);
|
|
1697
|
+
if (asset?.[1] !== undefined) {
|
|
1698
|
+
return await serveAssetAt(request, label, asset[1]);
|
|
1699
|
+
}
|
|
1490
1700
|
const document = DOCUMENT_PATH.exec(url.pathname);
|
|
1491
1701
|
if (document?.[1] !== undefined) {
|
|
1492
1702
|
return await serveDocument(request, url, label, document[1]);
|
package/src/server/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { PortholeConfig } from "../config/schema.ts";
|
|
2
2
|
import type { FrameLocation, InstanceRef } from "../isolation/types.ts";
|
|
3
|
+
import type { ViewerLevel } from "../runtime/levels.ts";
|
|
3
4
|
import type { Naming } from "./naming.ts";
|
|
4
5
|
|
|
5
6
|
export interface ContentDomain {
|
|
@@ -62,6 +63,20 @@ export type Grant = (
|
|
|
62
63
|
* url-mode app's every request, rides on.
|
|
63
64
|
*/
|
|
64
65
|
readonly sessionSeconds?: number;
|
|
66
|
+
/**
|
|
67
|
+
* The viewer's level on this instance (P087), as the host decided it now: carried in the
|
|
68
|
+
* issue's answer (`FrameLocation.viewer`) to the display's bridge, which checks each runtime
|
|
69
|
+
* method's level against it. Never in the ticket or the cookie, never shown to content but by
|
|
70
|
+
* the `user` capability.
|
|
71
|
+
*/
|
|
72
|
+
readonly level?: ViewerLevel;
|
|
73
|
+
/**
|
|
74
|
+
* Who the viewer is to pages that declare `user` (P087): the host's public id for them (whose
|
|
75
|
+
* per-user data, `data/users/<id>`, the page reaches: one path segment, never `me`) and a name.
|
|
76
|
+
* Never `viewer`, unless the host means it to be seen; `pseudonymFor(secret, artifact, person)`
|
|
77
|
+
* gives an id per artifact, so pages can't link a person across artifacts. Only with `level`.
|
|
78
|
+
*/
|
|
79
|
+
readonly user?: { readonly id: string; readonly name?: string };
|
|
65
80
|
};
|
|
66
81
|
|
|
67
82
|
export interface TicketRecord {
|
|
@@ -132,6 +147,52 @@ export interface StoredFiles {
|
|
|
132
147
|
/** A stored version: one page (`{ html }`, as in 0.5), or a set of files (P081). */
|
|
133
148
|
export type StoredVersion = { readonly html: string } | StoredFiles;
|
|
134
149
|
|
|
150
|
+
/**
|
|
151
|
+
* One asset of an artifact's library (P089), as the host stored it: served at `/_blob/<id>` on
|
|
152
|
+
* the origin of `scope`, and nowhere else. Its type is Porthole's for `name`'s extension, never
|
|
153
|
+
* the uploader's; it is never served as a page.
|
|
154
|
+
*/
|
|
155
|
+
export interface StoredAsset {
|
|
156
|
+
/** The storage scope whose origin serves it (the artifact's). */
|
|
157
|
+
readonly scope: string;
|
|
158
|
+
/**
|
|
159
|
+
* The artifact whose library holds it (`ServedDocument`'s `id`, after the review): served only
|
|
160
|
+
* to an access for that artifact, never another one sharing the scope.
|
|
161
|
+
*/
|
|
162
|
+
readonly artifact: string;
|
|
163
|
+
/**
|
|
164
|
+
* Whether it may run as code (P089, after the review): a script, stylesheet or WebAssembly an
|
|
165
|
+
* admin uploaded where uploaded code may run (the upload's `trusted`). Any other code-typed
|
|
166
|
+
* asset is served as a download, whatever the config says now.
|
|
167
|
+
*/
|
|
168
|
+
readonly trusted?: boolean;
|
|
169
|
+
/** Its file name (its extension types it). */
|
|
170
|
+
readonly name: string;
|
|
171
|
+
readonly size: number;
|
|
172
|
+
/** The SHA-256 of its bytes, lowercase hex: its strong ETag. */
|
|
173
|
+
readonly sha256: string;
|
|
174
|
+
read(): Promise<FileBody | null>;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Where an artifact's assets are kept (P089): the host's library, by id. Ids are the host's, 32
|
|
179
|
+
* lowercase hex characters made at random (`ASSET_ID`): an asset's address is known only to those
|
|
180
|
+
* shown it; Porthole serves one only on its own scope's origin.
|
|
181
|
+
*/
|
|
182
|
+
export interface AssetStore {
|
|
183
|
+
/**
|
|
184
|
+
* The asset, for `access` (who asks, for which artifact: the host decides, and its check runs
|
|
185
|
+
* after); null if it isn't there for them. `access` is null only in bearer mode.
|
|
186
|
+
*/
|
|
187
|
+
get(id: string, access: Access | null): Promise<StoredAsset | null>;
|
|
188
|
+
/**
|
|
189
|
+
* Bearer mode, an explicit opt-in (after the review): serve an asset to anyone holding its id,
|
|
190
|
+
* with no access and no host check (a public artifact's library). Its id is then a capability:
|
|
191
|
+
* whoever saw it keeps it until the asset is removed.
|
|
192
|
+
*/
|
|
193
|
+
readonly bearer?: boolean;
|
|
194
|
+
}
|
|
195
|
+
|
|
135
196
|
/** Stored pages (artifact mode). Versions are immutable: a new page is a new version. */
|
|
136
197
|
export interface ArtifactStore {
|
|
137
198
|
get(id: string, version: string): Promise<StoredVersion | null>;
|
|
@@ -204,6 +265,13 @@ export interface ServerOptions {
|
|
|
204
265
|
*/
|
|
205
266
|
readonly configs: Readonly<Record<string, PortholeConfig>>;
|
|
206
267
|
readonly artifacts?: ArtifactStore;
|
|
268
|
+
/**
|
|
269
|
+
* Artifacts' asset libraries (P089): `/_blob/<id>` on each content origin, for a request that
|
|
270
|
+
* carries an access to the asset's own artifact (a frame's sealed files' address as its
|
|
271
|
+
* `Referer`, or a session cookie), after the host's check. Absent: `/_blob/` is a clean address
|
|
272
|
+
* like any other.
|
|
273
|
+
*/
|
|
274
|
+
readonly assets?: AssetStore;
|
|
207
275
|
readonly tickets?: TicketStore;
|
|
208
276
|
/** Url mode: where an external instance's requests go once let in. Absent: nothing serves them. */
|
|
209
277
|
readonly upstream?: Upstream;
|
package/src/shell/document.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { hostOriginProblem } from "../isolation/served.ts";
|
|
2
2
|
import { ANNOTATE_RUNTIME } from "./annotate.ts";
|
|
3
|
+
import { LEGACY_GLOBALS_RUNTIME } from "./legacy.ts";
|
|
3
4
|
import { SHELL_RUNTIME } from "./runtime.ts";
|
|
5
|
+
import { USE_RUNTIME } from "./use.ts";
|
|
4
6
|
import type { ShellPlan } from "./plan.ts";
|
|
5
7
|
|
|
6
8
|
export interface ShellDocumentOptions {
|
|
@@ -36,6 +38,8 @@ export interface ShellDocumentOptions {
|
|
|
36
38
|
* (`https://a-x.example-usercontent.com/v/3/`). The shell lets the frame navigate to the set's
|
|
37
39
|
* other pages there, and to nothing else.
|
|
38
40
|
*/
|
|
41
|
+
/** `same-origin`: the document's loads tell its own origin its address (P091); else none. */
|
|
42
|
+
readonly referrer?: "same-origin";
|
|
39
43
|
readonly files?: {
|
|
40
44
|
readonly base: string;
|
|
41
45
|
/**
|
|
@@ -123,6 +127,19 @@ body {
|
|
|
123
127
|
* The frame's document: its policy first, then the base styles, an empty root, and the runtime.
|
|
124
128
|
* A given `document` travels in the runtime's plan, as data.
|
|
125
129
|
*/
|
|
130
|
+
/**
|
|
131
|
+
* The shell's runtime for a plan, before its plan goes in: annotations where declared, 0.6's
|
|
132
|
+
* globals (the legacy path, P085) for what the config declares, and the capability runtime where
|
|
133
|
+
* the config has one.
|
|
134
|
+
*/
|
|
135
|
+
export function composedRuntime(plan: ShellPlan): string {
|
|
136
|
+
return SHELL_RUNTIME.replace("__ANNOTATE__", () =>
|
|
137
|
+
plan.capabilities.annotate === undefined ? "" : ANNOTATE_RUNTIME,
|
|
138
|
+
)
|
|
139
|
+
.replace("__LEGACY__", () => LEGACY_GLOBALS_RUNTIME)
|
|
140
|
+
.replace("__RUNTIME__", () => (plan.runtime === undefined ? "" : USE_RUNTIME));
|
|
141
|
+
}
|
|
142
|
+
|
|
126
143
|
export function shellDocument(plan: ShellPlan, csp: string, options: ShellDocumentOptions): string {
|
|
127
144
|
for (const origin of options.origins) {
|
|
128
145
|
const problem = hostOriginProblem(origin);
|
|
@@ -145,9 +162,7 @@ export function shellDocument(plan: ShellPlan, csp: string, options: ShellDocume
|
|
|
145
162
|
const nonce = options.nonce === undefined ? "" : ` nonce="${options.nonce}"`;
|
|
146
163
|
// The annotation runtime only where it is declared; spliced in before the plan, which may carry
|
|
147
164
|
// any text (a document) and must not be searched.
|
|
148
|
-
const runtime =
|
|
149
|
-
plan.capabilities.annotate === undefined ? "" : ANNOTATE_RUNTIME,
|
|
150
|
-
).replace("__PLAN__", () =>
|
|
165
|
+
const runtime = composedRuntime(plan).replace("__PLAN__", () =>
|
|
151
166
|
scriptJson({
|
|
152
167
|
...plan,
|
|
153
168
|
origins: options.origins,
|
|
@@ -168,7 +183,9 @@ export function shellDocument(plan: ShellPlan, csp: string, options: ShellDocume
|
|
|
168
183
|
options.cspMeta === false
|
|
169
184
|
? null
|
|
170
185
|
: `<meta http-equiv="Content-Security-Policy" content="${escapeAttribute(csp)}">`,
|
|
171
|
-
|
|
186
|
+
// A framed page of files whose config serves assets says its sealed address to its own
|
|
187
|
+
// origin only, which its asset loads carry (P091); every other document says nothing.
|
|
188
|
+
`<meta name="referrer" content="${options.referrer === "same-origin" ? "same-origin" : "no-referrer"}">`,
|
|
172
189
|
// Off for good: an engine may prefetch names it sees in links, outside the CSP.
|
|
173
190
|
'<meta http-equiv="x-dns-prefetch-control" content="off">',
|
|
174
191
|
].filter((line) => line !== null);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The legacy path (P085): the globals pages written for Porthole 0.5 and 0.6 call
|
|
3
|
+
* (`sendPrompt`, `copyText`, `openLink`, `readData`, `subscribeData`), spliced into the shell's
|
|
4
|
+
* runtime (`__LEGACY__`) for each capability the config declares, as before. A page written for the
|
|
5
|
+
* capability runtime reaches the same host functions through `porthole.use` instead; the bridge
|
|
6
|
+
* gates both paths alike, and holds a page that declared capabilities to its declaration on
|
|
7
|
+
* either (P086).
|
|
8
|
+
*
|
|
9
|
+
* Since 0.7.0. Remove when no host serves a page written before it (hosts migrate their stored
|
|
10
|
+
* pages' calls to `porthole.use`); until then the manual teaches these only where the config has
|
|
11
|
+
* no runtime.
|
|
12
|
+
*/
|
|
13
|
+
export const LEGACY_GLOBALS_RUNTIME = String.raw`
|
|
14
|
+
if (declared("sendPrompt")) {
|
|
15
|
+
lock(window, "sendPrompt", (text) => {
|
|
16
|
+
if (!gestured("sendPrompt")) {
|
|
17
|
+
return refuse("sendPrompt needs a click: call it from a button's handler");
|
|
18
|
+
}
|
|
19
|
+
const words = String(text);
|
|
20
|
+
if (words.trim() === "" || words.length > plan.maxPromptChars) {
|
|
21
|
+
return refuse("sendPrompt takes 1 to " + plan.maxPromptChars + " characters");
|
|
22
|
+
}
|
|
23
|
+
return request("ui/message", { role: "user", content: [{ type: "text", text: words }] }).then(
|
|
24
|
+
(result) => !(result && result.isError),
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (declared("copyText")) {
|
|
29
|
+
lock(window, "copyText", (text) => {
|
|
30
|
+
if (!gestured("copyText")) {
|
|
31
|
+
return refuse("copyText needs a click: call it from a button's handler");
|
|
32
|
+
}
|
|
33
|
+
const words = String(text);
|
|
34
|
+
if (words === "" || words.length > plan.maxCopyChars) {
|
|
35
|
+
return refuse("copyText takes 1 to " + plan.maxCopyChars + " characters");
|
|
36
|
+
}
|
|
37
|
+
return request("porthole/copy-text", { text: words }).then((result) => !(result && result.isError));
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (declared("openLink")) {
|
|
41
|
+
lock(window, "openLink", (url) => {
|
|
42
|
+
if (!gestured("openLink")) {
|
|
43
|
+
return refuse("openLink needs a click: call it from a link's or button's handler");
|
|
44
|
+
}
|
|
45
|
+
return request("ui/open-link", { url: String(url) }).then((result) => !(result && result.isError));
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (declared("readData")) {
|
|
50
|
+
lock(window, "readData", readValue);
|
|
51
|
+
}
|
|
52
|
+
if (follow !== null) {
|
|
53
|
+
lock(window, "subscribeData", follow);
|
|
54
|
+
}
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
/** The globals the legacy path defines, by the capability that defines each. */
|
|
58
|
+
export const LEGACY_GLOBALS = {
|
|
59
|
+
sendPrompt: "sendPrompt",
|
|
60
|
+
copyText: "copyText",
|
|
61
|
+
openLink: "openLink",
|
|
62
|
+
readData: "readData",
|
|
63
|
+
subscribeData: "subscribeData",
|
|
64
|
+
} as const;
|
package/src/shell/plan.ts
CHANGED
|
@@ -2,6 +2,13 @@ import { MAX_COPY_CHARS, capabilitySpec, type CapabilityName } from "../config/c
|
|
|
2
2
|
import type { PortholeConfig } from "../config/schema.ts";
|
|
3
3
|
import { declaredCapabilities, effectiveGates } from "../compile/policy.ts";
|
|
4
4
|
import { MCP_APPS_PROTOCOL_VERSION } from "../protocol/mcp-apps.ts";
|
|
5
|
+
import {
|
|
6
|
+
RUNTIME_GLOBAL,
|
|
7
|
+
methodGates,
|
|
8
|
+
offeredCapabilities,
|
|
9
|
+
runtimeCapability,
|
|
10
|
+
type RuntimeVersion,
|
|
11
|
+
} from "../runtime/contract.ts";
|
|
5
12
|
|
|
6
13
|
/**
|
|
7
14
|
* What the shell wires, derived from the config: the JSON the shell's runtime is given. The
|
|
@@ -19,8 +26,23 @@ export interface ShellPlan {
|
|
|
19
26
|
readonly content: "fragment" | "document";
|
|
20
27
|
/** Each declared capability, and whether the shell refuses a call made without a gesture. */
|
|
21
28
|
readonly capabilities: { readonly [N in CapabilityName]?: { readonly userActivation: boolean } };
|
|
22
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* The globals the shell defines on `window` and the manual teaches: `porthole` where the config
|
|
31
|
+
* has the capability runtime (P085), else 0.6's (sugar over the protocol's requests).
|
|
32
|
+
*/
|
|
23
33
|
readonly globals: readonly string[];
|
|
34
|
+
/**
|
|
35
|
+
* 0.6's globals the shell still defines under the runtime, for pages written before it (the
|
|
36
|
+
* legacy path, `shell/legacy.ts`), which the manual no longer teaches. Empty without a runtime.
|
|
37
|
+
*/
|
|
38
|
+
readonly legacyGlobals: readonly string[];
|
|
39
|
+
/** The capability runtime's contract version, where the config has one (`porthole.use`). */
|
|
40
|
+
readonly runtime?: RuntimeVersion;
|
|
41
|
+
/**
|
|
42
|
+
* The runtime methods whose own gates want a gesture (`capability.method`: `artifact.publish`,
|
|
43
|
+
* `assets.delete`): the shell refuses them without one, as it does a host function's.
|
|
44
|
+
*/
|
|
45
|
+
readonly methodActivation?: readonly string[];
|
|
24
46
|
/** The theme variables the shell applies; it drops any other name the host sends. */
|
|
25
47
|
readonly themeVariables: readonly string[];
|
|
26
48
|
/** The window event fired after a theme change. */
|
|
@@ -48,18 +70,30 @@ export function shellPlan(config: PortholeConfig): ShellPlan | null {
|
|
|
48
70
|
userActivation: effectiveGates(config, name).includes("user-activation"),
|
|
49
71
|
};
|
|
50
72
|
}
|
|
51
|
-
const
|
|
73
|
+
const legacy =
|
|
52
74
|
protocol === "shell"
|
|
53
75
|
? declared.flatMap((name) => {
|
|
54
76
|
const spec = capabilitySpec(name);
|
|
55
77
|
return spec.global === undefined ? [] : [spec.global];
|
|
56
78
|
})
|
|
57
79
|
: [];
|
|
80
|
+
const runtime = protocol === "shell" ? config.runtime : undefined;
|
|
58
81
|
return {
|
|
59
82
|
protocol,
|
|
60
83
|
content: config.mode === "artifact" ? "document" : "fragment",
|
|
61
84
|
capabilities,
|
|
62
|
-
globals,
|
|
85
|
+
globals: runtime === undefined ? legacy : [RUNTIME_GLOBAL],
|
|
86
|
+
legacyGlobals: runtime === undefined ? [] : legacy,
|
|
87
|
+
...(runtime === undefined
|
|
88
|
+
? {}
|
|
89
|
+
: {
|
|
90
|
+
runtime,
|
|
91
|
+
methodActivation: offeredCapabilities(config).flatMap((family) =>
|
|
92
|
+
Object.keys(runtimeCapability(family).methods)
|
|
93
|
+
.filter((method) => methodGates(config, family, method).includes("user-activation"))
|
|
94
|
+
.map((method) => `${family}.${method}`),
|
|
95
|
+
),
|
|
96
|
+
}),
|
|
63
97
|
themeVariables: Object.keys(config.theme.variables),
|
|
64
98
|
themeEvent: config.theme.event,
|
|
65
99
|
themeDocuments: config.theme.documents ?? "declared",
|