Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

slides: sanitizeHtml walks an element's children before unwrapping it (#467)

nyblnet95b6b972edd7
diff --git a/scripts/test-sanitize.ts b/scripts/test-sanitize.ts
index cdb935df..337b6797 100644
--- a/scripts/test-sanitize.ts
+++ b/scripts/test-sanitize.ts
@@ -299,7 +299,7 @@ const CHROME = [
* it. Written without backticks or `${` so it can live in a template literal.
*/
const probeSource = (renderPath: string, modelPath: string) => `
-import { renderSlide } from ${JSON.stringify(renderPath)}
+import { renderSlide, sanitizeHtml } from ${JSON.stringify(renderPath)}
import { newDoc } from ${JSON.stringify(modelPath)}
const O = location.origin
@@ -358,6 +358,25 @@ if (location.pathname === '/meta.html') {
check('3 — no <base> survives the walk', based.querySelectorAll('base').length === 0)
check('3 — relative urls still resolve against this document', document.baseURI === baseBefore)
+ // --- 3c. nested markup is walked like top-level markup -----------------
+ // An allowed tag inside a tag the walk does not know, at any depth, is
+ // held to the same rule as one at the top level. Control: the same child
+ // under an allowed parent.
+ const nested = sanitizeHtml(
+ '<section><b onclick="window.__pwn(41)">a</b></section>' +
+ '<foo><span style="position:fixed;inset:0">b</span></foo>' +
+ '<div><foo><bar><i onmouseover="window.__pwn(42)">c</i></bar></foo></div>' +
+ '<div><b onclick="window.__pwn(45)">e</b></div>')
+ const nbox = document.createElement('div'); nbox.innerHTML = nested
+ check('3c — nested markup is walked like top-level markup: no attribute survives at any depth',
+ !nbox.querySelector('[onclick],[onmouseover],[style]') && nbox.textContent === 'abce')
+ document.body.appendChild(nbox)
+ for (const el of Array.from(nbox.querySelectorAll('b, i, span'))) {
+ el.dispatchEvent(new MouseEvent('click', { bubbles: true }))
+ el.dispatchEvent(new MouseEvent('mouseover', { bubbles: true }))
+ }
+ check('3c — and clicking or hovering the lifted elements runs nothing', ![41, 42, 45].some((n) => pwned.includes(n)))
+
// --- 4. network out of a self-contained file -------------------------------
draw('<div>x</div><link rel="stylesheet" href="' + O + '/tracker.css">' +
'<svg><style>@import url("' + O + '/imported.css");.q{fill:red}</sty' + 'le>' +
diff --git a/slides/src/render.ts b/slides/src/render.ts
index f8b05e94..61d435c9 100644
--- a/slides/src/render.ts
+++ b/slides/src/render.ts
@@ -513,7 +513,9 @@ export function sanitizeHtml(html: string): string {
if (child.nodeType === Node.ELEMENT_NODE) {
const elChild = child as HTMLElement
if (!ALLOWED_TAGS.has(elChild.tagName)) {
- // unwrap unknown elements, keep their text
+ // unwrap unknown elements, keep their text — walking what they held
+ // first, so lifted children are held to the same rule as siblings
+ walk(elChild)
while (elChild.firstChild) node.insertBefore(elChild.firstChild, elChild)
elChild.remove()
continue