Skip to content

Commit

Permalink
Fix External Imperavi Redactor xss
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzegit committed Aug 9, 2023
1 parent ddf7ac3 commit 7e9d798
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
37 changes: 37 additions & 0 deletions wysiwyg/redactor/files/redactor.js
Expand Up @@ -780,6 +780,7 @@
html = html.replace(/$/g, '$');

html = this.cleanEmpty(html);
html = this.sanitizeHTML(html);

this.$editor.html(html);

Expand All @@ -788,6 +789,41 @@

this.sync();
},
sanitizeHTML: function(htmlStr)
{
function stringToHTML () {
let parser = new DOMParser();
let doc = parser.parseFromString(htmlStr, 'text/html');
return doc.body;
}
function clean (html) {
let nodes = html.children;
for (let node of nodes) {
removeAttributes(node);
clean(node);
}
}
function removeAttributes (elem) {
let atts = elem.attributes;
for (let {name, value} of atts) {
if (!isPossiblyDangerous(name, value)) { continue };
elem.removeAttribute(name);
}

}
function isPossiblyDangerous (name, value) {
let val = value.replace(/\s+/g, '').toLowerCase();
if (['src', 'href', 'xlink:href'].includes(name)) {
if (val.includes('javascript:') || val.includes('data:text/html')) { return true; }
}
if (name.startsWith('on')) { return true; }
}
let html = stringToHTML();

clean(html);

return html.innerHTML;
},
setCodeIframe: function(html)
{
var doc = this.iframePage();
Expand Down Expand Up @@ -822,6 +858,7 @@
html = this.cleanSavePreCode(html, true);
html = this.cleanConverters(html);
html = this.cleanEmpty(html);
html = this.sanitizeHTML(html);

this.$editor.html(html);

Expand Down
13 changes: 1 addition & 12 deletions wysiwyg/redactor/files/redactor.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion wysiwyg/redactor/wysiwyg.class.php
Expand Up @@ -109,7 +109,7 @@ private function loadRedactor() {

$template = cmsTemplate::getInstance();

$template->addJSFromContext('wysiwyg/redactor/files/redactor.js');
$template->addJSFromContext('wysiwyg/redactor/files/redactor.min.js');
$template->addTplJSNameFromContext('files');

$css_file = 'wysiwyg/redactor/files/redactor.css';
Expand Down

0 comments on commit 7e9d798

Please sign in to comment.