Skip to content
This repository has been archived by the owner on Jul 10, 2019. It is now read-only.

Commit

Permalink
[WO-03-002] Fix Insecure Regex Usage on DOMPurify Sanitizer Output (M…
Browse files Browse the repository at this point in the history
…edium)
  • Loading branch information
Tankred Hase committed Apr 22, 2015
1 parent 55406cf commit 246d19b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"Lawnchair",
"_",
"openpgp",
"PhoneNumber"
"PhoneNumber",
"DOMPurify"
],

"globals": {}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"browsersmtp": "https://github.com/whiteout-io/browsersmtp/tarball/master",
"chai": "~1.9.2",
"crypto-lib": "~0.2.1",
"dompurify": "~0.4.2",
"dompurify": "~0.6.3",
"grunt": "~0.4.1",
"grunt-angular-templates": "~0.5.7",
"grunt-autoprefixer": "~0.7.2",
Expand Down Expand Up @@ -78,4 +78,4 @@
"assemble": "~0.4.42",
"handlebars-helper-compose": "~0.2.12"
}
}
}
23 changes: 14 additions & 9 deletions src/js/controller/app/read-sandbox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'use strict';

// add DOMPurify hook to sanitze attributes
DOMPurify.addHook('afterSanitizeAttributes', function(node) {
// open all links in a new window
if ('target' in node) {
node.setAttribute('target', '_blank');
}
});

// set listener for event from main window
window.onmessage = function(e) {
var html = '';
Expand All @@ -13,19 +21,16 @@ window.onmessage = function(e) {
}

// sanitize HTML content: https://github.com/cure53/DOMPurify
html = window.DOMPurify.sanitize(html);
// make links open in a new window
html = html.replace(/<a /g, '<a target="_blank" ');

// remove sources where necessary
if (e.data.removeImages) {
html = html.replace(/(<img[^>]+\b)src=['"][^'">]+['"]/ig, function(match, prefix) {
return prefix;
// remove http leaks
document.body.innerHTML = DOMPurify.sanitize(html, {
FORBID_TAGS: ['style', 'svg', 'audio', 'video'],

This comment has been minimized.

Copy link
@cure53

cure53 Apr 22, 2015

I would recommend to add <math> as prohibited as well.
Otherwise the target-blank setter might not catch all elements.

See: https://github.com/cure53/DOMPurify/blob/master/demos/hooks-target-blank-demo.html#L24

This comment has been minimized.

Copy link
@tanx

tanx Apr 22, 2015

Member

Done: 0dc04e6

FORBID_ATTR: ['src']
});
} else {
document.body.innerHTML = DOMPurify.sanitize(html);
}

document.body.innerHTML = html;

attachClickHandlers();
};

Expand Down

0 comments on commit 246d19b

Please sign in to comment.