Skip to content

Commit 07f73c0

Browse files
committed
Moves transform to MongoTransform
- Adds ACL query injection in MongoTransform
1 parent 337d3c2 commit 07f73c0

File tree

6 files changed

+46
-35
lines changed

6 files changed

+46
-35
lines changed

spec/transform.spec.js renamed to spec/MongoTransform.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// These tests are unit tests designed to only test transform.js.
22

3-
var transform = require('../src/transform');
3+
var transform = require('../src/Adapters/Storage/Mongo/MongoTransform');
44

55
var dummySchema = {
66
data: {},

src/Adapters/Storage/Mongo/MongoSchemaCollection.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import MongoCollection from './MongoCollection';
3+
import * as transform from './MongoTransform';
34

45
function _mongoSchemaQueryFromNameQuery(name: string, query) {
56
return _mongoSchemaObjectFromNameFields(name, query);
@@ -55,4 +56,8 @@ export default class MongoSchemaCollection {
5556
upsertSchema(name: string, query: string, update) {
5657
return this._collection.upsertOne(_mongoSchemaQueryFromNameQuery(name, query), update);
5758
}
59+
60+
get transform() {
61+
return transform;
62+
}
5863
}

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
import MongoCollection from './MongoCollection';
33
import MongoSchemaCollection from './MongoSchemaCollection';
4-
import {parse as parseUrl, format as formatUrl} from '../../../vendor/mongodbUrl';
4+
import { parse as parseUrl, format as formatUrl } from '../../../vendor/mongodbUrl';
5+
import * as transform from './MongoTransform';
56

67
let mongodb = require('mongodb');
78
let MongoClient = mongodb.MongoClient;
@@ -78,6 +79,10 @@ export class MongoStorageAdapter {
7879
});
7980
});
8081
}
82+
83+
get transform() {
84+
return transform;
85+
}
8186
}
8287

8388
export default MongoStorageAdapter;

src/transform.js renamed to src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,27 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals
739739
}
740740
}
741741

