@glw907/cairn-cms 0.34.0 → 0.35.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/auth/crypto.d.ts +4 -0
  3. package/dist/auth/crypto.js +10 -0
  4. package/dist/components/AdminLayout.svelte +8 -1
  5. package/dist/components/ConceptList.svelte +3 -0
  6. package/dist/components/ConfirmPage.svelte +4 -2
  7. package/dist/components/ConfirmPage.svelte.d.ts +2 -1
  8. package/dist/components/CsrfField.svelte +20 -0
  9. package/dist/components/CsrfField.svelte.d.ts +12 -0
  10. package/dist/components/DeleteDialog.svelte +2 -0
  11. package/dist/components/EditPage.svelte +2 -0
  12. package/dist/components/LoginPage.svelte +4 -2
  13. package/dist/components/LoginPage.svelte.d.ts +2 -1
  14. package/dist/components/ManageEditors.svelte +4 -0
  15. package/dist/components/NavTree.svelte +2 -0
  16. package/dist/components/RenameDialog.svelte +3 -0
  17. package/dist/components/csrf-context.d.ts +2 -0
  18. package/dist/components/csrf-context.js +2 -0
  19. package/dist/components/index.d.ts +1 -0
  20. package/dist/components/index.js +1 -0
  21. package/dist/sveltekit/auth-routes.d.ts +2 -0
  22. package/dist/sveltekit/auth-routes.js +10 -3
  23. package/dist/sveltekit/content-routes.d.ts +6 -4
  24. package/dist/sveltekit/content-routes.js +2 -0
  25. package/dist/sveltekit/csrf-required-page.d.ts +2 -0
  26. package/dist/sveltekit/csrf-required-page.js +25 -0
  27. package/dist/sveltekit/csrf.d.ts +18 -0
  28. package/dist/sveltekit/csrf.js +60 -0
  29. package/dist/sveltekit/guard.js +30 -6
  30. package/dist/sveltekit/https-required-page.js +10 -191
  31. package/dist/sveltekit/static-admin-page.d.ts +11 -0
  32. package/dist/sveltekit/static-admin-page.js +195 -0
  33. package/package.json +1 -1
  34. package/src/lib/auth/crypto.ts +13 -0
  35. package/src/lib/components/AdminLayout.svelte +8 -1
  36. package/src/lib/components/ConceptList.svelte +3 -0
  37. package/src/lib/components/ConfirmPage.svelte +4 -2
  38. package/src/lib/components/CsrfField.svelte +20 -0
  39. package/src/lib/components/DeleteDialog.svelte +2 -0
  40. package/src/lib/components/EditPage.svelte +2 -0
  41. package/src/lib/components/LoginPage.svelte +4 -2
  42. package/src/lib/components/ManageEditors.svelte +4 -0
  43. package/src/lib/components/NavTree.svelte +2 -0
  44. package/src/lib/components/RenameDialog.svelte +3 -0
  45. package/src/lib/components/csrf-context.ts +2 -0
  46. package/src/lib/components/index.ts +1 -0
  47. package/src/lib/sveltekit/auth-routes.ts +12 -5
  48. package/src/lib/sveltekit/content-routes.ts +8 -2
  49. package/src/lib/sveltekit/csrf-required-page.ts +26 -0
  50. package/src/lib/sveltekit/csrf.ts +61 -0
  51. package/src/lib/sveltekit/guard.ts +37 -6
  52. package/src/lib/sveltekit/https-required-page.ts +10 -194
  53. package/src/lib/sveltekit/static-admin-page.ts +200 -0
@@ -8,12 +8,13 @@ the allowlist, so the page never leaks membership (spec §7.1).
8
8
  import './cairn-admin.css';
9
9
  import { onMount } from 'svelte';
10
10
  import CairnLogo from './CairnLogo.svelte';
11
+ import CsrfField from './CsrfField.svelte';
11
12
  import { cairnFaviconHref } from './cairn-favicon.js';
12
13
  import { warnIfChromeWrapped } from './chrome-guard.js';
13
14
 
14
15
  interface Props {
15
- /** The login load's data: the site name and an optional error. */
16
- data: { siteName: string; error: string | null };
16
+ /** The login load's data: the site name, an optional error, and the CSRF token. */
17
+ data: { siteName: string; error: string | null; csrf: string };
17
18
  /** The action result: `sent` is true once a request was accepted. */
18
19
  form: { sent?: boolean } | null;
19
20
  }
