Skip to content

Commit

Permalink
chore: Replace html-sanitizer with dompurify (#4390)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamareebjamal authored May 19, 2020
1 parent d7146e6 commit 3e1de9e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 96 deletions.
45 changes: 28 additions & 17 deletions app/services/sanitizer.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
import Service from '@ember/service';
import sanitizeHtml from 'sanitize-html';
import createDOMPurify from 'dompurify';

export default Service.extend({

sanitize: null,

_purify: null,

async init() {
this._super(...arguments);
if (typeof window !== 'undefined') {
this._purify = createDOMPurify(self);
} else {
const { JSDOM } = await import('jsdom');

this._purify = createDOMPurify(new JSDOM('').window);
}
this._purify.addHook('beforeSanitizeElements', function(node) {
if ('href' in node) {
node.setAttribute('target', '_blank');
node.setAttribute('rel', 'nofollow noopener');
}
});
},

// Ensure any changes to the sanitizer rules are set in the rich text editor @ components/widgets/forms/rich-text-editor.js
options: {
allowedTags : ['b', 'strong', 'i', 'em', 'u', 'ol', 'ul', 'li', 'a', 'p'],
allowedAttributes : {
'a': ['href', 'rel', 'target']
},
selfClosing : ['br'],
allowedSchemes : ['http', 'https', 'ftp', 'mailto'],
allowedSchemesByTag : {},
allowProtocolRelative : false,
transformTags : {
'i' : 'em',
'b' : 'strong',
'a' : sanitizeHtml.simpleTransform('a', { rel: 'nofollow', target: '_blank' })
}
allowedAttributes : ['href', 'rel', 'target']
},

purify(string) {
return sanitizeHtml(string, this.options);
return this._purify.sanitize(string, {
ALLOWED_TAGS : this.options.allowedTags,
ALLOWED_ATTR : this.options.allowedAttributes
});
},

strip(string) {
return sanitizeHtml(string, {
allowedTags : [],
allowedAttributes : []
return this._purify.sanitize(string, {
ALLOWED_TAGS : [],
ALLOWED_ATTR : []
});
}
});
5 changes: 1 addition & 4 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ module.exports = function(defaults) {
},
autoImport: {
webpack: {
node: {
path: true // TODO: Remove after https://github.com/fossasia/open-event-frontend/issues/3956
},
externals : { jquery: 'jQuery' },
plugins : env === 'production' ? [
plugins : process.env.ANALYE_BUNDLE === 'true' ? [
new BundleAnalyzerPlugin({
analyzerMode : 'static',
openAnalyzer : false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"broccoli-persistent-filter": "^3.0.0",
"croppie": "^2.6.4",
"css-loader": "^3.5.3",
"dompurify": "^2.0.11",
"ember-ajax": "5.0.0",
"ember-auto-import": "^1.5.3",
"ember-classic-decorator": "^1.0.8",
Expand Down Expand Up @@ -139,7 +140,6 @@
"pre-commit": "^1.2.2",
"query-string": "^6.12.1",
"qunit-dom": "^1.2.0",
"sanitize-html": "^1.23.0",
"sass": "^1.26.5",
"semantic-ui-calendar": "^0.0.8",
"semantic-ui-ember": "3.0.4",
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/services/sanitizer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ module('Unit | Service | sanitizer', function(hooks) {

sanitizedString = service.purify('<script src="https:/hackers.inc/xss.js">');
assert.ok(sanitizedString === '', sanitizedString);

assert.equal(service.purify('<a href="www.google.com">Google</a>'), '<a rel="nofollow noopener" target="_blank" href="www.google.com">Google</a>');
});

test('test strip', function(assert) {
const service = this.owner.lookup('service:sanitizer');

assert.equal(service.strip('<p>This is amazing</p><br /><a href="google.com">Google</a>'), 'This is amazingGoogle');
});
});
80 changes: 6 additions & 74 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5498,14 +5498,6 @@ dom-serializer@0, dom-serializer@~0.1.0:
domelementtype "~1.1.1"
entities "~1.1.1"

dom-serializer@^0.2.1:
version "0.2.2"
resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-0.2.2.tgz#1afb81f533717175d478655debc5e332d9f9bb51"
integrity sha512-2/xPb3ORsQ42nHYiSunXkDjPLBaEj/xTwUO4B7XCZQTRk7EBtTOPaygh10YAAh2OI1Qrp6NWfpAhzswj0ydt9g==
dependencies:
domelementtype "^2.0.1"
entities "^2.0.0"

dom-walk@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.1.tgz#672226dc74c8f799ad35307df936aba11acd6018"
Expand All @@ -5518,11 +5510,6 @@ domelementtype@1:
version "1.3.0"
resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-1.3.0.tgz#b17aed82e8ab59e52dd9c19b1756e0fc187204c2"

domelementtype@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-2.0.1.tgz#1f8bdfe91f5a78063274e803b4bdcedf6e94f94d"
integrity sha512-5HOHUDsYZWV8FGWN0Njbr/Rn7f/eWSQi1v7+HsUVwXgn8nWWlL64zKDkS0n8ZmQ3mlWOMuXOnR+7Nx/5tMO5AQ==

domelementtype@~1.1.1:
version "1.1.3"
resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-1.1.3.tgz#bd28773e2642881aec51544924299c5cd822185b"
Expand All @@ -5540,12 +5527,10 @@ domhandler@2.3:
dependencies:
domelementtype "1"

domhandler@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/domhandler/-/domhandler-3.0.0.tgz#51cd13efca31da95bbb0c5bee3a48300e333b3e9"
integrity sha512-eKLdI5v9m67kbXQbJSNn1zjh0SDzvzWVWtX+qEI3eMjZw8daH9k8rlj1FZY9memPwjiskQFbe7vHVVJIAqoEhw==
dependencies:
domelementtype "^2.0.1"
dompurify@^2.0.11:
version "2.0.11"
resolved "https://registry.yarnpkg.com/dompurify/-/dompurify-2.0.11.tgz#cd47935774230c5e478b183a572e726300b3891d"
integrity sha512-qVoGPjIW9IqxRij7klDQQ2j6nSe4UNWANBhZNLnsS7ScTtLb+3YdxkRY8brNTpkUiTtcXsCJO+jS0UCDfenLuA==

domutils@1.4:
version "1.4.3"
Expand All @@ -5560,15 +5545,6 @@ domutils@1.5:
dom-serializer "0"
domelementtype "1"

domutils@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/domutils/-/domutils-2.0.0.tgz#15b8278e37bfa8468d157478c58c367718133c08"
integrity sha512-n5SelJ1axbO636c2yUtOGia/IcJtVtlhQbFiVDBZHKV5ReJO1ViX7sFEemtuyoAnBxk5meNSYgA8V4s0271efg==
dependencies:
dom-serializer "^0.2.1"
domelementtype "^2.0.1"
domhandler "^3.0.0"

dot-case@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/dot-case/-/dot-case-3.0.3.tgz#21d3b52efaaba2ea5fda875bb1aa8124521cf4aa"
Expand Down Expand Up @@ -7359,7 +7335,7 @@ entities@^1.1.1, entities@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0"

entities@^2.0.0, entities@~2.0.0:
entities@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-2.0.0.tgz#68d6084cab1b079767540d80e56a39b423e4abf4"
integrity sha512-D9f7V0JSRwIxlRI2mjMqufDrRDnx8p+eEOz7aUM9SuvF8gsBzra0/6tbjl1m8eQHrZlYj6PxqE00hZ1SAIKPLw==
Expand Down Expand Up @@ -9359,16 +9335,6 @@ html-minifier-terser@^5.0.2:
relateurl "^0.2.7"
terser "^4.3.9"

htmlparser2@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-4.1.0.tgz#9a4ef161f2e4625ebf7dfbe6c0a2f52d18a59e78"
integrity sha512-4zDq1a1zhE4gQso/c5LP1OtrhYTncXNSpvJYtWJBtXAETPlMfi3IFNjGuQbYLuVY4ZR0QMqRVvo4Pdy9KLyP8Q==
dependencies:
domelementtype "^2.0.1"
domhandler "^3.0.0"
domutils "^2.0.0"
entities "^2.0.0"

htmlparser2@~3.8.1:
version "3.8.3"
resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.8.3.tgz#996c28b191516a8be86501a7d79757e5c70c1068"
Expand Down Expand Up @@ -10754,10 +10720,6 @@ lodash.escape@~2.3.0:
lodash._reunescapedhtml "~2.3.0"
lodash.keys "~2.3.0"

lodash.escaperegexp@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz#64762c48618082518ac3df4ccf5d5886dae20347"

lodash.find@^4.5.1, lodash.find@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.find/-/lodash.find-4.6.0.tgz#cb0704d47ab71789ffa0de8b97dd926fb88b13b1"
Expand Down Expand Up @@ -10855,10 +10817,6 @@ lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"

lodash.isstring@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"

lodash.kebabcase@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.kebabcase/-/lodash.kebabcase-4.1.1.tgz#8489b1cb0d29ff88195cceca448ff6d6cc295c36"
Expand Down Expand Up @@ -10903,11 +10861,6 @@ lodash.merge@^4.6.0, lodash.merge@^4.6.2:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.mergewith@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz#617121f89ac55f59047c7aec1ccd6654c6590f55"
integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==

lodash.noop@~2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/lodash.noop/-/lodash.noop-2.3.0.tgz#3059d628d51bbf937cd2a0b6fc3a7f212a669c2c"
Expand Down Expand Up @@ -13921,22 +13874,6 @@ sane@^4.0.0, sane@^4.1.0:
minimist "^1.1.1"
walker "~1.0.5"

sanitize-html@^1.23.0:
version "1.23.0"
resolved "https://registry.yarnpkg.com/sanitize-html/-/sanitize-html-1.23.0.tgz#e7a5ce7427cd2844dae5b9961cd372e349f91fb5"
integrity sha512-7MgUrbZpaig6zHwuHjpNqhkiuutFPWWoFY/RmdtEnvrFKMLafzSHfFyOozVpKWytkZIUhbYu3VQ/93OmYdo3ag==
dependencies:
chalk "^2.4.1"
htmlparser2 "^4.1.0"
lodash.clonedeep "^4.5.0"
lodash.escaperegexp "^4.1.2"
lodash.isplainobject "^4.0.6"
lodash.isstring "^4.0.1"
lodash.mergewith "^4.6.2"
postcss "^7.0.27"
srcset "^2.0.1"
xtend "^4.0.1"

sass-graph@^2.2.4:
version "2.2.4"
resolved "https://registry.yarnpkg.com/sass-graph/-/sass-graph-2.2.4.tgz#13fbd63cd1caf0908b9fd93476ad43a51d1e0b49"
Expand Down Expand Up @@ -14530,11 +14467,6 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=

srcset@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/srcset/-/srcset-2.0.1.tgz#8f842d357487eb797f413d9c309de7a5149df5ac"
integrity sha512-00kZI87TdRKwt+P8jj8UZxbfp7mK2ufxcIMWvhAOZNJTRROimpHeruWrGvCZneiuVDLqdyHefVp748ECTnyUBQ==

sshpk@^1.7.0:
version "1.14.1"
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.14.1.tgz#130f5975eddad963f1d56f92b9ac6c51fa9f83eb"
Expand Down Expand Up @@ -15933,7 +15865,7 @@ xmlhttprequest-ssl@~1.5.4:
version "1.5.5"
resolved "https://registry.yarnpkg.com/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz#c2876b06168aadc40e57d97e81191ac8f4398b3e"

xtend@^4.0.0, xtend@^4.0.1, xtend@~4.0.0, xtend@~4.0.1:
xtend@^4.0.0, xtend@~4.0.0, xtend@~4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"

Expand Down

1 comment on commit 3e1de9e

@vercel
Copy link

@vercel vercel bot commented on 3e1de9e May 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.