@milesroxas/design-hub 0.6.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -6
- package/core/review/model.ts +124 -6
- package/core/review/policy.test.ts +103 -24
- package/core/review/policy.ts +43 -7
- package/core/review/service.test.ts +569 -33
- package/core/review/service.ts +272 -76
- package/core/review/store.ts +24 -4
- package/dist/core/review/model.d.ts +59 -4
- package/dist/core/review/model.js +98 -4
- package/dist/core/review/policy.d.ts +21 -2
- package/dist/core/review/policy.js +34 -2
- package/dist/core/review/service.d.ts +39 -11
- package/dist/core/review/service.js +237 -77
- package/dist/core/review/store.d.ts +17 -4
- package/dist/core/review/store.js +21 -3
- package/dist/scripts/bootstrap.d.ts +16 -14
- package/dist/scripts/bootstrap.js +56 -39
- package/dist/scripts/comments.d.ts +10 -2
- package/dist/scripts/comments.js +18 -7
- package/dist/scripts/lib/deploy.d.ts +12 -4
- package/dist/scripts/lib/deploy.js +47 -13
- package/dist/scripts/lib/vercel.d.ts +8 -2
- package/dist/scripts/lib/vercel.js +23 -9
- package/dist/scripts/people.d.ts +13 -26
- package/dist/scripts/people.js +68 -35
- package/dist/scripts/preflight.js +2 -2
- package/dist/scripts/share.d.ts +2 -3
- package/dist/scripts/share.js +18 -10
- package/dist/server/api.d.ts +6 -2
- package/dist/server/api.js +65 -24
- package/dist/server/fake-upstash.js +3 -2
- package/dist/server/local.js +2 -0
- package/dist/server/session.d.ts +25 -10
- package/dist/server/session.js +69 -52
- package/dist/server/upstash.js +4 -3
- package/dist/vercel/proxy.d.ts +5 -3
- package/dist/vercel/proxy.js +183 -157
- package/dist/vite/plugin.js +5 -5
- package/kit/prefs.ts +19 -2
- package/package.json +2 -2
- package/shell/review/Bar.tsx +97 -4
- package/shell/review/People.tsx +178 -45
- package/shell/review/Pins.tsx +39 -24
- package/shell/review/Review.tsx +113 -29
- package/shell/review/ThreadCard.tsx +25 -13
- package/shell/review/client.ts +64 -23
- package/shell/review/review.css +16 -2
- package/shell/review/view.test.ts +73 -1
- package/shell/review/view.ts +49 -3
- package/skills/miles-deploy/SKILL.md +44 -16
- package/skills/miles-design-hub/SKILL.md +5 -5
- package/skills/miles-new-feature/SKILL.md +1 -1
- package/skills/miles-review/SKILL.md +10 -6
- package/skills/miles-worktrees/SKILL.md +1 -1
- package/templates/conductor/settings.toml +1 -1
- package/templates/guide.md +1 -1
package/dist/server/session.js
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
//
|
|
2
|
-
// gate (proxy.ts), the review function (api/review.ts) and their tests
|
|
3
|
-
// one implementation.
|
|
1
|
+
// The signed session cookie for the deployed prototypes, on Web Crypto only,
|
|
2
|
+
// so the gate (proxy.ts), the review function (api/review.ts) and their tests
|
|
3
|
+
// share one implementation. There is one session: a person who used their
|
|
4
|
+
// one-time link. It names the person and their epoch, which a sign out
|
|
5
|
+
// everywhere or a revoke bumps, so either ends every session they hold. It
|
|
6
|
+
// lasts until their access ends, at most 400 days (the longest a browser
|
|
7
|
+
// keeps a cookie). HMAC-SHA256 with GATE_SECRET.
|
|
4
8
|
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
|
|
11
|
-
// Both are HMAC-SHA256 with GATE_SECRET and last 30 days.
|
|
12
|
-
import { base64url, sha256 } from '../core/bytes.js';
|
|
13
|
-
export const GATE_COOKIE = '__Host-hub_gate';
|
|
9
|
+
// It also carries when the store last said yes (checkedAt). For CHECK_SECONDS
|
|
10
|
+
// after that the gate trusts the signature; then it asks the store again and
|
|
11
|
+
// issues a fresh cookie, so a revoke, a sign out everywhere or a changed
|
|
12
|
+
// access end reaches every request within CHECK_SECONDS, whatever it claims to be.
|
|
13
|
+
// Every function that reads the clock takes `at`, so tests need not wait.
|
|
14
|
+
import { base64url } from '../core/bytes.js';
|
|
14
15
|
export const PERSON_COOKIE = '__Host-hub_person';
|
|
15
|
-
const
|
|
16
|
-
/**
|
|
16
|
+
const MAX_SESSION_SECONDS = 60 * 60 * 24 * 400;
|
|
17
|
+
/** How long the store's last yes is trusted before the gate asks again. */
|
|
18
|
+
export const CHECK_SECONDS = 5 * 60;
|
|
19
|
+
/** The gate's settings, or null when GATE_SECRET is missing or short (the gate then fails closed). */
|
|
17
20
|
export function readGate(env) {
|
|
18
|
-
const password = env.GATE_PASSWORD ?? '';
|
|
19
21
|
const secret = env.GATE_SECRET ?? '';
|
|
20
|
-
|
|
22
|
+
const site = (env.KV_REST_API_URL ?? env.UPSTASH_REDIS_REST_URL ?? '').replace(/\/$/, '');
|
|
23
|
+
return secret.length >= 32 ? { secret, site } : null;
|
|
21
24
|
}
|
|
22
25
|
const encoder = new TextEncoder();
|
|
23
26
|
async function hmac(secret, message) {
|
|
@@ -33,44 +36,54 @@ function timingSafeEqual(a, b) {
|
|
|
33
36
|
return diff === 0;
|
|
34
37
|
}
|
|
35
38
|
const sameText = (a, b) => timingSafeEqual(encoder.encode(a), encoder.encode(b));
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
const seconds = (at) => Math.floor(at.getTime() / 1000);
|
|
40
|
+
/**
|
|
41
|
+
* How long a session started at `at` lasts, in seconds: to the access end,
|
|
42
|
+
* capped at 400 days, and 0 for an access end that has passed or does not parse.
|
|
43
|
+
*/
|
|
44
|
+
export function sessionSeconds(endsAt, at = new Date()) {
|
|
45
|
+
if (endsAt === null)
|
|
46
|
+
return MAX_SESSION_SECONDS;
|
|
47
|
+
const left = Math.floor(Date.parse(endsAt) / 1000) - seconds(at);
|
|
48
|
+
return Number.isFinite(left) ? Math.max(0, Math.min(MAX_SESSION_SECONDS, left)) : 0;
|
|
45
49
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
const VERSION = 'p3';
|
|
51
|
+
// The site is signed, never written into the token; a newline cannot occur in a URL, so the parts cannot run together.
|
|
52
|
+
const personMessage = (site, id, epoch, expires, checkedAt) => `${VERSION}\n${site}\n${id}.${epoch}.${expires}.${checkedAt}`;
|
|
53
|
+
const tokenText = (id, epoch, expires, checkedAt) => `${VERSION}.${id}.${epoch}.${expires}.${checkedAt}`;
|
|
54
|
+
/** A token for someone the store said yes to at `at`, lasting to their access end. */
|
|
55
|
+
export async function issuePersonToken(gate, claim, endsAt, at = new Date()) {
|
|
56
|
+
const checkedAt = seconds(at);
|
|
57
|
+
const expires = checkedAt + sessionSeconds(endsAt, at);
|
|
58
|
+
const message = personMessage(gate.site, claim.id, claim.epoch, expires, checkedAt);
|
|
59
|
+
return `${tokenText(claim.id, claim.epoch, expires, checkedAt)}.${await hmac(gate.secret, message)}`;
|
|
55
60
|
}
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
const expires
|
|
59
|
-
return
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
61
|
+
const isCount = (n) => Number.isInteger(n) && n >= 0;
|
|
62
|
+
const wellFormed = (parts) => {
|
|
63
|
+
const [version, id, epoch, expires, checkedAt, sig, extra] = parts;
|
|
64
|
+
return (version === VERSION &&
|
|
65
|
+
/^[a-z0-9]+$/.test(id ?? '') &&
|
|
66
|
+
[epoch, expires, checkedAt].every((n) => isCount(Number(n))) &&
|
|
67
|
+
!!sig &&
|
|
68
|
+
extra === undefined);
|
|
69
|
+
};
|
|
70
|
+
/** The person a token names when its signature and expiry hold; the caller decides whether to ask the store. */
|
|
71
|
+
export async function readPersonToken(gate, token, at = new Date()) {
|
|
72
|
+
const parts = (token ?? '').split('.');
|
|
73
|
+
if (!wellFormed(parts))
|
|
74
|
+
return null;
|
|
75
|
+
const [, id, epoch, expires, checkedAt, sig] = parts;
|
|
76
|
+
if (Number(expires) <= seconds(at))
|
|
72
77
|
return null;
|
|
73
|
-
|
|
78
|
+
const message = personMessage(gate.site, id, Number(epoch), Number(expires), Number(checkedAt));
|
|
79
|
+
return sameText(sig, await hmac(gate.secret, message))
|
|
80
|
+
? { id, epoch: Number(epoch), checkedAt: Number(checkedAt) }
|
|
81
|
+
: null;
|
|
82
|
+
}
|
|
83
|
+
/** Whether the store's yes in this claim is recent enough to trust without asking again. */
|
|
84
|
+
export function isFresh(claim, at = new Date()) {
|
|
85
|
+
const age = seconds(at) - claim.checkedAt;
|
|
86
|
+
return age >= 0 && age < CHECK_SECONDS;
|
|
74
87
|
}
|
|
75
88
|
export function readCookie(request, name) {
|
|
76
89
|
const header = request.headers.get('cookie');
|
|
@@ -83,13 +96,17 @@ export function readCookie(request, name) {
|
|
|
83
96
|
}
|
|
84
97
|
return undefined;
|
|
85
98
|
}
|
|
86
|
-
|
|
99
|
+
/** A session cookie that lasts `seconds`: sessionSeconds of the same access end as its token. */
|
|
100
|
+
export const sessionCookie = (name, value, seconds) => `${name}=${value}; Path=/; Max-Age=${seconds}; HttpOnly; Secure; SameSite=Lax`;
|
|
87
101
|
export const clearedCookie = (name) => `${name}=; Path=/; Max-Age=0; HttpOnly; Secure; SameSite=Lax`;
|
|
88
102
|
// Same-origin check for a state-changing request. Sec-Fetch-Site is the
|
|
89
103
|
// browser's own verdict; Origin backs it up for browsers without it. Host
|
|
90
104
|
// comes from the forwarded header, since request.url carries the deployment
|
|
91
105
|
// host behind a custom domain. A browser sends "Origin: null" on a navigation
|
|
92
106
|
// from a no-referrer document, so null is accepted only with a same-origin verdict.
|
|
107
|
+
// A request with neither header is not from a browser that could be tricked
|
|
108
|
+
// into it (every current browser sends Origin on a POST), so it passes: it
|
|
109
|
+
// holds whatever it sends, and a forged request needs a victim's browser.
|
|
93
110
|
export function sameOrigin(request) {
|
|
94
111
|
const site = request.headers.get('sec-fetch-site');
|
|
95
112
|
if (site && site !== 'same-origin')
|
package/dist/server/upstash.js
CHANGED
|
@@ -23,9 +23,10 @@ export function upstashKV({ url, token }, fetcher = fetch) {
|
|
|
23
23
|
hset: async (key, field, value) => {
|
|
24
24
|
await command('HSET', key, field, value);
|
|
25
25
|
},
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
// HDEL answers how many fields it removed, which is what lets a one-time link be claimed once.
|
|
27
|
+
hdel: (key, field) => command('HDEL', key, field),
|
|
28
|
+
// Atomic in Redis, so two sign-outs everywhere at once raise the epoch twice.
|
|
29
|
+
hincrby: (key, field, by) => command('HINCRBY', key, field, String(by)),
|
|
29
30
|
hgetall: async (key) => {
|
|
30
31
|
const flat = (await command('HGETALL', key)) ?? [];
|
|
31
32
|
const out = {};
|
package/dist/vercel/proxy.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export declare const GATE_FONT = "/_gate/geist.woff2";
|
|
2
|
-
export declare function gatePreview(
|
|
2
|
+
export declare function gatePreview(page: string | null, siteName?: string): Promise<{
|
|
3
3
|
html: string;
|
|
4
4
|
csp: string;
|
|
5
5
|
}>;
|
|
6
6
|
export type ProxyOptions = {
|
|
7
|
-
/** The gate
|
|
7
|
+
/** The gate pages' title: the project's name as reviewers know it. */
|
|
8
8
|
siteName?: string;
|
|
9
|
+
/** The gate's clock; tests move it instead of waiting. */
|
|
10
|
+
now?: () => Date;
|
|
9
11
|
};
|
|
10
12
|
/** The middleware a project's proxy.ts default-exports. */
|
|
11
|
-
export declare function createProxy({ siteName }?: ProxyOptions): (request: Request) => Promise<Response>;
|
|
13
|
+
export declare function createProxy({ siteName, now }?: ProxyOptions): (request: Request) => Promise<Response>;
|
package/dist/vercel/proxy.js
CHANGED
|
@@ -1,84 +1,59 @@
|
|
|
1
1
|
// Vercel Routing Middleware (the project's proxy.ts, named in vercel.json as
|
|
2
2
|
// proxy.entrypoint): the gate in front of every path of the deployed
|
|
3
|
-
// prototypes, the review API included.
|
|
4
|
-
//
|
|
3
|
+
// prototypes, the review API included. There is one way in, a one-time link
|
|
4
|
+
// that Studio makes for one named person:
|
|
5
5
|
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
// next page load
|
|
6
|
+
// GET /invite/<token> shows who the link is for, and uses nothing up, so
|
|
7
|
+
// a mail or chat preview that opens it costs nothing
|
|
8
|
+
// POST /invite/<token> uses the link up, sets the person's signed cookie
|
|
9
|
+
// (server/session.ts) and goes to the hub
|
|
11
10
|
//
|
|
12
|
-
//
|
|
11
|
+
// A used, expired or unknown link is 404. Every other path needs the cookie.
|
|
12
|
+
// It carries when the store last said yes; after CHECK_SECONDS (5 minutes)
|
|
13
|
+
// the gate asks the store again on the next request of any kind, so a revoke,
|
|
14
|
+
// a sign out everywhere or a changed access end reaches every request within
|
|
15
|
+
// minutes. A store that does not answer is 503, never a sign out. Without a session a
|
|
16
|
+
// page gets the sign-in page (401), which says what to do and has no form,
|
|
17
|
+
// and anything else a plain-text 401. Without GATE_SECRET the gate fails
|
|
18
|
+
// closed (503).
|
|
13
19
|
import { next } from '@vercel/functions';
|
|
14
20
|
import { base64, sha256 } from '../core/bytes.js';
|
|
15
21
|
import { escapeHtml } from '../core/html.js';
|
|
16
22
|
import { deployedReview } from '../server/api.js';
|
|
17
|
-
import { clearedCookie,
|
|
23
|
+
import { clearedCookie, isFresh, issuePersonToken, PERSON_COOKIE, readCookie, readGate, readPersonToken, sameOrigin, sessionCookie, sessionSeconds, } from '../server/session.js';
|
|
18
24
|
const DEFAULT_SITE_NAME = 'Prototypes';
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// What the gate page itself loads, served before sign-in: the hub's face
|
|
23
|
-
// (the Vite plugin emits it at this fixed path) and the favicon. Nothing else.
|
|
25
|
+
const LINK_PATH = /^\/invite\/([\w-]{20,100})\/?$/;
|
|
26
|
+
// What the gate pages load before sign-in: the hub's face (the Vite plugin
|
|
27
|
+
// emits it at this fixed path) and the favicon. Nothing else.
|
|
24
28
|
export const GATE_FONT = '/_gate/geist.woff2';
|
|
25
29
|
const GATE_ASSETS = new Set([GATE_FONT, '/favicon.svg']);
|
|
26
|
-
// The gate
|
|
27
|
-
//
|
|
28
|
-
// wears the hub's neutral system look (shell/system.css)
|
|
29
|
-
// project's brand. Geist (latin, variable) is the one
|
|
30
|
-
// first paint is already in it.
|
|
30
|
+
// The gate pages. The CSS is a constant so its CSP hash is computed once and
|
|
31
|
+
// the pages carry no 'unsafe-inline', and they run no script at all. The gate
|
|
32
|
+
// is hub chrome, so it wears the hub's neutral system look (shell/system.css)
|
|
33
|
+
// inlined, never the project's brand. Geist (latin, variable) is the one
|
|
34
|
+
// font, preloaded so the first paint is already in it.
|
|
31
35
|
const CSS = `
|
|
32
36
|
@font-face{font-family:Geist;src:url(${GATE_FONT}) format("woff2");font-weight:100 900;font-style:normal;font-display:swap}
|
|
33
|
-
:root{color-scheme:light;--bg:oklch(.985 0 0);--surface:oklch(1 0 0);--ink:oklch(.205 0 0);--ink-
|
|
34
|
-
@media(prefers-color-scheme:dark){:root{color-scheme:dark;--bg:oklch(.145 0 0);--surface:oklch(.175 0 0);--ink:oklch(.985 0 0);--ink-
|
|
37
|
+
:root{color-scheme:light;--bg:oklch(.985 0 0);--surface:oklch(1 0 0);--ink:oklch(.205 0 0);--ink-3:oklch(.46 0 0);--brand:oklch(.205 0 0);--brand-hover:oklch(.3 0 0);--on-brand:oklch(.985 0 0);--ring:0 0 0 2px oklch(1 0 0),0 0 0 4px oklch(.556 0 0);--shadow:0 1px 2px 0 oklch(0 0 0/.05);--raised:0 0 0 1px oklch(0 0 0/.06),0 1px 2px oklch(0 0 0/.05),0 12px 24px -8px oklch(0 0 0/.1);--ease-out:cubic-bezier(.23,1,.32,1);--ease-press:cubic-bezier(.33,1,.68,1);--ease-hover:cubic-bezier(.25,.46,.45,.94)}
|
|
38
|
+
@media(prefers-color-scheme:dark){:root{color-scheme:dark;--bg:oklch(.145 0 0);--surface:oklch(.175 0 0);--ink:oklch(.985 0 0);--ink-3:oklch(.72 0 0);--brand:oklch(.985 0 0);--brand-hover:oklch(.87 0 0);--on-brand:oklch(.205 0 0);--ring:0 0 0 2px oklch(.175 0 0),0 0 0 4px oklch(.708 0 0);--shadow:0 0 #0000;--raised:0 0 0 1px oklch(1 0 0/.09)}}
|
|
35
39
|
*{box-sizing:border-box}
|
|
36
40
|
html{height:100%;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}
|
|
37
41
|
body{margin:0;min-height:100%;display:grid;place-items:center;padding:max(24px,env(safe-area-inset-top)) max(16px,env(safe-area-inset-right)) max(24px,env(safe-area-inset-bottom)) max(16px,env(safe-area-inset-left));background:var(--bg);color:var(--ink);font:14px/20px Geist,ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif;font-optical-sizing:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;text-rendering:optimizeLegibility}
|
|
38
|
-
[hidden]{display:none!important}
|
|
39
42
|
.gate{width:100%;max-width:360px;padding:32px;background:var(--surface);border-radius:12px;box-shadow:var(--raised);transition:opacity 240ms var(--ease-out),transform 240ms var(--ease-out)}
|
|
40
43
|
@starting-style{.gate{opacity:0;transform:translateY(6px)}}
|
|
41
44
|
h1{margin:0 0 4px;font-size:20px;line-height:28px;font-weight:600;letter-spacing:-.02em;text-wrap:balance}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
input:focus-visible{outline:2px solid transparent;outline-offset:2px;box-shadow:var(--ring),var(--shadow)}
|
|
48
|
-
input[aria-invalid=true]{border-color:var(--error-line)}
|
|
49
|
-
input[aria-invalid=true]:focus-visible{box-shadow:var(--ring-error),var(--shadow)}
|
|
50
|
-
.toggle{position:absolute;top:4px;right:4px;width:32px;height:32px;display:grid;place-items:center;margin:0;padding:0;border:0;border-radius:2px;background:transparent;color:var(--ink-4);cursor:pointer;touch-action:manipulation;-webkit-user-select:none;user-select:none;transition:color 120ms var(--ease-hover),background-color 120ms var(--ease-hover)}
|
|
51
|
-
.toggle::before{content:"";position:absolute;inset:-4px}
|
|
52
|
-
.toggle svg{display:block}
|
|
53
|
-
.toggle:focus-visible{outline:2px solid transparent;box-shadow:var(--ring)}
|
|
54
|
-
.toggle:active{color:var(--ink-2)}
|
|
55
|
-
.toggle[aria-pressed=true] .show,.toggle[aria-pressed=false] .hide{display:none}
|
|
56
|
-
@media(hover:hover) and (pointer:fine){.toggle:hover{color:var(--ink-2);background:var(--fill-hover)}}
|
|
57
|
-
.note{margin:8px 0 0;color:var(--ink-3)}
|
|
58
|
-
.error{color:var(--error)}
|
|
59
|
-
.go{display:block;width:100%;height:44px;margin-top:20px;border:0;border-radius:6px;background:var(--brand);color:var(--on-brand);font:inherit;font-weight:500;cursor:pointer;touch-action:manipulation;-webkit-user-select:none;user-select:none;box-shadow:var(--shadow);transition:background-color 120ms var(--ease-hover),transform 160ms var(--ease-press),opacity 120ms var(--ease-hover)}
|
|
45
|
+
p{margin:0;color:var(--ink-3);text-wrap:pretty}
|
|
46
|
+
p+p{margin-top:8px}
|
|
47
|
+
strong{font-weight:500;color:var(--ink)}
|
|
48
|
+
form{margin:0}
|
|
49
|
+
.go{display:block;width:100%;height:44px;margin-top:24px;padding:0;border:0;border-radius:6px;background:var(--brand);color:var(--on-brand);font:inherit;font-weight:500;line-height:44px;text-align:center;text-decoration:none;cursor:pointer;touch-action:manipulation;-webkit-user-select:none;user-select:none;box-shadow:var(--shadow);transition:background-color 120ms var(--ease-hover),transform 160ms var(--ease-press)}
|
|
60
50
|
.go:focus-visible{outline:2px solid transparent;outline-offset:2px;box-shadow:var(--ring)}
|
|
61
51
|
.go:active{transform:scale(.97)}
|
|
62
|
-
form[data-busy] .go{opacity:.7;pointer-events:none}
|
|
63
52
|
@media(hover:hover) and (pointer:fine){.go:hover{background:var(--brand-hover)}}
|
|
64
|
-
@media(pointer:coarse){input{height:44px;font-size:16px}.toggle{width:36px;height:36px}}
|
|
65
53
|
@media(max-width:400px){.gate{padding:24px}}
|
|
66
54
|
@media(prefers-reduced-motion:reduce){.gate{transition:opacity 240ms var(--ease-out)}@starting-style{.gate{transform:none}}.go:active{transform:none}}
|
|
67
55
|
`;
|
|
68
|
-
|
|
69
|
-
// without the fragment), shows the password on request, and says when Caps
|
|
70
|
-
// Lock is on. The toggle swallows pointerdown so the caret stays in the field.
|
|
71
|
-
const JS = `var f=document.forms[0],p=document.getElementById('password'),t=document.getElementById('toggle'),c=document.getElementById('caps');
|
|
72
|
-
if(location.hash){f.action=location.pathname+location.search+location.hash}
|
|
73
|
-
t.addEventListener('pointerdown',function(e){e.preventDefault()});
|
|
74
|
-
t.addEventListener('click',function(){var s=p.type==='password';p.type=s?'text':'password';t.setAttribute('aria-pressed',s);t.setAttribute('aria-label',s?'Hide password':'Show password')});
|
|
75
|
-
function caps(e){if(e.getModifierState){c.hidden=!e.getModifierState('CapsLock')}}
|
|
76
|
-
p.addEventListener('keydown',caps);p.addEventListener('keyup',caps);p.addEventListener('blur',function(){c.hidden=true});
|
|
77
|
-
f.addEventListener('submit',function(){f.setAttribute('data-busy','')});
|
|
78
|
-
addEventListener('pageshow',function(){f.removeAttribute('data-busy')});`;
|
|
79
|
-
const EYE = '<svg class="show" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.42 12.713c-.136-.215-.204-.323-.242-.49a1.173 1.173 0 0 1 0-.446c.038-.167.106-.274.242-.49C3.546 9.505 6.895 5 12 5s8.455 4.505 9.58 6.287c.137.215.205.323.243.49.029.125.029.322 0 .446-.038.167-.106.274-.242.49C20.454 14.495 17.105 19 12 19S3.545 14.495 2.42 12.713Z"/><path d="M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6Z"/></svg>' +
|
|
80
|
-
'<svg class="hide" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10.743 5.092C11.149 5.032 11.569 5 12 5c5.105 0 8.455 4.505 9.58 6.287.137.215.205.323.243.49.029.125.029.322 0 .446-.038.167-.107.275-.244.492-.3.474-.757 1.141-1.363 1.865M6.724 6.715c-2.162 1.467-3.63 3.504-4.303 4.57-.137.216-.205.324-.243.49a1.173 1.173 0 0 0 0 .446c.038.167.106.274.242.49C3.546 14.495 6.895 19 12 19c2.059 0 3.832-.732 5.289-1.723M3 3l18 18M9.879 9.879a3 3 0 1 0 4.243 4.243"/></svg>';
|
|
81
|
-
function shell(siteName, body, script) {
|
|
56
|
+
function shell(siteName, page, body) {
|
|
82
57
|
return `<!doctype html>
|
|
83
58
|
<html lang="en">
|
|
84
59
|
<head>
|
|
@@ -92,53 +67,43 @@ function shell(siteName, body, script) {
|
|
|
92
67
|
<style>${CSS}</style>
|
|
93
68
|
</head>
|
|
94
69
|
<body>
|
|
95
|
-
<main class="gate">
|
|
70
|
+
<main class="gate" data-gate="${page}">
|
|
96
71
|
<h1>${escapeHtml(siteName)}</h1>
|
|
97
72
|
${body}
|
|
98
73
|
</main>
|
|
99
|
-
${script ? `<script>${JS}</script>` : ''}
|
|
100
74
|
</body>
|
|
101
75
|
</html>
|
|
102
76
|
`;
|
|
103
77
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
? `<p id="err" class="note error" role="alert">That password didn’t match. Try again.</p>`
|
|
107
|
-
: '';
|
|
108
|
-
const invalid = failed ? ' aria-invalid="true" aria-describedby="err"' : '';
|
|
109
|
-
return shell(siteName, `<p class="hint">Use the password from the message that shared this link.</p>
|
|
110
|
-
<form method="post" novalidate>
|
|
111
|
-
<label for="password">Password</label>
|
|
112
|
-
<div class="field">
|
|
113
|
-
<input id="password" name="password" type="password" autocomplete="current-password" autocapitalize="off" autocorrect="off" spellcheck="false" required autofocus maxlength="${MAX_PASSWORD_LENGTH}"${invalid}>
|
|
114
|
-
<button id="toggle" class="toggle" type="button" aria-pressed="false" aria-label="Show password" aria-controls="password">${EYE}</button>
|
|
115
|
-
</div>
|
|
116
|
-
<p id="caps" class="note" aria-live="polite" hidden>Caps Lock is on.</p>
|
|
117
|
-
${error}
|
|
118
|
-
<button class="go" type="submit">Open prototypes</button>
|
|
119
|
-
</form>`, true);
|
|
120
|
-
}
|
|
78
|
+
const signInPage = (siteName) => shell(siteName, 'sign-in', `<p>To sign in, open the link the studio sent you. Each link signs in one device, then stops working.</p>
|
|
79
|
+
<p>For another device, or if your link has stopped working, ask the studio for a new one.</p>`);
|
|
121
80
|
// A GET shows who the link is for and a POST signs them in, so a mail
|
|
122
|
-
// scanner that opens every link never uses
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
81
|
+
// scanner that opens every link never uses it up.
|
|
82
|
+
const linkPage = (siteName, person) => shell(siteName, 'link', `<p>This link signs in <strong>${escapeHtml(person.name)}</strong>, ${escapeHtml(person.team)}, on this device.</p>
|
|
83
|
+
<p>It works once. For another device, ask the studio for a new link.</p>
|
|
84
|
+
<form method="post"><button class="go" type="submit" autofocus>Continue</button></form>`);
|
|
85
|
+
// Someone who opens their link again on a device it already signed in gets
|
|
86
|
+
// here too, so the page offers the way in as well.
|
|
87
|
+
const usedPage = (siteName) => shell(siteName, 'used', `<p>This link has been used, has expired or was removed. Each link signs in one device, then stops working.</p>
|
|
88
|
+
<p>Ask the studio for a new one. If this device is already signed in, open the prototypes.</p>
|
|
89
|
+
<a class="go" href="/">Open the prototypes</a>`);
|
|
90
|
+
const SIGNED_OUT = 'Sign in with the link the studio sent you.';
|
|
91
|
+
const USED = 'This link has been used, has expired or was removed.';
|
|
92
|
+
const OPEN_IN_BROWSER = 'Open this link in a browser to sign in.';
|
|
93
|
+
const PREVIEWS = {
|
|
94
|
+
'sign-in': signInPage,
|
|
95
|
+
link: (siteName) => linkPage(siteName, { name: 'Rae Chen', team: 'Acme' }),
|
|
96
|
+
used: usedPage,
|
|
97
|
+
};
|
|
98
|
+
// `design-hub dev` has no gate; the Vite plugin serves this at /_gate/ (?page=link or ?page=used) so the pages can be reviewed.
|
|
99
|
+
export async function gatePreview(page, siteName = DEFAULT_SITE_NAME) {
|
|
100
|
+
// hasOwn, so ?page=__proto__ or toString never reaches Object.prototype.
|
|
101
|
+
const render = page !== null && Object.hasOwn(PREVIEWS, page) ? PREVIEWS[page] : signInPage;
|
|
102
|
+
return { html: render(siteName), csp: await csp() };
|
|
132
103
|
}
|
|
133
104
|
let cspPromise;
|
|
134
105
|
function csp() {
|
|
135
|
-
cspPromise ??=
|
|
136
|
-
const base = `default-src 'none'; style-src 'sha256-${base64(css)}'; font-src 'self'; img-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'`;
|
|
137
|
-
return {
|
|
138
|
-
withScript: `${base}; script-src 'sha256-${base64(js)}'`,
|
|
139
|
-
noScript: `${base}; script-src 'none'`,
|
|
140
|
-
};
|
|
141
|
-
});
|
|
106
|
+
cspPromise ??= sha256(CSS).then((css) => `default-src 'none'; style-src 'sha256-${base64(css)}'; script-src 'none'; font-src 'self'; img-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'`);
|
|
142
107
|
return cspPromise;
|
|
143
108
|
}
|
|
144
109
|
const NO_STORE = 'no-store, max-age=0';
|
|
@@ -148,91 +113,152 @@ function wantsHtml(request) {
|
|
|
148
113
|
return dest === 'document' || dest === 'iframe';
|
|
149
114
|
return (request.headers.get('accept') ?? '').includes('text/html');
|
|
150
115
|
}
|
|
151
|
-
|
|
152
|
-
|
|
116
|
+
const isRead = (request) => request.method === 'GET' || request.method === 'HEAD';
|
|
117
|
+
/**
|
|
118
|
+
* Every answer the gate makes itself carries these: never cached, never
|
|
119
|
+
* indexed, no referrer. The link page alone sends a same-origin referrer, so
|
|
120
|
+
* its Continue POST carries a real Origin: with no-referrer a browser sends
|
|
121
|
+
* `Origin: null`, which sameOrigin trusts only beside Sec-Fetch-Site, and
|
|
122
|
+
* older Safari sends no Sec-Fetch-Site.
|
|
123
|
+
*/
|
|
124
|
+
function gateHeaders(type, { cookie, allow, referrer = 'no-referrer', retryAfter }) {
|
|
125
|
+
const headers = new Headers({
|
|
126
|
+
'cache-control': NO_STORE,
|
|
127
|
+
'content-type': type,
|
|
128
|
+
'x-robots-tag': 'noindex, nofollow',
|
|
129
|
+
'referrer-policy': referrer,
|
|
130
|
+
});
|
|
153
131
|
if (cookie)
|
|
154
132
|
headers.append('set-cookie', cookie);
|
|
155
|
-
if (
|
|
156
|
-
headers.set('
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
headers.set('content-type', 'text/html; charset=utf-8');
|
|
161
|
-
headers.set('content-security-policy', script ? policy.withScript : policy.noScript);
|
|
162
|
-
headers.set('referrer-policy', 'no-referrer');
|
|
163
|
-
return new Response(request.method === 'HEAD' ? null : html, { status, headers });
|
|
133
|
+
if (allow)
|
|
134
|
+
headers.set('allow', allow);
|
|
135
|
+
if (retryAfter)
|
|
136
|
+
headers.set('retry-after', String(retryAfter));
|
|
137
|
+
return headers;
|
|
164
138
|
}
|
|
165
|
-
const plain = (status, text, extra = {}) => new Response(text, {
|
|
139
|
+
const plain = (request, status, text, extra = {}) => new Response(request.method === 'HEAD' ? null : text, {
|
|
166
140
|
status,
|
|
167
|
-
headers:
|
|
141
|
+
headers: gateHeaders('text/plain; charset=utf-8', extra),
|
|
168
142
|
});
|
|
169
|
-
|
|
170
|
-
async function
|
|
171
|
-
if (!(request
|
|
172
|
-
return
|
|
143
|
+
/** A gate page for a browser, or `text` for anything else (a fetch, an asset, a preview bot). */
|
|
144
|
+
async function page(request, status, html, text, extra = {}) {
|
|
145
|
+
if (!wantsHtml(request))
|
|
146
|
+
return plain(request, status, text, extra);
|
|
147
|
+
const headers = gateHeaders('text/html; charset=utf-8', extra);
|
|
148
|
+
headers.set('content-security-policy', await csp());
|
|
149
|
+
return new Response(request.method === 'HEAD' ? null : html, { status, headers });
|
|
150
|
+
}
|
|
151
|
+
const unavailable = (request, error) => plain(request, 503, error);
|
|
152
|
+
/** The store did not answer: a 503 that passes, so it says when to try again. */
|
|
153
|
+
const storeDown = (request) => plain(request, 503, STORE_DOWN, { retryAfter: 30 });
|
|
154
|
+
const STORE_DOWN = 'The review store did not answer. Try again in a minute.';
|
|
155
|
+
const NO_STORE_YET = 'Review is not set up on this site: pnpm bootstrap --vercel <project> --review.';
|
|
156
|
+
/**
|
|
157
|
+
* A store call that can fail without signing anyone out: `undefined` when the
|
|
158
|
+
* store did not answer, so the caller answers 503 and leaves the cookie alone.
|
|
159
|
+
*/
|
|
160
|
+
async function ask(run) {
|
|
173
161
|
try {
|
|
174
|
-
|
|
175
|
-
return typeof value === 'string' && value.length <= MAX_PASSWORD_LENGTH ? value : null;
|
|
162
|
+
return await run();
|
|
176
163
|
}
|
|
177
|
-
catch {
|
|
178
|
-
|
|
164
|
+
catch (error) {
|
|
165
|
+
console.error('[gate]', error);
|
|
166
|
+
return undefined;
|
|
179
167
|
}
|
|
180
168
|
}
|
|
181
|
-
async function
|
|
182
|
-
const
|
|
169
|
+
async function showLink(ctx, token) {
|
|
170
|
+
const { siteName, request } = ctx;
|
|
171
|
+
const setup = deployedReview(process.env, null, () => ctx.at);
|
|
183
172
|
if ('error' in setup)
|
|
184
|
-
return
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
return redirect('/', cookie);
|
|
173
|
+
return unavailable(request, setup.error);
|
|
174
|
+
const person = await ask(() => setup.review.peekLink(token));
|
|
175
|
+
if (person === undefined)
|
|
176
|
+
return storeDown(request);
|
|
177
|
+
return person
|
|
178
|
+
? page(request, 200, linkPage(siteName, person), OPEN_IN_BROWSER, { referrer: 'same-origin' })
|
|
179
|
+
: page(request, 404, usedPage(siteName), USED);
|
|
192
180
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (!wantsHtml(request))
|
|
198
|
-
return pass();
|
|
199
|
-
const setup = deployedReview(process.env);
|
|
200
|
-
const current = 'error' in setup ? null : await setup.review.sessionPerson(claim.id, claim.epoch).catch(() => null);
|
|
201
|
-
if (current)
|
|
202
|
-
return pass();
|
|
203
|
-
return htmlPage(request, 401, passwordPage(siteName, false), true, clearedCookie(PERSON_COOKIE));
|
|
181
|
+
/** The cookie for someone the store just said yes to, lasting to their access end. */
|
|
182
|
+
async function sessionFor(gate, person, at) {
|
|
183
|
+
const token = await issuePersonToken(gate, { id: person.id, epoch: person.epoch }, person.accessEndsAt, at);
|
|
184
|
+
return sessionCookie(PERSON_COOKIE, token, sessionSeconds(person.accessEndsAt, at));
|
|
204
185
|
}
|
|
205
|
-
async function
|
|
186
|
+
async function signInWithLink(ctx, token) {
|
|
187
|
+
const { siteName, request, at } = ctx;
|
|
206
188
|
if (!sameOrigin(request))
|
|
207
|
-
return plain(403, 'Forbidden');
|
|
208
|
-
const
|
|
209
|
-
if (
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
189
|
+
return plain(request, 403, 'Forbidden');
|
|
190
|
+
const setup = deployedReview(process.env, null, () => ctx.at);
|
|
191
|
+
if ('error' in setup)
|
|
192
|
+
return unavailable(request, setup.error);
|
|
193
|
+
const person = await ask(() => setup.review.useLink(token));
|
|
194
|
+
if (person === undefined)
|
|
195
|
+
return storeDown(request);
|
|
196
|
+
if (!person)
|
|
197
|
+
return page(request, 404, usedPage(siteName), USED);
|
|
198
|
+
const headers = gateHeaders('text/plain; charset=utf-8', {
|
|
199
|
+
cookie: await sessionFor(setup.gate, person, at),
|
|
200
|
+
});
|
|
201
|
+
headers.set('location', '/');
|
|
202
|
+
return new Response(null, { status: 303, headers });
|
|
203
|
+
}
|
|
204
|
+
async function link(ctx, token) {
|
|
205
|
+
if (ctx.request.method === 'POST')
|
|
206
|
+
return signInWithLink(ctx, token);
|
|
207
|
+
if (isRead(ctx.request))
|
|
208
|
+
return showLink(ctx, token);
|
|
209
|
+
return plain(ctx.request, 405, 'Method not allowed', { allow: 'GET, HEAD, POST' });
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* No session. A page gets the sign-in page, and anything else (an asset, a
|
|
213
|
+
* fetch, a POST to any path but a link) a plain 401: there is nothing to
|
|
214
|
+
* sign in with here. A cookie that was sent but does not hold is cleared.
|
|
215
|
+
*/
|
|
216
|
+
function signedOut({ siteName, request }, staleCookie) {
|
|
217
|
+
const extra = staleCookie ? { cookie: clearedCookie(PERSON_COOKIE) } : {};
|
|
218
|
+
if (!isRead(request))
|
|
219
|
+
return plain(request, 401, SIGNED_OUT, extra);
|
|
220
|
+
return page(request, 401, signInPage(siteName), SIGNED_OUT, extra);
|
|
221
|
+
}
|
|
222
|
+
const pass = (cookie) => next({ headers: { 'cache-control': 'private, no-store', ...(cookie ? { 'set-cookie': cookie } : {}) } });
|
|
223
|
+
/**
|
|
224
|
+
* A signed session. While the store's last yes is fresh the signature is
|
|
225
|
+
* enough. Once it is stale the store is asked, whatever the request says it
|
|
226
|
+
* is (its headers are the client's to choose): yes passes with a fresh cookie
|
|
227
|
+
* whose expiry follows the person's current access end; no signs out and
|
|
228
|
+
* clears the cookie; no answer is 503 and the cookie stays.
|
|
229
|
+
*/
|
|
230
|
+
async function personSession(ctx, gate, claim) {
|
|
231
|
+
if (isFresh(claim, ctx.at))
|
|
232
|
+
return pass();
|
|
233
|
+
const setup = deployedReview(process.env, null, () => ctx.at);
|
|
234
|
+
if ('error' in setup)
|
|
235
|
+
return unavailable(ctx.request, setup.error);
|
|
236
|
+
const current = await ask(() => setup.review.sessionPerson(claim.id, claim.epoch));
|
|
237
|
+
if (current === undefined)
|
|
238
|
+
return storeDown(ctx.request);
|
|
239
|
+
return current ? pass(await sessionFor(gate, current, ctx.at)) : signedOut(ctx, true);
|
|
214
240
|
}
|
|
215
241
|
/** The middleware a project's proxy.ts default-exports. */
|
|
216
|
-
export function createProxy({ siteName = DEFAULT_SITE_NAME } = {}) {
|
|
242
|
+
export function createProxy({ siteName = DEFAULT_SITE_NAME, now = () => new Date() } = {}) {
|
|
217
243
|
return async function proxy(request) {
|
|
218
244
|
const gate = readGate(process.env);
|
|
219
245
|
if (!gate)
|
|
220
|
-
return
|
|
246
|
+
return unavailable(request, 'The prototype gate is not configured.');
|
|
247
|
+
const ctx = { siteName, request, at: now() };
|
|
221
248
|
const url = new URL(request.url);
|
|
222
|
-
const
|
|
223
|
-
if (
|
|
224
|
-
return
|
|
225
|
-
if (GATE_ASSETS.has(url.pathname) && (request
|
|
249
|
+
const linked = url.pathname.match(LINK_PATH);
|
|
250
|
+
if (linked)
|
|
251
|
+
return link(ctx, linked[1]);
|
|
252
|
+
if (GATE_ASSETS.has(url.pathname) && isRead(request))
|
|
226
253
|
return next();
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const claim = await readPersonToken(gate, readCookie(request, PERSON_COOKIE));
|
|
254
|
+
const cookie = readCookie(request, PERSON_COOKIE);
|
|
255
|
+
const claim = await readPersonToken(gate, cookie, ctx.at);
|
|
230
256
|
if (claim)
|
|
231
|
-
return personSession(
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if (
|
|
235
|
-
return
|
|
236
|
-
return
|
|
257
|
+
return personSession(ctx, gate, claim);
|
|
258
|
+
// Tokens are signed to the store's URL, so without one none verifies. Clearing cookies then
|
|
259
|
+
// would turn a missing env var into everyone locked out, since links work once: answer 503 instead.
|
|
260
|
+
if (cookie !== undefined && gate.site === '')
|
|
261
|
+
return unavailable(request, NO_STORE_YET);
|
|
262
|
+
return signedOut(ctx, cookie !== undefined);
|
|
237
263
|
};
|
|
238
264
|
}
|