@strapi/utils 4.7.2-exp.175f7ac70ee76d6c825e4429e15fc85ee78d23bb → 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +2 -2
- package/lib/sanitize/index.js +91 -101
- package/lib/sanitize/sanitizers.js +6 -0
- package/lib/traverse/query-filters.js +2 -2
- package/lib/traverse/query-sort.js +11 -0
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -41,8 +41,8 @@ const { pipeAsync, mapAsync, reduceAsync, forEachAsync } = require('./async');
|
|
|
41
41
|
const convertQueryParams = require('./convert-query-params');
|
|
42
42
|
const importDefault = require('./import-default');
|
|
43
43
|
const template = require('./template');
|
|
44
|
-
const traverse = require('./traverse');
|
|
45
44
|
const file = require('./file');
|
|
45
|
+
const traverse = require('./traverse');
|
|
46
46
|
|
|
47
47
|
module.exports = {
|
|
48
48
|
yup,
|
|
@@ -91,6 +91,6 @@ module.exports = {
|
|
|
91
91
|
validateYupSchemaSync,
|
|
92
92
|
convertQueryParams,
|
|
93
93
|
importDefault,
|
|
94
|
-
traverse,
|
|
95
94
|
file,
|
|
95
|
+
traverse,
|
|
96
96
|
};
|
package/lib/sanitize/index.js
CHANGED
|
@@ -11,138 +11,128 @@ const traverseEntity = require('../traverse-entity');
|
|
|
11
11
|
|
|
12
12
|
const { traverseQueryFilters, traverseQuerySort, traverseQueryPopulate } = require('../traverse');
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const nonWritableAttributes = getNonWritableAttributes(schema);
|
|
22
|
-
|
|
23
|
-
const transforms = [
|
|
24
|
-
// Remove non writable attributes
|
|
25
|
-
traverseEntity(visitors.restrictedFields(nonWritableAttributes), { schema }),
|
|
26
|
-
];
|
|
14
|
+
const createContentAPISanitizers = () => {
|
|
15
|
+
const sanitizeInput = (data, schema, { auth } = {}) => {
|
|
16
|
+
if (isArray(data)) {
|
|
17
|
+
return Promise.all(data.map((entry) => sanitizeInput(entry, schema, { auth })));
|
|
18
|
+
}
|
|
27
19
|
|
|
28
|
-
|
|
29
|
-
// Remove restricted relations
|
|
30
|
-
transforms.push(traverseEntity(visitors.removeRestrictedRelations(auth), { schema }));
|
|
31
|
-
}
|
|
20
|
+
const nonWritableAttributes = getNonWritableAttributes(schema);
|
|
32
21
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
22
|
+
const transforms = [
|
|
23
|
+
// Remove non writable attributes
|
|
24
|
+
traverseEntity(visitors.restrictedFields(nonWritableAttributes), { schema }),
|
|
25
|
+
];
|
|
37
26
|
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
if (auth) {
|
|
28
|
+
// Remove restricted relations
|
|
29
|
+
transforms.push(traverseEntity(visitors.removeRestrictedRelations(auth), { schema }));
|
|
30
|
+
}
|
|
40
31
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
// Apply sanitizers from registry if exists
|
|
33
|
+
strapi.sanitizers
|
|
34
|
+
.get('content-api.input')
|
|
35
|
+
.forEach((sanitizer) => transforms.push(sanitizer(schema)));
|
|
45
36
|
|
|
46
|
-
|
|
37
|
+
return pipeAsync(...transforms)(data);
|
|
38
|
+
};
|
|
47
39
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
40
|
+
const sanitizeOuput = (data, schema, { auth } = {}) => {
|
|
41
|
+
if (isArray(data)) {
|
|
42
|
+
return Promise.all(data.map((entry) => sanitizeOuput(entry, schema, { auth })));
|
|
43
|
+
}
|
|
51
44
|
|
|
52
|
-
|
|
53
|
-
strapi.sanitizers
|
|
54
|
-
.get('content-api.output')
|
|
55
|
-
.forEach((sanitizer) => transforms.push(sanitizer(schema)));
|
|
45
|
+
const transforms = [sanitizers.defaultSanitizeOutput(schema)];
|
|
56
46
|
|
|
57
|
-
|
|
58
|
-
|
|
47
|
+
if (auth) {
|
|
48
|
+
transforms.push(traverseEntity(visitors.removeRestrictedRelations(auth), { schema }));
|
|
49
|
+
}
|
|
59
50
|
|
|
60
|
-
|
|
61
|
-
|
|
51
|
+
// Apply sanitizers from registry if exists
|
|
52
|
+
strapi.sanitizers
|
|
53
|
+
.get('content-api.output')
|
|
54
|
+
.forEach((sanitizer) => transforms.push(sanitizer(schema)));
|
|
62
55
|
|
|
63
|
-
|
|
56
|
+
return pipeAsync(...transforms)(data);
|
|
57
|
+
};
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
59
|
+
const sanitizeQuery = async (query, schema, { auth } = {}) => {
|
|
60
|
+
const { filters, sort, fields, populate } = query;
|
|
68
61
|
|
|
69
|
-
|
|
70
|
-
Object.assign(sanitizedParams, { sort: await this.sort(sort, schema, { auth }) });
|
|
71
|
-
}
|
|
62
|
+
const sanitizedQuery = cloneDeep(query);
|
|
72
63
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
64
|
+
if (filters) {
|
|
65
|
+
Object.assign(sanitizedQuery, { filters: await sanitizeFilters(filters, schema, { auth }) });
|
|
66
|
+
}
|
|
76
67
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
68
|
+
if (sort) {
|
|
69
|
+
Object.assign(sanitizedQuery, { sort: await sanitizeSort(sort, schema, { auth }) });
|
|
70
|
+
}
|
|
80
71
|
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
if (fields) {
|
|
73
|
+
Object.assign(sanitizedQuery, { fields: await sanitizeFields(fields, schema) });
|
|
74
|
+
}
|
|
83
75
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
76
|
+
if (populate) {
|
|
77
|
+
Object.assign(sanitizedQuery, { populate: await sanitizePopulate(populate, schema) });
|
|
78
|
+
}
|
|
88
79
|
|
|
89
|
-
|
|
80
|
+
return sanitizedQuery;
|
|
81
|
+
};
|
|
90
82
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
83
|
+
const sanitizeFilters = (filters, schema, { auth } = {}) => {
|
|
84
|
+
if (isArray(filters)) {
|
|
85
|
+
return Promise.all(filters.map((filter) => sanitizeFilters(filter, schema, { auth })));
|
|
86
|
+
}
|
|
94
87
|
|
|
95
|
-
|
|
96
|
-
strapi.sanitizers
|
|
97
|
-
.get('content-api.filters')
|
|
98
|
-
.forEach((sanitizer) => transforms.push(sanitizer(schema)));
|
|
88
|
+
const transforms = [sanitizers.defaultSanitizeFilters(schema)];
|
|
99
89
|
|
|
100
|
-
|
|
101
|
-
|
|
90
|
+
if (auth) {
|
|
91
|
+
transforms.push(traverseQueryFilters(visitors.removeRestrictedRelations(auth), { schema }));
|
|
92
|
+
}
|
|
102
93
|
|
|
103
|
-
|
|
104
|
-
|
|
94
|
+
return pipeAsync(...transforms)(filters);
|
|
95
|
+
};
|
|
105
96
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
97
|
+
const sanitizeSort = (sort, schema, { auth } = {}) => {
|
|
98
|
+
const transforms = [sanitizers.defaultSanitizeSort(schema)];
|
|
109
99
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
.forEach((sanitizer) => transforms.push(sanitizer(schema)));
|
|
100
|
+
if (auth) {
|
|
101
|
+
transforms.push(traverseQuerySort(visitors.removeRestrictedRelations(auth), { schema }));
|
|
102
|
+
}
|
|
114
103
|
|
|
115
|
-
|
|
116
|
-
|
|
104
|
+
return pipeAsync(...transforms)(sort);
|
|
105
|
+
};
|
|
117
106
|
|
|
118
|
-
|
|
119
|
-
|
|
107
|
+
const sanitizeFields = (fields, schema) => {
|
|
108
|
+
const transforms = [sanitizers.defaultSanitizeFields(schema)];
|
|
120
109
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
.get('content-api.fields')
|
|
124
|
-
.forEach((sanitizer) => transforms.push(sanitizer(schema)));
|
|
110
|
+
return pipeAsync(...transforms)(fields);
|
|
111
|
+
};
|
|
125
112
|
|
|
126
|
-
|
|
127
|
-
|
|
113
|
+
const sanitizePopulate = (populate, schema, { auth } = {}) => {
|
|
114
|
+
const transforms = [sanitizers.defaultSanitizePopulate(schema)];
|
|
128
115
|
|
|
129
|
-
|
|
130
|
-
|
|
116
|
+
if (auth) {
|
|
117
|
+
transforms.push(traverseQueryPopulate(visitors.removeRestrictedRelations(auth), { schema }));
|
|
118
|
+
}
|
|
131
119
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
traverseQueryPopulate(visitors.removeRestrictedRelations(auth), { schema })
|
|
135
|
-
);
|
|
136
|
-
}
|
|
120
|
+
return pipeAsync(...transforms)(populate);
|
|
121
|
+
};
|
|
137
122
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
123
|
+
return {
|
|
124
|
+
input: sanitizeInput,
|
|
125
|
+
output: sanitizeOuput,
|
|
126
|
+
query: sanitizeQuery,
|
|
127
|
+
filters: sanitizeFilters,
|
|
128
|
+
sort: sanitizeSort,
|
|
129
|
+
fields: sanitizeFields,
|
|
130
|
+
populate: sanitizePopulate,
|
|
131
|
+
};
|
|
132
|
+
};
|
|
142
133
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
},
|
|
134
|
+
module.exports = {
|
|
135
|
+
contentAPI: createContentAPISanitizers(),
|
|
146
136
|
|
|
147
137
|
sanitizers,
|
|
148
138
|
visitors,
|
|
@@ -38,6 +38,8 @@ const defaultSanitizeFilters = curry((schema, filters) => {
|
|
|
38
38
|
traverseQueryFilters(removeDynamicZones, { schema }),
|
|
39
39
|
// Remove morpTo relations from filters
|
|
40
40
|
traverseQueryFilters(removeMorphToRelations, { schema }),
|
|
41
|
+
// Remove passwords from filters
|
|
42
|
+
traverseQueryFilters(removePassword, { schema }),
|
|
41
43
|
// Remove private from filters
|
|
42
44
|
traverseQueryFilters(removePrivate, { schema }),
|
|
43
45
|
// Remove empty objects
|
|
@@ -69,6 +71,8 @@ const defaultSanitizeSort = curry((schema, sort) => {
|
|
|
69
71
|
traverseQuerySort(removeMorphToRelations, { schema }),
|
|
70
72
|
// Remove private from sort
|
|
71
73
|
traverseQuerySort(removePrivate, { schema }),
|
|
74
|
+
// Remove passwords from filters
|
|
75
|
+
traverseQuerySort(removePassword, { schema }),
|
|
72
76
|
// Remove keys for empty non-scalar values
|
|
73
77
|
traverseQuerySort(
|
|
74
78
|
({ key, attribute, value }, { remove }) => {
|
|
@@ -94,6 +98,8 @@ const defaultSanitizeFields = curry((schema, fields) => {
|
|
|
94
98
|
),
|
|
95
99
|
// Remove private fields
|
|
96
100
|
traverseQueryFields(removePrivate, { schema }),
|
|
101
|
+
// Remove password fields
|
|
102
|
+
traverseQueryFields(removePassword, { schema }),
|
|
97
103
|
// Remove nil values from fields array
|
|
98
104
|
(value) => (isArray(value) ? value.filter((field) => !isNil(field)) : value)
|
|
99
105
|
)(fields);
|
|
@@ -11,8 +11,8 @@ const filters = traverseFactory()
|
|
|
11
11
|
async (visitor, options, filters, { recurse }) => {
|
|
12
12
|
return Promise.all(
|
|
13
13
|
filters.map((filter, i) => {
|
|
14
|
-
// In filters, only operators such as $and or $or
|
|
15
|
-
// array, thus we can update the raw path but not the attribute one
|
|
14
|
+
// In filters, only operators such as $and, $in, $notIn or $or and implicit operators like [...]
|
|
15
|
+
// can have a value array, thus we can update the raw path but not the attribute one
|
|
16
16
|
const newPath = { ...options.path, raw: `${options.path.raw}[${i}]` };
|
|
17
17
|
|
|
18
18
|
return recurse(visitor, { ...options, path: newPath }, filter);
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const {
|
|
4
4
|
curry,
|
|
5
5
|
isString,
|
|
6
|
+
isObject,
|
|
6
7
|
map,
|
|
7
8
|
trim,
|
|
8
9
|
split,
|
|
@@ -21,6 +22,7 @@ const ORDER_VALUES = Object.values(ORDERS);
|
|
|
21
22
|
|
|
22
23
|
const isSortOrder = (value) => ORDER_VALUES.includes(value.toLowerCase());
|
|
23
24
|
const isStringArray = (value) => Array.isArray(value) && value.every(isString);
|
|
25
|
+
const isObjectArray = (value) => Array.isArray(value) && value.every(isObject);
|
|
24
26
|
const isNestedSorts = (value) => isString(value) && value.split(',').length > 1;
|
|
25
27
|
|
|
26
28
|
const sort = traverseFactory()
|
|
@@ -45,6 +47,15 @@ const sort = traverseFactory()
|
|
|
45
47
|
);
|
|
46
48
|
}
|
|
47
49
|
)
|
|
50
|
+
.intercept(
|
|
51
|
+
// Array of objects [{ foo: 'asc' }, { bar: 'desc', baz: 'asc' }] => map(recurse), then filter out empty items
|
|
52
|
+
isObjectArray,
|
|
53
|
+
async (visitor, options, sort, { recurse }) => {
|
|
54
|
+
return Promise.all(sort.map((nestedSort) => recurse(visitor, options, nestedSort))).then(
|
|
55
|
+
(res) => res.filter((nestedSort) => !isEmpty(nestedSort))
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
)
|
|
48
59
|
// Parse string values
|
|
49
60
|
.parse(
|
|
50
61
|
(sort) => typeof sort === 'string',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.8.0",
|
|
4
4
|
"description": "Shared utilities for the Strapi packages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"node": ">=14.19.1 <=18.x.x",
|
|
47
47
|
"npm": ">=6.0.0"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "e239e408f99c9e61e6d02b38f01632ce67412f2a"
|
|
50
50
|
}
|