@@ -54,6 +55,7 @@ the allowlist, so the page never leaks membership (spec §7.1).
54
55
  <div role="alert" class="alert alert-error mb-3 text-sm">That link expired. Request a new one below.</div>
55
56
  {/if}
56
57
  <form method="POST" class="flex flex-col gap-3">
58
+ <CsrfField token={data.csrf} />
57
59
  <label class="flex flex-col gap-1">
58
60
  <span class="text-sm font-medium">Email</span>
59
61
  <input
@@ -6,6 +6,7 @@ last-owner anti-lockout rule itself is enforced server-side (editors-routes). Ac
6
6
  named `?/setRole`, `?/remove`, and `?/add` actions.
7
7
  -->
8
8
  <script lang="ts">
9
+ import CsrfField from './CsrfField.svelte';
9
10
  import type { Editor } from '../auth/types.js';
10
11
 
11
12
  interface Props {
@@ -45,6 +46,7 @@ named `?/setRole`, `?/remove`, and `?/add` actions.
45
46
  </td>
46
47
  <td class="flex justify-end gap-2">
47
48
  <form method="POST" action="?/setRole">
49
+ <CsrfField />
48
50
  <input type="hidden" name="email" value={editor.email} />
49
51
  <input type="hidden" name="role" value={editor.role === 'owner' ? 'editor' : 'owner'} />
50
52
  <button type="submit" class="btn btn-ghost btn-xs" disabled={isSelf} aria-label={`Toggle role for ${editor.displayName}`}>
@@ -52,6 +54,7 @@ named `?/setRole`, `?/remove`, and `?/add` actions.
52
54
  </button>
53
55
  </form>
54
56
  <form method="POST" action="?/remove">
57
+ <CsrfField />
55
58
  <input type="hidden" name="email" value={editor.email} />
56
59
  <button type="submit" class="btn btn-ghost btn-xs text-error" disabled={isSelf} aria-label={`Remove ${editor.displayName}`}>
57
60
  Remove
@@ -65,6 +68,7 @@ named `?/setRole`, `?/remove`, and `?/add` actions.
65
68
  </div>
66
69
 
67
70
  <form method="POST" action="?/add" class="rounded-box border border-[var(--cairn-card-border)] bg-base-100 grid gap-3 p-4 shadow-[var(--cairn-shadow)] sm:grid-cols-[1fr_1fr_auto_auto] sm:items-end">
71
+ <CsrfField />
68
72
  <label class="flex flex-col gap-1">
69
73
  <span class="text-sm font-medium">Name</span>
70
74
  <input class="input" name="name" aria-label="Name" required />
@@ -8,6 +8,7 @@ validates on save.
8
8
  -->
9
9
  <script lang="ts">
10
10
  import { untrack } from 'svelte';
11
+ import CsrfField from './CsrfField.svelte';
11
12
  import { SortableList, sortItems } from '@rodrigodagostino/svelte-sortable-list';
12
13
  import type { SortableList as SortableListNS } from '@rodrigodagostino/svelte-sortable-list';
13
14
  import '@rodrigodagostino/svelte-sortable-list/styles.css';
@@ -98,6 +99,7 @@ validates on save.
98
99
  {/if}
99
100
 
100
101
  <form method="POST" action="?/save">
102
+ <CsrfField />
101
103
  <input type="hidden" name="tree" value={treeJson} />
102
104
 
103
105
  <div class="mb-2">
@@ -6,6 +6,8 @@ dated post keeps its date; only the slug changes. Built on a native <dialog>, fo
6
6
  DeleteDialog a11y conventions.
7
7
  -->
8
8
  <script lang="ts">
9
+ import CsrfField from './CsrfField.svelte';
10
+
9
11
  interface Props {
10
12
  /** The concept this entry belongs to, e.g. "posts". Posted with the confirm. */
11
13
  conceptId: string;
@@ -50,6 +52,7 @@ DeleteDialog a11y conventions.
50
52
  <button type="button" class="btn btn-ghost btn-sm" aria-label="Close" onclick={close}>✕</button>
51
53
  </div>
52
54
  <form method="POST" action="?/rename" class="flex flex-col gap-3">
55
+ <CsrfField />
53
56
  <input type="hidden" name="concept" value={conceptId} />
54
57
  <input type="hidden" name="id" value={id} />
55
58
  <label class="flex flex-col gap-1">
@@ -0,0 +1,2 @@
1
+ /** The Svelte context key AdminLayout uses to hand a CSRF-token getter to descendant admin forms. */
2
+ export const CSRF_CONTEXT_KEY = 'cairn:csrf';
@@ -3,6 +3,7 @@
3
3
  export { default as AdminLayout } from './AdminLayout.svelte';
4
4
  export { default as LoginPage } from './LoginPage.svelte';
5
5
  export { default as ConfirmPage } from './ConfirmPage.svelte';
6
+ export { default as CsrfField } from './CsrfField.svelte';
6
7
  export { default as ConceptList } from './ConceptList.svelte';
7
8
  export { default as EditPage } from './EditPage.svelte';
8
9
  export { default as ManageEditors } from './ManageEditors.svelte';
@@ -14,6 +14,7 @@ import {
14
14
  } from '../auth/crypto.js';
15
15
  import { findEditor, issueToken, consumeToken, createSession, deleteSession, recentlyIssued } from '../auth/store.js';
16
16
  import { buildMagicLinkMessage, cloudflareSend, type AuthBranding, type SendMagicLink } from '../email.js';
17
+ import { issueCsrfToken } from './csrf.js';
17
18
  import type { RequestContext } from './types.js';
18
19
 
19
20
  export interface AuthRoutesConfig {
@@ -63,23 +64,29 @@ export function createAuthRoutes(config: AuthRoutesConfig) {
63
64
  return { sent: true };
64
65
  }
65
66
 
66
- /** GET /admin/login. Public. Carries the site name and an optional `?error` for the form. */
67
- function loginLoad(event: RequestContext): { siteName: string; error: string | null } {
68
- return { siteName: config.branding.siteName, error: event.url.searchParams.get('error') };
67
+ /** GET /admin/login. Public. Carries the site name, an optional `?error`, and the CSRF token. */
68
+ function loginLoad(event: RequestContext): { siteName: string; error: string | null; csrf: string } {
69
+ return {
70
+ siteName: config.branding.siteName,
71
+ error: event.url.searchParams.get('error'),
72
+ csrf: issueCsrfToken(event),
73
+ };
69
74
  }
70
75
 
71
76
  /**
72
77
  * GET /admin/auth/confirm. Renders the confirm page and consumes nothing; only the POST
73
- * verifies. Sets Referrer-Policy: no-referrer so the token does not leak to a referrer.
78
+ * verifies. Sets Referrer-Policy: no-referrer so the token does not leak to a referrer, and
79
+ * issues the CSRF token so the confirm form can render the hidden field.
74
80
  */
75
81
  function confirmLoad(
76
82
  event: RequestContext,
77
- ): { token: string; siteName: string; error: string | null } {
83
+ ): { token: string; siteName: string; error: string | null; csrf: string } {
78
84
  event.setHeaders({ 'Referrer-Policy': 'no-referrer' });
79
85
  return {
80
86
  token: event.url.searchParams.get('token') ?? '',
81
87
  siteName: config.branding.siteName,
82
88
  error: event.url.searchParams.get('error'),
89
+ csrf: issueCsrfToken(event),
83
90
  };
84
91
  }
85
92
 
@@ -13,6 +13,8 @@ import { listMarkdown, readRaw, commitFiles, type FileChange } from '../github/r
13
13
  import { cachedInstallationToken } from '../github/signing.js';
14
14
  import { emptyManifest, manifestEntryFromFile, parseManifest, serializeManifest, upsertEntry, removeEntry, inboundLinks, type LinkTarget, type InboundLink } from '../content/manifest.js';
15
15
  import { CommitConflictError } from '../github/types.js';
16
+ import { issueCsrfToken } from './csrf.js';
17
+ import type { CookieJar } from './types.js';
16
18
  import type { CairnRuntime, ConceptDescriptor, FrontmatterField } from '../content/types.js';
17
19
  import type { Editor, Role } from '../auth/types.js';
18
20
 
@@ -36,6 +38,8 @@ export interface LayoutData {
36
38
  /** The nav group labels the user has collapsed, from the persisted cookie. Read at SSR so a
37
39
  * collapsed group renders collapsed with no flash. Empty when none are collapsed. */
38
40
  collapsedNav: string[];
41
+ /** The session's CSRF double-submit token, rendered as a hidden field in every admin form. */
42
+ csrf: string;
39
43
  }
40
44
 
41
45
  /** One row in a concept's list view. */
@@ -88,8 +92,9 @@ export interface ContentEvent {
88
92
  request: Request;
89
93
  locals: { editor?: Editor | null };
90
94
  platform?: { env?: GithubKeyEnv };
91
- /** SvelteKit's cookie jar; the layout load reads the persisted admin theme. Optional for non-route callers. */
92
- cookies?: { get(name: string): string | undefined };
95
+ /** SvelteKit's cookie jar. The layout load reads the persisted admin theme and issues the CSRF
96
+ * token. Optional for non-route callers. */
97
+ cookies?: CookieJar;
93
98
  }
94
99
 
95
100
  /** Injectable dependencies; tests stub the token mint to avoid signing a real key. */
@@ -134,6 +139,7 @@ export function createContentRoutes(runtime: CairnRuntime, deps: ContentRoutesDe
134
139
  navLabel: runtime.navMenu?.label ?? null,
135
140
  theme,
136
141
  collapsedNav,
142
+ csrf: event.cookies ? issueCsrfToken({ url: event.url, cookies: event.cookies }) : '',
137
143
  };
138
144
  }
139
145
 
@@ -0,0 +1,26 @@
1
+ // The branded 403 the guard serves when an admin form POST fails the double-submit token check.
2
+ // A sibling to https-required-page, built through the shared shell. It names the likely cause and
3
+ // offers a fresh sign-in, and it does not mention Origin headers (the token path does not read them).
4
+ import { renderStaticAdminPage } from './static-admin-page.js';
5
+
6
+ /** Render the full HTML document for the CSRF-failed page. */
7
+ export function csrfRequiredPage(): string {
8
+ const inner = `
9
+ <span class="eyebrow">
10
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
11
+ Security check
12
+ </span>
13
+ <h1>Let's try that again</h1>
14
+ <p>Your sign-in form could not be verified. This usually means the page was open across a browser restart, or cookies are blocked for this site.</p>
15
+
16
+ <a class="cta" href="/admin/login">
17
+ Back to sign-in
18
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></svg>
19
+ </a>
20
+
21
+ <div class="fix">
22
+ <h2>If it keeps happening</h2>
23
+ <p>Allow cookies for this site, then open the sign-in page fresh and request a new link.</p>
24
+ </div>`;
25
+ return renderStaticAdminPage({ title: 'Security check · Cairn', innerHtml: inner });
26
+ }
@@ -0,0 +1,61 @@
1
+ // cairn owns CSRF for the admin once a site disables SvelteKit's global checkOrigin. These helpers
2
+ // back the guard's two rules and the loads that issue the double-submit token. See
3
+ // docs/superpowers/specs/2026-06-08-cairn-login-csrf-ownership-design.md.
4
+ import { csrfCookieName, generateCsrfToken } from '../auth/crypto.js';
5
+ import type { CookieJar, RequestContext } from './types.js';
6
+
7
+ const UNSAFE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
8
+ const FORM_CONTENT_TYPES = new Set([
9
+ 'application/x-www-form-urlencoded',
10
+ 'multipart/form-data',
11
+ 'text/plain',
12
+ ]);
13
+
14
+ /** True for a request SvelteKit's CSRF guard screens: an unsafe method with a form content type. */
15
+ export function isUnsafeFormRequest(request: Request): boolean {
16
+ if (!UNSAFE_METHODS.has(request.method)) return false;
17
+ const type = (request.headers.get('content-type') ?? '').split(';', 1)[0].trim().toLowerCase();
18
+ return FORM_CONTENT_TYPES.has(type);
19
+ }
20
+
21
+ /** The faithful framework check: the Origin header equals the request's own origin. */
22
+ export function originMatches(event: Pick<RequestContext, 'url' | 'request'>): boolean {
23
+ return event.request.headers.get('origin') === event.url.origin;
24
+ }
25
+
26
+ /** A length-checked constant-time compare, so the token check leaks no timing. */
27
+ export function tokensMatch(a: string, b: string): boolean {
28
+ if (a.length === 0 || a.length !== b.length) return false;
29
+ let diff = 0;
30
+ for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
31
+ return diff === 0;
32
+ }
33
+
34
+ /**
35
+ * Return the session's CSRF token, minting and setting it when absent. Lazy and stable: a second
36
+ * open admin tab reuses the same value, so its form field still matches the cookie. Session-scoped
37
+ * (no maxAge), HttpOnly (the server sets both halves), SameSite=Strict, and __Host- on https.
38
+ */
39
+ export function issueCsrfToken(event: { url: URL; cookies: CookieJar }): string {
40
+ const secure = event.url.protocol === 'https:';
41
+ const name = csrfCookieName(secure);
42
+ const existing = event.cookies.get(name);
43
+ if (existing) return existing;
44
+ const token = generateCsrfToken();
45
+ event.cookies.set(name, token, { path: '/', httpOnly: true, secure, sameSite: 'strict' });
46
+ return token;
47
+ }
48
+
49
+ /** Validate the double-submit token on an admin form POST, reading the field from a body clone. */
50
+ export async function validateCsrfToken(event: RequestContext): Promise<boolean> {
51
+ const cookie = event.cookies.get(csrfCookieName(event.url.protocol === 'https:'));
52
+ if (!cookie) return false;
53
+ let submitted = '';
54
+ try {
55
+ const form = await event.request.clone().formData();
56
+ submitted = String(form.get('csrf') ?? '');
57
+ } catch {
58
+ return false;
59
+ }
60
+ return tokensMatch(submitted, cookie);
61
+ }
@@ -5,6 +5,8 @@ import { redirect, error } from '@sveltejs/kit';
5
5
  import { resolveSession } from '../auth/store.js';
6
6
  import { sessionCookieName } from '../auth/crypto.js';
7
7
  import { httpsRequiredPage } from './https-required-page.js';
8
+ import { isUnsafeFormRequest, originMatches, validateCsrfToken } from './csrf.js';
9
+ import { csrfRequiredPage } from './csrf-required-page.js';
8
10
  import type { Editor } from '../auth/types.js';
9
11
  import type { HandleInput, RequestContext } from './types.js';
10
12
 
@@ -46,23 +48,45 @@ function applySecurityHeaders(headers: Headers): void {
46
48
  headers.set('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');
47
49
  }
48
50
 
51
+ /** A branded full-document admin page, hardened with the baseline headers and never cached. */
52
+ function brandedAdminPage(status: number, body: string): Response {
53
+ const headers = new Headers({ 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' });
54
+ applySecurityHeaders(headers);
55
+ return new Response(body, { status, headers });
56
+ }
57
+
49
58
  /** The hardened 400 help page for a deployed admin request that arrived over http. */
50
59
  function httpsRequiredResponse(url: URL): Response {
51
60
  const httpsUrl = new URL(url);
52
61
  httpsUrl.protocol = 'https:';
53
- const headers = new Headers({
54
- 'Content-Type': 'text/html; charset=utf-8',
55
- 'Cache-Control': 'no-store',
62
+ return brandedAdminPage(400, httpsRequiredPage(httpsUrl.toString()));
63
+ }
64
+
65
+ /** A plain 403 for a non-admin cross-origin form POST, matching the framework's wording. */
66
+ function csrfForbidden(): Response {
67
+ return new Response('Cross-site POST form submissions are forbidden', {
68
+ status: 403,
69
+ headers: { 'Content-Type': 'text/plain; charset=utf-8' },
56
70
  });
57
- applySecurityHeaders(headers);
58
- return new Response(httpsRequiredPage(httpsUrl.toString()), { status: 400, headers });
71
+ }
72
+
73
+ /** The branded 403 for a failed admin double-submit token check. */
74
+ function csrfRequiredResponse(): Response {
75
+ return brandedAdminPage(403, csrfRequiredPage());
59
76
  }
60
77
 
61
78
  /** The SvelteKit `Handle` that guards `/admin/**` and hardens admin responses. */
62
79
  export function createAuthGuard() {
63
80
  return async function handle({ event, resolve }: HandleInput): Promise<Response> {
64
81
  const { pathname } = event.url;
65
- if (!isAdminPath(pathname)) return resolve(event);
82
+
83
+ // Rule 2 - non-admin: restore the framework's strict Origin check the consumer disabled when
84
+ // they set checkOrigin: false to hand cairn the admin CSRF authority.
85
+ if (!isAdminPath(pathname)) {
86
+ if (isUnsafeFormRequest(event.request) && !originMatches(event)) return csrfForbidden();
87
+ return resolve(event);
88
+ }
89
+
66
90
  // A deployed admin request over http never works: the magic-link form POST would fail the
67
91
  // framework's CSRF guard with an opaque 403. Serve the help page instead, before resolve()
68
92
  // runs that check. This covers the public login/auth paths too, since that is where the form
@@ -70,6 +94,13 @@ export function createAuthGuard() {
70
94
  if (event.url.protocol === 'http:' && !isLocalHost(event.url.hostname)) {
71
95
  return httpsRequiredResponse(event.url);
72
96
  }
97
+
98
+ // Rule 1 - admin: every unsafe form POST carries a valid double-submit token, else the branded
99
+ // 403 before resolve() runs. This covers the public login/auth posts too.
100
+ if (isUnsafeFormRequest(event.request) && !(await validateCsrfToken(event))) {
101
+ return csrfRequiredResponse();
102
+ }
103
+
73
104
  if (!isPublicAdminPath(pathname)) {
74
105
  const env = event.platform?.env ?? {};
75
106
  const id = event.cookies.get(sessionCookieName(event.url.protocol === 'https:'));
@@ -1,32 +1,10 @@
1
- // The standalone "this admin needs HTTPS" page. The auth guard serves it when a request reaches a
2
- // deployed Worker over http, which is the one case that makes the magic-link sign-in fail: the
3
- // JS-free login form posts over http, and the framework's CSRF guard rejects a form POST whose
4
- // origin scheme does not match, so the editor would otherwise hit an opaque 403. This page names
5
- // the problem, says why https is needed, and gives the exact Cloudflare fix.
6
- //
7
- // It is served raw from the edge, before SvelteKit renders anything, so it carries no external
8
- // request: the Warm Stone tokens are inlined for both colour schemes and the type falls back to the
9
- // system stack (the shipped admin fonts are not reachable from here). The cairn glyph is the same
10
- // public-domain Temaki mark the admin chrome uses. See docs/internal/admin-design-system.md.
11
-
12
- /** Escape a string for safe interpolation into HTML text and double-quoted attributes. */
13
- function escapeHtml(value: string): string {
14
- return value
15
- .replace(/&/g, '&amp;')
16
- .replace(/</g, '&lt;')
17
- .replace(/>/g, '&gt;')
18
- .replace(/"/g, '&quot;');
19
- }
20
-
21
- // The cairn stone-stack glyph (Temaki, CC0), drawn in currentColor like CairnLogo.svelte.
22
- const CAIRN_GLYPH =
23
- '<path d="M6.28 14C5.56 14 1 13.89 1 12.91C1 11.46 2.16 11.07 3.2 10.81C4.36 10.51 13.18 9.77 ' +
24
- '13.76 10.07C14.46 10.43 13.52 12.49 12.44 12.77C11.28 13.07 10.21 14 8.48 14C7.05 14 9.69 14 ' +
25
- '6.28 14ZM6.92 4.5C6.67 4.5 5 4.43 5 3.88C5 3.07 5.75 2.51 5.96 2.35C6.36 2.03 6.32 1.62 6.54 ' +
26
- '1.27C6.84 0.79 7.61 0.5 7.88 0.5C8.1 0.5 8.75 0.9 9.23 1.42C9.45 1.66 10 2.77 10 3.12C10 4.22 ' +
27
- '9.36 4.5 8.85 4.5C8.33 4.5 8.15 4.5 6.92 4.5ZM3.68 8.22C3 7.73 3.67 6.86 4.57 6.21C5.38 5.63 ' +
28
- '5.92 5.96 6.79 5.7C8.33 5.24 9.02 5.72 9.02 5.72L10.9 6.82C12.03 7.63 10.99 7.67 10.38 8.56C9.79 ' +
29
- '9.42 8.18 9.11 7.42 9.33C6.78 9.53 5.75 9.71 4.62 8.9L3.68 8.22Z"/>';
1
+ // The "this admin needs HTTPS" page. The auth guard serves it when a request reaches a deployed
2
+ // Worker over http, which is the one case that makes the magic-link sign-in fail: the JS-free login
3
+ // form posts over http, and the framework's CSRF guard rejects a form POST whose origin scheme does
4
+ // not match, so the editor would otherwise hit an opaque 403. This page names the problem, says why
5
+ // https is needed, and gives the exact Cloudflare fix. The shared shell lives in
6
+ // static-admin-page.ts. See guard.ts.
7
+ import { escapeHtml, renderStaticAdminPage } from './static-admin-page.js';
30
8
 
31
9
  /**
32
10
  * Render the full HTML document for the HTTPS-required page.
@@ -34,166 +12,7 @@ const CAIRN_GLYPH =
34
12
  */
35
13
  export function httpsRequiredPage(httpsUrl: string): string {
36
14
  const href = escapeHtml(httpsUrl);
37
- return `<!doctype html>
38
- <html lang="en">
39
- <head>
40
- <meta charset="utf-8" />
41
- <meta name="viewport" content="width=device-width, initial-scale=1" />
42
- <meta name="robots" content="noindex, nofollow" />
43
- <title>HTTPS required · Cairn</title>
44
- <style>
45
- :root {
46
- color-scheme: light;
47
- --bg: oklch(96.5% 0.006 75);
48
- --glow: oklch(52% 0.2 293 / 0.06);
49
- --panel: oklch(99% 0.004 75);
50
- --recessed: oklch(95% 0.008 75);
51
- --ink: oklch(26% 0.014 75);
52
- --muted: oklch(48% 0.01 75);
53
- --subtle: oklch(42% 0.01 75);
54
- --primary: oklch(52% 0.2 293);
55
- --primary-content: oklch(98% 0.012 293);
56
- --border: oklch(93% 0.008 75);
57
- --shadow: 0 1px 2px oklch(28% 0.02 75 / 0.05), 0 18px 40px -12px oklch(28% 0.02 75 / 0.16);
58
- --radius-box: 1rem;
59
- --radius-field: 0.625rem;
60
- --font: 'Figtree Variable', system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
61
- }
62
- @media (prefers-color-scheme: dark) {
63
- :root {
64
- color-scheme: dark;
65
- --bg: oklch(15.5% 0.009 75);
66
- --glow: oklch(68% 0.18 293 / 0.1);
67
- --panel: oklch(24% 0.01 75);
68
- --recessed: oklch(20% 0.01 75);
69
- --ink: oklch(93% 0.006 75);
70
- --muted: oklch(72% 0.01 75);
71
- --subtle: oklch(80% 0.008 75);
72
- --primary: oklch(68% 0.18 293);
73
- --primary-content: oklch(20% 0.04 293);
74
- --border: oklch(30% 0.014 75);
75
- --shadow: 0 1px 2px oklch(0% 0 0 / 0.35), 0 18px 40px -12px oklch(0% 0 0 / 0.55);
76
- }
77
- }
78
- * { box-sizing: border-box; }
79
- body {
80
- margin: 0;
81
- min-height: 100vh;
82
- display: flex;
83
- align-items: center;
84
- justify-content: center;
85
- padding: 1.5rem;
86
- font-family: var(--font);
87
- color: var(--ink);
88
- background-color: var(--bg);
89
- background-image: radial-gradient(80rem 50rem at 50% -20%, var(--glow), transparent 60%);
90
- -webkit-font-smoothing: antialiased;
91
- -moz-osx-font-smoothing: grayscale;
92
- line-height: 1.55;
93
- }
94
- main {
95
- width: 100%;
96
- max-width: 30rem;
97
- background: var(--panel);
98
- border: 1px solid var(--border);
99
- border-radius: var(--radius-box);
100
- box-shadow: var(--shadow);
101
- padding: 2.25rem;
102
- }
103
- .brand { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 1.75rem; }
104
- .brand .tile {
105
- display: grid;
106
- place-items: center;
107
- width: 2rem;
108
- height: 2rem;
109
- border-radius: 0.75rem;
110
- background: var(--primary);
111
- color: var(--primary-content);
112
- box-shadow: 0 1px 2px oklch(0% 0 0 / 0.12);
113
- }
114
- .brand .tile svg { width: 1.25rem; height: 1.25rem; }
115
- .brand .word {
116
- font-weight: 700;
117
- font-size: 1.25rem;
118
- letter-spacing: -0.01em;
119
- }
120
- .eyebrow {
121
- display: inline-flex;
122
- align-items: center;
123
- gap: 0.4rem;
124
- font-size: 0.6875rem;
125
- font-weight: 600;
126
- text-transform: uppercase;
127
- letter-spacing: 0.08em;
128
- color: var(--muted);
129
- margin-bottom: 0.6rem;
130
- }
131
- .eyebrow svg { width: 0.85rem; height: 0.85rem; }
132
- h1 {
133
- margin: 0 0 0.75rem;
134
- font-size: 1.6rem;
135
- font-weight: 800;
136
- letter-spacing: -0.02em;
137
- line-height: 1.15;
138
- }
139
- p { margin: 0 0 1rem; color: var(--subtle); }
140
- .cta {
141
- display: inline-flex;
142
- align-items: center;
143
- gap: 0.5rem;
144
- margin: 0.25rem 0 0.5rem;
145
- padding: 0.7rem 1.15rem;
146
- border-radius: var(--radius-field);
147
- background: var(--primary);
148
- color: var(--primary-content);
149
- font-weight: 600;
150
- font-size: 0.95rem;
151
- text-decoration: none;
152
- box-shadow: 0 4px 14px -4px oklch(52% 0.2 293 / 0.5);
153
- transition: transform 0.12s ease, box-shadow 0.12s ease;
154
- }
155
- .cta:hover { transform: translateY(-1px); box-shadow: 0 8px 20px -6px oklch(52% 0.2 293 / 0.55); }
156
- .cta:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
157
- .cta svg { width: 1rem; height: 1rem; }
158
- .fix {
159
- margin-top: 1.75rem;
160
- padding: 1.1rem 1.2rem;
161
- background: var(--recessed);
162
- border: 1px solid var(--border);
163
- border-radius: var(--radius-field);
164
- }
165
- .fix h2 {
166
- margin: 0 0 0.5rem;
167
- font-size: 0.8125rem;
168
- font-weight: 700;
169
- letter-spacing: 0.01em;
170
- }
171
- .fix p { margin: 0 0 0.65rem; font-size: 0.875rem; }
172
- .fix p:last-child { margin-bottom: 0; }
173
- .path {
174
- display: block;
175
- font-size: 0.8125rem;
176
- font-weight: 600;
177
- color: var(--ink);
178
- letter-spacing: 0.01em;
179
- margin: 0 0 0.65rem;
180
- }
181
- .path .arrow { color: var(--muted); padding: 0 0.35rem; font-weight: 400; }
182
- .foot {
183
- margin-top: 1.75rem;
184
- text-align: center;
185
- font-size: 0.75rem;
186
- color: var(--muted);
187
- }
188
- </style>
189
- </head>
190
- <body>
191
- <main>
192
- <div class="brand">
193
- <span class="tile"><svg viewBox="0 0 15 15" fill="currentColor" aria-hidden="true">${CAIRN_GLYPH}</svg></span>
194
- <span class="word">Cairn</span>
195
- </div>
196
-
15
+ const inner = `
197
16
  <span class="eyebrow">
198
17
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>
199
18
  Secure connection required
@@ -212,9 +31,6 @@ p { margin: 0 0 1rem; color: var(--subtle); }
212
31
  <span class="path">SSL/TLS<span class="arrow">&rsaquo;</span>Edge Certificates<span class="arrow">&rsaquo;</span>Always Use HTTPS</span>
213
32
  <p>Keep HSTS on too. The browser then stays on https and sign-in works.</p>
214
33
  </div>
215
-
216
- <p class="foot">Powered by Cairn</p>
217
- </main>
218
- </body>
219
- </html>`;
34
+ `;
35
+ return renderStaticAdminPage({ title: 'HTTPS required · Cairn', innerHtml: inner });
220
36
  }