Adding DOM Sanitizer

This commit is contained in:
okbel
2018-02-23 12:29:30 -03:00
parent 91e7dadfda
commit 2f5c4906f7
4 changed files with 32 additions and 13 deletions
+1
View File
@@ -7,6 +7,7 @@
"author": "The Coral Project Team <coral@mozillafoundation.org>",
"license": "Apache-2.0",
"dependencies": {
"dompurify": "^1.0.3",
"pell": "^0.7.0"
}
}
+6
View File
@@ -0,0 +1,6 @@
const config = {
// Super strict rules to make sure users only submit the tags they are allowed
dompurify: { ALLOWED_TAGS: ['b', 'i', 'blockquote'] },
};
module.exports = config;
+21 -13
View File
@@ -1,25 +1,33 @@
const { merge } = require('lodash');
const { merge, get } = require('lodash');
const DOMPurify = require('dompurify');
const config = require('./config');
module.exports = {
RootMutation: {
createComment: {
async pre(_, { input }, _context, _info) {
input.metadata = merge(
{},
{
htmlBody: input.htmlBody,
}
);
// Let's sanitize the body
const dirtyInput = input.htmlBody;
const cleanInput = DOMPurify.sanitize(dirtyInput, config.dompurify);
// Adding the clean body to the comment.metadata field
input.metadata = merge(get(input, 'metadata'), {
htmlBody: cleanInput,
});
},
},
editComment: {
async pre(_, { edit }, _context, _info) {
edit.metadata = merge(
{},
{
htmlBody: edit.htmlBody,
}
);
// Let's sanitize the body
const dirtyInput = edit.htmlBody;
const cleanInput = DOMPurify.sanitize(dirtyInput, config.dompurify);
// Adding the clean body to the comment.metadata field
edit.metadata = merge(get(edit, 'metadata'), {
htmlBody: cleanInput,
});
},
},
},
+4
View File
@@ -2,6 +2,10 @@
# yarn lockfile v1
dompurify@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/dompurify/-/dompurify-1.0.3.tgz#3f2f6ecb6ecd27599a506b410ff47d6eb90fd05d"
pell@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/pell/-/pell-0.7.0.tgz#46b3fcdfa8dd7e5999f73c550a337ecc80193dcc"