[HTML5 Article Renderer] Isolate the DOMPurify instance
❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.
Overview
SafeHTML registers its style= allowlist hook on the shared DOMPurify singleton, so the hook applies to every DOMPurify.sanitize() call in Kolibri, not just SafeHTML's. Give SafeHTML its own DOMPurify instance so the hook is scoped to it.
Complexity: Low Target branch: develop
Context
packages/kolibri-common/components/SafeHTML/index.js filters style= down to text-align, color, and background-color with a hook registered at module scope:
DOMPurify.addHook('uponSanitizeAttribute', (node, data) => {
if (data.attrName !== 'style') {
return;
}
...
});
dompurify's default export is one instance shared by every importer, so this hook applies to every sanitize() call in the app. Importing SafeHTML changes sanitization behaviour for any other caller that appears later.
The Change
SafeHTML sanitizes through a DOMPurify instance it owns, with the uponSanitizeAttribute hook registered on that instance. Importing SafeHTML leaves the behaviour of the shared default export untouched.
dompurify@3's default export is callable — (root?: WindowLike) => DOMPurify — so an isolated instance needs no new dependency.
Out of Scope
- Splitting
index.jsinto separate sanitizer and node-mapper modules — the previous scope of this issue. - Changing what the sanitizer allows: tags, attributes, or style properties.
Acceptance Criteria
General
- [ ] SafeHTML sanitizes through a DOMPurify instance it owns; no
addHookcall on the shared default export remains.
Testing
- [ ] A test asserts that a plain
DOMPurify.sanitize(), called from a module that has imported SafeHTML, retains a non-allowlisted property such asfont-size. - [ ] The existing inline-style allowlist cases in
SafeHTML.spec.jspass unchanged.
References
- The
style=allowlist hook was added in #14984. [email protected]typings:interface DOMPurify { (root?: WindowLike): DOMPurify; ... }
AI usage
Claude drafted this issue while checking the HTML5 renderer's sub-issues against the code and a running dev server. It found the global addHook while reading index.js. I checked the callable default export in the installed dompurify typings before agreeing to the approach, and dropped the file-split scope this issue previously carried, which no longer had a case for it.
Plan: SafeHTML sanitizes through a DOMPurify instance it owns, so its style= allowlist hook stops applying to every other DOMPurify.sanitize() caller in the app.
- [ ] Scope the
style=hook to a SafeHTML-owned DOMPurify instance - [ ] Self-review the produced code
@rtibblesbot's comments are generated by an LLM, and should be evaluated accordingly
How was this generated?
- Implemented code following the pre-approved plan using test-driven development (red-green-refactor)
- Ran the project's full test suite and linter before committing
- For UI changes: captured live screenshots to verify rendering
- For backend changes: verified with example API/CLI output