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

Allow custom attributes and added option to only allow specific elements #1105

Open
wants to merge 2 commits 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
10 changes: 10 additions & 0 deletions src/trix/config/html_sanitizer_allowed_attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const attributes = [
"style",
"href",
"src",
"width",
"height",
"class",
]

export default attributes
3 changes: 3 additions & 0 deletions src/trix/config/html_sanitizer_allowed_elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const allowedElements = []

export default allowedElements
3 changes: 3 additions & 0 deletions src/trix/config/html_sanitizer_allowed_protocols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const allowedProtocols = []

export default allowedProtocols
7 changes: 7 additions & 0 deletions src/trix/config/html_sanitizer_forbidden_elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const forbiddenElements = [
"script",
"iframe",
"form",
]

export default forbiddenElements
5 changes: 5 additions & 0 deletions src/trix/config/html_sanitizer_forbidden_protocols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const forbiddenProtocols = [
"javascript:",
]

export default forbiddenProtocols
5 changes: 5 additions & 0 deletions src/trix/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export { default as parser } from "./parser"
export { default as textAttributes } from "./text_attributes"
export { default as toolbar } from "./toolbar"
export { default as undo } from "./undo"
export { default as htmlSanitizerAllowedAttributes } from "./html_sanitizer_allowed_attributes"
export { default as htmlSanitizerAllowedElements } from "./html_sanitizer_allowed_elements"
export { default as htmlSanitizerAllowedProtocols } from "./html_sanitizer_allowed_protocols"
export { default as htmlSanitizerForbiddenElements } from "./html_sanitizer_forbidden_elements"
export { default as htmlSanitizerForbiddenProtocols } from "./html_sanitizer_forbidden_protocols"
25 changes: 15 additions & 10 deletions src/trix/models/html_sanitizer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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 class".split(" ")
const DEFAULT_FORBIDDEN_PROTOCOLS = "javascript:".split(" ")
const DEFAULT_FORBIDDEN_ELEMENTS = "script iframe form".split(" ")
import {
htmlSanitizerAllowedAttributes,
htmlSanitizerAllowedElements,
htmlSanitizerAllowedProtocols,
htmlSanitizerForbiddenElements,
htmlSanitizerForbiddenProtocols
} from "../config"

export default class HTMLSanitizer extends BasicObject {
static sanitize(html, options) {
Expand All @@ -13,11 +16,13 @@ export default class HTMLSanitizer extends BasicObject {
return sanitizer
}

constructor(html, { allowedAttributes, forbiddenProtocols, forbiddenElements } = {}) {
constructor(html, { allowedAttributes, allowedElements, allowedProtocols, forbiddenProtocols, forbiddenElements } = {}) {
super(...arguments)
this.allowedAttributes = allowedAttributes || DEFAULT_ALLOWED_ATTRIBUTES
this.forbiddenProtocols = forbiddenProtocols || DEFAULT_FORBIDDEN_PROTOCOLS
this.forbiddenElements = forbiddenElements || DEFAULT_FORBIDDEN_ELEMENTS
this.allowedAttributes = allowedAttributes || htmlSanitizerAllowedAttributes
this.allowedElements = allowedElements || htmlSanitizerAllowedElements
this.allowedProtocols = allowedProtocols || htmlSanitizerAllowedProtocols
this.forbiddenElements = forbiddenElements || htmlSanitizerForbiddenElements
this.forbiddenProtocols = forbiddenProtocols || htmlSanitizerForbiddenProtocols
this.body = createBodyElementForHTML(html)
}

Expand Down Expand Up @@ -63,7 +68,7 @@ export default class HTMLSanitizer extends BasicObject {

sanitizeElement(element) {
if (element.hasAttribute("href")) {
if (this.forbiddenProtocols.includes(element.protocol)) {
if (this.forbiddenProtocols.includes(element.protocol) || this.allowedProtocols.length > 0 && !this.allowedProtocols.includes(element.protocol)) {
element.removeAttribute("href")
}
}
Expand Down Expand Up @@ -96,7 +101,7 @@ export default class HTMLSanitizer extends BasicObject {
}

elementIsForbidden(element) {
return this.forbiddenElements.includes(tagName(element))
return this.forbiddenElements.includes(tagName(element)) || this.allowedElements.length > 0 && !this.allowedElements.includes(tagName(element))
}

elementIsntSerializable(element) {
Expand Down
14 changes: 11 additions & 3 deletions src/trix/views/piece_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,25 @@ export default class PieceView extends ObjectView {
}

createContainerElement() {
const attributes = {}
let groupTagName

for (const key in this.attributes) {
const value = this.attributes[key]
const config = getTextConfig(key)
if (config) {
if (config.groupTagName) {
const attributes = {}
if (!groupTagName && config.groupTagName) {
attributes[key] = value
groupTagName = config.groupTagName
} else if (config.groupTagName && groupTagName === config.groupTagName) {
attributes[key] = value
return makeElement(config.groupTagName, attributes)
}
}
}

if (Object.entries(attributes).length > 0 && groupTagName) {
return makeElement(groupTagName, attributes)
}
}

preserveSpaces(string) {
Expand Down