@botiverse/porthole 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -0
- package/dist/bridge/bridge.d.ts +12 -4
- package/dist/bridge/bridge.js +62 -27
- package/dist/bridge/navigation.d.ts +6 -0
- package/dist/bridge/navigation.js +41 -0
- package/dist/bridge/refusal.d.ts +53 -0
- package/dist/bridge/refusal.js +121 -0
- package/dist/bridge/types.d.ts +7 -0
- package/dist/check/check.d.ts +12 -3
- package/dist/check/check.js +54 -8
- package/dist/check/files.d.ts +26 -0
- package/dist/check/files.js +307 -0
- package/dist/check/lint.js +27 -10
- package/dist/check/page-source.js +1 -1
- package/dist/check/page.js +5 -0
- package/dist/check/tools.d.ts +23 -0
- package/dist/check/tools.js +25 -0
- package/dist/compile/policy.d.ts +15 -0
- package/dist/compile/policy.js +32 -0
- package/dist/config/capabilities.d.ts +5 -0
- package/dist/config/schema.d.ts +8 -0
- package/dist/config/validate.js +8 -1
- package/dist/conformance/harness-source.js +1 -1
- 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/guide.js +25 -5
- package/dist/host/create-host.js +36 -4
- package/dist/host/types.d.ts +15 -2
- package/dist/index.d.ts +7 -3
- package/dist/index.js +10 -3
- package/dist/mcp/adapter.js +22 -7
- package/dist/protocol/adapter.d.ts +4 -0
- package/dist/protocol/mcp-apps.d.ts +12 -0
- package/dist/protocol/mcp-apps.js +12 -0
- package/dist/protocol/shell-adapter.js +18 -2
- package/dist/react/frame.d.ts +19 -4
- package/dist/react/frame.js +37 -9
- package/dist/server/files.d.ts +44 -0
- package/dist/server/files.js +244 -0
- package/dist/server/host.d.ts +38 -1
- package/dist/server/host.js +37 -2
- package/dist/server/index.d.ts +4 -2
- package/dist/server/index.js +4 -1
- package/dist/server/keys.d.ts +8 -0
- package/dist/server/keys.js +34 -0
- package/dist/server/server.js +287 -22
- package/dist/server/types.d.ts +49 -5
- package/dist/shell/document.d.ts +19 -0
- package/dist/shell/document.js +30 -0
- package/dist/shell/runtime.d.ts +2 -1
- package/dist/shell/runtime.js +117 -10
- package/docs/ADOPTING.md +95 -13
- package/docs/ARCHITECTURE.md +13 -3
- package/docs/DECISIONS.md +396 -4
- package/docs/DESIGN.md +43 -10
- package/package.json +1 -1
- package/src/bridge/bridge.ts +74 -23
- package/src/bridge/navigation.ts +46 -0
- package/src/bridge/refusal.ts +151 -0
- package/src/bridge/types.ts +12 -1
- package/src/check/check.ts +92 -14
- package/src/check/files.ts +384 -0
- package/src/check/lint.ts +29 -10
- package/src/check/page.ts +7 -0
- package/src/check/tools.ts +50 -0
- package/src/compile/policy.ts +38 -0
- package/src/config/capabilities.ts +5 -1
- package/src/config/schema.ts +8 -0
- package/src/config/validate.ts +8 -1
- package/src/files/paths.ts +323 -0
- package/src/files/types.ts +101 -0
- package/src/guide/guide.ts +38 -4
- package/src/host/create-host.ts +37 -4
- package/src/host/types.ts +13 -2
- package/src/index.ts +41 -0
- package/src/mcp/adapter.ts +26 -6
- package/src/protocol/adapter.ts +3 -1
- package/src/protocol/mcp-apps.ts +14 -0
- package/src/protocol/shell-adapter.ts +20 -2
- package/src/react/frame.tsx +53 -14
- package/src/server/files.ts +312 -0
- package/src/server/host.ts +72 -3
- package/src/server/index.ts +19 -0
- package/src/server/keys.ts +53 -0
- package/src/server/server.ts +353 -24
- package/src/server/types.ts +56 -4
- package/src/shell/document.ts +53 -0
- package/src/shell/runtime.ts +117 -10
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import type { CompiledConfig } from "../compile/compile.ts";
|
|
2
|
+
import { fileSetCspDirectives, INERT_FILE_CSP, serializeCsp } from "../compile/policy.ts";
|
|
3
|
+
import { DEFAULT_ENTRY, lookupFile, requestFilePath, type FileLookup } from "../files/paths.ts";
|
|
4
|
+
import { fileTypeOf } from "../files/types.ts";
|
|
5
|
+
import { sha256, utf8 } from "./crypto.ts";
|
|
6
|
+
import type { FileBody, StoredFiles } from "./types.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Serving a version made of files (P081, P082): each path looked up in its manifest only, each
|
|
10
|
+
* answer typed by Porthole's table, with `nosniff`, its CSP in a header, its cache rule and a
|
|
11
|
+
* strong ETag; pages in the shell, ranges for the rest.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export interface FileServing {
|
|
15
|
+
readonly request: Request;
|
|
16
|
+
readonly set: StoredFiles;
|
|
17
|
+
/** The request's path below the base, as the URL has it (percent-encoded). */
|
|
18
|
+
readonly rest: string;
|
|
19
|
+
/** The search part, kept on a redirect. */
|
|
20
|
+
readonly search: string;
|
|
21
|
+
/** The version's own address on its origin: a path ending in `/`. */
|
|
22
|
+
readonly base: string;
|
|
23
|
+
readonly origin: string;
|
|
24
|
+
/**
|
|
25
|
+
* Whether `base` names this version and no other (a pinned address, a frame's): its files are
|
|
26
|
+
* immutable there. Else they are revalidated (an address that may mean another version later).
|
|
27
|
+
*/
|
|
28
|
+
readonly pinned: boolean;
|
|
29
|
+
/** The CSP source the version's files load from (P082): its path on the origin. */
|
|
30
|
+
readonly source: string;
|
|
31
|
+
readonly compiled: CompiledConfig;
|
|
32
|
+
readonly hostOrigins: readonly string[];
|
|
33
|
+
readonly frameAncestors: string;
|
|
34
|
+
/**
|
|
35
|
+
* A ticket's answer (P083): the entry, served at the ticket's address, never stored; its shell
|
|
36
|
+
* moves the frame to the entry's own address under `base` before anything loads, and renews
|
|
37
|
+
* that address before it ends (`expires`, epoch ms).
|
|
38
|
+
*/
|
|
39
|
+
readonly ticket?: { readonly expires: number };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const HEX = /^[0-9a-f]{64}$/u;
|
|
43
|
+
const YEAR = 31_536_000;
|
|
44
|
+
|
|
45
|
+
const hex = (bytes: Uint8Array): string =>
|
|
46
|
+
Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
47
|
+
|
|
48
|
+
/** A path's segments percent-encoded, for a `Location`. */
|
|
49
|
+
const encodePath = (path: string): string =>
|
|
50
|
+
path
|
|
51
|
+
.split("/")
|
|
52
|
+
.map((segment) => encodeURIComponent(segment))
|
|
53
|
+
.join("/");
|
|
54
|
+
|
|
55
|
+
/** Whether the request is a page loading (a navigation), not a subresource. */
|
|
56
|
+
export function isNavigation(request: Request): boolean {
|
|
57
|
+
const mode = request.headers.get("sec-fetch-mode");
|
|
58
|
+
const dest = request.headers.get("sec-fetch-dest");
|
|
59
|
+
if (mode !== null || dest !== null) {
|
|
60
|
+
return mode === "navigate" || dest === "document" || dest === "iframe" || dest === "frame";
|
|
61
|
+
}
|
|
62
|
+
return (request.headers.get("accept") ?? "").includes("text/html");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Whether the request is a running page's own load: from its own origin (`Sec-Fetch-Site`, which
|
|
67
|
+
* only a browser sets and no page can change), and not a navigation.
|
|
68
|
+
*/
|
|
69
|
+
export const ownLoad = (request: Request): boolean =>
|
|
70
|
+
request.headers.get("sec-fetch-site") === "same-origin" && !isNavigation(request);
|
|
71
|
+
|
|
72
|
+
/** Whether `If-None-Match` names this ETag. */
|
|
73
|
+
function notModified(request: Request, etag: string): boolean {
|
|
74
|
+
const asked = request.headers.get("if-none-match");
|
|
75
|
+
if (asked === null) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return asked
|
|
79
|
+
.split(",")
|
|
80
|
+
.map((each) => each.trim().replace(/^W\//u, ""))
|
|
81
|
+
.some((each) => each === "*" || each === etag);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A single `bytes=` range of a body of `size` bytes: the range, "unsatisfiable", or null (ignore it). */
|
|
85
|
+
function rangeOf(
|
|
86
|
+
header: string | null,
|
|
87
|
+
size: number,
|
|
88
|
+
): { readonly start: number; readonly end: number } | "unsatisfiable" | null {
|
|
89
|
+
if (header === null) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const match = /^bytes=(\d*)-(\d*)$/u.exec(header.trim());
|
|
93
|
+
if (match === null) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
const [, from = "", to = ""] = match;
|
|
97
|
+
if (from === "" && to === "") {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
if (from === "") {
|
|
101
|
+
// The last `to` bytes.
|
|
102
|
+
const length = Number(to);
|
|
103
|
+
if (length === 0) {
|
|
104
|
+
return "unsatisfiable";
|
|
105
|
+
}
|
|
106
|
+
return size === 0 ? "unsatisfiable" : { start: Math.max(0, size - length), end: size - 1 };
|
|
107
|
+
}
|
|
108
|
+
const start = Number(from);
|
|
109
|
+
const end = to === "" ? size - 1 : Math.min(Number(to), size - 1);
|
|
110
|
+
if (!Number.isSafeInteger(start) || start >= size || end < start) {
|
|
111
|
+
return "unsatisfiable";
|
|
112
|
+
}
|
|
113
|
+
return { start, end };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** `body` from byte `start` through `end`. */
|
|
117
|
+
function slice(body: FileBody, start: number, end: number): FileBody {
|
|
118
|
+
if (body instanceof Uint8Array) {
|
|
119
|
+
return body.subarray(start, end + 1);
|
|
120
|
+
}
|
|
121
|
+
let offset = 0;
|
|
122
|
+
return body.pipeThrough(
|
|
123
|
+
new TransformStream<Uint8Array, Uint8Array>({
|
|
124
|
+
transform(chunk, controller) {
|
|
125
|
+
const from = Math.max(start - offset, 0);
|
|
126
|
+
const to = Math.min(end + 1 - offset, chunk.byteLength);
|
|
127
|
+
if (from < to) {
|
|
128
|
+
controller.enqueue(chunk.subarray(from, to));
|
|
129
|
+
}
|
|
130
|
+
offset += chunk.byteLength;
|
|
131
|
+
if (offset > end) {
|
|
132
|
+
controller.terminate();
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function textOf(body: FileBody): Promise<string> {
|
|
140
|
+
if (body instanceof Uint8Array) {
|
|
141
|
+
return new TextDecoder().decode(body);
|
|
142
|
+
}
|
|
143
|
+
return new Response(body).text();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const asBody = (body: FileBody): BodyInit =>
|
|
147
|
+
body instanceof Uint8Array ? new Uint8Array(body) : body;
|
|
148
|
+
|
|
149
|
+
/** The answer for nothing found: plain, inert. */
|
|
150
|
+
function nothing(serving: FileServing, status: 403 | 404 | 405, method: string): Response {
|
|
151
|
+
const text =
|
|
152
|
+
status === 404 ? "Not found" : status === 405 ? "Content here is only read" : "Not allowed";
|
|
153
|
+
return new Response(method === "HEAD" ? null : text, {
|
|
154
|
+
status,
|
|
155
|
+
headers: {
|
|
156
|
+
"content-type": "text/plain; charset=utf-8",
|
|
157
|
+
"content-security-policy": `${INERT_FILE_CSP}; frame-ancestors ${serving.frameAncestors}`,
|
|
158
|
+
"cache-control": "no-store",
|
|
159
|
+
"x-content-type-options": "nosniff",
|
|
160
|
+
"referrer-policy": "no-referrer",
|
|
161
|
+
"x-robots-tag": "noindex, nofollow",
|
|
162
|
+
...(status === 405 ? { allow: "GET, HEAD" } : {}),
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export async function serveFile(serving: FileServing): Promise<Response> {
|
|
168
|
+
const { request, set } = serving;
|
|
169
|
+
const method = request.method.toUpperCase();
|
|
170
|
+
// No service worker, ever: one would answer this origin's requests with documents of its
|
|
171
|
+
// own making, outside the shell and its CSP (P081).
|
|
172
|
+
if (
|
|
173
|
+
request.headers.get("service-worker") !== null ||
|
|
174
|
+
request.headers.get("sec-fetch-dest") === "serviceworker"
|
|
175
|
+
) {
|
|
176
|
+
return nothing(serving, 403, method);
|
|
177
|
+
}
|
|
178
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
179
|
+
return nothing(serving, 405, method);
|
|
180
|
+
}
|
|
181
|
+
const path = requestFilePath(serving.rest);
|
|
182
|
+
if (path === null) {
|
|
183
|
+
return nothing(serving, 404, method);
|
|
184
|
+
}
|
|
185
|
+
const has = (each: string): boolean => Object.hasOwn(set.files, each);
|
|
186
|
+
const entryPath = set.entry ?? DEFAULT_ENTRY;
|
|
187
|
+
const found: FileLookup =
|
|
188
|
+
serving.ticket !== undefined
|
|
189
|
+
? has(entryPath)
|
|
190
|
+
? { kind: "file", path: entryPath, status: 200 }
|
|
191
|
+
: { kind: "none" }
|
|
192
|
+
: lookupFile(has, path, {
|
|
193
|
+
...(set.entry === undefined ? {} : { entry: set.entry }),
|
|
194
|
+
...(set.spa === undefined ? {} : { spa: set.spa }),
|
|
195
|
+
navigation: isNavigation(request),
|
|
196
|
+
});
|
|
197
|
+
if (found.kind === "none") {
|
|
198
|
+
return nothing(serving, 404, method);
|
|
199
|
+
}
|
|
200
|
+
// The version's own address, as a URL writes it.
|
|
201
|
+
const base = new URL(serving.base, serving.origin).href;
|
|
202
|
+
if (found.kind === "redirect") {
|
|
203
|
+
return new Response(null, {
|
|
204
|
+
status: 307,
|
|
205
|
+
headers: {
|
|
206
|
+
location: `${base}${encodePath(found.to)}${serving.search}`,
|
|
207
|
+
"cache-control": "no-store",
|
|
208
|
+
"referrer-policy": "no-referrer",
|
|
209
|
+
"x-robots-tag": "noindex, nofollow",
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
const entry = set.files[found.path];
|
|
214
|
+
const body = entry === undefined ? null : await set.read(found.path);
|
|
215
|
+
if (entry === undefined || body === null) {
|
|
216
|
+
return nothing(serving, 404, method);
|
|
217
|
+
}
|
|
218
|
+
const { type, role } = fileTypeOf(found.path);
|
|
219
|
+
const frameCsp = serializeCsp(
|
|
220
|
+
fileSetCspDirectives(serving.compiled.cspDirectives, serving.source),
|
|
221
|
+
);
|
|
222
|
+
const headers = new Headers({
|
|
223
|
+
"content-type": type,
|
|
224
|
+
"x-content-type-options": "nosniff",
|
|
225
|
+
"referrer-policy": "no-referrer",
|
|
226
|
+
"x-robots-tag": "noindex, nofollow",
|
|
227
|
+
// A page and a script (a worker's policy is its script's) get the page's CSP; anything else
|
|
228
|
+
// runs nothing when opened on its own.
|
|
229
|
+
"content-security-policy": `${role === "file" ? INERT_FILE_CSP : frameCsp}; frame-ancestors ${serving.frameAncestors}`,
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
if (role === "page") {
|
|
233
|
+
// Every page of the set in the shell (P081): its hardening, its host functions, its theme.
|
|
234
|
+
const html = serving.compiled.shellDocument({
|
|
235
|
+
origins: serving.hostOrigins,
|
|
236
|
+
document: await textOf(body),
|
|
237
|
+
cspMeta: false,
|
|
238
|
+
files: {
|
|
239
|
+
base,
|
|
240
|
+
// At a ticket's address: the entry's own (its directory's, for an entry in one).
|
|
241
|
+
...(serving.ticket === undefined
|
|
242
|
+
? {}
|
|
243
|
+
: {
|
|
244
|
+
address: `${base}${found.path.includes("/") ? encodePath(found.path) : ""}`,
|
|
245
|
+
expires: serving.ticket.expires,
|
|
246
|
+
}),
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
if (serving.ticket !== undefined) {
|
|
250
|
+
headers.set("cache-control", "no-store");
|
|
251
|
+
return new Response(method === "HEAD" ? null : html, { status: 200, headers });
|
|
252
|
+
}
|
|
253
|
+
const etag = `"${hex(sha256(utf8(html)))}"`;
|
|
254
|
+
headers.set("etag", etag);
|
|
255
|
+
// A page is asked again each time it loads, so the host's check runs on every navigation.
|
|
256
|
+
headers.set("cache-control", "private, no-cache");
|
|
257
|
+
if (found.status === 200 && notModified(request, etag)) {
|
|
258
|
+
return new Response(null, { status: 304, headers });
|
|
259
|
+
}
|
|
260
|
+
return new Response(method === "HEAD" ? null : html, { status: found.status, headers });
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const size = body instanceof Uint8Array ? body.byteLength : entry.size;
|
|
264
|
+
const etag = HEX.test(entry.sha256) ? `"${entry.sha256}"` : null;
|
|
265
|
+
if (etag !== null) {
|
|
266
|
+
headers.set("etag", etag);
|
|
267
|
+
}
|
|
268
|
+
headers.set(
|
|
269
|
+
"cache-control",
|
|
270
|
+
serving.pinned ? `private, max-age=${YEAR}, immutable` : "private, no-cache",
|
|
271
|
+
);
|
|
272
|
+
// Only this origin's own pages load it: no other content origin (a sibling, same-site without
|
|
273
|
+
// the public suffix list) can embed it with the viewer's cookie.
|
|
274
|
+
headers.set("cross-origin-resource-policy", "same-origin");
|
|
275
|
+
headers.set("accept-ranges", "bytes");
|
|
276
|
+
if (role === "file" && type === "application/octet-stream") {
|
|
277
|
+
headers.set("content-disposition", "attachment");
|
|
278
|
+
}
|
|
279
|
+
if (etag !== null && notModified(request, etag)) {
|
|
280
|
+
if (!(body instanceof Uint8Array)) {
|
|
281
|
+
await body.cancel();
|
|
282
|
+
}
|
|
283
|
+
return new Response(null, { status: 304, headers });
|
|
284
|
+
}
|
|
285
|
+
const range = found.status === 200 ? rangeOf(request.headers.get("range"), size) : null;
|
|
286
|
+
if (range === "unsatisfiable") {
|
|
287
|
+
if (!(body instanceof Uint8Array)) {
|
|
288
|
+
await body.cancel();
|
|
289
|
+
}
|
|
290
|
+
headers.set("content-range", `bytes */${size}`);
|
|
291
|
+
return new Response(null, { status: 416, headers });
|
|
292
|
+
}
|
|
293
|
+
if (range !== null) {
|
|
294
|
+
headers.set("content-range", `bytes ${range.start}-${range.end}/${size}`);
|
|
295
|
+
headers.set("content-length", String(range.end - range.start + 1));
|
|
296
|
+
if (method === "HEAD" && !(body instanceof Uint8Array)) {
|
|
297
|
+
await body.cancel();
|
|
298
|
+
}
|
|
299
|
+
return new Response(method === "HEAD" ? null : asBody(slice(body, range.start, range.end)), {
|
|
300
|
+
status: 206,
|
|
301
|
+
headers,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
// A stream's length is the manifest's word: said for HEAD, left to the transfer otherwise.
|
|
305
|
+
if (body instanceof Uint8Array || method === "HEAD") {
|
|
306
|
+
headers.set("content-length", String(size));
|
|
307
|
+
}
|
|
308
|
+
if (method === "HEAD" && !(body instanceof Uint8Array)) {
|
|
309
|
+
await body.cancel();
|
|
310
|
+
}
|
|
311
|
+
return new Response(method === "HEAD" ? null : asBody(body), { status: found.status, headers });
|
|
312
|
+
}
|
package/src/server/host.ts
CHANGED
|
@@ -199,7 +199,7 @@ export interface EntryOptions<V> {
|
|
|
199
199
|
path: string,
|
|
200
200
|
viewer: V | null,
|
|
201
201
|
request: Request,
|
|
202
|
-
) => Awaitable<InstanceRef | null | undefined>;
|
|
202
|
+
) => Awaitable<InstanceRef | FileAddress | null | undefined>;
|
|
203
203
|
/**
|
|
204
204
|
* The app's own sign-in, which comes back to `returnTo` (the authorize URL, as `authorizeUrl`
|
|
205
205
|
* names the app, with the entry's state) once signed in.
|
|
@@ -244,6 +244,61 @@ export interface EntryOptions<V> {
|
|
|
244
244
|
) => Awaitable<Response | null | undefined>;
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
+
/**
|
|
248
|
+
* What a clean address means when its instance is a version made of files (P081): the instance,
|
|
249
|
+
* the version's own address (`base`, a path ending in `/`: `/v/3/`), and the file the address
|
|
250
|
+
* asks for below it (`""`: the version's entry). The session the entry buys serves every path
|
|
251
|
+
* under `base`, so the page's relative addresses are its own version's. An address that isn't
|
|
252
|
+
* under `base` (`/`, `/style.css`: the latest) goes to `base` + `file`, so a page never mixes
|
|
253
|
+
* versions when a new one is published while it loads. `versionedAddress` reads the usual
|
|
254
|
+
* convention (`/v/<n>/<file>`, else the latest).
|
|
255
|
+
*/
|
|
256
|
+
export interface FileAddress {
|
|
257
|
+
readonly instance: InstanceRef;
|
|
258
|
+
/** A path ending in `/`, each segment of `A-Z a-z 0-9 . _ : ~ -`, no `.` or `..` segment. */
|
|
259
|
+
readonly base: string;
|
|
260
|
+
readonly file?: string;
|
|
261
|
+
/**
|
|
262
|
+
* Whether `base` names this version and no other (`/v/3/`, never `/` or `/latest/`): its files
|
|
263
|
+
* are then immutable there. Default false: revalidated, as an address that may mean another
|
|
264
|
+
* version later.
|
|
265
|
+
*/
|
|
266
|
+
readonly pinned?: boolean;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Whether `entry.at`'s answer is a file set's address rather than an instance. */
|
|
270
|
+
export const isFileAddress = (value: InstanceRef | FileAddress): value is FileAddress =>
|
|
271
|
+
"base" in value && "instance" in value;
|
|
272
|
+
|
|
273
|
+
/** Kept free on every content origin for an asset library across versions (P081): not built. */
|
|
274
|
+
export const ASSET_LIBRARY_PATH = "/_blob/";
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The convention for a versioned scope's clean addresses (P081): `/v/<version>/<file>` is that
|
|
278
|
+
* version, pinned; any other path is the latest's `<file>` (`version` null). Null for a
|
|
279
|
+
* `/v/<version>` whose version isn't a name (`A-Z a-z 0-9 . _ : -`), and under `/_blob/`, kept for
|
|
280
|
+
* an asset library outside every version. `versionBase(version)` is the base to answer with.
|
|
281
|
+
*/
|
|
282
|
+
export function versionedAddress(
|
|
283
|
+
path: string,
|
|
284
|
+
): { readonly version: string | null; readonly file: string } | null {
|
|
285
|
+
if (path.startsWith(ASSET_LIBRARY_PATH)) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
const pinned = /^\/v\/([^/]*)(?:\/(.*))?$/u.exec(path);
|
|
289
|
+
if (pinned === null) {
|
|
290
|
+
return { version: null, file: path.replace(/^\//u, "") };
|
|
291
|
+
}
|
|
292
|
+
const [, version = "", file = ""] = pinned;
|
|
293
|
+
// A version is a name, never a dot segment.
|
|
294
|
+
return /^[A-Za-z0-9._:-]{1,200}$/u.test(version) && !/^\.{1,2}$/u.test(version)
|
|
295
|
+
? { version, file }
|
|
296
|
+
: null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** A version's own address in `versionedAddress`'s convention: `/v/<version>/`. */
|
|
300
|
+
export const versionBase = (version: string): string => `/v/${version}/`;
|
|
301
|
+
|
|
247
302
|
/** The clean address a redirect entry is opening, as its authorize route decoded it (P070). */
|
|
248
303
|
export interface EntryContext {
|
|
249
304
|
/** The address's label, and the scope it names (null: one the naming never makes). */
|
|
@@ -1036,7 +1091,7 @@ export function createContentHost<V>(options: ContentHostOptions<V>): ContentHos
|
|
|
1036
1091
|
);
|
|
1037
1092
|
}
|
|
1038
1093
|
try {
|
|
1039
|
-
const
|
|
1094
|
+
const answer =
|
|
1040
1095
|
scope === null
|
|
1041
1096
|
? null
|
|
1042
1097
|
: ((await entry.at(
|
|
@@ -1045,6 +1100,9 @@ export function createContentHost<V>(options: ContentHostOptions<V>): ContentHos
|
|
|
1045
1100
|
viewer,
|
|
1046
1101
|
request,
|
|
1047
1102
|
)) ?? null);
|
|
1103
|
+
// A file set's address (P081) names its instance and where its version lives.
|
|
1104
|
+
const address = answer !== null && isFileAddress(answer) ? answer : null;
|
|
1105
|
+
const instance = address === null ? (answer as InstanceRef | null) : address.instance;
|
|
1048
1106
|
const grant = instance === null ? null : await options.access(instance, viewer, request);
|
|
1049
1107
|
// The address being opened, decoded, for the app's sign-in and refusal pages.
|
|
1050
1108
|
const context: EntryContext = {
|
|
@@ -1091,7 +1149,18 @@ export function createContentHost<V>(options: ContentHostOptions<V>): ContentHos
|
|
|
1091
1149
|
}
|
|
1092
1150
|
let location: string;
|
|
1093
1151
|
try {
|
|
1094
|
-
location = await server.enterBy(
|
|
1152
|
+
location = await server.enterBy(
|
|
1153
|
+
state,
|
|
1154
|
+
instance,
|
|
1155
|
+
grant,
|
|
1156
|
+
address === null
|
|
1157
|
+
? undefined
|
|
1158
|
+
: {
|
|
1159
|
+
base: address.base,
|
|
1160
|
+
...(address.file === undefined ? {} : { file: address.file }),
|
|
1161
|
+
...(address.pinned === true ? { pinned: true } : {}),
|
|
1162
|
+
},
|
|
1163
|
+
);
|
|
1095
1164
|
} catch {
|
|
1096
1165
|
return appPage(
|
|
1097
1166
|
409,
|
package/src/server/index.ts
CHANGED
|
@@ -44,6 +44,11 @@ export {
|
|
|
44
44
|
type ContentLogger,
|
|
45
45
|
type EntryContext,
|
|
46
46
|
type EntryOptions,
|
|
47
|
+
ASSET_LIBRARY_PATH,
|
|
48
|
+
type FileAddress,
|
|
49
|
+
isFileAddress,
|
|
50
|
+
versionBase,
|
|
51
|
+
versionedAddress,
|
|
47
52
|
type ContentMeta,
|
|
48
53
|
type HealthOptions,
|
|
49
54
|
type HealthProbe,
|
|
@@ -79,9 +84,23 @@ export {
|
|
|
79
84
|
} from "./proxy.ts";
|
|
80
85
|
export { statusPageOf as statusPage } from "./pages.ts";
|
|
81
86
|
export { contentHostFor, type ContentHostFor } from "./bootstrap.ts";
|
|
87
|
+
// A version made of files (P081): the rules its paths follow, its limits and its types.
|
|
88
|
+
export {
|
|
89
|
+
checkFileSet,
|
|
90
|
+
DEFAULT_ENTRY,
|
|
91
|
+
fileLimits,
|
|
92
|
+
filePathProblem,
|
|
93
|
+
type FileLimits,
|
|
94
|
+
type FileSetProblem,
|
|
95
|
+
} from "../files/paths.ts";
|
|
96
|
+
export { fileTypeOf, type FileRole, type FileType } from "../files/types.ts";
|
|
82
97
|
export type {
|
|
83
98
|
Access,
|
|
84
99
|
ArtifactStore,
|
|
100
|
+
FileBody,
|
|
101
|
+
StoredFile,
|
|
102
|
+
StoredFiles,
|
|
103
|
+
StoredVersion,
|
|
85
104
|
AuthorizeAnswer,
|
|
86
105
|
AuthorizeRequest,
|
|
87
106
|
Authorizer,
|
package/src/server/keys.ts
CHANGED
|
@@ -59,8 +59,19 @@ export interface Keys {
|
|
|
59
59
|
sign(value: unknown): string;
|
|
60
60
|
/** The signed value, or null when the signature doesn't hold. */
|
|
61
61
|
verify(token: string): unknown;
|
|
62
|
+
/**
|
|
63
|
+
* `value` sealed (AES-256-GCM, Web Crypto): unreadable and unforgeable without the secret,
|
|
64
|
+
* base64url. A file set's access in a frame's path (P083), which content can read: the viewer
|
|
65
|
+
* and the host's subject inside it stay the host's.
|
|
66
|
+
*/
|
|
67
|
+
seal(value: unknown): Promise<string>;
|
|
68
|
+
/** The sealed value, or null when it doesn't open. */
|
|
69
|
+
unseal(token: string): Promise<unknown>;
|
|
62
70
|
}
|
|
63
71
|
|
|
72
|
+
/** What a sealed value is bound to, so it can never be taken for anything else sealed. */
|
|
73
|
+
const SEAL_CONTEXT = utf8("porthole/files");
|
|
74
|
+
|
|
64
75
|
export function createKeys(secret: Uint8Array): Keys {
|
|
65
76
|
if (secret.byteLength < 32) {
|
|
66
77
|
throw new TypeError("The server's secret must be at least 32 random bytes");
|
|
@@ -69,6 +80,13 @@ export function createKeys(secret: Uint8Array): Keys {
|
|
|
69
80
|
const labelKey = derive("label");
|
|
70
81
|
const instanceKey = derive("instance");
|
|
71
82
|
const signKey = derive("session");
|
|
83
|
+
const sealKey = derive("files");
|
|
84
|
+
let sealing: Promise<CryptoKey> | null = null;
|
|
85
|
+
const sealingKey = (): Promise<CryptoKey> =>
|
|
86
|
+
(sealing ??= crypto.subtle.importKey("raw", new Uint8Array(sealKey), "AES-GCM", false, [
|
|
87
|
+
"encrypt",
|
|
88
|
+
"decrypt",
|
|
89
|
+
]));
|
|
72
90
|
const mac = (key: Uint8Array, text: string): Uint8Array => hmacSha256(key, utf8(text));
|
|
73
91
|
|
|
74
92
|
return {
|
|
@@ -98,6 +116,41 @@ export function createKeys(secret: Uint8Array): Keys {
|
|
|
98
116
|
return null;
|
|
99
117
|
}
|
|
100
118
|
},
|
|
119
|
+
seal: async (value) => {
|
|
120
|
+
const iv = randomBytes(12);
|
|
121
|
+
const sealed = new Uint8Array(
|
|
122
|
+
await crypto.subtle.encrypt(
|
|
123
|
+
{ name: "AES-GCM", iv: new Uint8Array(iv), additionalData: new Uint8Array(SEAL_CONTEXT) },
|
|
124
|
+
await sealingKey(),
|
|
125
|
+
new Uint8Array(utf8(JSON.stringify(value))),
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
const token = new Uint8Array(iv.length + sealed.length);
|
|
129
|
+
token.set(iv);
|
|
130
|
+
token.set(sealed, iv.length);
|
|
131
|
+
return base64url(token);
|
|
132
|
+
},
|
|
133
|
+
unseal: async (token) => {
|
|
134
|
+
const bytes = token.length > 8_192 ? null : fromBase64url(token);
|
|
135
|
+
// 12 bytes of IV and 16 of tag at least.
|
|
136
|
+
if (bytes === null || bytes.length < 29) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const opened = await crypto.subtle.decrypt(
|
|
141
|
+
{
|
|
142
|
+
name: "AES-GCM",
|
|
143
|
+
iv: new Uint8Array(bytes.subarray(0, 12)),
|
|
144
|
+
additionalData: new Uint8Array(SEAL_CONTEXT),
|
|
145
|
+
},
|
|
146
|
+
await sealingKey(),
|
|
147
|
+
new Uint8Array(bytes.subarray(12)),
|
|
148
|
+
);
|
|
149
|
+
return JSON.parse(fromUtf8(new Uint8Array(opened))) as unknown;
|
|
150
|
+
} catch {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
},
|
|
101
154
|
};
|
|
102
155
|
}
|
|
103
156
|
|