Honor a style= allowlist in the SafeHTML renderer instead of stripping all inline CSS
❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.
Overview
KPUB content is rendered by the safe_html5_viewer plugin through the shared SafeHTML component, which sanitizes with DOMPurify and strips all CSS — <style>/<link> tags and inline style= attributes. Relax the inline-style= handling to honor a bounded property allowlist (text-align, color, background-color), applied globally to all SafeHTML consumers, while still stripping <style> blocks entirely.
Complexity: Medium Target branch: develop
Context
- KPUB (
KPUB_ZIPpreset) renders via thesafe_html5_viewerplugin, not the generic HTML5 or epub viewers (kolibri/plugins/safe_html5_viewer/kolibri_plugin.py:12-15). SafeHtml5RendererIndex.vueloads the zip'sindex.htmland renders it with<SafeHTML :html="html" />.SafeHTML(packages/kolibri-common/components/SafeHTML/index.js) sanitizes with DOMPurify, then rebuilds the node tree with Vueh()into the main app DOM — no iframe, no Shadow DOM.- CSS is stripped by the DOMPurify config:
FORBID_TAGS = ['style','link']andFORBID_ATTR = ['style','width','height'](index.js:8-9). - Because content is injected into the app document (no iframe),
<style>selectors could target app chrome — so<style>must stay forbidden. - Inline
style=is element-scoped, so a bounded allowlist is safe to render. SafeHTMLis shared with the QTI viewer (qti_viewer/frontend/components/AssessmentItem.vue:52); this change applies to all consumers, which is intended.- ricecooker learningequality/ricecooker#685 already sanitizes KPUB ingest to the same allowlist.
- Until this renderer change lands, those retained styles never display.
- The frontend does not consume le_utils; constants are hand-duplicated.
- So the allowlist is a JS constant kept in sync with the ricecooker/le_utils definition by hand.
The Change
- Allow a bounded
style=property allowlist:{text-align, color, background-color}, defined as one module-level JS constant. - Remove
'style'fromFORBID_ATTRso DOMPurify retains the attribute. - Rely on DOMPurify's built-in CSS sanitizer, which still runs first and strips dangerous values (
url(),expression(), …). - Register a module-level DOMPurify
uponSanitizeAttributehook that filters eachstyle=down to the allowlisted properties and rewrites the value. - Drop the attribute when no allowlisted property survives.
- Parse the declarations via the CSSOM (not string-splitting) so the browser validates and normalises values and rejects malformed input.
- Carry the surviving
style=through the Vueh()node reconstruction onto rendered elements, including theSafeHtmlTableandSafeHtmlImagesub-components. - Decide how author
color/background-colorshould behave against the app surface and any future dark theme — needs design input, since content colours are static rather than theme-token-driven.
Recommended sanitization mechanism (established DOMPurify pattern)
Reference sketch — the load-bearing detail this issue exists to de-risk; adapt as needed:
const ALLOWED_STYLE_PROPS = ['text-align', 'color', 'background-color'];
// FORBID_ATTR no longer contains 'style'
DOMPurify.addHook('uponSanitizeAttribute', (node, data) => {
if (data.attrName !== 'style') {
return;
}
// Parse via the CSSOM so the browser validates/normalises values.
const probe = document.createElement('span');
probe.style.cssText = data.attrValue;
const kept = ALLOWED_STYLE_PROPS.map(prop => {
const value = probe.style.getPropertyValue(prop);
return value ? `${prop}: ${value}` : null;
})
.filter(Boolean)
.join('; ');
if (kept) {
data.attrValue = kept;
} else {
data.keepAttr = false; // nothing allowlisted → strip the attribute
}
});
Verify the hook's effect under the test runner's DOM implementation (jsdom).
Out of Scope
- Adding a dark theme for KPUB content, or converting content colours to theme tokens.
- Re-allowing the
width/heightpresentational attributes (also currently stripped). - Building an le_utils→JavaScript codegen bridge to share constants.
- Widening the allowlist beyond
{text-align, color, background-color}. - The ricecooker ingest sanitizer and docx→KPUB conversion (learningequality/ricecooker#685).
- Adding colour controls to the Studio rich text editor (separate, needs design).
Acceptance Criteria
- [ ] KPUB content with
style="text-align:…"renders aligned. - [ ] KPUB content with
style="color:…"renders text colour. - [ ] KPUB content with
style="background-color:…"renders highlight colour. - [ ]
<style>blocks and<link>stylesheets in KPUB content still have no effect. - [ ] Non-allowlisted
style=properties (e.g.position,display) are dropped. - [ ] A
style=with no allowlisted properties is removed entirely. - [ ] Allowlisted styles render inside the
SafeHtmlTableandSafeHtmlImagepaths. - [ ] Colour/background behaviour against the app surface is signed off by design.
- [ ] Tests cover the allowlisted-render, non-allowlisted-drop, empty-after-filter, and
<style>-strip cases.
References
kolibri/plugins/safe_html5_viewer/kolibri_plugin.py:12-15— KPUB preset → rendererkolibri/plugins/safe_html5_viewer/frontend/views/SafeHtml5RendererIndex.vue— loads zip, renders<SafeHTML>packages/kolibri-common/components/SafeHTML/index.js:8-9— DOMPurifyFORBID_TAGS/FORBID_ATTR(the CSS stripping)packages/kolibri-common/components/SafeHTML/SafeHtmlTable.vue,SafeHtmlImage.vue— sub-component render pathskolibri/plugins/qti_viewer/frontend/components/AssessmentItem.vue:52— othercreateSafeHTMLconsumer- DOMPurify
uponSanitizeAttributehook — https://github.com/cure53/DOMPurify#hooks - ricecooker: learningequality/ricecooker#685
AI usage
Drafted with Claude Code (Opus); the renderer path and DOMPurify stripping config were located and read in the Kolibri develop worktree. It is the coupled counterpart to learningequality/ricecooker#685.
Triage: blocking prerequisite before implementation
The technical mechanism here is well-specified and de-risked — the DOMPurify uponSanitizeAttribute hook + CSSOM-parsed allowlist sketch, the FORBID_ATTR change, the sub-component (SafeHtmlTable/SafeHtmlImage) carry-through, and the test cases are all clear enough to implement directly.
One acceptance criterion, however, requires a decision that a coding change can't make on its own:
- [ ] Colour/background behaviour against the app surface is signed off by design.
and correspondingly in The Change:
- Decide how author
color/background-colorshould behave against the app surface and any future dark theme — needs design input, since content colours are static rather than theme-token-driven.
This is a genuine gate rather than a cosmetic note: whether color and background-color belong in the allowlist at all depends on the design call. Static author colours are not theme-token-driven, so against the app surface (and any future dark theme) author text/highlight colours can become low-contrast or unreadable. Possible outcomes design might choose:
- Ship all three (
text-align,color,background-color) as-is. - Ship
text-alignnow, defercolor/background-colorpending a contrast/dark-theme story. - Constrain colours somehow (e.g. only against a guaranteed-light content surface).
Each leads to a different allowlist constant and a different set of AC checkboxes, so implementing before the sign-off risks building the wrong thing.
Question for design: should color/background-color be honored globally against the current app surface as authored, or should this first ship text-align only? Once that's answered the rest is ready to build.
1 Ship all three as is - restrictions will happen at the edit layer.