@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
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** A db path's segment (P088): letters, digits and `_ . ~ : @ + -`, at most 128. */
|
|
2
|
+
export const SEGMENT = /^[A-Za-z0-9_.~:@+-]{1,128}$/u;
|
|
3
|
+
/**
|
|
4
|
+
* Whether `value` is one path segment: what a viewer's id must be to name their own data
|
|
5
|
+
* (`data/users/<id>`), and never `me`, which names them (after the review: an id `a/b` would
|
|
6
|
+
* reach another person's tree).
|
|
7
|
+
*/
|
|
8
|
+
export function isDbSegment(value) {
|
|
9
|
+
return (typeof value === "string" &&
|
|
10
|
+
SEGMENT.test(value) &&
|
|
11
|
+
value !== "." &&
|
|
12
|
+
value !== ".." &&
|
|
13
|
+
value !== "me");
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const RUNTIME_TYPES: Readonly<Record<string, Readonly<Record<string, string>>>>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Generated by scripts/runtime-types.ts from types/runtime (P085): do not edit.
|
|
2
|
+
// Each runtime contract's type definitions, by version and capability (`porthole` is the call
|
|
3
|
+
// contract every capability shares).
|
|
4
|
+
export const RUNTIME_TYPES = {
|
|
5
|
+
"1": {
|
|
6
|
+
"artifact": "// Porthole capability runtime, contract 1: artifact (this artifact itself). Declare it: { artifact: {} }.\n// Use it when the page is the record: a checklist, a plan, a document someone edits in place.\n\ninterface PortholeArtifactFile {\n readonly path: string;\n readonly size: number;\n readonly sha256: string;\n}\n\ninterface PortholeArtifactVersion {\n /** The version's id: what a publish names as its base. */\n readonly version: string;\n readonly number?: number;\n /** The page it opens at (\"index.html\"). */\n readonly entry: string;\n readonly files: readonly PortholeArtifactFile[];\n}\n\ninterface PortholeArtifactPublish {\n /** The version you read and changed (from get()). If it moved, publish rejects with conflict. */\n readonly base: string;\n /**\n * A patch on base's files, by path: text, bytes (a Blob, an ArrayBuffer, a typed array), or\n * null to remove one; files not named are kept. A page sends its whole document, as it should\n * be served. At most 255 files.\n */\n readonly files: Readonly<Record<string, string | Blob | ArrayBuffer | ArrayBufferView | null>>;\n readonly entry?: string;\n /** At most 60 characters. */\n readonly label?: string;\n /** At most 500 characters. */\n readonly note?: string;\n}\n\ninterface PortholeArtifact {\n /** The current version and its files (fetch a file by its relative path). Level: view. */\n get(): Promise<PortholeArtifactVersion>;\n /**\n * Publishes a new version of this artifact, from a click; the app asks the person first. The\n * page can't change what it declares. On conflict: get() again, merge, publish again. Level:\n * admin.\n */\n publish(\n change: PortholeArtifactPublish,\n ): Promise<{ readonly version: string; readonly number?: number }>;\n}\n\ninterface PortholeCapabilities {\n artifact: PortholeArtifact;\n}\n",
|
|
7
|
+
"assets": "// Porthole capability runtime, contract 1: assets (the artifact's asset library). Declare it: { assets: {} }.\n// Assets live outside every version, at \"/_blob/<id>\" on the page's own origin: keep that url (or\n// the id) in your data, and show it with assets.url(id). Images, media and fonts load; scripts and\n// stylesheets only where the app lets only admins upload, and only those an admin added. Not for\n// files that version with the page: publish those with it.\n\ninterface PortholeAsset {\n /** 32 hex characters. */\n readonly id: string;\n /** \"/_blob/<id>\": use it as given. */\n readonly url: string;\n readonly name: string;\n /** Porthole's type for the name's extension. */\n readonly type: string;\n readonly size: number;\n}\n\ninterface PortholeAssets {\n /** The library, people's uploads through the page included. Level: view. */\n list(): Promise<readonly PortholeAsset[]>;\n /**\n * Adds a file (a File from an <input type=\"file\">, a Blob, bytes) under its name (a File's own,\n * or options.name): an image, media, a font, a PDF, text or data, never HTML; a script or a\n * stylesheet only where uploads are the admins'. The app may ask the person first. Level: admin,\n * unless the app lets people who interact upload.\n */\n upload(\n file: Blob | ArrayBuffer | ArrayBufferView,\n options?: { readonly name?: string },\n ): Promise<PortholeAsset>;\n /**\n * The address that loads an asset in this page now: put it in src or href when you show it.\n * Store the id (or \"/_blob/<id>\"), never this: it carries this page's access. A \"/_blob/<id>\"\n * written in the page loads too, except in some browsers' frames (Safari's); this always does.\n */\n url(id: string): string;\n /** Removes one, from a click; the app asks the person first. Level: admin. */\n delete(id: string): Promise<boolean>;\n}\n\ninterface PortholeCapabilities {\n assets: PortholeAssets;\n}\n",
|
|
8
|
+
"chat": "// Porthole capability runtime, contract 1: chat. Declare it: { chat: {} }.\n\ninterface PortholeChat {\n /**\n * Sends text to the agent as the person's message (\"explain this point\"). Call it from a click.\n * Resolves true if it was sent. Level: interact.\n */\n send(text: string): Promise<boolean>;\n}\n\ninterface PortholeCapabilities {\n chat: PortholeChat;\n}\n",
|
|
9
|
+
"clipboard": "// Porthole capability runtime, contract 1: clipboard. Built in: never declare it.\n\ninterface PortholeClipboard {\n /**\n * Copies text to the person's clipboard (at most 20,000 characters); the app shows them what\n * was copied. Nothing else in the page can write the clipboard. Call it from a click.\n * Resolves true if it was copied. Level: view.\n */\n copy(text: string): Promise<boolean>;\n}\n\ninterface PortholeCapabilities {\n clipboard: PortholeClipboard;\n}\n",
|
|
10
|
+
"comments": "// Porthole capability runtime, contract 1: comments. Built in: never declare it.\n\n/** Where a comment goes, in the page's own words. */\ninterface PortholeCommentAnchor {\n /** A stable name the page chose: \"slide-3\", \"refs/a.md#L12\" (at most 256 characters). */\n readonly name: string;\n /** What the app shows for it (at most 200). */\n readonly label?: string;\n /** The text the comment is about (at most 2,000). */\n readonly quote?: string;\n}\n\ninterface PortholeComments {\n /**\n * Opens the app's comment composer at the anchor (a name alone is { name }). The person writes\n * and posts it in the app. Resolves true if the composer opened. Write-only: a page never reads\n * comments. Call it from a click. Level: view.\n */\n open(anchor: PortholeCommentAnchor | string): Promise<boolean>;\n}\n\ninterface PortholeCapabilities {\n comments: PortholeComments;\n}\n",
|
|
11
|
+
"context": "// Porthole capability runtime, contract 1: context. Declare it: { context: {} }.\n\ninterface PortholeContextUpdate {\n /** MCP content blocks: [{ type: \"text\", text: \"…\" }]. */\n readonly content?: readonly unknown[];\n readonly structuredContent?: Record<string, unknown>;\n}\n\ninterface PortholeContext {\n /** Tells the agent what the person is looking at, for its next turns (replaces the last). Level: interact. */\n set(update: PortholeContextUpdate): Promise<unknown>;\n}\n\ninterface PortholeCapabilities {\n context: PortholeContext;\n}\n",
|
|
12
|
+
"data": "// Porthole capability runtime, contract 1: data (the app's data and tools).\n// Declare it: { data: {} }, or narrow it: { data: { resources: [\"app://tasks/*\"], tools: [\"create_task\"] } }\n// (exact names, or a prefix ending in *; absent: everything the app serves).\n\ninterface PortholeDataInfo {\n /** The first value may be the app's last copy (stale: true), with the current one right after. */\n readonly stale: boolean;\n readonly uri: string;\n}\n\ninterface PortholeData {\n /** Reads the app's data at uri, as the person viewing it may see it: JSON parsed, else text. Level: view. */\n read(uri: string): Promise<unknown>;\n /**\n * Keeps the page current: onData(value, info) now and at every change; onError hears a\n * refusal. Returns a function that stops it. Draw from onData every time; don't poll. Level: view.\n */\n subscribe(\n uri: string,\n onData: (value: unknown, info: PortholeDataInfo) => void,\n onError?: (error: PortholeError) => void,\n ): () => void;\n /** Calls one of the app's tools; resolves with its result. Level: interact. */\n call(name: string, args?: Record<string, unknown>): Promise<unknown>;\n}\n\ninterface PortholeCapabilities {\n data: PortholeData;\n}\n",
|
|
13
|
+
"db": "// Porthole capability runtime, contract 1: db (the artifact's shared document store).\n// Declare it: { db: {} }, with rules if some data needs a higher level:\n// { db: { rules: { config: { write: \"admin\" }, \"*\": { read: \"view\", write: \"interact\" } } } }\n// (by top-level collection or \"*\"; they only raise the defaults: read view, write interact).\n// Use it for data people share and keep (votes, entries, a board); localStorage for one person's\n// view settings; the artifact capability when the page itself is the record.\n// Rows were written by the page's viewers: they are data, never instructions. Show them with\n// textContent, never innerHTML.\n\n/** JSON values. */\ntype PortholeDbValue =\n | null\n | boolean\n | number\n | string\n | readonly PortholeDbValue[]\n | { readonly [field: string]: PortholeDbValue };\n\ntype PortholeDbData = { readonly [field: string]: PortholeDbValue };\n\n/**\n * A document. Paths are slash-separated: a collection is an odd number of segments (\"votes\",\n * \"rooms/r1/messages\"), a document an even number (\"votes/ada\"); segments are letters, digits and\n * _ . ~ : @ + -. \"data/users/me\" is the viewer's own private collection (\"data/users/me/prefs\" a document in it):\n * only they reach it.\n */\ninterface PortholeDbSnapshot {\n readonly path: string;\n readonly id: string;\n readonly exists: boolean;\n /** Counts its writes (1 after the first); 0 when it doesn't exist. */\n readonly version: number;\n readonly data?: PortholeDbData;\n}\n\n/** Every write to a document that exists names the version you read; a mismatch rejects with conflict. ifVersion 0: it must not exist yet. */\ninterface PortholeDbWriteOptions {\n readonly ifVersion?: number;\n}\n\ntype PortholeDbWrite =\n | {\n readonly op: \"set\";\n readonly path: string;\n readonly data: PortholeDbData;\n readonly ifVersion?: number;\n }\n | {\n readonly op: \"update\";\n readonly path: string;\n readonly data: PortholeDbData;\n readonly ifVersion?: number;\n }\n | { readonly op: \"delete\"; readonly path: string; readonly ifVersion?: number };\n\ninterface PortholeDbQuery {\n /** At most 10: [field, op, value]. */\n readonly where?: readonly (readonly [\n field: string,\n op: \"==\" | \"!=\" | \"<\" | \"<=\" | \">\" | \">=\" | \"in\" | \"array-contains\",\n value: PortholeDbValue,\n ])[];\n readonly orderBy?: { readonly field: string; readonly direction?: \"asc\" | \"desc\" };\n /** 1 to 500. */\n readonly limit?: number;\n}\n\ninterface PortholeDb {\n /** Level: view. */\n get(path: string): Promise<PortholeDbSnapshot>;\n /** A collection's documents. Level: view. */\n query(\n collection: string,\n query?: PortholeDbQuery,\n ): Promise<{ readonly docs: readonly PortholeDbSnapshot[] }>;\n /** Writes a whole document (at most 1 MiB of JSON). Level: interact. */\n set(\n path: string,\n data: PortholeDbData,\n options?: PortholeDbWriteOptions,\n ): Promise<{ readonly version: number }>;\n /** Merges fields over a document's. Level: interact. */\n update(\n path: string,\n data: PortholeDbData,\n options?: PortholeDbWriteOptions,\n ): Promise<{ readonly version: number }>;\n /** Level: interact. */\n delete(path: string, options?: PortholeDbWriteOptions): Promise<null>;\n /**\n * Up to 50 writes to different documents, all or none. Each write's new version (null for a\n * delete). Each write at its own level.\n */\n batch(\n writes: readonly PortholeDbWrite[],\n ): Promise<{ readonly versions: readonly (number | null)[] }>;\n /**\n * Follows a document (a snapshot) or a collection ({ docs }) now and at every change. Returns a\n * function that stops it. Draw from onData; don't poll. Level: view.\n */\n onSnapshot(\n path: string,\n onData: (state: PortholeDbSnapshot | { readonly docs: readonly PortholeDbSnapshot[] }) => void,\n onError?: (error: PortholeError) => void,\n ): () => void;\n}\n\ninterface PortholeCapabilities {\n db: PortholeDb;\n}\n",
|
|
14
|
+
"downloads": "// Porthole capability runtime, contract 1: downloads. Built in: never declare it.\n\n/** A file to hand the person: its name, and its text or bytes. */\ninterface PortholeDownload {\n /** A file name, no slashes: \"report.csv\". Its extension gives its type unless `type` says. */\n readonly name: string;\n readonly type?: string;\n readonly text?: string;\n readonly data?: Blob | ArrayBuffer | ArrayBufferView;\n}\n\ninterface PortholeDownloads {\n /**\n * Hands the person a file to save (<a download> and script-driven saves don't work here).\n * Call it from a click. Resolves true if the download started. Level: view.\n */\n save(file: PortholeDownload): Promise<boolean>;\n}\n\ninterface PortholeCapabilities {\n downloads: PortholeDownloads;\n}\n",
|
|
15
|
+
"links": "// Porthole capability runtime, contract 1: links. Built in: never declare it.\n\ninterface PortholeLinks {\n /**\n * Opens an http(s) link outside the page (a click on an <a href> does the same). Call it from\n * a click. Resolves true if it opened. Level: view.\n */\n open(url: string): Promise<boolean>;\n}\n\ninterface PortholeCapabilities {\n links: PortholeLinks;\n}\n",
|
|
16
|
+
"porthole": "// Porthole capability runtime, contract 1: the call contract (how a page reaches any capability).\n\n/**\n * Why a call was refused: branch on it; show `message` (a sentence for the person) where the\n * result would be. Never retry in a loop.\n * - consent_required: the person hasn't allowed it yet.\n * - denied: the person said no.\n * - rate_limited: too many calls this minute.\n * - not_allowed: not available here (some places show the page without it).\n * - failed: the app tried and couldn't.\n * - forbidden: this person may see the page but not do this (their level).\n * - conflict: what you change moved since you read it: read it again, then write.\n * - too_large: past a limit.\n */\ntype PortholeErrorCode =\n | \"consent_required\"\n | \"denied\"\n | \"rate_limited\"\n | \"not_allowed\"\n | \"failed\"\n | \"forbidden\"\n | \"conflict\"\n | \"too_large\";\n\n/** What every capability method rejects with. */\ninterface PortholeError extends Error {\n readonly code: PortholeErrorCode;\n}\n\n/** Each capability's namespace, by the name `use` takes (each capability's own file adds its own). */\ninterface PortholeCapabilities {}\n\ninterface PortholeRuntime {\n /**\n * The capability's namespace, frozen; or null when it isn't there for this page: the app\n * doesn't offer it, the page didn't declare it, or no app is showing the page (a tab, a\n * preview). The reasons are indistinguishable: draw the page without it. `use` never rejects;\n * permission problems come when you call a method (a rejected promise, `PortholeError`).\n */\n use<N extends keyof PortholeCapabilities>(name: N): Promise<PortholeCapabilities[N] | null>;\n}\n\n/** The page's one global. Nothing else is added to `window`. */\ndeclare const porthole: PortholeRuntime;\n",
|
|
17
|
+
"user": "// Porthole capability runtime, contract 1: user. Declare it: { user: {} }.\n\n/** Who is viewing, as the app says. */\ninterface PortholeUserInfo {\n /** The app's id for this person (whose data \"data/users/me\" is), or null if it gives none. */\n readonly id: string | null;\n readonly name: string | null;\n /** What they may do here: view < interact < admin < owner. */\n readonly level: \"view\" | \"interact\" | \"admin\" | \"owner\";\n}\n\ninterface PortholeUser {\n /** Level: view. */\n get(): Promise<PortholeUserInfo>;\n}\n\ninterface PortholeCapabilities {\n user: PortholeUser;\n}\n",
|
|
18
|
+
},
|
|
19
|
+
};
|
package/dist/server/files.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CompiledConfig } from "../compile/compile.ts";
|
|
2
|
-
import type { StoredFiles } from "./types.ts";
|
|
2
|
+
import type { StoredAsset, StoredFiles } from "./types.ts";
|
|
3
3
|
/**
|
|
4
4
|
* Serving a version made of files (P081, P082): each path looked up in its manifest only, each
|
|
5
5
|
* answer typed by Porthole's table, with `nosniff`, its CSP in a header, its cache rule and a
|
|
@@ -42,3 +42,16 @@ export declare function isNavigation(request: Request): boolean;
|
|
|
42
42
|
*/
|
|
43
43
|
export declare const ownLoad: (request: Request) => boolean;
|
|
44
44
|
export declare function serveFile(serving: FileServing): Promise<Response>;
|
|
45
|
+
/**
|
|
46
|
+
* An asset of an artifact's library (P089), at `/_blob/<id>`: typed by Porthole's table by its
|
|
47
|
+
* name, never as a page (one named so goes as a download), inert opened on its own (an SVG runs
|
|
48
|
+
* nothing), `nosniff`, loaded only by its own origin's pages (CORP), immutable (an id never names
|
|
49
|
+
* other bytes), its sha256 the ETag, ranges for media.
|
|
50
|
+
*/
|
|
51
|
+
export declare function serveAsset(serving: {
|
|
52
|
+
readonly request: Request;
|
|
53
|
+
readonly asset: StoredAsset;
|
|
54
|
+
readonly frameAncestors: string;
|
|
55
|
+
/** Whether a code-typed asset is served as code (trusted, where uploaded code runs). */
|
|
56
|
+
readonly code: boolean;
|
|
57
|
+
}): Promise<Response>;
|
package/dist/server/files.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { fileSetCspDirectives, INERT_FILE_CSP, serializeCsp } from "../compile/policy.js";
|
|
1
|
+
import { assetCspDirectives, fileSetCspDirectives, INERT_FILE_CSP, serializeCsp, } from "../compile/policy.js";
|
|
2
2
|
import { DEFAULT_ENTRY, lookupFile, requestFilePath } from "../files/paths.js";
|
|
3
3
|
import { fileTypeOf } from "../files/types.js";
|
|
4
|
+
import { isCodeAsset } from "../runtime/requests.js";
|
|
4
5
|
import { sha256, utf8 } from "./crypto.js";
|
|
5
6
|
const HEX = /^[0-9a-f]{64}$/u;
|
|
6
7
|
const YEAR = 31_536_000;
|
|
@@ -155,11 +156,18 @@ export async function serveFile(serving) {
|
|
|
155
156
|
return nothing(serving, 404, method);
|
|
156
157
|
}
|
|
157
158
|
const { type, role } = fileTypeOf(found.path);
|
|
158
|
-
|
|
159
|
+
// In a frame, the version's files are at a sealed address (P083), which their own loads of the
|
|
160
|
+
// artifact's assets carry as their `Referer` (P091): to this origin only, never another.
|
|
161
|
+
const referrer = serving.base.startsWith("/__porthole/f/") &&
|
|
162
|
+
serving.compiled.config.capabilities.assets !== undefined
|
|
163
|
+
? "same-origin"
|
|
164
|
+
: "no-referrer";
|
|
165
|
+
// Its version's files (P082), and its artifact's assets where the config serves them (P089).
|
|
166
|
+
const frameCsp = serializeCsp(assetCspDirectives(serving.compiled.config, fileSetCspDirectives(serving.compiled.cspDirectives, serving.source), serving.origin));
|
|
159
167
|
const headers = new Headers({
|
|
160
168
|
"content-type": type,
|
|
161
169
|
"x-content-type-options": "nosniff",
|
|
162
|
-
"referrer-policy":
|
|
170
|
+
"referrer-policy": referrer,
|
|
163
171
|
"x-robots-tag": "noindex, nofollow",
|
|
164
172
|
// A page and a script (a worker's policy is its script's) get the page's CSP; anything else
|
|
165
173
|
// runs nothing when opened on its own.
|
|
@@ -171,6 +179,7 @@ export async function serveFile(serving) {
|
|
|
171
179
|
origins: serving.hostOrigins,
|
|
172
180
|
document: await textOf(body),
|
|
173
181
|
cspMeta: false,
|
|
182
|
+
...(referrer === "same-origin" ? { referrer } : {}),
|
|
174
183
|
files: {
|
|
175
184
|
base,
|
|
176
185
|
// At a ticket's address: the entry's own (its directory's, for an entry in one).
|
|
@@ -242,3 +251,88 @@ export async function serveFile(serving) {
|
|
|
242
251
|
}
|
|
243
252
|
return new Response(method === "HEAD" ? null : asBody(body), { status: found.status, headers });
|
|
244
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* An asset of an artifact's library (P089), at `/_blob/<id>`: typed by Porthole's table by its
|
|
256
|
+
* name, never as a page (one named so goes as a download), inert opened on its own (an SVG runs
|
|
257
|
+
* nothing), `nosniff`, loaded only by its own origin's pages (CORP), immutable (an id never names
|
|
258
|
+
* other bytes), its sha256 the ETag, ranges for media.
|
|
259
|
+
*/
|
|
260
|
+
export async function serveAsset(serving) {
|
|
261
|
+
const { request, asset } = serving;
|
|
262
|
+
const method = request.method.toUpperCase();
|
|
263
|
+
const plain = (status) => new Response(method === "HEAD" ? null : status === 404 ? "Not found" : "Only read", {
|
|
264
|
+
status,
|
|
265
|
+
headers: {
|
|
266
|
+
"content-type": "text/plain; charset=utf-8",
|
|
267
|
+
"content-security-policy": `${INERT_FILE_CSP}; frame-ancestors ${serving.frameAncestors}`,
|
|
268
|
+
"cache-control": "no-store",
|
|
269
|
+
"x-content-type-options": "nosniff",
|
|
270
|
+
...(status === 405 ? { allow: "GET, HEAD" } : {}),
|
|
271
|
+
},
|
|
272
|
+
});
|
|
273
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
274
|
+
return plain(405);
|
|
275
|
+
}
|
|
276
|
+
const body = await asset.read();
|
|
277
|
+
if (body === null) {
|
|
278
|
+
return plain(404);
|
|
279
|
+
}
|
|
280
|
+
const typed = fileTypeOf(asset.name);
|
|
281
|
+
// Never a page: an asset is the library's, outside the shell (P089); code only where it was
|
|
282
|
+
// trusted when it was uploaded (after the review).
|
|
283
|
+
const type = typed.role === "page" || (isCodeAsset(asset.name) && !serving.code)
|
|
284
|
+
? "application/octet-stream"
|
|
285
|
+
: typed.type;
|
|
286
|
+
const headers = new Headers({
|
|
287
|
+
"content-type": type,
|
|
288
|
+
"x-content-type-options": "nosniff",
|
|
289
|
+
"referrer-policy": "no-referrer",
|
|
290
|
+
"x-robots-tag": "noindex, nofollow",
|
|
291
|
+
"content-security-policy": `${INERT_FILE_CSP}; frame-ancestors ${serving.frameAncestors}`,
|
|
292
|
+
// Asked again at each load, so a removal or a revocation holds at once (after the review);
|
|
293
|
+
// the ETag keeps it cheap.
|
|
294
|
+
"cache-control": "private, no-cache",
|
|
295
|
+
"cross-origin-resource-policy": "same-origin",
|
|
296
|
+
"accept-ranges": "bytes",
|
|
297
|
+
});
|
|
298
|
+
if (type === "application/octet-stream") {
|
|
299
|
+
headers.set("content-disposition", "attachment");
|
|
300
|
+
}
|
|
301
|
+
const etag = HEX.test(asset.sha256) ? `"${asset.sha256}"` : null;
|
|
302
|
+
if (etag !== null) {
|
|
303
|
+
headers.set("etag", etag);
|
|
304
|
+
if (notModified(request, etag)) {
|
|
305
|
+
if (!(body instanceof Uint8Array)) {
|
|
306
|
+
await body.cancel();
|
|
307
|
+
}
|
|
308
|
+
return new Response(null, { status: 304, headers });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
const size = body instanceof Uint8Array ? body.byteLength : asset.size;
|
|
312
|
+
const range = rangeOf(request.headers.get("range"), size);
|
|
313
|
+
if (range === "unsatisfiable") {
|
|
314
|
+
if (!(body instanceof Uint8Array)) {
|
|
315
|
+
await body.cancel();
|
|
316
|
+
}
|
|
317
|
+
headers.set("content-range", `bytes */${size}`);
|
|
318
|
+
return new Response(null, { status: 416, headers });
|
|
319
|
+
}
|
|
320
|
+
if (range !== null) {
|
|
321
|
+
headers.set("content-range", `bytes ${range.start}-${range.end}/${size}`);
|
|
322
|
+
headers.set("content-length", String(range.end - range.start + 1));
|
|
323
|
+
if (method === "HEAD" && !(body instanceof Uint8Array)) {
|
|
324
|
+
await body.cancel();
|
|
325
|
+
}
|
|
326
|
+
return new Response(method === "HEAD" ? null : asBody(slice(body, range.start, range.end)), {
|
|
327
|
+
status: 206,
|
|
328
|
+
headers,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
if (body instanceof Uint8Array || method === "HEAD") {
|
|
332
|
+
headers.set("content-length", String(size));
|
|
333
|
+
}
|
|
334
|
+
if (method === "HEAD" && !(body instanceof Uint8Array)) {
|
|
335
|
+
await body.cancel();
|
|
336
|
+
}
|
|
337
|
+
return new Response(method === "HEAD" ? null : asBody(body), { status: 200, headers });
|
|
338
|
+
}
|
package/dist/server/host.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { Displays } from "../displays/displays.ts";
|
|
|
4
4
|
import type { ContentClientMeta } from "../host/client.ts";
|
|
5
5
|
import { type ContentDomainInfo } from "./domain.ts";
|
|
6
6
|
import { type Naming } from "./naming.ts";
|
|
7
|
-
import type { Access, ArtifactStore, AuthorizeAnswer, AuthorizeRequest, Grant, PortholeServer, TicketStore, Upstream } from "./types.ts";
|
|
7
|
+
import type { Access, ArtifactStore, AssetStore, AuthorizeAnswer, AuthorizeRequest, Grant, PortholeServer, TicketStore, Upstream } from "./types.ts";
|
|
8
8
|
/**
|
|
9
9
|
* The host kit (P066): everything around `/server` that every host wrote, so an app writes only
|
|
10
10
|
* its policy (four callbacks) and its settings. Web-standard and runtime-agnostic: Node (through
|
|
@@ -115,6 +115,11 @@ export interface ContentHostOptions<V> {
|
|
|
115
115
|
readonly authorizeTtlSeconds?: number;
|
|
116
116
|
/** Policy 4, source: a stored page by id and version (artifact documents). */
|
|
117
117
|
readonly source?: ArtifactStore["get"];
|
|
118
|
+
/**
|
|
119
|
+
* Policy 5, assets (P089): an artifact's asset library by id, served at `/_blob/<id>` to an
|
|
120
|
+
* access for its artifact; or the store itself, with `bearer` (an explicit opt-in).
|
|
121
|
+
*/
|
|
122
|
+
readonly assets?: AssetStore["get"] | AssetStore;
|
|
118
123
|
readonly upstream?: Upstream;
|
|
119
124
|
/**
|
|
120
125
|
* The health gate, and with it the opaque fallback (P066): the kit checks the content domain
|
package/dist/server/host.js
CHANGED
|
@@ -167,6 +167,11 @@ export function createContentHost(options) {
|
|
|
167
167
|
hostOrigins: options.appOrigins,
|
|
168
168
|
configs,
|
|
169
169
|
...(options.source === undefined ? {} : { artifacts: { get: options.source } }),
|
|
170
|
+
...(options.assets === undefined
|
|
171
|
+
? {}
|
|
172
|
+
: {
|
|
173
|
+
assets: typeof options.assets === "function" ? { get: options.assets } : options.assets,
|
|
174
|
+
}),
|
|
170
175
|
...(options.tickets === undefined ? {} : { tickets: options.tickets }),
|
|
171
176
|
...(options.upstream === undefined ? {} : { upstream: options.upstream }),
|
|
172
177
|
...(options.entry === undefined
|
package/dist/server/index.d.ts
CHANGED
|
@@ -46,4 +46,5 @@ export { statusPageOf as statusPage } from "./pages.ts";
|
|
|
46
46
|
export { contentHostFor, type ContentHostFor } from "./bootstrap.ts";
|
|
47
47
|
export { checkFileSet, DEFAULT_ENTRY, fileLimits, filePathProblem, type FileLimits, type FileSetProblem, } from "../files/paths.ts";
|
|
48
48
|
export { fileTypeOf, type FileRole, type FileType } from "../files/types.ts";
|
|
49
|
-
export
|
|
49
|
+
export { pseudonymFor } from "./pseudonym.ts";
|
|
50
|
+
export type { Access, ArtifactStore, AssetStore, StoredAsset, FileBody, StoredFile, StoredFiles, StoredVersion, AuthorizeAnswer, AuthorizeRequest, Authorizer, ContentDomain, EntryState, Grant, PortholeServer, ServerOptions, TicketRecord, TicketStore, Upstream, } from "./types.ts";
|
package/dist/server/index.js
CHANGED
|
@@ -46,3 +46,4 @@ export { contentHostFor } from "./bootstrap.js";
|
|
|
46
46
|
// A version made of files (P081): the rules its paths follow, its limits and its types.
|
|
47
47
|
export { checkFileSet, DEFAULT_ENTRY, fileLimits, filePathProblem, } from "../files/paths.js";
|
|
48
48
|
export { fileTypeOf } from "../files/types.js";
|
|
49
|
+
export { pseudonymFor } from "./pseudonym.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A person's id for one artifact's pages (P087, after the review): what `Grant.user.id` may be
|
|
3
|
+
* where pages shouldn't learn the account id or link one person across artifacts. Stable for the
|
|
4
|
+
* same secret, artifact and person (their `data/users/<id>` stays theirs across visits), a
|
|
5
|
+
* different one on every other artifact, not reversible without the secret. 26 lowercase base32
|
|
6
|
+
* characters: one db path segment. The secret is the host's (at least 32 random bytes); keep it
|
|
7
|
+
* apart from Porthole's server secret.
|
|
8
|
+
*/
|
|
9
|
+
export declare function pseudonymFor(secret: Uint8Array, artifact: string, person: string): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { hmacSha256, utf8 } from "./crypto.js";
|
|
2
|
+
import { base32 } from "./keys.js";
|
|
3
|
+
/**
|
|
4
|
+
* A person's id for one artifact's pages (P087, after the review): what `Grant.user.id` may be
|
|
5
|
+
* where pages shouldn't learn the account id or link one person across artifacts. Stable for the
|
|
6
|
+
* same secret, artifact and person (their `data/users/<id>` stays theirs across visits), a
|
|
7
|
+
* different one on every other artifact, not reversible without the secret. 26 lowercase base32
|
|
8
|
+
* characters: one db path segment. The secret is the host's (at least 32 random bytes); keep it
|
|
9
|
+
* apart from Porthole's server secret.
|
|
10
|
+
*/
|
|
11
|
+
export function pseudonymFor(secret, artifact, person) {
|
|
12
|
+
if (!(secret instanceof Uint8Array) || secret.length < 32) {
|
|
13
|
+
throw new TypeError("pseudonymFor needs a secret of at least 32 random bytes");
|
|
14
|
+
}
|
|
15
|
+
if (artifact === "" || person === "") {
|
|
16
|
+
throw new TypeError("pseudonymFor needs an artifact and a person");
|
|
17
|
+
}
|
|
18
|
+
const mac = hmacSha256(secret, utf8(`porthole/pseudonym\u0000${artifact}\u0000${person}`));
|
|
19
|
+
return base32(mac.slice(0, 16)).slice(0, 26);
|
|
20
|
+
}
|
package/dist/server/server.d.ts
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
1
|
import type { PortholeServer, ServerOptions } from "./types.ts";
|
|
2
|
-
/**
|
|
3
|
-
* Serving for domain, artifact and url modes, and the MCP Apps sandbox proxy (P016, P020): a
|
|
4
|
-
* fetch-style handler a host's backend mounts in front of its own routes, plus `issue`, which the
|
|
5
|
-
* host's authenticated API calls once it has decided a viewer may see an instance.
|
|
6
|
-
*/
|
|
7
2
|
export declare function createServer(options: ServerOptions): PortholeServer;
|
package/dist/server/server.js
CHANGED
|
@@ -6,8 +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, serveFile } from "./files.js";
|
|
10
|
-
import { INERT_FILE_CSP } from "../compile/policy.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";
|
|
11
12
|
import { memoryTicketStore } from "./tickets.js";
|
|
12
13
|
/**
|
|
13
14
|
* Paths on a content origin. Porthole's own live under `/__porthole/`; on a url-mode app's origin
|
|
@@ -22,6 +23,10 @@ const DOCUMENT_PATH = /^\/__porthole\/d\/([a-z2-7]{16})$/u;
|
|
|
22
23
|
*/
|
|
23
24
|
const FILES_PATH = /^\/__porthole\/f\/([A-Za-z0-9_-]{40,4096})(\/.*)?$/u;
|
|
24
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;
|
|
25
30
|
/** How long a sealed files' address opens a page's files on its own (P083); the shell renews it. */
|
|
26
31
|
const FILES_ADDRESS_MS = 3_600_000;
|
|
27
32
|
/** Below a sealed address: where the page's shell renews it (no file has a `__porthole/` path). */
|
|
@@ -176,6 +181,23 @@ function cookiesOf(request) {
|
|
|
176
181
|
* fetch-style handler a host's backend mounts in front of its own routes, plus `issue`, which the
|
|
177
182
|
* host's authenticated API calls once it has decided a viewer may see an instance.
|
|
178
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
|
+
}
|
|
179
201
|
export function createServer(options) {
|
|
180
202
|
const { content } = options;
|
|
181
203
|
const domain = content.domain;
|
|
@@ -390,6 +412,7 @@ export function createServer(options) {
|
|
|
390
412
|
return {
|
|
391
413
|
html: config.shellDocument({ origins, document: stored.html, cspMeta: false }),
|
|
392
414
|
csp: config.csp,
|
|
415
|
+
compiled: config,
|
|
393
416
|
};
|
|
394
417
|
}
|
|
395
418
|
case "mcp-proxy":
|
|
@@ -582,6 +605,40 @@ export function createServer(options) {
|
|
|
582
605
|
"x-robots-tag": "noindex, nofollow",
|
|
583
606
|
});
|
|
584
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
|
+
}
|
|
585
642
|
/**
|
|
586
643
|
* A file set's file in a frame (P083): the sealed access in its path opens it, once it holds
|
|
587
644
|
* for this origin and the host's check lets it in; every path under it is the same version's.
|
|
@@ -616,39 +673,20 @@ export function createServer(options) {
|
|
|
616
673
|
headers: { location: `${origin}${base}${url.search}`, "cache-control": "no-store" },
|
|
617
674
|
});
|
|
618
675
|
}
|
|
619
|
-
const
|
|
620
|
-
|
|
621
|
-
let instance = null;
|
|
622
|
-
let end = 0;
|
|
623
|
-
if (isRecord(session) &&
|
|
624
|
-
typeof session.w === "string" &&
|
|
625
|
-
typeof session.x === "number" &&
|
|
626
|
-
typeof session.e === "number" &&
|
|
627
|
-
(session.s === undefined || typeof session.s === "string")) {
|
|
628
|
-
end = session.e;
|
|
629
|
-
const open = session.x > now || (session.e > now && ownLoad(request));
|
|
630
|
-
try {
|
|
631
|
-
const clean = cleanInstance(session.i);
|
|
632
|
-
instance =
|
|
633
|
-
open && clean.document.kind === "artifact" && nameOf(clean.scope) === label
|
|
634
|
-
? clean
|
|
635
|
-
: null;
|
|
636
|
-
}
|
|
637
|
-
catch {
|
|
638
|
-
instance = null;
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
if (instance === null || !isRecord(session)) {
|
|
676
|
+
const access = await sealedAccess(request, label, token);
|
|
677
|
+
if (access === null) {
|
|
642
678
|
return page(401, "This has ended", "Content here opens for a while. Open it again from the app that showed it.", method, "session");
|
|
643
679
|
}
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
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
|
+
}
|
|
652
690
|
if (rest === RENEW_PATH) {
|
|
653
691
|
// Only the page itself renews its address, while its session lasts and the host lets it.
|
|
654
692
|
if (method !== "GET" || !ownLoad(request)) {
|
|
@@ -761,12 +799,145 @@ export function createServer(options) {
|
|
|
761
799
|
if (at !== undefined && at.rest !== "") {
|
|
762
800
|
return page(404, "Not found", "This page, or this version of it, doesn't exist.", method);
|
|
763
801
|
}
|
|
764
|
-
|
|
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)));
|
|
765
806
|
if (cookie !== null) {
|
|
766
807
|
headers.set("set-cookie", cookie);
|
|
767
808
|
}
|
|
768
809
|
return new Response(method === "HEAD" ? null : served.html, { status: 200, headers });
|
|
769
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
|
+
}
|
|
770
941
|
/**
|
|
771
942
|
* A clean address without a cookie for it (P068): off to the host's authorize route with a
|
|
772
943
|
* signed state, the browser holding the state's nonce in a cookie of this origin. A path just
|
|
@@ -1085,7 +1256,9 @@ export function createServer(options) {
|
|
|
1085
1256
|
const path = clean.document.kind === "external"
|
|
1086
1257
|
? `${ENTER_PATH}?ticket=${ticket}`
|
|
1087
1258
|
: `${PREFIX}d/${key}?ticket=${ticket}`;
|
|
1088
|
-
|
|
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 } : {}) };
|
|
1089
1262
|
},
|
|
1090
1263
|
forget(filter = {}) {
|
|
1091
1264
|
for (const [key, each] of answers) {
|
|
@@ -1204,6 +1377,12 @@ export function createServer(options) {
|
|
|
1204
1377
|
const status = access === null ? 401 : (await denied(request, access)) === null ? 204 : 403;
|
|
1205
1378
|
return new Response(null, { status, headers: { "cache-control": "no-store" } });
|
|
1206
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
|
+
}
|
|
1207
1386
|
const document = DOCUMENT_PATH.exec(url.pathname);
|
|
1208
1387
|
if (document?.[1] !== undefined) {
|
|
1209
1388
|
return await serveDocument(request, url, label, document[1]);
|