@botiverse/porthole 0.5.1 → 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.js +8 -5
- package/dist/bridge/navigation.d.ts +1 -6
- package/dist/bridge/navigation.js +11 -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/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/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 +11 -1
- package/dist/host/create-host.js +21 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.js +8 -3
- package/dist/protocol/adapter.d.ts +4 -0
- package/dist/protocol/mcp-apps.d.ts +6 -0
- package/dist/protocol/mcp-apps.js +6 -0
- package/dist/protocol/shell-adapter.js +5 -1
- 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 +85 -4
- package/docs/ADOPTING.md +56 -4
- package/docs/ARCHITECTURE.md +13 -3
- package/docs/DECISIONS.md +304 -4
- package/docs/DESIGN.md +31 -3
- package/package.json +1 -1
- package/src/bridge/bridge.ts +8 -5
- package/src/bridge/navigation.ts +12 -0
- package/src/check/check.ts +92 -14
- package/src/check/files.ts +384 -0
- package/src/check/lint.ts +29 -10
- package/src/check/tools.ts +50 -0
- package/src/compile/policy.ts +38 -0
- 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 +20 -0
- package/src/host/create-host.ts +21 -0
- package/src/index.ts +32 -0
- package/src/protocol/adapter.ts +3 -1
- package/src/protocol/mcp-apps.ts +7 -0
- package/src/protocol/shell-adapter.ts +5 -0
- 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 +85 -4
package/src/server/server.ts
CHANGED
|
@@ -13,15 +13,18 @@ import { randomBytes } from "./crypto.ts";
|
|
|
13
13
|
import { isHostLabel, reservedLabels } from "./naming.ts";
|
|
14
14
|
import { enterPage, pageScript, SESSION_PATH, statusPage } from "./pages.ts";
|
|
15
15
|
import { libraryFile } from "./libraries.ts";
|
|
16
|
+
import { ownLoad, serveFile } from "./files.ts";
|
|
17
|
+
import { INERT_FILE_CSP } from "../compile/policy.ts";
|
|
16
18
|
import type { FrameBlock } from "../host/types.ts";
|
|
17
19
|
import { memoryTicketStore } from "./tickets.ts";
|
|
18
20
|
import type {
|
|
19
21
|
Access,
|
|
20
22
|
AuthorizeAnswer,
|
|
21
|
-
EntryState,
|
|
22
23
|
Grant,
|
|
23
24
|
PortholeServer,
|
|
24
25
|
ServerOptions,
|
|
26
|
+
StoredFiles,
|
|
27
|
+
StoredVersion,
|
|
25
28
|
TicketRecord,
|
|
26
29
|
TicketStore,
|
|
27
30
|
} from "./types.ts";
|
|
@@ -33,6 +36,21 @@ import type {
|
|
|
33
36
|
const PREFIX = "/__porthole/";
|
|
34
37
|
/** A served document (shell, artifact page, sandbox proxy), by its instance's key. */
|
|
35
38
|
const DOCUMENT_PATH = /^\/__porthole\/d\/([a-z2-7]{16})$/u;
|
|
39
|
+
/**
|
|
40
|
+
* A file set's files in a frame (P083): `/__porthole/f/<sealed access>/<path>`. The access is
|
|
41
|
+
* in the path, so a frame whose engine keeps no cookie (WebKit, P025) loads them too.
|
|
42
|
+
*/
|
|
43
|
+
const FILES_PATH = /^\/__porthole\/f\/([A-Za-z0-9_-]{40,4096})(\/.*)?$/u;
|
|
44
|
+
const FILES_PREFIX = "/__porthole/f/";
|
|
45
|
+
/** How long a sealed files' address opens a page's files on its own (P083); the shell renews it. */
|
|
46
|
+
const FILES_ADDRESS_MS = 3_600_000;
|
|
47
|
+
/** Below a sealed address: where the page's shell renews it (no file has a `__porthole/` path). */
|
|
48
|
+
const RENEW_PATH = "/__porthole/renew";
|
|
49
|
+
/**
|
|
50
|
+
* A clean address's base (P081): a path ending in `/`, each segment of letters, digits and
|
|
51
|
+
* `. _ : ~ -` (nothing a CSP source or a header would read otherwise), never `.` or `..`.
|
|
52
|
+
*/
|
|
53
|
+
const BASE = /^\/(?:(?!\.{1,2}\/)[A-Za-z0-9._:~-]{1,200}\/){0,16}$/u;
|
|
36
54
|
/** Where a url-mode app's ticket is taken, before the app's own landing path. */
|
|
37
55
|
const ENTER_PATH = "/__porthole/enter";
|
|
38
56
|
/** Where a redirect entry's code is redeemed, before its clean address (P068). */
|
|
@@ -83,11 +101,30 @@ interface Session {
|
|
|
83
101
|
readonly s?: string;
|
|
84
102
|
/** The clean path a document was entered at (P068): it is served there by this cookie. */
|
|
85
103
|
readonly p?: string;
|
|
104
|
+
/** A file set's base (P081): every path under it is served by this cookie. */
|
|
105
|
+
readonly b?: string;
|
|
106
|
+
/** Whether the base is the version's own address (the app said so): its files are immutable. */
|
|
107
|
+
readonly k?: 1;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Where a request falls under a file set's base: the base, and the path below it. */
|
|
111
|
+
interface FilesAt {
|
|
112
|
+
readonly base: string;
|
|
113
|
+
readonly rest: string;
|
|
114
|
+
readonly search: string;
|
|
115
|
+
readonly pinned: boolean;
|
|
116
|
+
/** The CSP source the version's files load from (P082). */
|
|
117
|
+
readonly source: string;
|
|
86
118
|
}
|
|
87
119
|
|
|
88
120
|
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
89
121
|
value !== null && typeof value === "object" && !Array.isArray(value);
|
|
90
122
|
|
|
123
|
+
const isStoredFiles = (stored: StoredVersion): stored is StoredFiles =>
|
|
124
|
+
isRecord(stored) &&
|
|
125
|
+
isRecord((stored as Partial<StoredFiles>).files) &&
|
|
126
|
+
typeof (stored as Partial<StoredFiles>).read === "function";
|
|
127
|
+
|
|
91
128
|
/** Which configs a kind of served document may be served under. */
|
|
92
129
|
const MODES: Readonly<Record<Exclude<ServedDocument["kind"], "external">, readonly DisplayMode[]>> =
|
|
93
130
|
{
|
|
@@ -438,7 +475,11 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
438
475
|
|
|
439
476
|
async function documentOf(
|
|
440
477
|
instance: InstanceRef,
|
|
441
|
-
): Promise<
|
|
478
|
+
): Promise<
|
|
479
|
+
| { readonly html: string; readonly csp: string }
|
|
480
|
+
| { readonly files: StoredFiles; readonly compiled: CompiledConfig }
|
|
481
|
+
| null
|
|
482
|
+
> {
|
|
442
483
|
const { document } = instance;
|
|
443
484
|
const config = compiledFor(instance);
|
|
444
485
|
if (config === null) {
|
|
@@ -450,7 +491,14 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
450
491
|
return { html: config.shellDocument({ origins, cspMeta: false }), csp: config.csp };
|
|
451
492
|
case "artifact": {
|
|
452
493
|
const stored = await options.artifacts?.get(document.id, document.version);
|
|
453
|
-
if (stored === null || stored === undefined
|
|
494
|
+
if (stored === null || stored === undefined) {
|
|
495
|
+
return null;
|
|
496
|
+
}
|
|
497
|
+
// A version made of files (P081): served file by file under its base.
|
|
498
|
+
if (isStoredFiles(stored)) {
|
|
499
|
+
return { files: stored, compiled: config };
|
|
500
|
+
}
|
|
501
|
+
if (typeof stored.html !== "string") {
|
|
454
502
|
return null;
|
|
455
503
|
}
|
|
456
504
|
return {
|
|
@@ -476,7 +524,11 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
476
524
|
* page that frames it; on a tab of its own, `Lax`. (Engines that keep no cookie in a cross-site
|
|
477
525
|
* frame, measured: WebKit, still get the document, which comes with the ticket's answer.)
|
|
478
526
|
*/
|
|
479
|
-
function sessionCookie(
|
|
527
|
+
function sessionCookie(
|
|
528
|
+
request: Request,
|
|
529
|
+
record: TicketRecord,
|
|
530
|
+
at: { readonly p?: string; readonly b?: string; readonly k?: 1 } = {},
|
|
531
|
+
): string {
|
|
480
532
|
const sessionSeconds = record.sessionSeconds ?? DEFAULT_SESSION_SECONDS;
|
|
481
533
|
const session: Session = {
|
|
482
534
|
i: record.instance,
|
|
@@ -484,7 +536,9 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
484
536
|
x: Date.now() + sessionSeconds * 1_000,
|
|
485
537
|
...(record.anonymous === true ? { a: 1 as const } : {}),
|
|
486
538
|
...(record.subject === undefined ? {} : { s: record.subject }),
|
|
487
|
-
...(
|
|
539
|
+
...(at.p === undefined ? {} : { p: at.p }),
|
|
540
|
+
...(at.b === undefined ? {} : { b: at.b }),
|
|
541
|
+
...(at.k === undefined ? {} : { k: at.k }),
|
|
488
542
|
};
|
|
489
543
|
return [
|
|
490
544
|
`${cookieName(keys.instanceKey(record.instance))}=${keys.sign(session)}`,
|
|
@@ -557,17 +611,35 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
557
611
|
return best;
|
|
558
612
|
}
|
|
559
613
|
|
|
560
|
-
/**
|
|
561
|
-
|
|
562
|
-
|
|
614
|
+
/**
|
|
615
|
+
* The access a request's cookies give to the document entered at `pathname` (P068), or to a
|
|
616
|
+
* file set's path under the base it was entered at (P081): with where the path falls under the
|
|
617
|
+
* base, or the base itself to go to (`/v/3` → `/v/3/`).
|
|
618
|
+
*/
|
|
619
|
+
function accessAtPath(
|
|
620
|
+
request: Request,
|
|
621
|
+
label: string,
|
|
622
|
+
url: URL,
|
|
623
|
+
): {
|
|
624
|
+
readonly access: Access;
|
|
625
|
+
readonly at?: FilesAt;
|
|
626
|
+
readonly redirect?: string;
|
|
627
|
+
} | null {
|
|
628
|
+
const { pathname } = url;
|
|
629
|
+
let best: { rank: number; access: Access; at?: FilesAt; redirect?: string } | null = null;
|
|
563
630
|
for (const [name, value] of cookiesOf(request)) {
|
|
564
631
|
if (!ourCookie(name)) {
|
|
565
632
|
continue;
|
|
566
633
|
}
|
|
567
634
|
const session = keys.verify(value);
|
|
635
|
+
if (!isRecord(session)) {
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
const base = typeof session.b === "string" && BASE.test(session.b) ? session.b : null;
|
|
639
|
+
const under = base !== null && pathname.startsWith(base);
|
|
640
|
+
const atBase = base !== null && `${pathname}/` === base;
|
|
568
641
|
if (
|
|
569
|
-
|
|
570
|
-
session.p !== pathname ||
|
|
642
|
+
(session.p !== pathname && !under && !atBase) ||
|
|
571
643
|
typeof session.w !== "string" ||
|
|
572
644
|
typeof session.x !== "number" ||
|
|
573
645
|
session.x <= Date.now() ||
|
|
@@ -584,14 +656,39 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
584
656
|
} catch {
|
|
585
657
|
continue;
|
|
586
658
|
}
|
|
587
|
-
|
|
659
|
+
// The most specific first: the path entered itself, then the longest base, then the
|
|
660
|
+
// latest to end.
|
|
661
|
+
const rank = session.p === pathname ? Number.MAX_SAFE_INTEGER : (base?.length ?? 0);
|
|
662
|
+
if (
|
|
663
|
+
best === null ||
|
|
664
|
+
rank > best.rank ||
|
|
665
|
+
(rank === best.rank && session.x > best.access.expiresAt)
|
|
666
|
+
) {
|
|
667
|
+
const origin = originOfLabel(label);
|
|
588
668
|
best = {
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
669
|
+
rank,
|
|
670
|
+
access: {
|
|
671
|
+
instance,
|
|
672
|
+
viewer: session.w,
|
|
673
|
+
origin,
|
|
674
|
+
expiresAt: session.x,
|
|
675
|
+
...(session.a === 1 ? { anonymous: true } : {}),
|
|
676
|
+
...(typeof session.s === "string" ? { subject: session.s } : {}),
|
|
677
|
+
},
|
|
678
|
+
...(base !== null && under
|
|
679
|
+
? {
|
|
680
|
+
at: {
|
|
681
|
+
base,
|
|
682
|
+
rest: pathname.slice(base.length),
|
|
683
|
+
search: url.search,
|
|
684
|
+
pinned: session.k === 1,
|
|
685
|
+
source: new URL(base, origin).href,
|
|
686
|
+
},
|
|
687
|
+
}
|
|
688
|
+
: {}),
|
|
689
|
+
...(base !== null && atBase && session.p !== pathname
|
|
690
|
+
? { redirect: `${origin}${base}${url.search}` }
|
|
691
|
+
: {}),
|
|
595
692
|
};
|
|
596
693
|
}
|
|
597
694
|
}
|
|
@@ -626,6 +723,123 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
626
723
|
});
|
|
627
724
|
}
|
|
628
725
|
|
|
726
|
+
/**
|
|
727
|
+
* A file set's file in a frame (P083): the sealed access in its path opens it, once it holds
|
|
728
|
+
* for this origin and the host's check lets it in; every path under it is the same version's.
|
|
729
|
+
*/
|
|
730
|
+
/** A files' address for an access (P083): sealed, its own end within the session's. */
|
|
731
|
+
async function sealFiles(
|
|
732
|
+
access: Access,
|
|
733
|
+
): Promise<{ readonly base: string; readonly expires: number }> {
|
|
734
|
+
const expires = Math.min(access.expiresAt, Date.now() + FILES_ADDRESS_MS);
|
|
735
|
+
const token = await keys.seal({
|
|
736
|
+
i: access.instance,
|
|
737
|
+
w: access.viewer,
|
|
738
|
+
x: expires,
|
|
739
|
+
e: access.expiresAt,
|
|
740
|
+
...(access.anonymous === true ? { a: 1 } : {}),
|
|
741
|
+
...(access.subject === undefined ? {} : { s: access.subject }),
|
|
742
|
+
});
|
|
743
|
+
return { base: `${FILES_PREFIX}${token}/`, expires };
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* A file set's file in a frame (P083): the sealed access in its path opens it, once it holds
|
|
748
|
+
* for this origin and the host's check lets it in; every path under it is the same version's.
|
|
749
|
+
* The address opens on its own for an hour (its `x`); after that, until the session's end (its
|
|
750
|
+
* `e`), only the running page's own loads (same-origin, not a navigation), from addresses it
|
|
751
|
+
* already holds, while its shell renews the address (`__porthole/renew`).
|
|
752
|
+
*/
|
|
753
|
+
async function serveSealed(
|
|
754
|
+
request: Request,
|
|
755
|
+
url: URL,
|
|
756
|
+
label: string,
|
|
757
|
+
token: string,
|
|
758
|
+
rest: string | undefined,
|
|
759
|
+
): Promise<Response> {
|
|
760
|
+
const method = request.method.toUpperCase();
|
|
761
|
+
const origin = originOfLabel(label);
|
|
762
|
+
const base = `${FILES_PREFIX}${token}/`;
|
|
763
|
+
if (rest === undefined) {
|
|
764
|
+
return new Response(null, {
|
|
765
|
+
status: 307,
|
|
766
|
+
headers: { location: `${origin}${base}${url.search}`, "cache-control": "no-store" },
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
const session = await keys.unseal(token);
|
|
770
|
+
const now = Date.now();
|
|
771
|
+
let instance: InstanceRef | null = null;
|
|
772
|
+
let end = 0;
|
|
773
|
+
if (
|
|
774
|
+
isRecord(session) &&
|
|
775
|
+
typeof session.w === "string" &&
|
|
776
|
+
typeof session.x === "number" &&
|
|
777
|
+
typeof session.e === "number" &&
|
|
778
|
+
(session.s === undefined || typeof session.s === "string")
|
|
779
|
+
) {
|
|
780
|
+
end = session.e;
|
|
781
|
+
const open = session.x > now || (session.e > now && ownLoad(request));
|
|
782
|
+
try {
|
|
783
|
+
const clean = cleanInstance(session.i);
|
|
784
|
+
instance =
|
|
785
|
+
open && clean.document.kind === "artifact" && nameOf(clean.scope) === label
|
|
786
|
+
? clean
|
|
787
|
+
: null;
|
|
788
|
+
} catch {
|
|
789
|
+
instance = null;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
if (instance === null || !isRecord(session)) {
|
|
793
|
+
return page(
|
|
794
|
+
401,
|
|
795
|
+
"This has ended",
|
|
796
|
+
"Content here opens for a while. Open it again from the app that showed it.",
|
|
797
|
+
method,
|
|
798
|
+
"session",
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
const access: Access = {
|
|
802
|
+
instance,
|
|
803
|
+
viewer: session.w as string,
|
|
804
|
+
origin,
|
|
805
|
+
expiresAt: end,
|
|
806
|
+
...(session.a === 1 ? { anonymous: true } : {}),
|
|
807
|
+
...(typeof session.s === "string" ? { subject: session.s } : {}),
|
|
808
|
+
};
|
|
809
|
+
if (rest === RENEW_PATH) {
|
|
810
|
+
// Only the page itself renews its address, while its session lasts and the host lets it.
|
|
811
|
+
if (method !== "GET" || !ownLoad(request)) {
|
|
812
|
+
return page(403, "Not allowed", "Only the page renews its own address.", method);
|
|
813
|
+
}
|
|
814
|
+
const refusal = await denied(request, access);
|
|
815
|
+
if (refusal !== null) {
|
|
816
|
+
return refusal;
|
|
817
|
+
}
|
|
818
|
+
const renewed = await sealFiles(access);
|
|
819
|
+
return Response.json(
|
|
820
|
+
{ base: `${origin}${renewed.base}`, expires: renewed.expires },
|
|
821
|
+
{
|
|
822
|
+
headers: {
|
|
823
|
+
"cache-control": "no-store",
|
|
824
|
+
"x-content-type-options": "nosniff",
|
|
825
|
+
"cross-origin-resource-policy": "same-origin",
|
|
826
|
+
"content-security-policy": `${INERT_FILE_CSP}; frame-ancestors ${frameAncestors}`,
|
|
827
|
+
"referrer-policy": "no-referrer",
|
|
828
|
+
},
|
|
829
|
+
},
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
return serveAccess(request, access, null, {
|
|
833
|
+
base,
|
|
834
|
+
rest: rest.slice(1),
|
|
835
|
+
search: url.search,
|
|
836
|
+
pinned: true,
|
|
837
|
+
// Every frame's files on this origin share the prefix (the scope's, P082): a page knows
|
|
838
|
+
// only its own access.
|
|
839
|
+
source: `${origin}${FILES_PREFIX}`,
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
|
|
629
843
|
async function serveDocument(
|
|
630
844
|
request: Request,
|
|
631
845
|
url: URL,
|
|
@@ -669,11 +883,15 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
669
883
|
return serveAccess(request, access, cookie);
|
|
670
884
|
}
|
|
671
885
|
|
|
672
|
-
/**
|
|
886
|
+
/**
|
|
887
|
+
* The document an access opens, once the host's check lets it in: at its document's path, or
|
|
888
|
+
* (`at`) a path under a file set's base (P081).
|
|
889
|
+
*/
|
|
673
890
|
async function serveAccess(
|
|
674
891
|
request: Request,
|
|
675
892
|
access: Access,
|
|
676
893
|
cookie: string | null,
|
|
894
|
+
at?: FilesAt,
|
|
677
895
|
): Promise<Response> {
|
|
678
896
|
const method = request.method.toUpperCase();
|
|
679
897
|
const refusal = await denied(request, access);
|
|
@@ -684,6 +902,45 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
684
902
|
if (served === null) {
|
|
685
903
|
return page(404, "Not found", "This page, or this version of it, doesn't exist.", method);
|
|
686
904
|
}
|
|
905
|
+
if ("files" in served) {
|
|
906
|
+
if (at !== undefined) {
|
|
907
|
+
return serveFile({
|
|
908
|
+
request,
|
|
909
|
+
set: served.files,
|
|
910
|
+
rest: at.rest,
|
|
911
|
+
search: at.search,
|
|
912
|
+
base: at.base,
|
|
913
|
+
origin: access.origin,
|
|
914
|
+
pinned: at.pinned,
|
|
915
|
+
source: at.source,
|
|
916
|
+
compiled: served.compiled,
|
|
917
|
+
hostOrigins: options.hostOrigins,
|
|
918
|
+
frameAncestors,
|
|
919
|
+
});
|
|
920
|
+
}
|
|
921
|
+
// A frame's document (a ticket's): its files carry its access in their path (P083). The
|
|
922
|
+
// entry comes in the ticket's own answer (P025), its shell moving the frame to its address
|
|
923
|
+
// there before anything loads, and renewing it before it ends.
|
|
924
|
+
const sealed = await sealFiles(access);
|
|
925
|
+
return serveFile({
|
|
926
|
+
request,
|
|
927
|
+
set: served.files,
|
|
928
|
+
rest: "",
|
|
929
|
+
search: "",
|
|
930
|
+
base: sealed.base,
|
|
931
|
+
origin: access.origin,
|
|
932
|
+
pinned: true,
|
|
933
|
+
source: `${access.origin}${FILES_PREFIX}`,
|
|
934
|
+
compiled: served.compiled,
|
|
935
|
+
hostOrigins: options.hostOrigins,
|
|
936
|
+
frameAncestors,
|
|
937
|
+
ticket: { expires: sealed.expires },
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
// One page at a file set's base (P081): its base is the page, nothing under it is.
|
|
941
|
+
if (at !== undefined && at.rest !== "") {
|
|
942
|
+
return page(404, "Not found", "This page, or this version of it, doesn't exist.", method);
|
|
943
|
+
}
|
|
687
944
|
const headers = documentHeaders(served.csp);
|
|
688
945
|
if (cookie !== null) {
|
|
689
946
|
headers.set("set-cookie", cookie);
|
|
@@ -707,6 +964,22 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
707
964
|
) {
|
|
708
965
|
return page(404, "Nothing here", "This address names no content.", method);
|
|
709
966
|
}
|
|
967
|
+
// Only a page opening is sent to sign in (a browser's navigation; a client that sends no fetch
|
|
968
|
+
// metadata may be one): a browser's subresource without a session is refused here, with no
|
|
969
|
+
// state of its own (no nonce cookie, nothing cached).
|
|
970
|
+
const mode = request.headers.get("sec-fetch-mode");
|
|
971
|
+
const dest = request.headers.get("sec-fetch-dest") ?? "";
|
|
972
|
+
if (mode !== null && mode !== "navigate" && !["document", "iframe", "frame"].includes(dest)) {
|
|
973
|
+
return new Response(method === "HEAD" ? null : "Open this address in a browser.", {
|
|
974
|
+
status: 401,
|
|
975
|
+
headers: {
|
|
976
|
+
"content-type": "text/plain; charset=utf-8",
|
|
977
|
+
"cache-control": "no-store",
|
|
978
|
+
"x-content-type-options": "nosniff",
|
|
979
|
+
"x-robots-tag": "noindex, nofollow",
|
|
980
|
+
},
|
|
981
|
+
});
|
|
982
|
+
}
|
|
710
983
|
if (method !== "GET" && method !== "HEAD") {
|
|
711
984
|
return page(
|
|
712
985
|
401,
|
|
@@ -780,7 +1053,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
780
1053
|
"session",
|
|
781
1054
|
);
|
|
782
1055
|
}
|
|
783
|
-
const { nonce, path } = record.entry;
|
|
1056
|
+
const { nonce, path, base, pinned } = record.entry;
|
|
784
1057
|
const named = nonceCookie(nonce);
|
|
785
1058
|
if (!cookiesOf(request).some(([name, value]) => name === named && value === "1")) {
|
|
786
1059
|
return page(
|
|
@@ -812,9 +1085,19 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
812
1085
|
"referrer-policy": "no-referrer",
|
|
813
1086
|
"x-robots-tag": "noindex, nofollow",
|
|
814
1087
|
});
|
|
1088
|
+
// A file set's address (P081): the session serves every path under its base, the version's
|
|
1089
|
+
// own address. Else the path it was entered at, alone (P068).
|
|
815
1090
|
headers.append(
|
|
816
1091
|
"set-cookie",
|
|
817
|
-
sessionCookie(
|
|
1092
|
+
sessionCookie(
|
|
1093
|
+
request,
|
|
1094
|
+
record,
|
|
1095
|
+
external
|
|
1096
|
+
? {}
|
|
1097
|
+
: base !== undefined
|
|
1098
|
+
? { b: base, ...(pinned === true ? { k: 1 as const } : {}) }
|
|
1099
|
+
: { p: target.pathname },
|
|
1100
|
+
),
|
|
818
1101
|
);
|
|
819
1102
|
headers.append(
|
|
820
1103
|
"set-cookie",
|
|
@@ -982,6 +1265,9 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
982
1265
|
...(anonymous ? { a: 1 } : {}),
|
|
983
1266
|
...(grant.subject === undefined ? {} : { s: grant.subject }),
|
|
984
1267
|
...(entry === undefined ? {} : { p: entry.path }),
|
|
1268
|
+
...(entry?.base === undefined
|
|
1269
|
+
? {}
|
|
1270
|
+
: { b: entry.base, ...(entry.pinned === true ? { k: 1 } : {}) }),
|
|
985
1271
|
});
|
|
986
1272
|
if (cookieName(key).length + probe.length + 120 > MAX_COOKIE_BYTES) {
|
|
987
1273
|
throw new TypeError("This instance is too large to carry in a cookie (fewer origins?)");
|
|
@@ -1104,7 +1390,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1104
1390
|
};
|
|
1105
1391
|
},
|
|
1106
1392
|
|
|
1107
|
-
async enterBy(state
|
|
1393
|
+
async enterBy(state, instance, grant, address): Promise<string> {
|
|
1108
1394
|
if (state.expired) {
|
|
1109
1395
|
throw new TypeError("This entry's state has expired: start again from the clean address");
|
|
1110
1396
|
}
|
|
@@ -1112,12 +1398,45 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1112
1398
|
if (nameOf(clean.scope) !== state.label) {
|
|
1113
1399
|
throw new TypeError("The instance isn't served on the address the entry began at");
|
|
1114
1400
|
}
|
|
1401
|
+
let path = state.path;
|
|
1402
|
+
if (address !== undefined) {
|
|
1403
|
+
// A file set's own address (P081): a path ending in /, not Porthole's own. An address
|
|
1404
|
+
// that isn't under it (the latest's) goes to the same file under it, which must stay
|
|
1405
|
+
// under it: no dot segment, no backslash, nothing the URL parser would resolve away.
|
|
1406
|
+
const { base, file = "" } = address;
|
|
1407
|
+
const segments = typeof file === "string" ? file.split("/") : [];
|
|
1408
|
+
if (
|
|
1409
|
+
typeof base !== "string" ||
|
|
1410
|
+
!BASE.test(base) ||
|
|
1411
|
+
base.startsWith(PREFIX) ||
|
|
1412
|
+
typeof file !== "string" ||
|
|
1413
|
+
file.length > MAX_PATH ||
|
|
1414
|
+
file.startsWith("/") ||
|
|
1415
|
+
// oxlint-disable-next-line no-control-regex -- control characters are what it refuses
|
|
1416
|
+
/[?#\\\u0000-\u001f\u007f]/u.test(file) ||
|
|
1417
|
+
segments.some((each) => /^(?:\.|%2e){1,2}$/iu.test(each))
|
|
1418
|
+
) {
|
|
1419
|
+
throw new TypeError(
|
|
1420
|
+
"An entry's base is a path ending in / (not Porthole's own), its file a relative path below it",
|
|
1421
|
+
);
|
|
1422
|
+
}
|
|
1423
|
+
const asked = new URL(state.path, "https://x.invalid");
|
|
1424
|
+
if (!asked.pathname.startsWith(base)) {
|
|
1425
|
+
path = `${base}${file}${asked.search}`;
|
|
1426
|
+
}
|
|
1427
|
+
if (!new URL(path, "https://x.invalid").pathname.startsWith(base)) {
|
|
1428
|
+
throw new TypeError("An entry's file must stay below its base");
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1115
1431
|
const { ticket } = await mint(
|
|
1116
1432
|
clean,
|
|
1117
1433
|
{ ...grant, ttlSeconds: ENTRY_CODE_SECONDS },
|
|
1118
1434
|
{
|
|
1119
|
-
path
|
|
1435
|
+
path,
|
|
1120
1436
|
nonce: state.nonce,
|
|
1437
|
+
...(address === undefined
|
|
1438
|
+
? {}
|
|
1439
|
+
: { base: address.base, ...(address.pinned === true ? { pinned: true } : {}) }),
|
|
1121
1440
|
},
|
|
1122
1441
|
);
|
|
1123
1442
|
return `${originOfLabel(state.label)}${ENTRY_PATH}?code=${ticket}`;
|
|
@@ -1172,14 +1491,24 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1172
1491
|
if (document?.[1] !== undefined) {
|
|
1173
1492
|
return await serveDocument(request, url, label, document[1]);
|
|
1174
1493
|
}
|
|
1494
|
+
const sealed = FILES_PATH.exec(url.pathname);
|
|
1495
|
+
if (sealed?.[1] !== undefined) {
|
|
1496
|
+
return await serveSealed(request, url, label, sealed[1], sealed[2]);
|
|
1497
|
+
}
|
|
1175
1498
|
if (url.pathname.startsWith(PREFIX)) {
|
|
1176
1499
|
return page(404, "Nothing here", "This address names no content.", method);
|
|
1177
1500
|
}
|
|
1178
1501
|
// A clean address (P068): the document entered here, else the app, else entry.
|
|
1179
1502
|
if (authorizeUrl !== null) {
|
|
1180
|
-
const entered = accessAtPath(request, label, url
|
|
1503
|
+
const entered = accessAtPath(request, label, url);
|
|
1504
|
+
if (entered?.redirect !== undefined) {
|
|
1505
|
+
return new Response(null, {
|
|
1506
|
+
status: 307,
|
|
1507
|
+
headers: { location: entered.redirect, "cache-control": "no-store" },
|
|
1508
|
+
});
|
|
1509
|
+
}
|
|
1181
1510
|
if (entered !== null) {
|
|
1182
|
-
return await serveAccess(request, entered, null);
|
|
1511
|
+
return await serveAccess(request, entered.access, null, entered.at);
|
|
1183
1512
|
}
|
|
1184
1513
|
if (options.upstream === undefined || accessOf(request, label, null) === null) {
|
|
1185
1514
|
return await beginEntry(request, url, label, authorizeUrl);
|
package/src/server/types.ts
CHANGED
|
@@ -78,7 +78,14 @@ export interface TicketRecord {
|
|
|
78
78
|
* An entry code's (P068): the clean path it returns to, and the nonce of the browser that asked
|
|
79
79
|
* (its cookie must come with the code). A ticket from `issue` has none.
|
|
80
80
|
*/
|
|
81
|
-
readonly entry?: {
|
|
81
|
+
readonly entry?: {
|
|
82
|
+
readonly path: string;
|
|
83
|
+
readonly nonce: string;
|
|
84
|
+
/** A file set's version's own address (P081): the session serves every path under it. */
|
|
85
|
+
readonly base?: string;
|
|
86
|
+
/** Whether that address names this version and no other: its files are immutable there. */
|
|
87
|
+
readonly pinned?: boolean;
|
|
88
|
+
};
|
|
82
89
|
}
|
|
83
90
|
|
|
84
91
|
/** Where tickets live between `issue` and their one use. Default: in memory (one process). */
|
|
@@ -88,9 +95,46 @@ export interface TicketStore {
|
|
|
88
95
|
take(id: string): Promise<TicketRecord | null>;
|
|
89
96
|
}
|
|
90
97
|
|
|
98
|
+
/** One file of a version (P081), as the host stored it when the version was published. */
|
|
99
|
+
export interface StoredFile {
|
|
100
|
+
/** Its length, bytes. */
|
|
101
|
+
readonly size: number;
|
|
102
|
+
/** The SHA-256 of its bytes, lowercase hex: its strong ETag. */
|
|
103
|
+
readonly sha256: string;
|
|
104
|
+
/**
|
|
105
|
+
* The type it was published with, for the host's own use (a file list): never served. A file
|
|
106
|
+
* is served with the type Porthole's table gives its extension (`fileTypeOf`), never sniffed.
|
|
107
|
+
*/
|
|
108
|
+
readonly type?: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A file's bytes, whole: `/server` cuts the range a request asks for. */
|
|
112
|
+
export type FileBody = Uint8Array | ReadableStream<Uint8Array>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A version made of files (P081): a manifest, and the bytes of each file by its path. Paths
|
|
116
|
+
* follow `checkFileSet`'s rules (check them when the version is published); a request is looked
|
|
117
|
+
* up in `files` only, and `read` is asked only for a path in it.
|
|
118
|
+
*/
|
|
119
|
+
export interface StoredFiles {
|
|
120
|
+
readonly files: Readonly<Record<string, StoredFile>>;
|
|
121
|
+
/** The page the version opens at: one of `files` (default `index.html`). */
|
|
122
|
+
readonly entry?: string;
|
|
123
|
+
/**
|
|
124
|
+
* A single-page app's routes: a navigation whose path finds no file answers the entry, where
|
|
125
|
+
* it would get the nearest `404.html` (default false). Subresources still 404.
|
|
126
|
+
*/
|
|
127
|
+
readonly spa?: boolean;
|
|
128
|
+
/** The bytes of a file in `files`; null if they are gone (a 404). */
|
|
129
|
+
read(path: string): Promise<FileBody | null>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A stored version: one page (`{ html }`, as in 0.5), or a set of files (P081). */
|
|
133
|
+
export type StoredVersion = { readonly html: string } | StoredFiles;
|
|
134
|
+
|
|
91
135
|
/** Stored pages (artifact mode). Versions are immutable: a new page is a new version. */
|
|
92
136
|
export interface ArtifactStore {
|
|
93
|
-
get(id: string, version: string): Promise<
|
|
137
|
+
get(id: string, version: string): Promise<StoredVersion | null>;
|
|
94
138
|
}
|
|
95
139
|
|
|
96
140
|
/** What a request's cookie lets in: one instance, for one viewer, on one origin, until a time. */
|
|
@@ -227,7 +271,15 @@ export interface PortholeServer {
|
|
|
227
271
|
entryState(token: string): EntryState | null;
|
|
228
272
|
/**
|
|
229
273
|
* Once the host decided (its authorize route): a one-time code for `instance`, which must be
|
|
230
|
-
* served on the state's label, and the URL that redeems it there (`/__porthole/entry`).
|
|
274
|
+
* served on the state's label, and the URL that redeems it there (`/__porthole/entry`). With an
|
|
275
|
+
* `address` (a file set's, P081: `entry.at`'s `base` and `file`), the session it buys serves
|
|
276
|
+
* every path under `base`, and the redeem goes to `base` + `file` when the state's path isn't
|
|
277
|
+
* under it (a "latest" address goes to the version's own).
|
|
231
278
|
*/
|
|
232
|
-
enterBy(
|
|
279
|
+
enterBy(
|
|
280
|
+
state: EntryState,
|
|
281
|
+
instance: InstanceRef,
|
|
282
|
+
grant: Grant,
|
|
283
|
+
address?: { readonly base: string; readonly file?: string; readonly pinned?: boolean },
|
|
284
|
+
): Promise<string>;
|
|
233
285
|
}
|