mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
perf(shared): keep the DOMPurify sanitisers out of the root barrel
isomorphic-dompurify builds a jsdom window as soon as it loads on Node, and the root barrel is what the server and each of its ~900 test files import, so every one of them loaded jsdom without ever sanitising. The sanitisers move behind @trek/shared/sanitize (a build entry of its own, like roadtrip), escapeHtml stays in the root from its own module, and the client's translation context imports the sanitiser from the subpath. The production server stops loading jsdom at boot as well.
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -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.
|
||||
|
||||
+3
-3
@@ -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/<locale>`.
|
||||
`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/<locale>`, `@trek/shared/roadtrip` and `@trek/shared/sanitize` (the DOMPurify sanitisers, kept out of the root barrel, see below).
|
||||
|
||||
## Contracts (`src/<domain>/<domain>.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`)
|
||||
|
||||
|
||||
@@ -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": {
|
||||
|
||||
+4
-3
@@ -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.
|
||||
|
||||
@@ -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, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
/**
|
||||
* Strict inline sanitiser. Use for short, mostly-text strings that may include
|
||||
* basic emphasis (`<strong>`, `<em>`, …) — e.g. the Journey suggestion banner
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user