Skip to content

Commit cc8489b

Browse files
sanatize data on output as a quick fix
1 parent 1d8de78 commit cc8489b

File tree

5 files changed

+83
-6
lines changed

5 files changed

+83
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"raven": "~2.4.0",
9595
"request": "~2.83.0",
9696
"request-promise": "~4.2.2",
97+
"sanitize-html": "^1.18.2",
9798
"serve-favicon": "~2.4.5",
9899
"shortid": "~2.2.8",
99100
"slug": "~0.9.1",

server/helper/thumbor-helper.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ThumborUrlHelper.prototype = {
1818
* Set path of image
1919
* @param {String} imagePath [description]
2020
*/
21-
setImagePath: function(imagePath) {
21+
setImagePath: function (imagePath) {
2222
this.imagePath = (imagePath.charAt(0) === '/') ?
2323
imagePath.substring(1, imagePath.length) : imagePath;
2424
return this;
@@ -27,7 +27,7 @@ ThumborUrlHelper.prototype = {
2727
* Combine image url and operations with secure and unsecure (unsafe) paths
2828
* @return {String}
2929
*/
30-
buildUrl: function(operations) {
30+
buildUrl: function (operations) {
3131

3232
if (this.THUMBOR_SECURITY_KEY) {
3333

@@ -45,4 +45,4 @@ ThumborUrlHelper.prototype = {
4545
}
4646
};
4747

48-
module.exports = ThumborUrlHelper;
48+
module.exports = ThumborUrlHelper;

server/hooks/xss.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const sanitizeHtml = require('sanitize-html');
2+
const _ = require('lodash');
3+
4+
function clean (dirty, hook) {
5+
return sanitizeHtml(dirty, {
6+
allowedTags: ['iframe', 'img', 'p', 'br', 'b', 'i', 'em', 'strong', 'a', 'pre'],
7+
allowedAttributes: {
8+
a: ['href', 'data-*'],
9+
img: [ 'src' ],
10+
iframe: ['src', 'class', 'frameborder', 'allowfullscreen']
11+
},
12+
allowedIframeHostnames: ['www.youtube.com', 'player.vimeo.com'],
13+
parser: {
14+
lowerCaseTags: true
15+
}
16+
// transformTags: {
17+
// 'img': function (tagName, attribs) {
18+
// let src = attribs.src;
19+
20+
// const config = hook.app.get('thumbor');
21+
// if (config && src.indexOf(config < 0)) {
22+
// // download image
23+
24+
// // make thumbnail
25+
26+
// // const ThumborUrlHelper = require('../helper/thumbor-helper');
27+
// // const Thumbor = new ThumborUrlHelper(config.key || null, config.url || null);
28+
// // src = Thumbor
29+
// // .setImagePath(src)
30+
// // .buildUrl('740x0');
31+
// }
32+
// return {
33+
// tagName: 'img',
34+
// attribs: {
35+
// src: src
36+
// }
37+
// };
38+
// }
39+
// }
40+
});
41+
}
42+
43+
module.exports = function (options = { fields: [] }) {
44+
return function (hook) {
45+
return new Promise(resolve => {
46+
options.fields.forEach(field => {
47+
try {
48+
if (!_.isEmpty(hook.result) && !_.isEmpty(hook.result[field])) {
49+
hook.result[field] = clean(hook.result[field], hook);
50+
} else if (!_.isEmpty(hook.result) && !_.isEmpty(hook.result.data)) {
51+
hook.result.data.forEach((result, i) => {
52+
if (!_.isEmpty(hook.result.data[i][field])) {
53+
hook.result.data[i][field] = clean(hook.result.data[i][field], hook);
54+
}
55+
});
56+
} else if (!_.isEmpty(hook.data) && !_.isEmpty(hook.data[field])) {
57+
hook.data[field] = clean(hook.data[field], hook);
58+
}
59+
} catch (err) {
60+
hook.app.error(err);
61+
}
62+
});
63+
64+
resolve(hook);
65+
});
66+
};
67+
};

server/services/contributions/contributions.hooks.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const getAssociatedCanDos = require('./hooks/get-associated-can-dos');
1717
const createMentionNotifications = require('./hooks/create-mention-notifications');
1818
const metascraper = require('./hooks/metascraper');
1919
const isSingleItem = require('../../hooks/is-single-item');
20+
const xss = require('../../hooks/xss');
2021

2122
const userSchema = {
2223
include: {
@@ -91,6 +92,7 @@ module.exports = {
9192
isVerified()
9293
),
9394
associateCurrentUser(),
95+
// xss({ fields: ['content'] }),
9496
createSlug({ field: 'title' }),
9597
metascraper(),
9698
saveRemoteImages(['teaserImg']),
@@ -105,6 +107,7 @@ module.exports = {
105107
excludeDisabled(),
106108
restrictToOwner()
107109
),
110+
// xss({ fields: ['content'] }),
108111
metascraper(),
109112
saveRemoteImages(['teaserImg']),
110113
createExcerpt()
@@ -118,6 +121,7 @@ module.exports = {
118121
excludeDisabled(),
119122
restrictToOwner()
120123
),
124+
// xss({ fields: ['content'] }),
121125
metascraper(),
122126
saveRemoteImages(['teaserImg']),
123127
createExcerpt()
@@ -143,6 +147,7 @@ module.exports = {
143147
when(isSingleItem(),
144148
getAssociatedCanDos()
145149
),
150+
xss({ fields: ['content'] }),
146151
thumbnails({
147152
teaserImg: {
148153
cardS: '300x0',
@@ -157,6 +162,7 @@ module.exports = {
157162
],
158163
get: [
159164
getAssociatedCanDos(),
165+
xss({ fields: ['content'] }),
160166
thumbnails({
161167
teaserImg: {
162168
cardS: '300x0',
@@ -171,6 +177,7 @@ module.exports = {
171177
],
172178
create: [
173179
createMentionNotifications(),
180+
xss({ fields: ['content'] }),
174181
thumbnails({
175182
teaserImg: {
176183
cardS: '300x0',
@@ -185,6 +192,7 @@ module.exports = {
185192
],
186193
update: [
187194
createMentionNotifications(),
195+
xss({ fields: ['content'] }),
188196
thumbnails({
189197
teaserImg: {
190198
cardS: '300x0',
@@ -199,6 +207,7 @@ module.exports = {
199207
],
200208
patch: [
201209
createMentionNotifications(),
210+
xss({ fields: ['content'] }),
202211
thumbnails({
203212
teaserImg: {
204213
cardS: '300x0',

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,7 @@ got@^6.7.1:
22852285
unzip-response "^2.0.1"
22862286
url-parse-lax "^1.0.0"
22872287

2288-
got@^8.3.0:
2288+
got@~8.3.0:
22892289
version "8.3.0"
22902290
resolved "https://registry.yarnpkg.com/got/-/got-8.3.0.tgz#6ba26e75f8a6cc4c6b3eb1fe7ce4fec7abac8533"
22912291
dependencies:
@@ -3573,7 +3573,7 @@ metascraper-video@^3.9.2:
35733573
"@metascraper/helpers" "^3.9.2"
35743574
video-extensions "~1.1.0"
35753575

3576-
metascraper@^3.9.2:
3576+
metascraper@~3.9.2:
35773577
version "3.9.2"
35783578
resolved "https://registry.yarnpkg.com/metascraper/-/metascraper-3.9.2.tgz#bc2d1705a80be619ddc254716d2bf9d98e92b1c2"
35793579
dependencies:
@@ -4682,7 +4682,7 @@ [email protected], safe-buffer@^5.0.1, safe-buffer@^5.1.1, safe-buffer@~5.1.0, s
46824682
version "5.1.1"
46834683
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"
46844684

4685-
sanitize-html@~1.18.2:
4685+
sanitize-html@^1.18.2, sanitize-html@~1.18.2:
46864686
version "1.18.2"
46874687
resolved "https://registry.yarnpkg.com/sanitize-html/-/sanitize-html-1.18.2.tgz#61877ba5a910327e42880a28803c2fbafa8e4642"
46884688
dependencies:

0 commit comments

Comments
 (0)