Files
inline-comment/test/dom-sinks.test.js
T
Oliver_Herb 9053327486 Inline Comment 0.2.0 - initial public release
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)
2026-11-24 12:07:09 +01:00

93 lines
3.2 KiB
JavaScript

/*
* Copyright 2026 Oliver Herb
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
"use strict";
/*
* The add-on must not write markup into DOM sinks: addons.thunderbird.net
* reviewers reject innerHTML/outerHTML assignments, insertAdjacentHTML and
* document.write. DOM is built with createElement, textContent and
* setAttribute instead. Reading outerHTML (to serialize a node for
* execCommand "insertHTML") is allowed.
*
* These are static checks: every file below src/ is read as text and matched
* against regular expressions for the forbidden sinks; no DOM is involved.
*/
const { test } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const SRC = path.join(__dirname, "..", "src");
const FORBIDDEN = Object.freeze([
{ name: "innerHTML", pattern: /innerHTML/ },
{ name: "outerHTML =", pattern: /outerHTML\s*\+?=(?!=)/ },
{ name: "insertAdjacentHTML", pattern: /insertAdjacentHTML/ },
{ name: "document.write", pattern: /document\.write(?:ln)?\s*\(/ },
]);
/**
* Lists all files below dir, recursively (every type: scripts, pages,
* styles, JSON, images).
* @param {string} dir Absolute directory path.
* @returns {string[]} Absolute paths of all files found.
*/
function allFiles(dir) {
const files = [];
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...allFiles(full));
} else {
files.push(full);
}
}
return files;
}
/**
* Finds the forbidden DOM sinks that occur in a source text.
* @param {string} source File content to check.
* @returns {string[]} Names (from FORBIDDEN) of the sinks found; empty if
* the text is clean.
*/
function sinksIn(source) {
return FORBIDDEN.filter(({ pattern }) => pattern.test(source)).map(
({ name }) => name,
);
}
test("the sink patterns catch writes and allow reading outerHTML", () => {
assert.deepEqual(sinksIn("el.innerHTML = html;"), ["innerHTML"]);
assert.deepEqual(sinksIn("const x = el.innerHTML;"), ["innerHTML"]);
assert.deepEqual(sinksIn("el.outerHTML = html;"), ["outerHTML ="]);
assert.deepEqual(sinksIn("el.outerHTML=html;"), ["outerHTML ="]);
assert.deepEqual(sinksIn("el.outerHTML += html;"), ["outerHTML ="]);
assert.deepEqual(sinksIn("el.insertAdjacentHTML('beforeend', html);"), [
"insertAdjacentHTML",
]);
assert.deepEqual(sinksIn("document.write('<p>');"), ["document.write"]);
assert.deepEqual(sinksIn('exec("insertHTML", node.outerHTML);'), []);
assert.deepEqual(sinksIn("if (a.outerHTML === b) {}"), []);
});
test("no file in src/ writes markup into a DOM sink", () => {
const files = allFiles(SRC);
assert.ok(
files.some((file) => file.endsWith(path.join("compose", "compose.js"))),
"src/ was not scanned",
);
for (const file of files) {
const found = sinksIn(fs.readFileSync(file, "utf8"));
assert.deepEqual(found, [], path.relative(SRC, file));
}
});