Skip to content

Commit 66b8a84

Browse files
committed
Lift query key validation out of transformQueryKeyValue
1 parent b24ff15 commit 66b8a84

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ const valueAsDate = value => {
141141
return false;
142142
}
143143

144-
function transformQueryKeyValue(className, key, value, { validate } = {}, schema) {
144+
function transformQueryKeyValue(className, key, value, schema) {
145145
switch(key) {
146146
case 'createdAt':
147147
if (valueAsDate(value)) {
@@ -184,9 +184,6 @@ function transformQueryKeyValue(className, key, value, { validate } = {}, schema
184184
// Special-case auth data.
185185
return {key: `_auth_data_${provider}.id`, value};
186186
}
187-
if (validate && !key.match(/^[a-zA-Z][a-zA-Z0-9_\.]*$/)) {
188-
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'invalid key name: ' + key);
189-
}
190187
}
191188

192189
const expectedTypeIsArray =
@@ -224,13 +221,17 @@ function transformQueryKeyValue(className, key, value, { validate } = {}, schema
224221
// restWhere is the "where" clause in REST API form.
225222
// Returns the mongo form of the query.
226223
// Throws a Parse.Error if the input query is invalid.
224+
const specialQuerykeys = ['$and', '$or', '_rperm', '_wperm', '_perishable_token', '_email_verify_token'];
227225
function transformWhere(className, restWhere, { validate = true } = {}, schema) {
228226
let mongoWhere = {};
229227
if (restWhere['ACL']) {
230228
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Cannot query on ACL.');
231229
}
232230
for (let restKey in restWhere) {
233-
let out = transformQueryKeyValue(className, restKey, restWhere[restKey], { validate }, schema);
231+
if (validate && !specialQuerykeys.includes(restKey) && !restKey.match(/^[a-zA-Z][a-zA-Z0-9_\.]*$/)) {
232+
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid key name: ${restKey}`);
233+
}
234+
let out = transformQueryKeyValue(className, restKey, restWhere[restKey], schema);
234235
mongoWhere[out.key] = out.value;
235236
}
236237
return mongoWhere;

0 commit comments

Comments
 (0)