@botiverse/porthole 0.14.4 → 0.14.6
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/CHANGELOG.md +71 -0
- package/dist/check/page-source.js +1 -1
- package/dist/check/page.js +5 -1
- package/dist/compile/policy.d.ts +6 -0
- package/dist/compile/policy.js +6 -0
- package/dist/conformance/entry.d.ts +25 -6
- package/dist/conformance/entry.js +69 -18
- package/dist/conformance/harness/page.d.ts +12 -0
- package/dist/conformance/harness/page.js +95 -51
- package/dist/conformance/harness-source.js +1 -1
- package/dist/conformance/host-kit.js +39 -13
- package/dist/conformance/served.js +140 -0
- package/dist/files/paths.d.ts +2 -2
- package/dist/files/paths.js +12 -2
- package/dist/host/client.d.ts +8 -0
- package/dist/host/client.js +47 -17
- package/dist/host/create-host.d.ts +10 -0
- package/dist/host/create-host.js +126 -10
- package/dist/host/door.d.ts +41 -0
- package/dist/host/door.js +55 -0
- package/dist/host/locate.js +2 -0
- package/dist/host/locator.d.ts +55 -3
- package/dist/host/locator.js +152 -42
- package/dist/host/outcome.d.ts +58 -1
- package/dist/host/outcome.js +74 -6
- package/dist/host/types.d.ts +20 -4
- package/dist/isolation/served.d.ts +1 -1
- package/dist/isolation/served.js +35 -0
- package/dist/isolation/types.d.ts +20 -2
- package/dist/isolation/types.js +2 -0
- package/dist/platform/types.d.ts +22 -0
- package/dist/platform/web.js +55 -12
- package/dist/react/frame.d.ts +2 -1
- package/dist/react/view.d.ts +13 -0
- package/dist/react/view.js +101 -2
- package/dist/server/files.d.ts +11 -0
- package/dist/server/files.js +26 -4
- package/dist/server/host.js +106 -28
- package/dist/server/index.d.ts +2 -1
- package/dist/server/index.js +6 -1
- package/dist/server/pages.d.ts +32 -0
- package/dist/server/pages.js +82 -6
- package/dist/server/server.js +140 -11
- package/dist/server/types.d.ts +15 -1
- package/dist/transport/post-message.d.ts +1 -9
- package/dist/transport/post-message.js +2 -8
- package/docs/ADOPTING.md +58 -0
- package/docs/DECISIONS.md +290 -0
- package/docs/DESIGN.md +24 -16
- package/docs/THREAT_MODEL.md +98 -3
- package/package.json +1 -1
- package/src/check/page.ts +6 -1
- package/src/compile/policy.ts +7 -0
- package/src/conformance/entry.ts +129 -24
- package/src/conformance/harness/page.ts +112 -49
- package/src/conformance/host-kit.ts +53 -16
- package/src/conformance/served.ts +175 -0
- package/src/files/paths.ts +11 -2
- package/src/host/client.ts +59 -22
- package/src/host/create-host.ts +146 -18
- package/src/host/door.ts +97 -0
- package/src/host/locate.ts +2 -0
- package/src/host/locator.ts +208 -57
- package/src/host/outcome.ts +88 -7
- package/src/host/types.ts +20 -4
- package/src/isolation/served.ts +40 -1
- package/src/isolation/types.ts +19 -2
- package/src/platform/types.ts +21 -0
- package/src/platform/web.ts +55 -20
- package/src/react/frame.tsx +2 -1
- package/src/react/view.tsx +149 -2
- package/src/server/files.ts +37 -5
- package/src/server/host.ts +131 -46
- package/src/server/index.ts +6 -1
- package/src/server/pages.ts +101 -6
- package/src/server/server.ts +186 -19
- package/src/server/types.ts +16 -1
- package/src/transport/post-message.ts +1 -9
package/dist/server/files.d.ts
CHANGED
|
@@ -32,6 +32,11 @@ export interface FileServing {
|
|
|
32
32
|
*/
|
|
33
33
|
readonly ticket?: {
|
|
34
34
|
readonly expires: number;
|
|
35
|
+
/**
|
|
36
|
+
* A page below the base the ticket was issued for (P125: a clean address's file, resolved
|
|
37
|
+
* through `issue`), opened instead of the entry (`ticketPage`).
|
|
38
|
+
*/
|
|
39
|
+
readonly page?: string;
|
|
35
40
|
};
|
|
36
41
|
/** The artifact version the set is, which its pages name in their notices to the host (P098). */
|
|
37
42
|
readonly artifact?: {
|
|
@@ -46,6 +51,12 @@ export declare function isNavigation(request: Request): boolean;
|
|
|
46
51
|
* only a browser sets and no page can change), and not a navigation.
|
|
47
52
|
*/
|
|
48
53
|
export declare const ownLoad: (request: Request) => boolean;
|
|
54
|
+
/**
|
|
55
|
+
* The page a ticket opens in a set (P081, P125): its entry, or the page below the base it was
|
|
56
|
+
* issued for, found as a navigation finds it (`dir/` its index, `name` its `.html`); null when
|
|
57
|
+
* that is no page of the set. The door asks the same (`serveDoor`), so both say `not-found` alike.
|
|
58
|
+
*/
|
|
59
|
+
export declare function ticketPage(set: StoredFiles, page?: string): string | null;
|
|
49
60
|
export declare function serveFile(serving: FileServing): Promise<Response>;
|
|
50
61
|
/**
|
|
51
62
|
* An asset of an artifact's library (P089), at `/_blob/<id>`: typed by Porthole's table by its
|
package/dist/server/files.js
CHANGED
|
@@ -108,6 +108,30 @@ function nothing(serving, status, method) {
|
|
|
108
108
|
},
|
|
109
109
|
});
|
|
110
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* The page a ticket opens in a set (P081, P125): its entry, or the page below the base it was
|
|
113
|
+
* issued for, found as a navigation finds it (`dir/` its index, `name` its `.html`); null when
|
|
114
|
+
* that is no page of the set. The door asks the same (`serveDoor`), so both say `not-found` alike.
|
|
115
|
+
*/
|
|
116
|
+
export function ticketPage(set, page) {
|
|
117
|
+
const has = (each) => Object.hasOwn(set.files, each);
|
|
118
|
+
const entry = set.entry ?? DEFAULT_ENTRY;
|
|
119
|
+
if (page === undefined || page === "") {
|
|
120
|
+
return has(entry) ? entry : null;
|
|
121
|
+
}
|
|
122
|
+
const path = requestFilePath(page);
|
|
123
|
+
if (path === null) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
let found = lookupFile(has, path, { entry, navigation: false });
|
|
127
|
+
if (found.kind === "redirect") {
|
|
128
|
+
found = lookupFile(has, found.to, { entry, navigation: false });
|
|
129
|
+
}
|
|
130
|
+
return found.kind === "file" && found.status === 200 && fileTypeOf(found.path).role === "page"
|
|
131
|
+
? found.path
|
|
132
|
+
: null;
|
|
133
|
+
}
|
|
134
|
+
const ticketLookup = (path) => path === null ? { kind: "none" } : { kind: "file", path, status: 200 };
|
|
111
135
|
export async function serveFile(serving) {
|
|
112
136
|
const { request, set } = serving;
|
|
113
137
|
const method = request.method.toUpperCase();
|
|
@@ -127,9 +151,7 @@ export async function serveFile(serving) {
|
|
|
127
151
|
const has = (each) => Object.hasOwn(set.files, each);
|
|
128
152
|
const entryPath = set.entry ?? DEFAULT_ENTRY;
|
|
129
153
|
const found = serving.ticket !== undefined
|
|
130
|
-
?
|
|
131
|
-
? { kind: "file", path: entryPath, status: 200 }
|
|
132
|
-
: { kind: "none" }
|
|
154
|
+
? ticketLookup(ticketPage(set, serving.ticket.page))
|
|
133
155
|
: lookupFile(has, path, {
|
|
134
156
|
...(set.entry === undefined ? {} : { entry: set.entry }),
|
|
135
157
|
...(set.spa === undefined ? {} : { spa: set.spa }),
|
|
@@ -206,7 +228,7 @@ export async function serveFile(serving) {
|
|
|
206
228
|
...(serving.ticket === undefined
|
|
207
229
|
? {}
|
|
208
230
|
: {
|
|
209
|
-
address: `${base}${found.path.includes("/") ? encodePath(found.path)
|
|
231
|
+
address: `${base}${found.path === entryPath && !found.path.includes("/") ? "" : encodePath(found.path)}`,
|
|
210
232
|
expires: serving.ticket.expires,
|
|
211
233
|
}),
|
|
212
234
|
...(serving.artifact === undefined ? {} : { set: serving.artifact }),
|
package/dist/server/host.js
CHANGED
|
@@ -3,7 +3,7 @@ import { base64url, fromBase64url, fromUtf8, hmacSha256, timingSafeEqual, utf8 }
|
|
|
3
3
|
import { ContentDomainError, contentDomain, serverDomain, } from "./domain.js";
|
|
4
4
|
import { reservedLabels } from "./naming.js";
|
|
5
5
|
import { peerAddress } from "./peer.js";
|
|
6
|
-
import { appStatusPage } from "./pages.js";
|
|
6
|
+
import { appStatusPage, tabOnlyPage } from "./pages.js";
|
|
7
7
|
import { createServer } from "./server.js";
|
|
8
8
|
/**
|
|
9
9
|
* The host kit (P066): everything around `/server` that every host wrote, so an app writes only
|
|
@@ -425,6 +425,20 @@ export function createContentHost(options) {
|
|
|
425
425
|
};
|
|
426
426
|
// Issues per key per minute, in this process.
|
|
427
427
|
const issued = new Map();
|
|
428
|
+
/**
|
|
429
|
+
* What a clean address names (P068, P125): its label's scope (the naming's `scopeOf`) and what
|
|
430
|
+
* `entry.at` answers for its path, for this viewer. One mapping for a tab (`authorize`) and for a
|
|
431
|
+
* frame (`issue` given `{ address }`), so a frame shows what the address's tab would.
|
|
432
|
+
*/
|
|
433
|
+
const entryAt = async (label, pathname, viewer, request) => {
|
|
434
|
+
const scope = options.name?.scopeOf?.(label) ?? null;
|
|
435
|
+
const at = options.entry?.at;
|
|
436
|
+
const answer = scope === null || at === undefined
|
|
437
|
+
? null
|
|
438
|
+
: ((await at(scope, new URL(pathname, "https://x.invalid").pathname, viewer, request)) ??
|
|
439
|
+
null);
|
|
440
|
+
return { scope, answer };
|
|
441
|
+
};
|
|
428
442
|
const withinRate = (key) => {
|
|
429
443
|
const limit = options.rateLimit;
|
|
430
444
|
if (limit === undefined) {
|
|
@@ -619,6 +633,10 @@ export function createContentHost(options) {
|
|
|
619
633
|
catch {
|
|
620
634
|
return refusal(400, "bad-json", "The body is JSON: { instance }");
|
|
621
635
|
}
|
|
636
|
+
// A clean address (P125): what it names, as its tab would open it, issued for a frame.
|
|
637
|
+
if (isRecord(body) && body.instance === undefined && body.address !== undefined) {
|
|
638
|
+
return issueAddress(body.address, viewer, request);
|
|
639
|
+
}
|
|
622
640
|
const proposal = isRecord(body) ? body.instance : undefined;
|
|
623
641
|
if (!isRecord(proposal) ||
|
|
624
642
|
typeof proposal.scope !== "string" ||
|
|
@@ -637,28 +655,86 @@ export function createContentHost(options) {
|
|
|
637
655
|
? refusal(401, "unauthenticated", "Sign in to see this")
|
|
638
656
|
: refusal(403, "forbidden", "You can't see this");
|
|
639
657
|
}
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
658
|
+
return issuedFor(instance, grant, request);
|
|
659
|
+
}
|
|
660
|
+
catch {
|
|
661
|
+
return refusal(500, "failed", "The app's policy failed");
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
/** A location issued for what the host's access granted, with its clean address. */
|
|
665
|
+
async function issuedFor(instance, grant, request, more = {}) {
|
|
666
|
+
let location;
|
|
667
|
+
try {
|
|
668
|
+
location = await server.issue(instance, grant, more.page === undefined ? undefined : { page: more.page });
|
|
669
|
+
}
|
|
670
|
+
catch (error) {
|
|
671
|
+
return refusal(409, "refused", error instanceof Error ? error.message : String(error));
|
|
672
|
+
}
|
|
673
|
+
// The clean address, where the app says the instance's path (P072, P074).
|
|
674
|
+
let link = null;
|
|
675
|
+
try {
|
|
676
|
+
const path = (await options.entry?.pathOf?.(instance, request)) ?? null;
|
|
677
|
+
link = path === null ? null : server.urlOf(instance.scope, path);
|
|
678
|
+
}
|
|
679
|
+
catch {
|
|
680
|
+
link = null;
|
|
681
|
+
}
|
|
682
|
+
return json(201, {
|
|
683
|
+
...location,
|
|
684
|
+
...(link === null ? {} : { link }),
|
|
685
|
+
// What a clean address named (P125): the instance its later frames are issued for.
|
|
686
|
+
...(more.resolved === true ? { instance } : {}),
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* A clean address asked about for a frame (P125): resolved as its tab's authorize would
|
|
691
|
+
* resolve it (`entry.at`, then the host's access), and answered before any frame: a location
|
|
692
|
+
* with its door, or the same refusal the tab would get (401 signed out, 403 no access, 404
|
|
693
|
+
* nothing there). Never a redirect, never a page: the host shows its fallback from this.
|
|
694
|
+
*/
|
|
695
|
+
async function issueAddress(given, viewer, request) {
|
|
696
|
+
let url;
|
|
697
|
+
try {
|
|
698
|
+
if (typeof given !== "string" || given.length > 4_096) {
|
|
699
|
+
throw new TypeError("not an address");
|
|
646
700
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
701
|
+
url = new URL(given);
|
|
702
|
+
}
|
|
703
|
+
catch {
|
|
704
|
+
return refusal(400, "bad-request", "The body is { address: <a clean address> }");
|
|
705
|
+
}
|
|
706
|
+
const label = domain?.labelOf(url.host) ?? null;
|
|
707
|
+
if (options.entry === undefined ||
|
|
708
|
+
domain === null ||
|
|
709
|
+
label === null ||
|
|
710
|
+
domain.originOf(label) !== url.origin ||
|
|
711
|
+
url.pathname.startsWith("/__porthole/")) {
|
|
712
|
+
return refusal(404, "not-found", "That address names nothing here");
|
|
713
|
+
}
|
|
714
|
+
try {
|
|
715
|
+
const { answer } = await entryAt(label, `${url.pathname}${url.search}`, viewer, request);
|
|
716
|
+
const address = answer !== null && isFileAddress(answer) ? answer : null;
|
|
717
|
+
const instance = address === null ? answer : address.instance;
|
|
718
|
+
if (instance === null) {
|
|
719
|
+
return refusal(404, "not-found", "That address names nothing here");
|
|
652
720
|
}
|
|
653
|
-
|
|
654
|
-
|
|
721
|
+
const grant = await options.access(instance, viewer, request);
|
|
722
|
+
if (grant === null) {
|
|
723
|
+
return viewer === null
|
|
724
|
+
? refusal(401, "unauthenticated", "Sign in to see this")
|
|
725
|
+
: refusal(403, "forbidden", "You can't see this");
|
|
655
726
|
}
|
|
656
|
-
|
|
727
|
+
// A file set's address below its version (P081): the frame opens at that page.
|
|
728
|
+
const page = address?.file !== undefined && address.file !== "" ? address.file : undefined;
|
|
729
|
+
return await issuedFor(instance, grant, request, {
|
|
730
|
+
...(page === undefined ? {} : { page }),
|
|
731
|
+
resolved: true,
|
|
732
|
+
});
|
|
657
733
|
}
|
|
658
734
|
catch {
|
|
659
735
|
return refusal(500, "failed", "The app's policy failed");
|
|
660
736
|
}
|
|
661
|
-
}
|
|
737
|
+
}
|
|
662
738
|
},
|
|
663
739
|
meta: () => ({
|
|
664
740
|
served: served(),
|
|
@@ -777,13 +853,19 @@ export function createContentHost(options) {
|
|
|
777
853
|
if (state === null) {
|
|
778
854
|
return appPage(400, "This link isn't valid", "Open the address again.", "error", method);
|
|
779
855
|
}
|
|
856
|
+
// Redirect entry is for a tab alone (P125): asked in a frame, it never shows the app's
|
|
857
|
+
// sign-in or refusal there, and says nothing to the page that frames it.
|
|
780
858
|
const dest = request.headers.get("sec-fetch-dest");
|
|
781
|
-
|
|
859
|
+
if (dest === "iframe" || dest === "frame") {
|
|
860
|
+
return tabOnlyPage(options.appOrigins.join(" "), method);
|
|
861
|
+
}
|
|
782
862
|
const clean = `${domain.originOf(state.label)}${state.path}`;
|
|
783
863
|
if (state.expired) {
|
|
784
|
-
return appPage(401, "This took too long", "Open the address again.", "session", method,
|
|
864
|
+
return appPage(401, "This took too long", "Open the address again.", "session", method, {
|
|
865
|
+
href: clean,
|
|
866
|
+
text: "Open it again",
|
|
867
|
+
});
|
|
785
868
|
}
|
|
786
|
-
const scope = options.name?.scopeOf?.(state.label) ?? null;
|
|
787
869
|
let viewer;
|
|
788
870
|
try {
|
|
789
871
|
viewer = (await viewerOf(request)) ?? (await handedOver(request));
|
|
@@ -792,9 +874,7 @@ export function createContentHost(options) {
|
|
|
792
874
|
return appPage(500, "Something went wrong", "The app couldn't tell who is asking.", "error", method);
|
|
793
875
|
}
|
|
794
876
|
try {
|
|
795
|
-
const answer =
|
|
796
|
-
? null
|
|
797
|
-
: ((await entry.at(scope, new URL(state.path, "https://x.invalid").pathname, viewer, request)) ?? null);
|
|
877
|
+
const { scope, answer } = await entryAt(state.label, state.path, viewer, request);
|
|
798
878
|
// A file set's address (P081) names its instance and where its version lives.
|
|
799
879
|
const address = answer !== null && isFileAddress(answer) ? answer : null;
|
|
800
880
|
const instance = address === null ? answer : address.instance;
|
|
@@ -806,17 +886,15 @@ export function createContentHost(options) {
|
|
|
806
886
|
path: state.path,
|
|
807
887
|
url: clean,
|
|
808
888
|
instance,
|
|
809
|
-
|
|
889
|
+
// A frame never gets here since P125: kept for the hooks' type.
|
|
890
|
+
framed: false,
|
|
810
891
|
};
|
|
811
892
|
if (instance === null || grant === null) {
|
|
812
893
|
if (viewer === null) {
|
|
813
|
-
|
|
814
|
-
return framed
|
|
815
|
-
? appPage(401, "Open this in a new tab to sign in", "Signing in doesn't work inside a frame.", "sign-in", method)
|
|
816
|
-
: await signInWith(entry, request, context);
|
|
894
|
+
return await signInWith(entry, request, context);
|
|
817
895
|
}
|
|
818
896
|
const status = instance === null ? 404 : 403;
|
|
819
|
-
const own =
|
|
897
|
+
const own = await entry.refusal?.(status, request, context);
|
|
820
898
|
if (own !== null && own !== undefined) {
|
|
821
899
|
return own;
|
|
822
900
|
}
|
package/dist/server/index.d.ts
CHANGED
|
@@ -36,7 +36,8 @@ export { createServer } from "./server.ts";
|
|
|
36
36
|
export { createContentHost, HEALTH_PATH, ISSUE_BODY_BYTES, type ContentHealthView, type ContentHost, type ContentHostEvent, type ContentHostOptions, type ContentClientMeta, type ContentLogger, type EntryContext, type EntryOptions, ASSET_LIBRARY_PATH, type FileAddress, isFileAddress, versionBase, versionedAddress, type ContentMeta, type HealthOptions, type HealthProbe, } from "./host.ts";
|
|
37
37
|
export { clashOf, contentDomain, ContentDomainError, hostnameOf, type ContentDomainInfo, type ContentDomainProblem, } from "./domain.ts";
|
|
38
38
|
export { d1TicketSchema, d1TicketStore, memoryTicketStore, type D1Like, type D1StatementLike, } from "./tickets.ts";
|
|
39
|
-
export { FRAME_STATUS_METHOD } from "../isolation/types.ts";
|
|
39
|
+
export { DOOR_PATH, FRAME_STATUS_METHOD } from "../isolation/types.ts";
|
|
40
|
+
export { doorResponse, doorStatusResponse, type DoorOptions } from "./pages.ts";
|
|
40
41
|
export { isNestedLabel, NESTED_FILE_HEADERS, NESTED_FILES_PREFIX, nestedAncestors, nestedFileCsp, nestedLabelOf, nestedShellCsp, nestedShellResponse, PORTHOLE_LABEL_PREFIX, } from "./nested.ts";
|
|
41
42
|
export { isHostLabel, prefixedName, type Naming } from "./naming.ts";
|
|
42
43
|
export { conformancePage } from "../conformance/page.ts";
|
package/dist/server/index.js
CHANGED
|
@@ -36,7 +36,12 @@ export { createServer } from "./server.js";
|
|
|
36
36
|
export { createContentHost, HEALTH_PATH, ISSUE_BODY_BYTES, ASSET_LIBRARY_PATH, isFileAddress, versionBase, versionedAddress, } from "./host.js";
|
|
37
37
|
export { clashOf, contentDomain, ContentDomainError, hostnameOf, } from "./domain.js";
|
|
38
38
|
export { d1TicketSchema, d1TicketStore, memoryTicketStore, } from "./tickets.js";
|
|
39
|
-
export { FRAME_STATUS_METHOD } from "../isolation/types.js";
|
|
39
|
+
export { DOOR_PATH, FRAME_STATUS_METHOD } from "../isolation/types.js";
|
|
40
|
+
// A served display's door (P125): `/server` serves it for a shell, a page that isn't nested and the
|
|
41
|
+
// sandbox proxy; a content Worker of a host's own answers the same at `DOOR_PATH<key>`, after
|
|
42
|
+
// taking the door's own ticket (`TicketRecord.door`) and running the checks its content's address
|
|
43
|
+
// runs (docs/ADOPTING.md, "The door").
|
|
44
|
+
export { doorResponse, doorStatusResponse } from "./pages.js";
|
|
40
45
|
// A nested page (P111): Porthole's shell at the framed address, the page and its files below the
|
|
41
46
|
// sealed prefix, all on the shell's own label (P114, `nestedLabelOf`). `/server` serves them for a
|
|
42
47
|
// config with `nested`; a content Worker of its own answers the same (docs/ADOPTING.md, "Nested
|
package/dist/server/pages.d.ts
CHANGED
|
@@ -37,3 +37,35 @@ export declare function statusPageOf(options: {
|
|
|
37
37
|
};
|
|
38
38
|
readonly method?: string;
|
|
39
39
|
}): Response;
|
|
40
|
+
/** What a door is given: the host origins it tells, and who may frame it (default, those). */
|
|
41
|
+
export interface DoorOptions {
|
|
42
|
+
readonly hostOrigins: readonly string[];
|
|
43
|
+
readonly frameAncestors?: string;
|
|
44
|
+
readonly method?: string;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A door that opens (P125): its display may be framed. It says `open` to the page that frames it,
|
|
48
|
+
* at each web host origin and (a desktop scheme among them) to its parent (P070), once, as it
|
|
49
|
+
* parses. For a content Worker of a host's own: answer it at `DOOR_PATH<key>` only once the door's
|
|
50
|
+
* own ticket was taken and the checks the content's address runs passed for the same grant.
|
|
51
|
+
*/
|
|
52
|
+
export declare function doorResponse(options: DoorOptions): Response;
|
|
53
|
+
/**
|
|
54
|
+
* A door that doesn't open (P125): why, said `blocked` to the page that frames it, as a content
|
|
55
|
+
* origin's status page says it (`session` 401: no ticket, or one used, ended, or not a door's of
|
|
56
|
+
* this instance on this origin; `denied` 403 with the host's code; `not-found` 404: no document, or
|
|
57
|
+
* a file set whose entry isn't a page; `error` 500).
|
|
58
|
+
*/
|
|
59
|
+
export declare function doorStatusResponse(options: DoorOptions & {
|
|
60
|
+
readonly status: number;
|
|
61
|
+
readonly reason: FrameBlock;
|
|
62
|
+
readonly code?: string;
|
|
63
|
+
}): Response;
|
|
64
|
+
/**
|
|
65
|
+
* What a clean address answers in a frame (P125): a clean address is for a tab of its own, so in a
|
|
66
|
+
* frame it never goes to the app (no authorize, no sign-in, no refusal page of the app's) and says
|
|
67
|
+
* nothing to the page that frames it: no script at all, under the status pages' policy without
|
|
68
|
+
* `script-src` (so `default-src 'none'` forbids every script), framed by the host origins only. A
|
|
69
|
+
* host frames what a clean address names by resolving it through `issue` (`client.resolve`).
|
|
70
|
+
*/
|
|
71
|
+
export declare function tabOnlyPage(frameAncestors: string, method?: string): Response;
|
package/dist/server/pages.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DOOR_SANDBOX_TOKENS } from "../compile/policy.js";
|
|
1
2
|
import { FRAME_STATUS_METHOD } from "../isolation/types.js";
|
|
2
3
|
import { base64url, sha256, utf8 } from "./crypto.js";
|
|
3
4
|
/**
|
|
@@ -109,15 +110,16 @@ export function enterPage(target, frameAncestors, hostOrigins, cookie) {
|
|
|
109
110
|
headers.set("set-cookie", cookie);
|
|
110
111
|
return new Response(body, { status: 200, headers });
|
|
111
112
|
}
|
|
112
|
-
/**
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
statusHash ??= base64url(sha256(utf8(STATUS_SCRIPT)))
|
|
113
|
+
/** A script's hash, for a page that carries it inline (`script-src 'sha256-…'`). */
|
|
114
|
+
const hashOf = (script) => {
|
|
115
|
+
const hash = base64url(sha256(utf8(script)))
|
|
116
116
|
.replace(/-/gu, "+")
|
|
117
117
|
.replace(/_/gu, "/");
|
|
118
|
-
const padding = (4 - (
|
|
119
|
-
return `'sha256-${
|
|
118
|
+
const padding = (4 - (hash.length % 4)) % 4;
|
|
119
|
+
return `'sha256-${hash}${"=".repeat(padding)}'`;
|
|
120
120
|
};
|
|
121
|
+
let statusHash = null;
|
|
122
|
+
const hashOfStatus = () => (statusHash ??= hashOf(STATUS_SCRIPT));
|
|
121
123
|
/**
|
|
122
124
|
* A redirect entry's page on the app's own origin (P068): why it shows nothing, told to the page
|
|
123
125
|
* that frames it like a content origin's page. Plain, no app chrome; its one script inline under
|
|
@@ -148,3 +150,77 @@ export function appStatusPage(status, title, detail, frameAncestors, reason, hos
|
|
|
148
150
|
export function statusPageOf(options) {
|
|
149
151
|
return appStatusPage(options.status, options.title, options.detail, options.frameAncestors ?? options.hostOrigins.join(" "), options.reason ?? "error", options.hostOrigins, options.link, options.method);
|
|
150
152
|
}
|
|
153
|
+
/** A door's word that its display may be framed (P125), from the page's data attributes. */
|
|
154
|
+
const DOOR_SCRIPT = `(() => {
|
|
155
|
+
const main = document.querySelector("[data-porthole-door]");
|
|
156
|
+
if (main === null) return;
|
|
157
|
+
const hosts = JSON.parse(main.dataset.portholeHosts || "[]");
|
|
158
|
+
${say()}
|
|
159
|
+
say({ state: "open" });
|
|
160
|
+
})();`;
|
|
161
|
+
let doorHash = null;
|
|
162
|
+
const hashOfDoor = () => (doorHash ??= hashOf(DOOR_SCRIPT));
|
|
163
|
+
/**
|
|
164
|
+
* A door's headers (P125): its one script by its hash (an opaque document has no `'self'` to load
|
|
165
|
+
* one from), nothing else loads, sandboxed with scripts alone in its header too (opaque on a tab, and
|
|
166
|
+
* in a frame whose attribute a host's own platform forgot), framed by the host origins, never
|
|
167
|
+
* cached, never sent on, no cookie.
|
|
168
|
+
*/
|
|
169
|
+
const doorHeaders = (script, frameAncestors) => new Headers({
|
|
170
|
+
"content-type": "text/html; charset=utf-8",
|
|
171
|
+
"cache-control": "no-store",
|
|
172
|
+
"content-security-policy": `default-src 'none'; script-src ${script}; base-uri 'none'; form-action 'none'; sandbox ${DOOR_SANDBOX_TOKENS.join(" ")}; frame-ancestors ${frameAncestors}`,
|
|
173
|
+
"referrer-policy": "no-referrer",
|
|
174
|
+
"x-content-type-options": "nosniff",
|
|
175
|
+
"x-robots-tag": "noindex, nofollow",
|
|
176
|
+
});
|
|
177
|
+
/**
|
|
178
|
+
* A door that opens (P125): its display may be framed. It says `open` to the page that frames it,
|
|
179
|
+
* at each web host origin and (a desktop scheme among them) to its parent (P070), once, as it
|
|
180
|
+
* parses. For a content Worker of a host's own: answer it at `DOOR_PATH<key>` only once the door's
|
|
181
|
+
* own ticket was taken and the checks the content's address runs passed for the same grant.
|
|
182
|
+
*/
|
|
183
|
+
export function doorResponse(options) {
|
|
184
|
+
const hosts = escape(JSON.stringify(options.hostOrigins));
|
|
185
|
+
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Opening</title></head><body><main data-porthole-door="" data-porthole-hosts="${hosts}"></main><script>${DOOR_SCRIPT}</script></body></html>`;
|
|
186
|
+
return new Response(options.method?.toUpperCase() === "HEAD" ? null : body, {
|
|
187
|
+
status: 200,
|
|
188
|
+
headers: doorHeaders(hashOfDoor(), options.frameAncestors ?? options.hostOrigins.join(" ")),
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* A door that doesn't open (P125): why, said `blocked` to the page that frames it, as a content
|
|
193
|
+
* origin's status page says it (`session` 401: no ticket, or one used, ended, or not a door's of
|
|
194
|
+
* this instance on this origin; `denied` 403 with the host's code; `not-found` 404: no document, or
|
|
195
|
+
* a file set whose entry isn't a page; `error` 500).
|
|
196
|
+
*/
|
|
197
|
+
export function doorStatusResponse(options) {
|
|
198
|
+
const { status, reason, code } = options;
|
|
199
|
+
const hosts = escape(JSON.stringify(options.hostOrigins));
|
|
200
|
+
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Not shown</title></head><body><main data-porthole-status="${status}" data-porthole-reason="${escape(reason)}"${code === undefined ? "" : ` data-porthole-code="${escape(code)}"`} data-porthole-hosts="${hosts}"></main><script>${STATUS_SCRIPT}</script></body></html>`;
|
|
201
|
+
return new Response(options.method?.toUpperCase() === "HEAD" ? null : body, {
|
|
202
|
+
status,
|
|
203
|
+
headers: doorHeaders(hashOfStatus(), options.frameAncestors ?? options.hostOrigins.join(" ")),
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* What a clean address answers in a frame (P125): a clean address is for a tab of its own, so in a
|
|
208
|
+
* frame it never goes to the app (no authorize, no sign-in, no refusal page of the app's) and says
|
|
209
|
+
* nothing to the page that frames it: no script at all, under the status pages' policy without
|
|
210
|
+
* `script-src` (so `default-src 'none'` forbids every script), framed by the host origins only. A
|
|
211
|
+
* host frames what a clean address names by resolving it through `issue` (`client.resolve`).
|
|
212
|
+
*/
|
|
213
|
+
export function tabOnlyPage(frameAncestors, method = "GET") {
|
|
214
|
+
const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Open this in a new tab</title><style>${STYLE}</style></head><body><main data-porthole-tab-only=""><h1>Open this in a new tab</h1><p>This address opens in a tab of its own, not inside a frame.</p></main></body></html>`;
|
|
215
|
+
return new Response(method.toUpperCase() === "HEAD" ? null : body, {
|
|
216
|
+
status: 403,
|
|
217
|
+
headers: {
|
|
218
|
+
"content-type": "text/html; charset=utf-8",
|
|
219
|
+
"cache-control": "no-store",
|
|
220
|
+
"content-security-policy": `default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors ${frameAncestors}`,
|
|
221
|
+
"referrer-policy": "no-referrer",
|
|
222
|
+
"x-content-type-options": "nosniff",
|
|
223
|
+
"x-robots-tag": "noindex, nofollow",
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
}
|