Skip to content

Commit e4998c2

Browse files
committed
Move field name validation logic out of mongo (#1752)
* Remove transformKey(...) * Move validation logic into Parse Server and out of Mongo Adapter * Fix nits
1 parent 4bfe2c5 commit e4998c2

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

spec/MongoTransform.spec.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,6 @@ describe('untransformObject', () => {
191191
});
192192
});
193193

194-
describe('transformKey', () => {
195-
it('throws out _password', (done) => {
196-
try {
197-
transform.transformKey(dummySchema, '_User', '_password');
198-
fail('should have thrown');
199-
} catch (e) {
200-
done();
201-
}
202-
});
203-
});
204-
205194
describe('transform schema key changes', () => {
206195

207196
it('changes new pointer key', (done) => {

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ var Parse = require('parse/node').Parse;
1515
// in the value are converted to a mongo update form. Otherwise they are
1616
// converted to static data.
1717
//
18-
// validate: true indicates that key names are to be validated.
19-
//
2018
// Returns an object with {key: key, value: value}.
2119
function transformKeyValue(schema, className, restKey, restValue, {
2220
inArray,
2321
inObject,
2422
update,
25-
validate,
2623
} = {}) {
2724
// Check if the schema is known since it's a built-in field.
2825
var key = restKey;
@@ -71,9 +68,6 @@ function transformKeyValue(schema, className, restKey, restValue, {
7168
if (authDataMatch) {
7269
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'can only query on ' + key);
7370
}
74-
if (validate && !key.match(/^[a-zA-Z][a-zA-Z0-9_\.]*$/)) {
75-
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'invalid key name: ' + key);
76-
}
7771
}
7872

7973
// Handle special schema key changes
@@ -454,11 +448,6 @@ function untransformACL(mongoObject) {
454448
return output;
455449
}
456450

457-
// Transforms a key used in the REST API format to its mongo format.
458-
function transformKey(schema, className, key) {
459-
return transformKeyValue(schema, className, key, null, {validate: true}).key;
460-
}
461-
462451
// A sentinel value that helper transformations return when they
463452
// cannot perform a transformation
464453
function CannotTransform() {}
@@ -1038,7 +1027,7 @@ var FileCoder = {
10381027
};
10391028

10401029
module.exports = {
1041-
transformKey,
1030+
transformKeyValue,
10421031
parseObjectToMongoObjectForCreate,
10431032
transformUpdate,
10441033
transformWhere,

src/Controllers/DatabaseController.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,22 @@ DatabaseController.prototype.find = function(className, query, {
618618
.then(schemaController => {
619619
if (sort) {
620620
mongoOptions.sort = {};
621-
for (let key in sort) {
622-
let mongoKey = this.transform.transformKey(schemaController, className, key);
623-
mongoOptions.sort[mongoKey] = sort[key];
621+
for (let fieldName in sort) {
622+
// Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt,
623+
// so duplicate that behaviour here.
624+
if (fieldName === '_created_at') {
625+
fieldName = 'createdAt';
626+
sort['createdAt'] = sort['_created_at'];
627+
} else if (fieldName === '_updated_at') {
628+
fieldName = 'updatedAt';
629+
sort['updatedAt'] = sort['_updated_at'];
630+
}
631+
632+
if (!SchemaController.fieldNameIsValid(fieldName)) {
633+
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
634+
}
635+
const mongoKey = this.transform.transformKeyValue(schemaController, className, fieldName, null).key;
636+
mongoOptions.sort[mongoKey] = sort[fieldName];
624637
}
625638
}
626639
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, op))

src/Controllers/SchemaController.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class SchemaController {
253253
this.data[schema.className] = schema.fields;
254254
this.perms[schema.className] = schema.classLevelPermissions;
255255
});
256-
256+
257257
// Inject the in-memory classes
258258
volatileClasses.forEach(className => {
259259
this.data[className] = injectDefaultSchema({
@@ -466,15 +466,16 @@ class SchemaController {
466466
// If 'freeze' is true, refuse to update the schema for this field.
467467
validateField(className, fieldName, type, freeze) {
468468
return this.reloadData().then(() => {
469-
// Just to check that the fieldName is valid
470-
this._collection.transform.transformKey(this, className, fieldName);
471-
472-
if( fieldName.indexOf(".") > 0 ) {
469+
if (fieldName.indexOf(".") > 0) {
473470
// subdocument key (x.y) => ok if x is of type 'object'
474471
fieldName = fieldName.split(".")[ 0 ];
475472
type = 'Object';
476473
}
477474

475+
if (!fieldNameIsValid(fieldName)) {
476+
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
477+
}
478+
478479
let expected = this.data[className][fieldName];
479480
if (expected) {
480481
expected = (expected === 'map' ? 'Object' : expected);
@@ -847,6 +848,7 @@ function getObjectType(obj) {
847848
export {
848849
load,
849850
classNameIsValid,
851+
fieldNameIsValid,
850852
invalidClassNameMessage,
851853
buildMergedSchemaObject,
852854
systemClasses,

0 commit comments

Comments
 (0)