742+
function addWriteACL(mongoWhere, acl) {
743+
var writePerms = [
744+
{_wperm: {'$exists': false}}
745+
];
746+
for (var entry of acl) {
747+
writePerms.push({_wperm: {'$in': [entry]}});
748+
}
749+
return {'$and': [mongoWhere, {'$or': writePerms}]};
750+
}
751+
752+
function addReadACL(mongoWhere, acl) {
753+
var orParts = [
754+
{"_rperm" : { "$exists": false }},
755+
{"_rperm" : { "$in" : ["*"]}}
756+
];
757+
for (var entry of acl) {
758+
orParts.push({"_rperm" : { "$in" : [entry]}});
759+
}
760+
return {'$and': [mongoWhere, {'$or': orParts}]};
761+
}
762+
742763
var DateCoder = {
743764
JSONToDatabase(json) {
744765
return new Date(json.iso);
@@ -832,5 +853,7 @@ module.exports = {
832853
transformCreate: transformCreate,
833854
transformUpdate: transformUpdate,
834855
transformWhere: transformWhere,
856+
addReadACL: addReadACL,
857+
addWriteACL: addWriteACL,
835858
untransformObject: untransformObject
836859
};

src/Controllers/DatabaseController.js

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var mongodb = require('mongodb');
55
var Parse = require('parse/node').Parse;
66

77
var Schema = require('./../Schema');
8-
var transform = require('./../transform');
98
const deepcopy = require('deepcopy');
109

1110
// options can contain:
@@ -115,7 +114,7 @@ DatabaseController.prototype.validateObject = function(className, object, query,
115114
// Filters out any data that shouldn't be on this REST-formatted object.
116115
DatabaseController.prototype.untransformObject = function(
117116
schema, isMaster, aclGroup, className, mongoObject) {
118-
var object = transform.untransformObject(schema, className, mongoObject);
117+
var object = this.adapter.transform.untransformObject(schema, className, mongoObject);
119118

120119
if (className !== '_User') {
121120
return object;
@@ -161,17 +160,11 @@ DatabaseController.prototype.update = function(className, query, update, options
161160
.then(() => this.handleRelationUpdates(className, query.objectId, update))
162161
.then(() => this.adaptiveCollection(className))
163162
.then(collection => {
164-
var mongoWhere = transform.transformWhere(schema, className, query);
163+
var mongoWhere = this.adapter.transform.transformWhere(schema, className, query);
165164
if (options.acl) {
166-
var writePerms = [
167-
{_wperm: {'$exists': false}}
168-
];
169-
for (var entry of options.acl) {
170-
writePerms.push({_wperm: {'$in': [entry]}});
171-
}
172-
mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]};
165+
mongoWhere = this.adapter.transform.addWriteACL(mongoWhere, options.acl);
173166
}
174-
mongoUpdate = transform.transformUpdate(schema, className, update);
167+
mongoUpdate = this.adapter.transform.transformUpdate(schema, className, update);
175168
return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
176169
})
177170
.then(result => {
@@ -298,16 +291,9 @@ DatabaseController.prototype.destroy = function(className, query, options = {})
298291
})
299292
.then(() => this.adaptiveCollection(className))
300293
.then(collection => {
301-
let mongoWhere = transform.transformWhere(schema, className, query);
302-
294+
let mongoWhere = this.adapter.transform.transformWhere(schema, className, query, options);
303295
if (options.acl) {
304-
var writePerms = [
305-
{ _wperm: { '$exists': false } }
306-
];
307-
for (var entry of options.acl) {
308-
writePerms.push({ _wperm: { '$in': [entry] } });
309-
}
310-
mongoWhere = { '$and': [mongoWhere, { '$or': writePerms }] };
296+
mongoWhere = this.adapter.transform.addWriteACL(mongoWhere, options.acl);
311297
}
312298
return collection.deleteMany(mongoWhere);
313299
})
@@ -343,7 +329,7 @@ DatabaseController.prototype.create = function(className, object, options) {
343329
.then(() => this.handleRelationUpdates(className, null, object))
344330
.then(() => this.adaptiveCollection(className))
345331
.then(coll => {
346-
var mongoObject = transform.transformCreate(schema, className, object);
332+
var mongoObject = this.adapter.transform.transformCreate(schema, className, object);
347333
return coll.insertOne(mongoObject);
348334
})
349335
.then(result => {
@@ -537,7 +523,7 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
537523
if (options.sort) {
538524
mongoOptions.sort = {};
539525
for (var key in options.sort) {
540-
var mongoKey = transform.transformKey(schema, className, key);
526+
var mongoKey = this.adapter.transform.transformKey(schema, className, key);
541527
mongoOptions.sort[mongoKey] = options.sort[key];
542528
}
543529
}
@@ -558,16 +544,9 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
558544
}).then(() => {
559545
return this.adaptiveCollection(className);
560546
}).then(collection => {
561-
var mongoWhere = transform.transformWhere(schema, className, query);
547+
var mongoWhere = this.adapter.transform.transformWhere(schema, className, query);
562548
if (!isMaster) {
563-
var orParts = [
564-
{"_rperm" : { "$exists": false }},
565-
{"_rperm" : { "$in" : ["*"]}}
566-
];
567-
for (var acl of aclGroup) {
568-
orParts.push({"_rperm" : { "$in" : [acl]}});
569-
}
570-
mongoWhere = {'$and': [mongoWhere, {'$or': orParts}]};
549+
mongoWhere = this.adapter.transform.addReadACL(mongoWhere, aclGroup);
571550
}
572551
if (options.count) {
573552
delete mongoOptions.limit;

src/Schema.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// TODO: hide all schema logic inside the database adapter.
1616

1717
var Parse = require('parse/node').Parse;
18-
var transform = require('./transform');
1918

2019
const defaultColumns = Object.freeze({
2120
// Contain the default columns for every parse object type (except _Join collection)
@@ -416,7 +415,7 @@ class Schema {
416415
// If 'freeze' is true, refuse to update the schema for this field.
417416
validateField(className, key, type, freeze) {
418417
// Just to check that the key is valid
419-
transform.transformKey(this, className, key);
418+
this._collection.transform.transformKey(this, className, key);
420419

421420
if( key.indexOf(".") > 0 ) {
422421
// subdocument key (x.y) => ok if x is of type 'object'

0 commit comments

Comments
 (0)