Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated content injection demo and added onerror in allowed attributes for DOMPurify
- Loading branch information
1 parent
69690bb
commit 3f32c76
Showing
3 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,59 @@ | ||
window.addEventListener('DOMContentLoaded', function() { | ||
var html = document.getElementById('MentalRender').textContent, js=MentalJS(); | ||
js.init({dom:true}); | ||
js.init({ | ||
dom:true, | ||
parseInnerHTML : function(dirty) { | ||
var config = { | ||
ADD_TAGS: ['script'], | ||
ADD_ATTR: ['onclick', 'onmouseover','onerror'] | ||
}; | ||
DOMPurify.addHook('uponSanitizeElement', function(node, data) { | ||
if (data.tagName === 'script') { | ||
var script = node.textContent; | ||
if (!script || 'src' in node.attributes | ||
|| 'href' in node.attributes | ||
|| 'xlink:href' in node.attributes) { | ||
return node.parentNode.removeChild(node) | ||
} | ||
try { | ||
// Pass scripts to MentalJS | ||
var mental = MentalJS().parse( | ||
{ | ||
options: { | ||
eval: false, | ||
dom:true | ||
}, | ||
code:script | ||
} | ||
); | ||
return node.textContent = mental; | ||
} catch (e) { | ||
return node.parentNode.removeChild(node); | ||
} | ||
} | ||
}); | ||
DOMPurify.addHook('uponSanitizeAttribute', function(node, data) { | ||
if (data.attrName.match(/^on\w+/)) { | ||
var script = data.attrValue; | ||
try { | ||
// Pass scripts to MentalJS | ||
return data.attrValue = MentalJS().parse( | ||
{ | ||
options: { | ||
eval: false, | ||
dom: true | ||
}, | ||
code: script | ||
} | ||
); | ||
} catch (e) { | ||
return data.attrValue = ''; | ||
} | ||
} | ||
}); | ||
return DOMPurify.sanitize(dirty, config); | ||
} | ||
}); | ||
js.parse({options:{eval:true},code:'this.sandboxedFunction=function(str){document.body.innerHTML=str}'}); | ||
this.sandboxedFunction$(html); | ||
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters