Skip to content

Commit

Permalink
Backport XSS and mutation XSS fix to 1.3
Browse files Browse the repository at this point in the history
Backport adding DomPurify to Trix 1.3.x.

The vendored DOMPurify was created by running the dompurify node modules through
Babel to covert to es5 syntax.
  • Loading branch information
djmb committed Dec 9, 2024
1 parent d342fbf commit 6c263d2
Show file tree
Hide file tree
Showing 15 changed files with 604 additions and 15 deletions.
1 change: 1 addition & 0 deletions .blade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load_paths:
- test/vendor
- assets
- polyfills
- vendor
- src

logical_paths:
Expand Down
1 change: 1 addition & 0 deletions assets/trix-core.coffee
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#= require trix/banner
#= require trix/index
#= require vendor
1 change: 1 addition & 0 deletions assets/trix.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#= require trix/banner
#= require polyfills
#= require trix/index
#= require vendor
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"assets",
"bin",
"polyfills",
"vendor",
"src",
"test",
"*.md",
Expand Down
20 changes: 13 additions & 7 deletions dist/trix-core.js

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions dist/trix.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
"url": "https://github.com/basecamp/trix/issues"
},
"homepage": "https://trix-editor.org/",
"dependencies": {
"dompurify": "^3.2.0"
},
"devDependencies": {
"@babel/preset-env": "^7.26.0",
"svgo": "^0.6.1"
}
}
1 change: 1 addition & 0 deletions src/trix/inspector/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#= require ./control_element
#= require_tree ./templates
#= require_tree ./views
#= require_tree ./vendor

Trix.Inspector =
views: []
Expand Down
1 change: 1 addition & 0 deletions src/trix/models/html_sanitizer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Trix.HTMLSanitizer extends Trix.BasicObject
sanitize: ->
@sanitizeElements()
@normalizeListElementNesting()
DOMPurify.sanitize @body, ADD_ATTR: ["language"], RETURN_DOM: true

getHTML: ->
@body.innerHTML
Expand Down
1 change: 1 addition & 0 deletions test/.blade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load_paths:
- vendor
- ../assets
- ../polyfills
- ../vendor
- ../src

build:
Expand Down
24 changes: 24 additions & 0 deletions test/src/system/pasting_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ testGroup "Pasting", template: "editor_empty", ->
delete window.unsanitized
done()

test "paste data-trix-attachment encoded mathml", (done) ->
window.unsanitized = []
pasteData =
"text/plain": "x",
"text/html": "copy<div data-trix-attachment=\"{&quot;contentType&quot;:&quot;text/html5&quot;,&quot;content&quot;:&quot;&lt;math&gt;&lt;mtext&gt;&lt;table&gt;&lt;mglyph&gt;&lt;style&gt;&lt;img src=x onerror=alert()&gt;&lt;/style&gt;XSS POC&quot;}\"></div>me"

pasteContent pasteData, ->
after 20, ->
assert.deepEqual window.unsanitized, []
delete window.unsanitized
done()

test "paste data-trix-attachment encoded embed", (done) ->
window.unsanitized = []
pasteData =
"text/plain": "x",
"text/html": "copy<div data-trix-attachment=\"{&quot;contentType&quot;:&quot;text/html5&quot;,&quot;content&quot;:&quot;&lt;embed src='javascript:alert(1)'&gt;XSS POC&quot;}\"></div>me"

pasteContent pasteData, ->
after 20, ->
assert.deepEqual window.unsanitized, []
delete window.unsanitized
done()

test "prefers plain text when html lacks formatting", (expectDocument) ->
pasteData =
"text/html": "<meta charset='utf-8'>a\nb"
Expand Down
3 changes: 3 additions & 0 deletions vendor/dompurify-banner/index.coffee.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
###
<%= depend_on_asset("dompurify-banner.txt").to_s.chomp %>
###
4 changes: 4 additions & 0 deletions vendor/dompurify-banner/index.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DOMPurify 3.2.1
Copyright © Cure53 and other contributors
Released under the Apache license 2.0 and Mozilla Public License 2.0
github.com/cure53/DOMPurify/blob/3.2.1/LICENSE
533 changes: 533 additions & 0 deletions vendor/dompurify.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions vendor/vendor.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#= require ./dompurify-banner
#= require ./dompurify

0 comments on commit 6c263d2

Please sign in to comment.