@glw907/cairn-cms 0.34.0 → 0.36.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 (64) hide show
  1. package/CHANGELOG.md +37 -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/log/emit.d.ts +14 -0
  22. package/dist/log/emit.js +18 -0
  23. package/dist/log/events.d.ts +1 -0
  24. package/dist/log/events.js +1 -0
  25. package/dist/log/index.d.ts +3 -0
  26. package/dist/log/index.js +1 -0
  27. package/dist/sveltekit/auth-routes.d.ts +2 -0
  28. package/dist/sveltekit/auth-routes.js +22 -5
  29. package/dist/sveltekit/content-routes.d.ts +6 -4
  30. package/dist/sveltekit/content-routes.js +23 -0
  31. package/dist/sveltekit/csrf-required-page.d.ts +2 -0
  32. package/dist/sveltekit/csrf-required-page.js +25 -0
  33. package/dist/sveltekit/csrf.d.ts +18 -0
  34. package/dist/sveltekit/csrf.js +60 -0
  35. package/dist/sveltekit/guard.js +35 -6
  36. package/dist/sveltekit/https-required-page.js +10 -191
  37. package/dist/sveltekit/nav-routes.js +5 -0
  38. package/dist/sveltekit/static-admin-page.d.ts +11 -0
  39. package/dist/sveltekit/static-admin-page.js +195 -0
  40. package/package.json +1 -1
  41. package/src/lib/auth/crypto.ts +13 -0
  42. package/src/lib/components/AdminLayout.svelte +8 -1
  43. package/src/lib/components/ConceptList.svelte +3 -0
  44. package/src/lib/components/ConfirmPage.svelte +4 -2
  45. package/src/lib/components/CsrfField.svelte +20 -0
  46. package/src/lib/components/DeleteDialog.svelte +2 -0
  47. package/src/lib/components/EditPage.svelte +2 -0
  48. package/src/lib/components/LoginPage.svelte +4 -2
  49. package/src/lib/components/ManageEditors.svelte +4 -0
  50. package/src/lib/components/NavTree.svelte +2 -0
  51. package/src/lib/components/RenameDialog.svelte +3 -0
  52. package/src/lib/components/csrf-context.ts +2 -0
  53. package/src/lib/components/index.ts +1 -0
  54. package/src/lib/log/emit.ts +42 -0
  55. package/src/lib/log/events.ts +13 -0
  56. package/src/lib/log/index.ts +3 -0
  57. package/src/lib/sveltekit/auth-routes.ts +25 -7
  58. package/src/lib/sveltekit/content-routes.ts +29 -2
  59. package/src/lib/sveltekit/csrf-required-page.ts +26 -0
  60. package/src/lib/sveltekit/csrf.ts +61 -0
  61. package/src/lib/sveltekit/guard.ts +43 -6
  62. package/src/lib/sveltekit/https-required-page.ts +10 -194
  63. package/src/lib/sveltekit/nav-routes.ts +5 -0
  64. package/src/lib/sveltekit/static-admin-page.ts +200 -0
@@ -13,6 +13,8 @@ import { listMarkdown, readRaw, commitFiles } from '../github/repo.js';
13
13
  import { cachedInstallationToken } from '../github/signing.js';
14
14
  import { emptyManifest, manifestEntryFromFile, parseManifest, serializeManifest, upsertEntry, removeEntry, inboundLinks } from '../content/manifest.js';
15
15
  import { CommitConflictError } from '../github/types.js';
16
+ import { log } from '../log/index.js';
17
+ import { issueCsrfToken } from './csrf.js';
16
18
  /** The signed-in editor the guard resolved, or a login redirect. Kept local to decouple event shapes. */
17
19
  function sessionOf(event) {
18
20
  const editor = event.locals.editor;
@@ -47,6 +49,7 @@ export function createContentRoutes(runtime, deps = {}) {
47
49
  navLabel: runtime.navMenu?.label ?? null,
48
50
  theme,
49
51
  collapsedNav,
52
+ csrf: event.cookies ? issueCsrfToken({ url: event.url, cookies: event.cookies }) : '',
50
53
  };
51
54
  }
