diff --git a/README.md b/README.md index 0250455..53be052 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ fails if they do not become healthy. Every update is another | `CONTENT_DIR` | asked on the first deploy | | `JOBS_HOST_DIR` | `/srv`, mounted read only into the jobs service | | `SITE_URL` | `https://security-command.org` | -| `LEGAL_PAGES` | `impressum:impressum.txt,datenschutz:datenschutz.txt` | +| `LEGAL_PAGES` | `impressum/imprint:impressum.md,datenschutz/privacy:datenschutz.md` | | `REGISTRY` | `git.security-command.org/securitycommand` | | `BACKEND_PORT` | `8380`, must match the nginx config | | `ALLOWED_ORIGINS` | `https://security-command.org` | @@ -108,13 +108,14 @@ on the static site after two quiet seconds. Update it in place, for example with `git pull`, instead of replacing the directory, because running containers keep the mount of the old one. -- `motd` is shown on the boot screen and at login. - The legal pages from `LEGAL_PAGES` are rendered at `/impressum` and - `/datenschutz` and linked in the header. + `/datenschutz` and linked in the header. The English aliases `/imprint` + and `/privacy` redirect to them, and browsers that do not prefer German + show those names in the header. - Dotfiles, dot directories and symlinks are skipped. - Top level names used by the site itself (`ws`, `raw`, `ls`, `assets`, - `healthz`, `sitemap.xml`, `robots.txt`, the legal routes) are skipped - with a warning in the backend log. + `healthz`, `sitemap.xml`, `robots.txt`, the legal routes and aliases) + are skipped with a warning in the backend log. - Files above 1 MB are listed but not served; the whole index is capped at 64 MB and 10 000 entries. diff --git a/apps/backend/src/assets.test.ts b/apps/backend/src/assets.test.ts index 562ae31..4c0adbf 100644 --- a/apps/backend/src/assets.test.ts +++ b/apps/backend/src/assets.test.ts @@ -2,7 +2,7 @@ import { mkdtempSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { describe, expect, it } from 'vitest'; -import { readTheme } from './assets.ts'; +import { readTheme, setLegalLinks, type Asset } from './assets.ts'; describe('readTheme', () => { const base = Buffer.from(':root { --text: white; }'); @@ -19,3 +19,26 @@ describe('readTheme', () => { expect(await readTheme(undefined, '/nonexistent/theme.css')).toBeUndefined(); }); }); + +describe('setLegalLinks', () => { + const page = (): Map => + { + const index = { body: Buffer.from('
'), type: 'text/html', immutable: false }; + return new Map([['/index.html', index], ['/', index]]); + }; + + it('links only the configured legal pages, with their English names', () => { + const assets = page(); + setLegalLinks(assets, [{ route: 'datenschutz', aliases: ['privacy'], file: 'datenschutz.md' }]); + + expect(assets.get('/')?.body.toString()).toBe('
'); + expect(assets.get('/index.html')).toBe(assets.get('/')); + }); + + it('leaves the header empty without legal pages', () => { + const assets = page(); + setLegalLinks(assets, []); + + expect(assets.get('/')?.body.toString()).toBe('
'); + }); +}); diff --git a/apps/backend/src/assets.ts b/apps/backend/src/assets.ts index bd123b5..146a78e 100644 --- a/apps/backend/src/assets.ts +++ b/apps/backend/src/assets.ts @@ -1,7 +1,7 @@ import { existsSync, readdirSync, readFileSync } from 'node:fs'; import { readFile } from 'node:fs/promises'; import { extname, join, relative, sep } from 'node:path'; -import type { Font } from '@homepage/indexer'; +import { escapeHtml, type Font, type LegalPage } from '@homepage/indexer'; export type Asset = { body: Buffer; @@ -53,6 +53,27 @@ export function loadAssets(directory: string): Map return assets; } +/** + * Writes the configured legal pages into the header of the terminal page, + * which the frontend build cannot know. Without legal pages the header has no links. + */ +export function setLegalLinks(assets: Map, legal: LegalPage[]): void +{ + const index = assets.get('/index.html'); + if (!index) return; + + const links = legal.map((page) => + { + const english = page.aliases[0] ? ` data-en="${escapeHtml(page.aliases[0])}"` : ''; + return `${escapeHtml(page.route)}`; + }); + + const html = index.body.toString('utf8').replace(/