Skip to content

Commit

Permalink
Ensure we always sanitize HTML we set as innerHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
afcapel committed May 28, 2024
1 parent ccf7b20 commit e87da3f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/trix/models/html_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ export default class HTMLParser extends BasicObject {
parse() {
try {
this.createHiddenContainer()
const html = HTMLSanitizer.sanitize(this.html).getHTML()
this.containerElement.innerHTML = html
HTMLSanitizer.setHTML(this.containerElement, this.html)
const walker = walkTree(this.containerElement, { usingFilter: nodeFilter })
while (walker.nextNode()) {
this.processNode(walker.currentNode)
Expand Down
4 changes: 4 additions & 0 deletions src/trix/models/html_sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const DEFAULT_FORBIDDEN_PROTOCOLS = "javascript:".split(" ")
const DEFAULT_FORBIDDEN_ELEMENTS = "script iframe form noscript".split(" ")

export default class HTMLSanitizer extends BasicObject {
static setHTML(element, html) {
element.innerHTML = new this(html).sanitize().getHTML()
}

static sanitize(html, options) {
const sanitizer = new this(html, options)
sanitizer.sanitize()
Expand Down
5 changes: 3 additions & 2 deletions src/trix/views/attachment_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as config from "trix/config"
import { ZERO_WIDTH_SPACE } from "trix/constants"
import { copyObject, makeElement } from "trix/core/helpers"
import ObjectView from "trix/views/object_view"
import HTMLSanitizer from "trix/models/html_sanitizer"

const { css } = config

Expand Down Expand Up @@ -33,7 +34,7 @@ export default class AttachmentView extends ObjectView {
}

if (this.attachment.hasContent()) {
innerElement.innerHTML = this.attachment.getContent()
HTMLSanitizer.setHTML(innerElement, this.attachment.getContent())
} else {
this.createContentNodes().forEach((node) => {
innerElement.appendChild(node)
Expand Down Expand Up @@ -165,6 +166,6 @@ const createCursorTarget = (name) =>

const htmlContainsTagName = function(html, tagName) {
const div = makeElement("div")
div.innerHTML = html || ""
HTMLSanitizer.setHTML(div, html || "")
return div.querySelector(tagName)
}

0 comments on commit e87da3f

Please sign in to comment.