@coralreefventures/site-tools 0.3.0 → 0.4.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 +54 -5
- package/dist/bin/reef-door-bundle.js +19 -1
- package/dist/bin/reef-door-bundle.js.map +1 -1
- package/dist/door/config.d.ts +5 -1
- package/dist/door/config.d.ts.map +1 -1
- package/dist/door/config.js +3 -0
- package/dist/door/config.js.map +1 -1
- package/dist/door/package.d.ts +16 -0
- package/dist/door/package.d.ts.map +1 -1
- package/dist/door/package.js +47 -2
- package/dist/door/package.js.map +1 -1
- package/dist/door/page.d.ts +70 -0
- package/dist/door/page.d.ts.map +1 -0
- package/dist/door/page.js +171 -0
- package/dist/door/page.js.map +1 -0
- package/dist/door/server.d.ts +24 -3
- package/dist/door/server.d.ts.map +1 -1
- package/dist/door/server.js +85 -13
- package/dist/door/server.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/bin/reef-door-bundle.ts +19 -1
- package/src/door/config.ts +6 -1
- package/src/door/package.ts +57 -2
- package/src/door/page.ts +218 -0
- package/src/door/server.ts +92 -12
- package/src/index.ts +10 -0
package/src/door/page.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The coming-soon page: what a locked site answers a page load with when the visitor holds no session. It is the
|
|
5
|
+
* gate's own HTML and carries nothing of the site: one inline stylesheet, the mark as a data URI, no script and no
|
|
6
|
+
* request to anywhere. It is the same bytes for every path but one, the canonical next its sign-in link carries.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** An image the page shows, carried in gate.config.json as base64 so the page needs no request for it. */
|
|
10
|
+
export type PageImage = { type: "image/svg+xml" | "image/png"; data: string };
|
|
11
|
+
|
|
12
|
+
/** gate.config.json's `page`: everything a site says about its coming-soon page. All optional. */
|
|
13
|
+
export type PageConfigFile = {
|
|
14
|
+
/** The product's display name, as the page writes it. The site id, capitalized, by default. */
|
|
15
|
+
name?: string;
|
|
16
|
+
/** The mark, shown beside the name. None by default, and the name stands alone. */
|
|
17
|
+
mark?: PageImage;
|
|
18
|
+
/** The mark for a dark scheme. Without it the dark scheme shows `mark` on a light tile. */
|
|
19
|
+
markDark?: PageImage;
|
|
20
|
+
/** The button's color in the light scheme, `#rrggbb`. */
|
|
21
|
+
accent?: string;
|
|
22
|
+
/** The button's color in the dark scheme, `#rrggbb`. */
|
|
23
|
+
accentDark?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type PageConfig = Required<Pick<PageConfigFile, "name" | "accent" | "accentDark">> &
|
|
27
|
+
Pick<PageConfigFile, "mark" | "markDark">;
|
|
28
|
+
|
|
29
|
+
/** The page's own surfaces; a site's accent has to stand out from these. */
|
|
30
|
+
export const surfaces = {
|
|
31
|
+
light: { background: "#ffffff", text: "#1a1f24", muted: "#4b5560" },
|
|
32
|
+
dark: { background: "#11161b", text: "#e6e9ec", muted: "#a9b3bc" },
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const defaultAccent = "#1f2933";
|
|
36
|
+
export const defaultAccentDark = "#d5dbe1";
|
|
37
|
+
/** A mark larger than this is not a mark; the page is answered to every visitor without a session. */
|
|
38
|
+
export const maxMarkBytes = 64 * 1024;
|
|
39
|
+
|
|
40
|
+
const hexColor = /^#[0-9a-f]{6}$/i;
|
|
41
|
+
const base64 = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
42
|
+
|
|
43
|
+
/** WCAG 2's relative luminance of `#rrggbb`. */
|
|
44
|
+
export const luminance = (hex: string): number => {
|
|
45
|
+
const channel = (offset: number) => {
|
|
46
|
+
const value = Number.parseInt(hex.slice(offset, offset + 2), 16) / 255;
|
|
47
|
+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
48
|
+
};
|
|
49
|
+
return 0.2126 * channel(1) + 0.7152 * channel(3) + 0.0722 * channel(5);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** WCAG 2's contrast ratio between two `#rrggbb` colors, 1 to 21. */
|
|
53
|
+
export const contrast = (a: string, b: string): number => {
|
|
54
|
+
const [high, low] = [luminance(a), luminance(b)].sort((x, y) => y - x) as [number, number];
|
|
55
|
+
return (high + 0.05) / (low + 0.05);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The button's text on an accent: white or black, whichever reads better. One of the two always reaches 4.5:1 (the
|
|
60
|
+
* worst accent, where they tie, gives 4.58), so the label meets AA whatever color a site picks. Not a softer near-black:
|
|
61
|
+
* #111111 ties at 4.35, under the line.
|
|
62
|
+
*/
|
|
63
|
+
export const textOn = (accent: string): string =>
|
|
64
|
+
contrast(accent, "#ffffff") >= contrast(accent, "#000000") ? "#ffffff" : "#000000";
|
|
65
|
+
|
|
66
|
+
/** The site id as a display name: `driftline` becomes `Driftline`. */
|
|
67
|
+
export const defaultName = (site: string): string => site.charAt(0).toUpperCase() + site.slice(1);
|
|
68
|
+
|
|
69
|
+
const checkImage = (value: unknown, label: string): PageImage | undefined => {
|
|
70
|
+
if (value === undefined) return undefined;
|
|
71
|
+
const image = value as Partial<PageImage> | null;
|
|
72
|
+
if (image?.type !== "image/svg+xml" && image?.type !== "image/png") {
|
|
73
|
+
throw new Error(`the page's ${label} must be an SVG or a PNG`);
|
|
74
|
+
}
|
|
75
|
+
if (typeof image.data !== "string" || !base64.test(image.data)) {
|
|
76
|
+
throw new Error(`the page's ${label} is not base64`);
|
|
77
|
+
}
|
|
78
|
+
if (Buffer.byteLength(image.data, "base64") > maxMarkBytes) {
|
|
79
|
+
throw new Error(`the page's ${label} is over ${maxMarkBytes} bytes`);
|
|
80
|
+
}
|
|
81
|
+
return { type: image.type, data: image.data };
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const checkColor = (value: unknown, fallback: string, label: string, surface: string): string => {
|
|
85
|
+
if (value === undefined) return fallback;
|
|
86
|
+
if (typeof value !== "string" || !hexColor.test(value))
|
|
87
|
+
throw new Error(`the page's ${label} is not #rrggbb: ${value}`);
|
|
88
|
+
// WCAG 1.4.11: the button's edge and the focus ring are drawn in the accent, so it must reach 3:1 on the page.
|
|
89
|
+
const ratio = contrast(value, surface);
|
|
90
|
+
if (ratio < 3) {
|
|
91
|
+
throw new Error(`the page's ${label} ${value} is ${ratio.toFixed(2)}:1 on ${surface}; it needs 3:1`);
|
|
92
|
+
}
|
|
93
|
+
return value.toLowerCase();
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Checks gate.config.json's `page` and fills in the defaults, or throws naming what is wrong. With no `page` at all
|
|
98
|
+
* the result still renders a correct page: the site id as the name, no mark, and a neutral button.
|
|
99
|
+
*/
|
|
100
|
+
export const parsePageConfig = (value: unknown, site: string): PageConfig => {
|
|
101
|
+
if (value !== undefined && (typeof value !== "object" || value === null)) {
|
|
102
|
+
throw new Error("the page configuration is not an object");
|
|
103
|
+
}
|
|
104
|
+
const file = (value ?? {}) as PageConfigFile;
|
|
105
|
+
const name = file.name ?? defaultName(site);
|
|
106
|
+
if (typeof name !== "string" || name.trim() !== name || name.length === 0 || name.length > 60) {
|
|
107
|
+
throw new Error(`the page's name must be 1 to 60 characters with no space at either end: ${JSON.stringify(name)}`);
|
|
108
|
+
}
|
|
109
|
+
if ([...name].some((c) => c <= "\u001f" || c === "\u007f"))
|
|
110
|
+
throw new Error("the page's name has a control character");
|
|
111
|
+
const mark = checkImage(file.mark, "mark");
|
|
112
|
+
const markDark = checkImage(file.markDark, "dark mark");
|
|
113
|
+
if (markDark && !mark) throw new Error("the page has a dark mark but no mark");
|
|
114
|
+
return {
|
|
115
|
+
name,
|
|
116
|
+
accent: checkColor(file.accent, defaultAccent, "accent", surfaces.light.background),
|
|
117
|
+
accentDark: checkColor(file.accentDark, defaultAccentDark, "dark accent", surfaces.dark.background),
|
|
118
|
+
...(mark && { mark }),
|
|
119
|
+
...(markDark && { markDark }),
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const escapeHtml = (value: string) =>
|
|
124
|
+
value
|
|
125
|
+
.replace(/&/g, "&")
|
|
126
|
+
.replace(/</g, "<")
|
|
127
|
+
.replace(/>/g, ">")
|
|
128
|
+
.replace(/"/g, """)
|
|
129
|
+
.replace(/'/g, "'");
|
|
130
|
+
|
|
131
|
+
const dataUri = (image: PageImage) => `data:${image.type};base64,${image.data}`;
|
|
132
|
+
|
|
133
|
+
/** The stylesheet, which depends only on the configuration, so its hash is fixed for the gate's life. */
|
|
134
|
+
const stylesheet = (page: PageConfig): string => {
|
|
135
|
+
const { light, dark } = surfaces;
|
|
136
|
+
// The dark scheme shows the dark mark if there is one; otherwise the one mark, on a light tile so a dark-ink mark
|
|
137
|
+
// stays visible.
|
|
138
|
+
const darkMark = page.markDark
|
|
139
|
+
? ".mark-light{display:none}.mark-dark{display:block}"
|
|
140
|
+
: `.mark-light{background:${light.background};border-radius:10px;padding:4px}`;
|
|
141
|
+
return [
|
|
142
|
+
":root{color-scheme:light dark;",
|
|
143
|
+
`--bg:${light.background};--text:${light.text};--muted:${light.muted};`,
|
|
144
|
+
`--accent:${page.accent};--on-accent:${textOn(page.accent)}}`,
|
|
145
|
+
"@media (prefers-color-scheme:dark){:root{",
|
|
146
|
+
`--bg:${dark.background};--text:${dark.text};--muted:${dark.muted};`,
|
|
147
|
+
`--accent:${page.accentDark};--on-accent:${textOn(page.accentDark)}}${darkMark}}`,
|
|
148
|
+
"*{box-sizing:border-box}",
|
|
149
|
+
"html{-webkit-text-size-adjust:100%;text-size-adjust:100%}",
|
|
150
|
+
"body{margin:0;min-height:100vh;min-height:100dvh;display:flex;align-items:center;justify-content:center;",
|
|
151
|
+
"padding:48px 24px;background:var(--bg);color:var(--text);",
|
|
152
|
+
'font:400 1.0625rem/1.55 system-ui,-apple-system,"Segoe UI",Roboto,"Noto Sans",sans-serif}',
|
|
153
|
+
"main{width:100%;max-width:34rem}",
|
|
154
|
+
".lockup{display:flex;align-items:center;gap:12px;margin:0 0 40px;font-weight:700;font-size:1.25rem;",
|
|
155
|
+
"letter-spacing:-0.01em}",
|
|
156
|
+
".lockup img{display:block;height:48px;width:auto;max-width:96px}",
|
|
157
|
+
".mark-dark{display:none}",
|
|
158
|
+
"h1{margin:0 0 12px;font-size:clamp(1.75rem,6vw,2.5rem);line-height:1.15;letter-spacing:-0.02em;",
|
|
159
|
+
"font-weight:700;overflow-wrap:anywhere}",
|
|
160
|
+
".lede{margin:0 0 32px;color:var(--muted)}",
|
|
161
|
+
".actions{display:flex;flex-wrap:wrap;align-items:center;gap:16px 24px}",
|
|
162
|
+
".button{display:inline-flex;align-items:center;min-height:48px;padding:12px 20px;border-radius:8px;",
|
|
163
|
+
"background:var(--accent);color:var(--on-accent);font-weight:600;text-decoration:none;",
|
|
164
|
+
"border:2px solid var(--accent)}",
|
|
165
|
+
".button:hover{text-decoration:underline;text-underline-offset:3px}",
|
|
166
|
+
".link{color:var(--text);font-weight:600;text-decoration:underline;text-decoration-color:var(--accent);",
|
|
167
|
+
"text-decoration-thickness:2px;text-underline-offset:4px;padding:12px 0}",
|
|
168
|
+
".link:hover{text-decoration-thickness:3px}",
|
|
169
|
+
"a:focus-visible{outline:3px solid var(--accent);outline-offset:3px}",
|
|
170
|
+
"@media (max-width:480px){body{padding:32px 16px;align-items:flex-start}.lockup{margin-bottom:32px}}",
|
|
171
|
+
].join("");
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
export type ComingSoon = {
|
|
175
|
+
/** The page for one canonical next: the only part that differs between paths. */
|
|
176
|
+
html: (next: string) => string;
|
|
177
|
+
/** The Content-Security-Policy that allows exactly what the page uses. */
|
|
178
|
+
csp: string;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Builds the coming-soon page for a site once, as the gate starts. `door` is the door's origin, which the "Get
|
|
183
|
+
* involved" link points into; `site` is the id the door knows the site by.
|
|
184
|
+
*/
|
|
185
|
+
export const comingSoon = (page: PageConfig, door: string, site: string): ComingSoon => {
|
|
186
|
+
const css = stylesheet(page);
|
|
187
|
+
const styleHash = createHash("sha256").update(css).digest("base64");
|
|
188
|
+
const name = escapeHtml(page.name);
|
|
189
|
+
const getInvolved = escapeHtml(`${door}/get-involved/?${new URLSearchParams({ site })}`);
|
|
190
|
+
const marks = page.mark
|
|
191
|
+
? `<img class="mark-light" src="${dataUri(page.mark)}" alt="">` +
|
|
192
|
+
(page.markDark ? `<img class="mark-dark" src="${dataUri(page.markDark)}" alt="">` : "")
|
|
193
|
+
: "";
|
|
194
|
+
const csp = [
|
|
195
|
+
"default-src 'none'",
|
|
196
|
+
`style-src 'sha256-${styleHash}'`,
|
|
197
|
+
...(page.mark ? ["img-src data:"] : []),
|
|
198
|
+
"base-uri 'none'",
|
|
199
|
+
"form-action 'none'",
|
|
200
|
+
"frame-ancestors 'none'",
|
|
201
|
+
].join("; ");
|
|
202
|
+
const head =
|
|
203
|
+
'<!doctype html><html lang="en"><head><meta charset="utf-8">' +
|
|
204
|
+
'<meta name="viewport" content="width=device-width, initial-scale=1">' +
|
|
205
|
+
'<meta name="color-scheme" content="light dark">' +
|
|
206
|
+
'<meta name="robots" content="noindex, nofollow">' +
|
|
207
|
+
'<meta name="referrer" content="strict-origin-when-cross-origin">' +
|
|
208
|
+
`<title>${name} is coming soon</title><style>${css}</style></head>`;
|
|
209
|
+
const before =
|
|
210
|
+
`<body><main><p class="lockup">${marks}<span>${name}</span></p>` +
|
|
211
|
+
`<h1>${name} is coming soon.</h1><p class="lede">Open for now to invited guests.</p>` +
|
|
212
|
+
'<p class="actions"><a class="button" href="';
|
|
213
|
+
const after = `">Have an invitation? Sign in</a><a class="link" href="${getInvolved}">Get involved</a></p></main></body></html>`;
|
|
214
|
+
return {
|
|
215
|
+
html: (next) => `${head}${before}${escapeHtml(`/_door/signin?${new URLSearchParams({ next })}`)}${after}`,
|
|
216
|
+
csp,
|
|
217
|
+
};
|
|
218
|
+
};
|
package/src/door/server.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { fileURLToPath } from "node:url";
|
|
|
5
5
|
import { sendFile, staticTarget } from "../serve.ts";
|
|
6
6
|
import { type GateConfig, readGateConfig } from "./config.ts";
|
|
7
7
|
import { canonicalNext, unsafe } from "./next.ts";
|
|
8
|
+
import { comingSoon } from "./page.ts";
|
|
8
9
|
import { verifyTicket } from "./verify.ts";
|
|
9
10
|
|
|
10
11
|
// The gate is the one thing in a locked site's compute bundle, and the bundle carries no node_modules, so this file
|
|
@@ -15,6 +16,13 @@ export const sessionCookie = "__Host-crv_door";
|
|
|
15
16
|
/** The nonce the gate sets before it sends a visitor to the door, which the ticket must name as `st`. */
|
|
16
17
|
export const stateCookie = "__Host-crv_door_state";
|
|
17
18
|
export const stateLifetimeSeconds = 600;
|
|
19
|
+
/**
|
|
20
|
+
* A marker that this browser has held a session here, so that once the hour is up a page load bounces silently through
|
|
21
|
+
* the door (which issues a fresh ticket while its own sign-in lives) instead of meeting the coming-soon page. It holds
|
|
22
|
+
* no identity and opens nothing: without a valid session it changes only which way a page load is sent to sign in.
|
|
23
|
+
*/
|
|
24
|
+
export const seenCookie = "__Host-crv_door_seen";
|
|
25
|
+
export const seenLifetimeSeconds = 30 * 24 * 60 * 60;
|
|
18
26
|
|
|
19
27
|
const maxBodyBytes = 16 * 1024;
|
|
20
28
|
const encodedUnsafe = /%(5c|[01][0-9a-f]|7f)/i;
|
|
@@ -32,6 +40,8 @@ const cookie = (name: string, value: string, maxAge: number, sameSite: "Lax" | "
|
|
|
32
40
|
`${name}=${value}; Max-Age=${maxAge}; Path=/; Secure; HttpOnly; SameSite=${sameSite}`;
|
|
33
41
|
const clearSession = cookie(sessionCookie, "", 0, "Lax");
|
|
34
42
|
const clearState = cookie(stateCookie, "", 0, "None");
|
|
43
|
+
const markSeen = cookie(seenCookie, "1", seenLifetimeSeconds, "Lax");
|
|
44
|
+
const clearSeen = cookie(seenCookie, "", 0, "Lax");
|
|
35
45
|
|
|
36
46
|
/** One value of a cookie, or undefined when it is absent or sent twice (which a `__Host-` cookie never is). */
|
|
37
47
|
export const readCookie = (header: string | undefined, name: string): string | undefined => {
|
|
@@ -71,6 +81,15 @@ export const splitTarget = (target: string): { rawPath: string; rawQuery: string
|
|
|
71
81
|
/** GET, HEAD and POST; the gate answers anything else with 405. */
|
|
72
82
|
const isGatedMethod = (method: string | undefined) => method === "GET" || method === "HEAD" || method === "POST";
|
|
73
83
|
|
|
84
|
+
/**
|
|
85
|
+
* The challenge every 401 carries, as RFC 9110 requires of one. No registered scheme describes a session cookie from a
|
|
86
|
+
* sign-in page, so this is the shape of the Cookie scheme drafted for exactly that (draft-broyer-http-cookie-auth):
|
|
87
|
+
* where the sign-in starts, and the cookie it ends in. Browsers prompt only for the schemes they implement (Basic,
|
|
88
|
+
* Digest, Negotiate, NTLM), so for this one they render the body, which for a page load is the coming-soon page.
|
|
89
|
+
*/
|
|
90
|
+
export const challenge = (site: string) =>
|
|
91
|
+
`Cookie realm="${site}", form-action="/_door/signin", cookie-name="${sessionCookie}"`;
|
|
92
|
+
|
|
74
93
|
/** The paths the gate answers itself, with or without a session: robots.txt and its own /_door/ endpoints. */
|
|
75
94
|
const isOwnPath = (path: string) => path === "/robots.txt" || path.startsWith("/_door/");
|
|
76
95
|
|
|
@@ -82,6 +101,16 @@ export const isDocumentRequest = (request: IncomingMessage): boolean => {
|
|
|
82
101
|
return String(request.headers.accept ?? "").includes("text/html");
|
|
83
102
|
};
|
|
84
103
|
|
|
104
|
+
/**
|
|
105
|
+
* A page load in the window itself: a document request whose `Sec-Fetch-Dest`, when sent, is `document`. Only this
|
|
106
|
+
* may start a sign-in, so an image, a frame or a fetch from another site cannot overwrite a visitor's state cookie.
|
|
107
|
+
*/
|
|
108
|
+
export const isTopLevelNavigation = (request: IncomingMessage): boolean => {
|
|
109
|
+
if (!isDocumentRequest(request)) return false;
|
|
110
|
+
const dest = request.headers["sec-fetch-dest"];
|
|
111
|
+
return dest === undefined || dest === "document";
|
|
112
|
+
};
|
|
113
|
+
|
|
85
114
|
/**
|
|
86
115
|
* The form body, or null once it passes 16 KB. The rest of an oversized body is read and thrown away rather than the
|
|
87
116
|
* socket destroyed, so the visitor still gets the redirect back to the door; that response closes the connection.
|
|
@@ -124,9 +153,11 @@ export type GateOptions = {
|
|
|
124
153
|
* The gate: every request to a locked site, in this order. (1) A path with a backslash, a control character or a dot
|
|
125
154
|
* segment, raw or encoded, is a 400; a method other than GET, HEAD or POST is a 405. (2) A host not on the list is a
|
|
126
155
|
* 403. (3) robots.txt disallows everything, and /_door/health answers ok. (4) POST /_door verifies a ticket from the
|
|
127
|
-
* door and sets the session. (5) /_door/signout clears
|
|
128
|
-
* session is served from the export. (7) Without one, a page load
|
|
129
|
-
*
|
|
156
|
+
* door and sets the session; /_door/signin starts a sign-in at the door. (5) /_door/signout clears the door's cookies and walks
|
|
157
|
+
* a fixed map. (6) A request with a valid session is served from the export. (7) Without one, a page load gets the
|
|
158
|
+
* coming-soon page, a 401 whose sign-in link carries the canonical next (or, from a browser that has had a session
|
|
159
|
+
* here, a silent bounce through the door), and anything else gets an empty 401. (8) An
|
|
160
|
+
* exception is a 500 with nothing in it: the gate fails closed.
|
|
130
161
|
*/
|
|
131
162
|
export const createGate = ({
|
|
132
163
|
config,
|
|
@@ -149,6 +180,8 @@ export const createGate = ({
|
|
|
149
180
|
response.end(request.method === "HEAD" ? undefined : body);
|
|
150
181
|
};
|
|
151
182
|
const doorSignout = `${config.door}/signout/`;
|
|
183
|
+
const page = comingSoon(config.page, config.door, config.site);
|
|
184
|
+
const unauthorized = challenge(config.site);
|
|
152
185
|
|
|
153
186
|
/** Where `then` leads: the next stop's gate, or the door's sign-out page. Never a URL from the query. */
|
|
154
187
|
const signoutTarget = (then: string | null): string => {
|
|
@@ -190,20 +223,60 @@ export const createGate = ({
|
|
|
190
223
|
const claimNext = canonicalNext(verdict.claims.next, host);
|
|
191
224
|
if (formNext === null || claimNext === null || formNext !== claimNext) return toDoorWithError(response);
|
|
192
225
|
const maxAge = verdict.claims.exp - now();
|
|
193
|
-
return redirect(response, 303, claimNext, [
|
|
226
|
+
return redirect(response, 303, claimNext, [
|
|
227
|
+
cookie(sessionCookie, ticket ?? "", maxAge, "Lax"),
|
|
228
|
+
clearState,
|
|
229
|
+
markSeen,
|
|
230
|
+
]);
|
|
194
231
|
};
|
|
195
232
|
|
|
196
|
-
/**
|
|
197
|
-
|
|
198
|
-
|
|
233
|
+
/**
|
|
234
|
+
* Step 4's sign-in start, which the coming-soon page's button links to: a fresh state cookie and a 302 to the door,
|
|
235
|
+
* naming this site, this host and where to come back to. `next` is taken from the query only after *Canonical next*;
|
|
236
|
+
* one that fails, or is missing or sent twice, becomes `/`. Only a top-level navigation starts one; anything else
|
|
237
|
+
* gets an empty 401 and no cookie.
|
|
238
|
+
*/
|
|
239
|
+
const startSignin = (request: IncomingMessage, response: ServerResponse, rawQuery: string, host: string) => {
|
|
240
|
+
if (!isTopLevelNavigation(request)) return empty(response, 401, { "WWW-Authenticate": unauthorized });
|
|
241
|
+
const asked = new URLSearchParams(rawQuery).getAll("next");
|
|
242
|
+
const next = (asked.length === 1 ? canonicalNext(asked[0], host) : null) ?? "/";
|
|
243
|
+
return toDoor(response, next, host);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/** A fresh state cookie and a 302 to the door, for an already canonical `next`. */
|
|
247
|
+
const toDoor = (response: ServerResponse, next: string, host: string) => {
|
|
199
248
|
const nonce = randomBytes(32).toString("base64url");
|
|
200
|
-
const next = canonicalNext(target, host) ?? "/";
|
|
201
249
|
const query = new URLSearchParams({ site: config.site, host, next, state: nonce });
|
|
202
250
|
return redirect(response, 302, `${config.door}/?${query}`, [
|
|
203
251
|
cookie(stateCookie, nonce, stateLifetimeSeconds, "None"),
|
|
204
252
|
]);
|
|
205
253
|
};
|
|
206
254
|
|
|
255
|
+
/**
|
|
256
|
+
* Step 7: no session. A page load gets the coming-soon page, which is the gate's own and holds none of the site;
|
|
257
|
+
* its sign-in link carries the raw request target after *Canonical next*, or `/`. Anything else is a bare 401.
|
|
258
|
+
* Both are 401, so a request without a session never gets a 2xx with anything of the site's in it, and a probe can
|
|
259
|
+
* tell locked from open by the status alone. A page load from a browser that has held a session here (the seen
|
|
260
|
+
* cookie) is sent straight to the door instead, which renews the session silently while the door's sign-in lives.
|
|
261
|
+
*/
|
|
262
|
+
const refuse = (request: IncomingMessage, response: ServerResponse, target: string, host: string) => {
|
|
263
|
+
if (!isDocumentRequest(request)) return empty(response, 401, { "WWW-Authenticate": unauthorized });
|
|
264
|
+
const next = canonicalNext(target, host) ?? "/";
|
|
265
|
+
if (readCookie(request.headers.cookie, seenCookie) === "1" && isTopLevelNavigation(request)) {
|
|
266
|
+
return toDoor(response, next, host);
|
|
267
|
+
}
|
|
268
|
+
const body = page.html(next);
|
|
269
|
+
response.writeHead(401, {
|
|
270
|
+
...securityHeaders,
|
|
271
|
+
"Cache-Control": "no-store",
|
|
272
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
273
|
+
"Content-Length": String(Buffer.byteLength(body)),
|
|
274
|
+
"Content-Security-Policy": page.csp,
|
|
275
|
+
"WWW-Authenticate": unauthorized,
|
|
276
|
+
});
|
|
277
|
+
response.end(request.method === "HEAD" ? undefined : body);
|
|
278
|
+
};
|
|
279
|
+
|
|
207
280
|
/** Step 6: the export, as Amplify maps it, private to the browser that holds the session. */
|
|
208
281
|
const serveSite = async (
|
|
209
282
|
request: IncomingMessage,
|
|
@@ -229,13 +302,20 @@ export const createGate = ({
|
|
|
229
302
|
return sendFile(request, response, found.file, 200, { ...securityHeaders, "Cache-Control": cacheControl });
|
|
230
303
|
};
|
|
231
304
|
|
|
232
|
-
/** Steps 3 and 5: what the gate answers itself, which holds nothing of the site. */
|
|
233
|
-
const answerOwn = (
|
|
305
|
+
/** Steps 3, 4's sign-in start and 5: what the gate answers itself, which holds nothing of the site. */
|
|
306
|
+
const answerOwn = (
|
|
307
|
+
request: IncomingMessage,
|
|
308
|
+
response: ServerResponse,
|
|
309
|
+
path: string,
|
|
310
|
+
rawQuery: string,
|
|
311
|
+
host: string,
|
|
312
|
+
) => {
|
|
234
313
|
if (path === "/robots.txt") return text(response, request, "User-agent: *\nDisallow: /\n");
|
|
235
314
|
if (path === "/_door/health") return text(response, request, "ok");
|
|
315
|
+
if (path === "/_door/signin") return startSignin(request, response, rawQuery, host);
|
|
236
316
|
if (path === "/_door/signout") {
|
|
237
317
|
const then = new URLSearchParams(rawQuery).get("then");
|
|
238
|
-
return redirect(response, 303, signoutTarget(then), [clearSession, clearState]);
|
|
318
|
+
return redirect(response, 303, signoutTarget(then), [clearSession, clearState, clearSeen]);
|
|
239
319
|
}
|
|
240
320
|
return empty(response, 404);
|
|
241
321
|
};
|
|
@@ -267,7 +347,7 @@ export const createGate = ({
|
|
|
267
347
|
|
|
268
348
|
if (path === "/_door") return acceptTicket(request, response, host);
|
|
269
349
|
if (request.method === "POST") return empty(response, 405, { Allow: "GET, HEAD" });
|
|
270
|
-
if (isOwnPath(path)) return answerOwn(request, response, path, rawQuery);
|
|
350
|
+
if (isOwnPath(path)) return answerOwn(request, response, path, rawQuery, host);
|
|
271
351
|
|
|
272
352
|
if (!hasSession(request, host)) return refuse(request, response, target, host);
|
|
273
353
|
return serveSite(request, response, path, { rawPath, rawQuery, host });
|
package/src/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ export {
|
|
|
10
10
|
export { canonicalNext } from "./door/next.ts";
|
|
11
11
|
export {
|
|
12
12
|
type BundleDoorOptions,
|
|
13
|
+
type BundlePageOptions,
|
|
13
14
|
bundleDoor,
|
|
14
15
|
checkDeployment,
|
|
15
16
|
type DeployManifest,
|
|
@@ -17,10 +18,19 @@ export {
|
|
|
17
18
|
gateFiles,
|
|
18
19
|
maxBundleBytes,
|
|
19
20
|
} from "./door/package.ts";
|
|
21
|
+
export {
|
|
22
|
+
type ComingSoon,
|
|
23
|
+
comingSoon,
|
|
24
|
+
type PageConfig,
|
|
25
|
+
type PageConfigFile,
|
|
26
|
+
type PageImage,
|
|
27
|
+
parsePageConfig,
|
|
28
|
+
} from "./door/page.ts";
|
|
20
29
|
export {
|
|
21
30
|
createGate,
|
|
22
31
|
type GateOptions,
|
|
23
32
|
type StartGateOptions,
|
|
33
|
+
seenCookie,
|
|
24
34
|
sessionCookie,
|
|
25
35
|
startGate,
|
|
26
36
|
stateCookie,
|