Skip to content

Commit a865b5a

Browse files
fixed xss hook
1 parent 6970e24 commit a865b5a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

server/hooks/xss.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { getByDot, setByDot, getItems, replaceItems } = require('feathers-hooks-common');
22
const sanitizeHtml = require('sanitize-html');
33
// const embedToAnchor = require('quill-url-embeds/dist/embed-to-anchor');
4-
const { isEmpty } = require('lodash');
4+
const { isEmpty, intersection } = require('lodash');
55
const cheerio = require('cheerio');
66

77
const embedToAnchor = (content) => {
@@ -80,7 +80,7 @@ function clean (dirty) {
8080
}
8181

8282
// iterate through all fields and clean the values
83-
function cleanAllFields (items, fields) {
83+
function cleanAllFields (items, fields, hook) {
8484
if (!items) {
8585
return items;
8686
}
@@ -90,7 +90,7 @@ function cleanAllFields (items, fields) {
9090
items.forEach((item, key) => {
9191
items[key] = cleanAllFields(items[key], fields);
9292
});
93-
} else {
93+
} else if (intersection(Object.keys(items), fields).length) {
9494
// clean value for all fields on the single given item
9595
fields.forEach((field) => {
9696
// get item by dot notation
@@ -99,6 +99,11 @@ function cleanAllFields (items, fields) {
9999
setByDot(items, field, clean(value));
100100
});
101101
}
102+
103+
if (hook && items) {
104+
replaceItems(hook, items);
105+
}
106+
102107
return items;
103108
}
104109

@@ -108,7 +113,7 @@ module.exports = function (options = { fields: [] }) {
108113
const isFindOrGet = ['find', 'get'].includes(hook.method);
109114
const items = getItems(hook);
110115
if (!isEmpty(items) && !(isFindOrGet && hook.type === 'before')) {
111-
replaceItems(hook, cleanAllFields(items, options.fields));
116+
cleanAllFields(items, options.fields, hook);
112117
}
113118
resolve(hook);
114119
});

0 commit comments

Comments
 (0)