Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Sanitizer): read options from config #1183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

Comment on lines -16 to -21
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this function since it wasn't used, and to prevent any confusion with the non-static sanitize method

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