Skip to content

Commit

Permalink
Strip HTML comments in data attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Dec 12, 2024
1 parent 32b0431 commit 40d7a06
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/test/unit/html_sanitizer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ testGroup("HTMLSanitizer", () => {
assert.equal(document, expectedHTML)
})
})

test("strips HTML comments", () => {
const html = "<div><!-- --></div>"
const expectedHTML = "<div></div>"
const document = HTMLSanitizer.sanitize(html).body.innerHTML
assert.equal(document, expectedHTML)
})

test("strips HTML comments in attributes", () => {
const html = "<div data-trix-attachment=\"<!-- -->\"></div>"
const expectedHTML = "<div data-trix-attachment=\"\"></div>"
const document = HTMLSanitizer.sanitize(html).body.innerHTML
assert.equal(document, expectedHTML)
})
})

const withDOMPurifyConfig = (attrConfig = {}, fn) => {
Expand Down
7 changes: 2 additions & 5 deletions src/trix/models/html_sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ export default class HTMLSanitizer extends BasicObject {
this.sanitizeElement(node)
}
break
case Node.COMMENT_NODE:
nodesToRemove.push(node)
break
}
}

Expand Down Expand Up @@ -124,8 +121,8 @@ export default class HTMLSanitizer extends BasicObject {
}

const createBodyElementForHTML = function(html = "") {
// Remove everything after </html>
html = html.replace(/<\/html[^>]*>[^]*$/i, "</html>")
// Remove everything after </html> and HTML comments
html = html.replace(/<\/html[^>]*>[^]*$/i, "</html>").replace(/(<!--.*?-->)/g, "")
const doc = document.implementation.createHTMLDocument("")
doc.documentElement.innerHTML = html

Expand Down

0 comments on commit 40d7a06

Please sign in to comment.