52
55
  /** Redirect /admin to the first concept's list (spec §7.6: land on the first concept). */
@@ -190,6 +193,17 @@ export function createContentRoutes(runtime, deps = {}) {
190
193
  function isConflict(err) {
191
194
  return err instanceof CommitConflictError || err?.name === 'CommitConflictError';
192
195
  }
196
+ /** Log a failed commit: a conflict is the expected last-writer-wins outcome, so it warns with a
197
+ * reason; any other error is unexpected and logs at error with the stringified cause. The caller
198
+ * still owns the redirect or rethrow, so control flow stays at the call site. */
199
+ function logCommitFailed(fields, err) {
200
+ if (isConflict(err)) {
201
+ log.warn('commit.failed', { ...fields, reason: 'conflict' });
202
+ }
203
+ else {
204
+ log.error('commit.failed', { ...fields, error: String(err) });
205
+ }
206
+ }
193
207
  /** Save an edit: validate, then commit with the session editor as author. Fails safe on 409. */
194
208
  async function saveAction(event) {
195
209
  const editor = sessionOf(event);
@@ -243,13 +257,16 @@ export function createContentRoutes(runtime, deps = {}) {
243
257
  if (absent.length) {
244
258
  return fail(400, { brokenLinks: absent, body });
245
259
  }
260
+ const commitFields = { concept: concept.id, id, editor: editor.email };
246
261
  try {
247
262
  await commitFiles(runtime.backend, [
248
263
  { path, content: markdown },
249
264
  { path: runtime.manifestPath, content: nextManifest },
250
265
  ], { message: `Update ${concept.label.toLowerCase()}: ${id}`, author: { name: editor.displayName, email: editor.email } }, token);
266
+ log.info('commit.succeeded', commitFields);
251
267
  }
252
268
  catch (err) {
269
+ logCommitFailed(commitFields, err);
253
270
  if (isConflict(err)) {
254
271
  const message = 'This file changed since you opened it. Reload and reapply your edits.';
255
272
  throw redirect(303, `/admin/${concept.id}/${id}?error=${encodeURIComponent(message)}${suffix}`);
@@ -276,13 +293,16 @@ export function createContentRoutes(runtime, deps = {}) {
276
293
  return fail(409, { inboundLinks: inbound, id });
277
294
  }
278
295
  const nextManifest = serializeManifest(removeEntry(manifest, concept.id, id));
296
+ const commitFields = { concept: concept.id, id, editor: editor.email };
279
297
  try {
280
298
  await commitFiles(runtime.backend, [
281
299
  { path, content: null },
282
300
  { path: runtime.manifestPath, content: nextManifest },
283
301
  ], { message: `Delete ${concept.label.toLowerCase()}: ${id}`, author: { name: editor.displayName, email: editor.email } }, token);
302
+ log.info('commit.succeeded', commitFields);
284
303
  }
285
304
  catch (err) {
305
+ logCommitFailed(commitFields, err);
286
306
  if (isConflict(err)) {
287
307
  const message = 'This file changed since you opened it. Reload and try again.';
288
308
  throw redirect(303, `/admin/${concept.id}/${id}?error=${encodeURIComponent(message)}`);
@@ -376,10 +396,13 @@ export function createContentRoutes(runtime, deps = {}) {
376
396
  next = upsertEntry(next, manifestEntryFromFile(linkerConcept, { path: linkerPath, raw: rewritten }));
377
397
  }
378
398
  changes.push({ path: runtime.manifestPath, content: serializeManifest(next) });
399
+ const commitFields = { concept: concept.id, id: newId, editor: editor.email };
379
400
  try {
380
401
  await commitFiles(runtime.backend, changes, { message: `Rename ${concept.label.toLowerCase()}: ${id} to ${newId}`, author: { name: editor.displayName, email: editor.email } }, token);
402
+ log.info('commit.succeeded', commitFields);
381
403
  }
382
404
  catch (err) {
405
+ logCommitFailed(commitFields, err);
383
406
  if (isConflict(err)) {
384
407
  const message = 'This file changed since you opened it. Reload and try again.';
385
408
  throw redirect(303, `/admin/${concept.id}/${id}?error=${encodeURIComponent(message)}`);
@@ -0,0 +1,2 @@
1
+ /** Render the full HTML document for the CSRF-failed page. */
2
+ export declare function csrfRequiredPage(): string;
@@ -0,0 +1,25 @@
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
+ /** Render the full HTML document for the CSRF-failed page. */
6
+ export function csrfRequiredPage() {
7
+ const inner = `
8
+ <span class="eyebrow">
9
+ <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>
10
+ Security check
11
+ </span>
12
+ <h1>Let's try that again</h1>
13
+ <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>
14
+
15
+ <a class="cta" href="/admin/login">
16
+ Back to sign-in
17
+ <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>
18
+ </a>
19
+
20
+ <div class="fix">
21
+ <h2>If it keeps happening</h2>
22
+ <p>Allow cookies for this site, then open the sign-in page fresh and request a new link.</p>
23
+ </div>`;
24
+ return renderStaticAdminPage({ title: 'Security check · Cairn', innerHtml: inner });
25
+ }
@@ -0,0 +1,18 @@
1
+ import type { CookieJar, RequestContext } from './types.js';
2
+ /** True for a request SvelteKit's CSRF guard screens: an unsafe method with a form content type. */
3
+ export declare function isUnsafeFormRequest(request: Request): boolean;
4
+ /** The faithful framework check: the Origin header equals the request's own origin. */
5
+ export declare function originMatches(event: Pick<RequestContext, 'url' | 'request'>): boolean;
6
+ /** A length-checked constant-time compare, so the token check leaks no timing. */
7
+ export declare function tokensMatch(a: string, b: string): boolean;
8
+ /**
9
+ * Return the session's CSRF token, minting and setting it when absent. Lazy and stable: a second
10
+ * open admin tab reuses the same value, so its form field still matches the cookie. Session-scoped
11
+ * (no maxAge), HttpOnly (the server sets both halves), SameSite=Strict, and __Host- on https.
12
+ */
13
+ export declare function issueCsrfToken(event: {
14
+ url: URL;
15
+ cookies: CookieJar;
16
+ }): string;
17
+ /** Validate the double-submit token on an admin form POST, reading the field from a body clone. */
18
+ export declare function validateCsrfToken(event: RequestContext): Promise<boolean>;
@@ -0,0 +1,60 @@
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
+ const UNSAFE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
6
+ const FORM_CONTENT_TYPES = new Set([
7
+ 'application/x-www-form-urlencoded',
8
+ 'multipart/form-data',
9
+ 'text/plain',
10
+ ]);
11
+ /** True for a request SvelteKit's CSRF guard screens: an unsafe method with a form content type. */
12
+ export function isUnsafeFormRequest(request) {
13
+ if (!UNSAFE_METHODS.has(request.method))
14
+ return false;
15
+ const type = (request.headers.get('content-type') ?? '').split(';', 1)[0].trim().toLowerCase();
16
+ return FORM_CONTENT_TYPES.has(type);
17
+ }
18
+ /** The faithful framework check: the Origin header equals the request's own origin. */
19
+ export function originMatches(event) {
20
+ return event.request.headers.get('origin') === event.url.origin;
21
+ }
22
+ /** A length-checked constant-time compare, so the token check leaks no timing. */
23
+ export function tokensMatch(a, b) {
24
+ if (a.length === 0 || a.length !== b.length)
25
+ return false;
26
+ let diff = 0;
27
+ for (let i = 0; i < a.length; i++)
28
+ diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
29
+ return diff === 0;
30
+ }
31
+ /**
32
+ * Return the session's CSRF token, minting and setting it when absent. Lazy and stable: a second
33
+ * open admin tab reuses the same value, so its form field still matches the cookie. Session-scoped
34
+ * (no maxAge), HttpOnly (the server sets both halves), SameSite=Strict, and __Host- on https.
35
+ */
36
+ export function issueCsrfToken(event) {
37
+ const secure = event.url.protocol === 'https:';
38
+ const name = csrfCookieName(secure);
39
+ const existing = event.cookies.get(name);
40
+ if (existing)
41
+ return existing;
42
+ const token = generateCsrfToken();
43
+ event.cookies.set(name, token, { path: '/', httpOnly: true, secure, sameSite: 'strict' });
44
+ return token;
45
+ }
46
+ /** Validate the double-submit token on an admin form POST, reading the field from a body clone. */
47
+ export async function validateCsrfToken(event) {
48
+ const cookie = event.cookies.get(csrfCookieName(event.url.protocol === 'https:'));
49
+ if (!cookie)
50
+ return false;
51
+ let submitted = '';
52
+ try {
53
+ const form = await event.request.clone().formData();
54
+ submitted = String(form.get('csrf') ?? '');
55
+ }
56
+ catch {
57
+ return false;
58
+ }
59
+ return tokensMatch(submitted, cookie);
60
+ }
@@ -5,6 +5,9 @@ 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';
10
+ import { log } from '../log/index.js';
8
11
  /** The login page and the auth endpoints are public; everything else under /admin is gated. */
9
12
  function isPublicAdminPath(pathname) {
10
13
  return pathname === '/admin/login' || pathname.startsWith('/admin/auth/');
@@ -37,30 +40,56 @@ function applySecurityHeaders(headers) {
37
40
  headers.set('Strict-Transport-Security', 'max-age=63072000; includeSubDomains');
38
41
  headers.set('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');
39
42
  }
43
+ /** A branded full-document admin page, hardened with the baseline headers and never cached. */
44
+ function brandedAdminPage(status, body) {
45
+ const headers = new Headers({ 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' });
46
+ applySecurityHeaders(headers);
47
+ return new Response(body, { status, headers });
48
+ }
40
49
  /** The hardened 400 help page for a deployed admin request that arrived over http. */
41
50
  function httpsRequiredResponse(url) {
42
51
  const httpsUrl = new URL(url);
43
52
  httpsUrl.protocol = 'https:';
44
- const headers = new Headers({
45
- 'Content-Type': 'text/html; charset=utf-8',
46
- 'Cache-Control': 'no-store',
53
+ return brandedAdminPage(400, httpsRequiredPage(httpsUrl.toString()));
54
+ }
55
+ /** A plain 403 for a non-admin cross-origin form POST, matching the framework's wording. */
56
+ function csrfForbidden() {
57
+ return new Response('Cross-site POST form submissions are forbidden', {
58
+ status: 403,
59
+ headers: { 'Content-Type': 'text/plain; charset=utf-8' },
47
60
  });
48
- applySecurityHeaders(headers);
49
- return new Response(httpsRequiredPage(httpsUrl.toString()), { status: 400, headers });
61
+ }
62
+ /** The branded 403 for a failed admin double-submit token check. */
63
+ function csrfRequiredResponse() {
64
+ return brandedAdminPage(403, csrfRequiredPage());
50
65
  }
51
66
  /** The SvelteKit `Handle` that guards `/admin/**` and hardens admin responses. */
52
67
  export function createAuthGuard() {
53
68
  return async function handle({ event, resolve }) {
54
69
  const { pathname } = event.url;
55
- if (!isAdminPath(pathname))
70
+ // Rule 2 - non-admin: restore the framework's strict Origin check the consumer disabled when
71
+ // they set checkOrigin: false to hand cairn the admin CSRF authority.
72
+ if (!isAdminPath(pathname)) {
73
+ if (isUnsafeFormRequest(event.request) && !originMatches(event)) {
74
+ log.warn('guard.rejected', { reason: 'origin', path: pathname });
75
+ return csrfForbidden();
76
+ }
56
77
  return resolve(event);
78
+ }
57
79
  // A deployed admin request over http never works: the magic-link form POST would fail the
58
80
  // framework's CSRF guard with an opaque 403. Serve the help page instead, before resolve()
59
81
  // runs that check. This covers the public login/auth paths too, since that is where the form
60
82
  // posts. Local http (wrangler dev) is exempt.
61
83
  if (event.url.protocol === 'http:' && !isLocalHost(event.url.hostname)) {
84
+ log.warn('guard.rejected', { reason: 'https', path: pathname });
62
85
  return httpsRequiredResponse(event.url);
63
86
  }
87
+ // Rule 1 - admin: every unsafe form POST carries a valid double-submit token, else the branded
88
+ // 403 before resolve() runs. This covers the public login/auth posts too.
89
+ if (isUnsafeFormRequest(event.request) && !(await validateCsrfToken(event))) {
90
+ log.warn('guard.rejected', { reason: 'csrf', path: pathname });
91
+ return csrfRequiredResponse();
92
+ }
64
93
  if (!isPublicAdminPath(pathname)) {
65
94
  const env = event.platform?.env ?? {};
66
95
  const id = event.cookies.get(sessionCookieName(event.url.protocol === 'https:'));
@@ -1,195 +1,17 @@
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
- /** Escape a string for safe interpolation into HTML text and double-quoted attributes. */
12
- function escapeHtml(value) {
13
- return value
14
- .replace(/&/g, '&amp;')
15
- .replace(/</g, '&lt;')
16
- .replace(/>/g, '&gt;')
17
- .replace(/"/g, '&quot;');
18
- }
19
- // The cairn stone-stack glyph (Temaki, CC0), drawn in currentColor like CairnLogo.svelte.
20
- const CAIRN_GLYPH = '<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 ' +
21
- '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 ' +
22
- '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 ' +
23
- '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 ' +
24
- '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 ' +
25
- '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 ' +
26
- '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';
27
8
  /**
28
9
  * Render the full HTML document for the HTTPS-required page.
29
10
  * @param httpsUrl The same request rebuilt over https, offered as the one-click recovery link.
30
11
  */
31
12
  export function httpsRequiredPage(httpsUrl) {
32
13
  const href = escapeHtml(httpsUrl);
33
- return `<!doctype html>
34
- <html lang="en">
35
- <head>
36
- <meta charset="utf-8" />
37
- <meta name="viewport" content="width=device-width, initial-scale=1" />
38
- <meta name="robots" content="noindex, nofollow" />
39
- <title>HTTPS required · Cairn</title>
40
- <style>
41
- :root {
42
- color-scheme: light;
43
- --bg: oklch(96.5% 0.006 75);
44
- --glow: oklch(52% 0.2 293 / 0.06);
45
- --panel: oklch(99% 0.004 75);
46
- --recessed: oklch(95% 0.008 75);
47
- --ink: oklch(26% 0.014 75);
48
- --muted: oklch(48% 0.01 75);
49
- --subtle: oklch(42% 0.01 75);
50
- --primary: oklch(52% 0.2 293);
51
- --primary-content: oklch(98% 0.012 293);
52
- --border: oklch(93% 0.008 75);
53
- --shadow: 0 1px 2px oklch(28% 0.02 75 / 0.05), 0 18px 40px -12px oklch(28% 0.02 75 / 0.16);
54
- --radius-box: 1rem;
55
- --radius-field: 0.625rem;
56
- --font: 'Figtree Variable', system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
57
- }
58
- @media (prefers-color-scheme: dark) {
59
- :root {
60
- color-scheme: dark;
61
- --bg: oklch(15.5% 0.009 75);
62
- --glow: oklch(68% 0.18 293 / 0.1);
63
- --panel: oklch(24% 0.01 75);
64
- --recessed: oklch(20% 0.01 75);
65
- --ink: oklch(93% 0.006 75);
66
- --muted: oklch(72% 0.01 75);
67
- --subtle: oklch(80% 0.008 75);
68
- --primary: oklch(68% 0.18 293);
69
- --primary-content: oklch(20% 0.04 293);
70
- --border: oklch(30% 0.014 75);
71
- --shadow: 0 1px 2px oklch(0% 0 0 / 0.35), 0 18px 40px -12px oklch(0% 0 0 / 0.55);
72
- }
73
- }
74
- * { box-sizing: border-box; }
75
- body {
76
- margin: 0;
77
- min-height: 100vh;
78
- display: flex;
79
- align-items: center;
80
- justify-content: center;
81
- padding: 1.5rem;
82
- font-family: var(--font);
83
- color: var(--ink);
84
- background-color: var(--bg);
85
- background-image: radial-gradient(80rem 50rem at 50% -20%, var(--glow), transparent 60%);
86
- -webkit-font-smoothing: antialiased;
87
- -moz-osx-font-smoothing: grayscale;
88
- line-height: 1.55;
89
- }
90
- main {
91
- width: 100%;
92
- max-width: 30rem;
93
- background: var(--panel);
94
- border: 1px solid var(--border);
95
- border-radius: var(--radius-box);
96
- box-shadow: var(--shadow);
97
- padding: 2.25rem;
98
- }
99
- .brand { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 1.75rem; }
100
- .brand .tile {
101
- display: grid;
102
- place-items: center;
103
- width: 2rem;
104
- height: 2rem;
105
- border-radius: 0.75rem;
106
- background: var(--primary);
107
- color: var(--primary-content);
108
- box-shadow: 0 1px 2px oklch(0% 0 0 / 0.12);
109
- }
110
- .brand .tile svg { width: 1.25rem; height: 1.25rem; }
111
- .brand .word {
112
- font-weight: 700;
113
- font-size: 1.25rem;
114
- letter-spacing: -0.01em;
115
- }
116
- .eyebrow {
117
- display: inline-flex;
118
- align-items: center;
119
- gap: 0.4rem;
120
- font-size: 0.6875rem;
121
- font-weight: 600;
122
- text-transform: uppercase;
123
- letter-spacing: 0.08em;
124
- color: var(--muted);
125
- margin-bottom: 0.6rem;
126
- }
127
- .eyebrow svg { width: 0.85rem; height: 0.85rem; }
128
- h1 {
129
- margin: 0 0 0.75rem;
130
- font-size: 1.6rem;
131
- font-weight: 800;
132
- letter-spacing: -0.02em;
133
- line-height: 1.15;
134
- }
135
- p { margin: 0 0 1rem; color: var(--subtle); }
136
- .cta {
137
- display: inline-flex;
138
- align-items: center;
139
- gap: 0.5rem;
140
- margin: 0.25rem 0 0.5rem;
141
- padding: 0.7rem 1.15rem;
142
- border-radius: var(--radius-field);
143
- background: var(--primary);
144
- color: var(--primary-content);
145
- font-weight: 600;
146
- font-size: 0.95rem;
147
- text-decoration: none;
148
- box-shadow: 0 4px 14px -4px oklch(52% 0.2 293 / 0.5);
149
- transition: transform 0.12s ease, box-shadow 0.12s ease;
150
- }
151
- .cta:hover { transform: translateY(-1px); box-shadow: 0 8px 20px -6px oklch(52% 0.2 293 / 0.55); }
152
- .cta:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
153
- .cta svg { width: 1rem; height: 1rem; }
154
- .fix {
155
- margin-top: 1.75rem;
156
- padding: 1.1rem 1.2rem;
157
- background: var(--recessed);
158
- border: 1px solid var(--border);
159
- border-radius: var(--radius-field);
160
- }
161
- .fix h2 {
162
- margin: 0 0 0.5rem;
163
- font-size: 0.8125rem;
164
- font-weight: 700;
165
- letter-spacing: 0.01em;
166
- }
167
- .fix p { margin: 0 0 0.65rem; font-size: 0.875rem; }
168
- .fix p:last-child { margin-bottom: 0; }
169
- .path {
170
- display: block;
171
- font-size: 0.8125rem;
172
- font-weight: 600;
173
- color: var(--ink);
174
- letter-spacing: 0.01em;
175
- margin: 0 0 0.65rem;
176
- }
177
- .path .arrow { color: var(--muted); padding: 0 0.35rem; font-weight: 400; }
178
- .foot {
179
- margin-top: 1.75rem;
180
- text-align: center;
181
- font-size: 0.75rem;
182
- color: var(--muted);
183
- }
184
- </style>
185
- </head>
186
- <body>
187
- <main>
188
- <div class="brand">
189
- <span class="tile"><svg viewBox="0 0 15 15" fill="currentColor" aria-hidden="true">${CAIRN_GLYPH}</svg></span>
190
- <span class="word">Cairn</span>
191
- </div>
192
-
14
+ const inner = `
193
15
  <span class="eyebrow">
194
16
  <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>
195
17
  Secure connection required
@@ -208,9 +30,6 @@ p { margin: 0 0 1rem; color: var(--subtle); }
208
30
  <span class="path">SSL/TLS<span class="arrow">&rsaquo;</span>Edge Certificates<span class="arrow">&rsaquo;</span>Always Use HTTPS</span>
209
31
  <p>Keep HSTS on too. The browser then stays on https and sign-in works.</p>
210
32
  </div>
211
-
212
- <p class="foot">Powered by Cairn</p>
213
- </main>
214
- </body>
215
- </html>`;
33
+ `;
34
+ return renderStaticAdminPage({ title: 'HTTPS required · Cairn', innerHtml: inner });
216
35
  }
@@ -6,6 +6,7 @@ import { appCredentials } from '../github/credentials.js';
6
6
  import { cachedInstallationToken } from '../github/signing.js';
7
7
  import { listMarkdown, readRaw, commitFile } from '../github/repo.js';
8
8
  import { CommitConflictError } from '../github/types.js';
9
+ import { log } from '../log/index.js';
9
10
  import { parseSiteConfig, extractMenu, validateNavTree, setMenu } from '../nav/site-config.js';
10
11
  /** The signed-in editor the guard resolved, or a login redirect. */
11
12
  function sessionOf(event) {
@@ -87,14 +88,18 @@ export function createNavRoutes(runtime, deps = {}) {
87
88
  const raw = await readRaw(runtime.backend, config.configPath, token);
88
89
  if (raw === null)
89
90
  throw error(404, 'Site config not found');
91
+ const commitFields = { concept: 'nav', id: 'site-config', editor: editor.email };
90
92
  try {
91
93
  await commitFile(runtime.backend, config.configPath, setMenu(raw, config.menuName, tree), { message: `Update ${config.label.toLowerCase()}`, author: { name: editor.displayName, email: editor.email } }, token);
94
+ log.info('commit.succeeded', commitFields);
92
95
  }
93
96
  catch (err) {
94
97
  if (isConflict(err)) {
98
+ log.warn('commit.failed', { ...commitFields, reason: 'conflict' });
95
99
  const message = 'The site config changed since you opened it. Reload and reapply your edits.';
96
100
  throw redirect(303, `/admin/nav?error=${encodeURIComponent(message)}`);
97
101
  }
102
+ log.error('commit.failed', { ...commitFields, error: String(err) });
98
103
  throw err;
99
104
  }
100
105
  throw redirect(303, '/admin/nav?saved=1');
@@ -0,0 +1,11 @@
1
+ /** Escape a string for safe interpolation into HTML text and double-quoted attributes. */
2
+ export declare function escapeHtml(value: string): string;
3
+ /**
4
+ * Render a full self-contained admin page document. The caller supplies trusted inner HTML
5
+ * (eyebrow, heading, copy, CTA); the helper owns the head, the inlined style, the brand tile,
6
+ * and the footer.
7
+ */
8
+ export declare function renderStaticAdminPage(opts: {
9
+ title: string;
10
+ innerHtml: string;
11
+ }): string;