Skip to content

Commit

Permalink
fix(Sanitizer): read options from config
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Sep 2, 2024
1 parent 457a834 commit 35a0eea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/trix/config/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ export default {
removeBlankTableCells: false,
tableCellSeparator: " | ",
tableRowSeparator: "\n",
allowedAttributes: "style href src width height language class".split(" "),
forbiddenProtocols: "javascript:".split(" "),
forbiddenElements: "script iframe form noscript".split(" "),
}
19 changes: 5 additions & 14 deletions src/trix/models/html_sanitizer.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import * as config from "trix/config"
import BasicObject from "trix/core/basic_object"

import { nodeIsAttachmentElement, removeNode, tagName, walkTree } from "trix/core/helpers"

const DEFAULT_ALLOWED_ATTRIBUTES = "style href src width height language class".split(" ")
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) {
const sanitizedElement = new this(html).sanitize()
const sanitizedHtml = sanitizedElement.getHTML ? sanitizedElement.getHTML() : sanitizedElement.outerHTML
element.innerHTML = sanitizedHtml
}

static sanitize(html, options) {
const sanitizer = new this(html, options)
sanitizer.sanitize()
return sanitizer
}

constructor(html, { allowedAttributes, forbiddenProtocols, forbiddenElements } = {}) {
constructor(html) {
super(...arguments)
this.allowedAttributes = allowedAttributes || DEFAULT_ALLOWED_ATTRIBUTES
this.forbiddenProtocols = forbiddenProtocols || DEFAULT_FORBIDDEN_PROTOCOLS
this.forbiddenElements = forbiddenElements || DEFAULT_FORBIDDEN_ELEMENTS
this.allowedAttributes = config.parser.allowedAttributes
this.forbiddenProtocols = config.parser.forbiddenProtocols
this.forbiddenElements = config.parser.forbiddenElements
this.body = createBodyElementForHTML(html)
}

Expand Down

0 comments on commit 35a0eea

Please sign in to comment.