Thunderbird/Betterbird add-on (Manifest V3): typing inside a quote splits the quote and inserts your own text with a marker such as [OH]:. - English and German user interface - Licensed under the Mozilla Public License 2.0 - English documentation, JSDoc lint gate, 87 tests (node:test + jsdom)
3.0 KiB
0005 – No markup sinks: build DOM with DOM APIs
Date: 2026-11-24 · Status: accepted
Context
Version 0.1.0 assembled the comment block as an HTML string with hand-written escaping and wrote it
into the DOM with innerHTML for the manual split and the options page preview. Reviewers on
addons.thunderbird.net (ATN) reject add-ons that create DOM nodes from HTML strings that may contain
unsanitized data, i.e. code that writes markup into DOM sinks: assignments to innerHTML or
outerHTML, insertAdjacentHTML and document.write. Such sinks are also the usual way for
untrusted text to turn into markup; here the marker template, the identity name and translated
messages all end up in the page.
At the same time, the comment must reach the mail editor through execCommand("insertHTML"): only
editor commands end up in the undo history, so Ctrl+Z keeps working (see
decision 0002).
Decision
- DOM APIs only. Elements are built with
createElement,createTextNode,textContent,setAttribute,append,replaceChildrenandnew Option(). User data and messages are always set as text, never parsed as markup. - Comment block:
createCommentNode()insrc/lib/marker.jsbuilds the<div>with the marker<span>and the first text as a DOM node. The options page preview and the manual split insert this node directly. insertHTMLstays for undo. In the editor, the compose script serializes the same node withouterHTML(reading only) and passes the string toexecCommand("insertHTML", …). The HTML is therefore generated from DOM nodes, never assembled by hand, and text such as<b>in a marker template arrives as escaped text.- Translations are applied with
textContentandsetAttribute(localizeDocument()). BecausetextContentreplaces the whole content, elements withdata-i18nmust contain text only. - Guarded by a test:
test/dom-sinks.test.jsscans every file insrc/and fails oninnerHTML,outerHTMLassignments,insertAdjacentHTMLanddocument.write. ReadingouterHTMLis allowed.
Alternatives considered
- Insert the node with DOM methods (such as
insertBefore()) in the editor as well: no markup string at all, but the change bypasses the editor and Ctrl+Z no longer undoes it. Kept only for the manual split. execCommand("insertText")plus formatting commands: no undo problem, but colours, bold and data attributes would need several commands and are hard to control. Used only for plain-text messages.- Keep escaping HTML strings by hand (as in 0.1.0) or sanitize them: still writes into a markup sink and would not pass the review rule; every new string is another chance to forget the escaping.
Consequences
- New UI code must use DOM APIs;
npm testfails otherwise. - Elements with
data-i18nmust not contain child elements (checked bytest/i18n.test.js). - The preview in the options page and the comment in the editor are built by the same function and look the same.