mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +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:
+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
|
||||
|
||||
Reference in New Issue
Block a user