fix(16.2): WR-05 keep only view-safe attributes in the editor schema

captureAttributes and renderCaptured dropped only on* names, so id,
name, style, srcset, background and the like from stored HTML were
rendered by ProseMirror into the live admin DOM (DOM clobbering of
document.querySelector, fixed-position overlays, remote loads). htmlAttrs
now keeps an allowlist: class, title, alt, width, height, colspan,
rowspan, start, rel, target and data-*; href and src stay with the link
and image nodes. Other attributes are dropped from a block once it is
edited; untouched documents and untouched blocks keep their bytes.
This commit is contained in:
2026-10-10 16:38:25 +02:00
parent 2c961b8408
commit f1e6c11107
5 changed files with 138 additions and 60 deletions

View File

@@ -139,27 +139,68 @@ describe('figures', () => {
})
describe('marks and global attributes', () => {
it('keeps a span with attributes as a mark and drops a bare span', () => {
expect(shape(model('<p>a <span class="hl" style="color: red">b</span> c</p>'))).toBe(shape('<p>a <span class="hl" style="color: red">b</span> c</p>'))
it('keeps a span with kept attributes as a mark and drops a bare span', () => {
expect(shape(model('<p>a <span class="hl" title="t" data-x="1">b</span> c</p>'))).toBe(shape('<p>a <span class="hl" title="t" data-x="1">b</span> c</p>'))
expect(model('<p>a <span>b</span> c</p>')).toBe('<p>a b c</p>')
})
it('drops a span whose attributes are all view-unsafe', () => {
expect(model('<p>a <span style="color: red" id="s">b</span> c</p>')).toBe('<p>a b c</p>')
expect(model('<p>a <span class="hl" style="color: red">b</span> c</p>')).toBe('<p>a <span class="hl">b</span> c</p>')
})
it.each([
['paragraph', '<p class="lead" data-x="1">a</p>'],
['heading', '<h2 id="intro" class="h">a</h2>'],
['heading', '<h2 class="h" title="Intro">a</h2>'],
['blockquote', '<blockquote class="q"><p>a</p></blockquote>'],
['bullet list and item', '<ul class="list"><li class="item"><p>a</p></li></ul>'],
['ordered list with start', '<ol start="3" class="steps"><li><p>a</p></li></ol>'],
['code block', '<pre class="code"><code>a</code></pre>'],
['horizontal rule', '<hr class="sep">'],
['image', '<p><img src="/a.jpg" alt="a" class="photo" loading="lazy"></p>'],
['image', '<p><img src="/a.jpg" alt="a" class="photo" data-id="7"></p>'],
['figcaption', '<figure><img src="/a.jpg" alt="a"><figcaption class="cap">c</figcaption></figure>'],
['link', '<p><a href="/a" class="more" data-id="7">a</a></p>'],
['table, row and cells', '<table class="t"><tbody><tr class="r"><td class="c" style="text-align: right">a</td></tr></tbody></table>'],
['table, row and cells', '<table class="t" width="100%"><tbody><tr class="r"><td class="c" data-x="1" width="50" height="20">a</td></tr></tbody></table>'],
])('keeps unmodelled attributes on a %s', (_name, html) => {
expect(shape(model(html))).toBe(shape(html))
})
// WR-05: ProseMirror renders captured attributes into the live admin DOM,
// so only view-safe ones are kept once a block is serialized from the model.
it.each([
['id', '<h2 id="intro" class="h">a</h2>', '<h2 class="h">a</h2>'],
['name', '<p><img src="/a.jpg" alt="a" name="querySelector"></p>', '<p><img src="/a.jpg" alt="a"></p>'],
['style', '<p class="x" style="position:fixed;inset:0;z-index:99999">a</p>', '<p class="x">a</p>'],
['srcset and loading', '<p><img src="/a.jpg" alt="a" srcset="https://evil.test/a.jpg 2x" loading="lazy"></p>', '<p><img src="/a.jpg" alt="a"></p>'],
['background', '<table background="https://evil.test/bg.png"><tbody><tr><td>a</td></tr></tbody></table>', '<table><tbody><tr><td>a</td></tr></tbody></table>'],
['form, contenteditable and popover', '<p form="f" contenteditable="true" popover="auto" class="k">a</p>', '<p class="k">a</p>'],
['itemprop, dir and lang', '<p itemprop="description" dir="rtl" lang="pl">a</p>', '<p>a</p>'],
['a cell style', '<table><tbody><tr><td style="text-align: right" id="c">a</td></tr></tbody></table>', '<table><tbody><tr><td>a</td></tr></tbody></table>'],
])('drops the view-unsafe attribute %s from a block serialized from the model', (_name, html, want) => {
expect(shape(model(html))).toBe(shape(want))
})
it('never renders a view-unsafe stored attribute into the live editor DOM', () => {
const html =
'<h2 id="intro">a</h2><p name="querySelector" style="position:fixed" class="k" data-x="1">b <span style="color:red" id="s" class="hl">c</span></p>' +
'<p><img src="/a.jpg" alt="a" name="img" srcset="https://evil.test/a.jpg 2x" class="photo"></p>' +
'<table background="https://evil.test/bg.png"><tbody><tr><td style="color:red" id="c">d</td></tr></tbody></table>'
const { el } = mount(html)
const view = el.querySelector('.ProseMirror')!
for (const attribute of ['id', 'name', 'srcset', 'background']) {
expect(Array.from(view.querySelectorAll(`[${attribute}]`)).map((node) => node.outerHTML), attribute).toEqual([])
}
// The table node view sets its own min-width style; no stored style
// reaches the view.
const styles = Array.from(view.querySelectorAll('[style]')).map((node) => node.getAttribute('style'))
expect(styles.filter((style) => /fixed|red/.test(style ?? ''))).toEqual([])
expect(view.querySelector('p.k[data-x="1"]')).not.toBeNull()
expect(view.querySelector('span.hl')).not.toBeNull()
expect(view.querySelector('img.photo')).not.toBeNull()
// Untouched, the element still returns the stored bytes (D-03).
expect(el.value).toBe(html)
})
it('never keeps an event handler attribute', () => {
const out = model('<p class="a" onclick="x()" ONMOUSEOVER="y()">a</p><p><img src="/a.jpg" alt="a" onerror="z()"></p>')
expect(out).toBe('<p class="a">a</p><p><img src="/a.jpg" alt="a"></p>')

View File

@@ -146,7 +146,7 @@ describe('preservation schema', () => {
expect(img.getAttributeNames().filter((name) => name.startsWith('on'))).toEqual([])
})
it('keeps unmodelled attributes on edited blocks and spans as marks', () => {
it('keeps view-safe unmodelled attributes on edited blocks and spans as marks', () => {
const html =
'<p class="lead" data-x="1" itemprop="description">Tekst <span style="color: red;" class="s">czerwony</span></p>' +
'<h2 id="h" class="c">Tytuł</h2>' +
@@ -157,17 +157,28 @@ describe('preservation schema', () => {
'<table class="t"><tbody><tr><td class="c" style="text-align: right;">1</td></tr></tbody></table>'
const { el, editor } = mount(html)
insertAtEveryTextblock(editor, 'X')
// WR-05: itemprop, style and id are not view-safe, so an edited block
// drops them; class and data-* survive.
expect(el.value).toBe(
'<p class="lead" data-x="1" itemprop="description">XTekst <span style="color: red;" class="s">czerwony</span></p>' +
'<h2 id="h" class="c">XTytuł</h2>' +
'<p class="lead" data-x="1">XTekst <span class="s">czerwony</span></p>' +
'<h2 class="c">XTytuł</h2>' +
'<ul class="u"><li data-i="1"><p>XPunkt</p></li></ul>' +
'<ol class="o" start="3"><li><p>XTrzy</p></li></ol>' +
'<blockquote class="q"><p>XCytat</p></blockquote>' +
'<p><a class="lnk" data-t="1" href="/x">Xlink</a> <img class="photo" data-id="7" src="/a.png" alt="A"></p>' +
'<table class="t"><tbody><tr><td class="c" style="text-align: right;">X1</td></tr></tbody></table>',
'<table class="t"><tbody><tr><td class="c">X1</td></tr></tbody></table>',
)
})
it('keeps every stored attribute of a block the edit does not touch', () => {
const kept = '<p id="keep" style="color: red;" itemprop="x">Bez zmian</p>'
const html = `<p>Edytowany</p>\n${kept}\n`
const { el, editor } = mount(html)
expect(el.value).toBe(html)
insertAtBlock(editor, 0, 'X')
expect(el.value).toBe(`<p>XEdytowany</p>\n${kept}\n`)
})
it('leaves every other top-level block byte-identical after editing one paragraph', () => {
const { el, editor, changes } = mount(fixture)
insertAtBlock(editor, 1, 'X')