diff --git a/client/src/i18n/TranslationContext.tsx b/client/src/i18n/TranslationContext.tsx index 0dda99d6c..1b53e9c3e 100644 --- a/client/src/i18n/TranslationContext.tsx +++ b/client/src/i18n/TranslationContext.tsx @@ -8,8 +8,8 @@ import { getIntlLanguage, isRtlLanguage, escapeHtml, - sanitizeInlineHtml, } from '@trek/shared' +import { sanitizeInlineHtml } from '@trek/shared/sanitize' import type { TranslationStrings } from '@trek/shared/i18n' import { importChunk } from '../utils/chunkReload' import { resolveTemplate } from './resolveTemplate' diff --git a/knip.jsonc b/knip.jsonc index a54be7a67..28ff1f185 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -60,6 +60,7 @@ // The package entry points tsdown builds, one per locale for the lazy chunks. "src/index.ts", "src/roadtrip/planning.ts", + "src/sanitize/sanitize.ts", "src/i18n/index.ts", "src/i18n/*/index.ts", // open-request-schemas.d.mts types the .mjs script beside it. diff --git a/shared/CLAUDE.md b/shared/CLAUDE.md index cee2fdc84..8f4b0cb35 100644 --- a/shared/CLAUDE.md +++ b/shared/CLAUDE.md @@ -25,7 +25,7 @@ Single test: `npx vitest run src/i18n/i18n-parity.spec.ts`, or `npx vitest run - ## Build & exports -`tsdown.config.ts` emits the root barrel, the i18n metadata barrel, and **one entry per locale** so each locale is its own lazy-loadable chunk (the client dynamic-imports them). `zod` is never bundled. The `exports` map exposes `@trek/shared` (contracts), `@trek/shared/i18n` (metadata/types) and `@trek/shared/i18n/`. +`tsdown.config.ts` emits the root barrel, the i18n metadata barrel, and **one entry per locale** so each locale is its own lazy-loadable chunk (the client dynamic-imports them). `zod` is never bundled. The `exports` map exposes `@trek/shared` (contracts), `@trek/shared/i18n` (metadata/types), `@trek/shared/i18n/`, `@trek/shared/roadtrip` and `@trek/shared/sanitize` (the DOMPurify sanitisers, kept out of the root barrel, see below). ## Contracts (`src//.schema.ts`) @@ -59,9 +59,9 @@ The parity rule governs **existing** routes; it is not a license to mint new deb - **Further i18n specs run in `npm test`** and fail changes that parity lets through: placeholder parity (every `{placeholder}` in an `en` string must appear in each translation), no `en` string may call TREK "self-hosted" (the same build runs on managed installs; exemptions live in the spec), and a wording regression test for plugin permission descriptions (currently one string; add a case there when a permission's real behavior changes). None of these are part of `i18n:parity`, which checks key sets, plural forms, count strings and the untranslated ratchet. - **The i18n scripts read the tables as text** through `scripts/i18n-catalog.mjs`: a value must be one string literal on the key's line or the next, and anything else (a concatenation, a template literal) is an error rather than a skipped key. Their specs use the fixtures in `scripts/fixtures/`. That en holds every key the client names is checked on the client side (`npm run lint:i18n-keys` in `client/`). -## Sanitization (`src/sanitize/sanitize.ts`) +## Sanitization (`src/sanitize/sanitize.ts`, `src/sanitize/escape.ts`) -`isomorphic-dompurify` with a minimal inline-only allow-list (a rich-text variant already exists there for when rich text ships). `sanitizeInlineHtml` backs the client's HTML translation path; `escapeHtml` is used wherever the client builds markup from user strings (map popups, account screens). This is meant to be the one home for escaping. No lint rule enforces that yet and a handful of client files still carry a local `escapeHtml` — that is debt to fold in, not a pattern to copy. +`isomorphic-dompurify` with a minimal inline-only allow-list (a rich-text variant already exists there for when rich text ships). The sanitisers are imported from `@trek/shared/sanitize`, never re-exported from the root barrel: on Node, `isomorphic-dompurify` builds a jsdom window as soon as it loads, and the root barrel is what the server and each of its test files import. `sanitizeInlineHtml` backs the client's HTML translation path; `escapeHtml` (DOM-free, in the root barrel) is used wherever the client builds markup from user strings (map popups, account screens). This is meant to be the one home for escaping. No lint rule enforces that yet and a handful of client files still carry a local `escapeHtml` — that is debt to fold in, not a pattern to copy. ## Money (`src/money/money.ts`) diff --git a/shared/package.json b/shared/package.json index c8eb501ce..b0a044202 100644 --- a/shared/package.json +++ b/shared/package.json @@ -50,6 +50,16 @@ "types": "./dist/roadtrip/planning.d.cts", "default": "./dist/roadtrip/planning.cjs" } + }, + "./sanitize": { + "import": { + "types": "./dist/sanitize/sanitize.d.mts", + "default": "./dist/sanitize/sanitize.mjs" + }, + "require": { + "types": "./dist/sanitize/sanitize.d.cts", + "default": "./dist/sanitize/sanitize.cjs" + } } }, "typesVersions": { diff --git a/shared/src/index.ts b/shared/src/index.ts index ca1e58737..b3048b991 100644 --- a/shared/src/index.ts +++ b/shared/src/index.ts @@ -98,9 +98,10 @@ export * from './admin/admin.schema'; export * from './realtime/events.schema'; export * from './roadtrip/day-boundary.schema'; -// Sanitisation helpers — used by the client today, scoped here so the server -// has them ready if rich-text input ever ships. -export * from './sanitize/sanitize'; +// HTML escaping. The DOMPurify sanitisers live behind `@trek/shared/sanitize` +// instead (see sanitize/sanitize.ts): on Node they build a jsdom window when +// they load, and this barrel is what the server imports. +export * from './sanitize/escape'; // WGS-84 ⇄ GCJ-02. Shared because the two boundaries that need it sit on // opposite sides: the Amap provider on the server, the Leaflet CRS on the client. diff --git a/shared/src/sanitize/escape.ts b/shared/src/sanitize/escape.ts new file mode 100644 index 000000000..c45de813f --- /dev/null +++ b/shared/src/sanitize/escape.ts @@ -0,0 +1,16 @@ +/** + * Escapes the five HTML metacharacters so a raw string can be safely + * interpolated into an HTML template. Use this BEFORE substitution when a + * user-controlled value lands inside a markup-shaped translation string. + * + * This is *not* a substitute for `sanitizeInlineHtml`: escape input, then + * sanitise the resulting template — both layers run together in `tHtml`. + */ +export function escapeHtml(value: string): string { + return value + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} diff --git a/shared/src/sanitize/sanitize.spec.ts b/shared/src/sanitize/sanitize.spec.ts index ba504492e..1ec2da748 100644 --- a/shared/src/sanitize/sanitize.spec.ts +++ b/shared/src/sanitize/sanitize.spec.ts @@ -1,4 +1,5 @@ -import { sanitizeInlineHtml, sanitizeRichTextHtml, escapeHtml } from './sanitize'; +import { escapeHtml } from './escape'; +import { sanitizeInlineHtml, sanitizeRichTextHtml } from './sanitize'; import { describe, it, expect } from 'vitest'; diff --git a/shared/src/sanitize/sanitize.ts b/shared/src/sanitize/sanitize.ts index 67aea780e..9427ad508 100644 --- a/shared/src/sanitize/sanitize.ts +++ b/shared/src/sanitize/sanitize.ts @@ -12,6 +12,11 @@ import DOMPurify from 'isomorphic-dompurify'; * Why isomorphic-dompurify: works unchanged in browser (DOMPurify) and Node * (DOMPurify + jsdom). Tree-shakes correctly so the client bundle does not * pull jsdom. + * + * Reached through `@trek/shared/sanitize`, not the root barrel: on Node, + * isomorphic-dompurify builds a jsdom window as soon as it loads, and the + * root barrel is what the server and every server test import. `escapeHtml` + * needs no DOM and stays in the root (./escape.ts). */ // Inline-only tags. Block-level markup (paragraphs, lists, headings) is not @@ -55,23 +60,6 @@ const FULL_TAGS = [ const SAFE_ATTRIBUTES = ['href', 'rel', 'target'] as const; -/** - * Escapes the five HTML metacharacters so a raw string can be safely - * interpolated into an HTML template. Use this BEFORE substitution when a - * user-controlled value lands inside a markup-shaped translation string. - * - * This is *not* a substitute for `sanitizeInlineHtml`: escape input, then - * sanitise the resulting template — both layers run together in `tHtml`. - */ -export function escapeHtml(value: string): string { - return value - .replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"') - .replace(/'/g, '''); -} - /** * Strict inline sanitiser. Use for short, mostly-text strings that may include * basic emphasis (``, ``, …) — e.g. the Journey suggestion banner diff --git a/shared/tsdown.config.ts b/shared/tsdown.config.ts index f7edfd224..91dd965a0 100644 --- a/shared/tsdown.config.ts +++ b/shared/tsdown.config.ts @@ -1,8 +1,15 @@ import { defineConfig } from 'tsdown'; export default defineConfig({ - // Root barrel + i18n metadata barrel + one entry per locale (lazy-load chunks) - entry: ['src/index.ts', 'src/roadtrip/planning.ts', 'src/i18n/index.ts', 'src/i18n/*/index.ts'], + // Root barrel + i18n metadata barrel + one entry per locale (lazy-load chunks), + // plus the DOMPurify sanitisers, kept out of the root (src/sanitize/sanitize.ts) + entry: [ + 'src/index.ts', + 'src/roadtrip/planning.ts', + 'src/sanitize/sanitize.ts', + 'src/i18n/index.ts', + 'src/i18n/*/index.ts', + ], format: ['cjs', 'esm'], dts: true, clean: true,