Skip to content

Non trivial before save pointer clobber #2406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,44 @@ describe('Cloud Code', () => {
});
});

it('beforeSave should not affect fetched pointers', done => {
Parse.Cloud.beforeSave('BeforeSaveUnchanged', (req, res) => {
res.success();
});

Parse.Cloud.beforeSave('BeforeSaveChanged', function(req, res) {
req.object.set('foo', 'baz');
res.success();
});

var TestObject = Parse.Object.extend("TestObject");
var BeforeSaveUnchangedObject = Parse.Object.extend("BeforeSaveUnchanged");
var BeforeSaveChangedObject = Parse.Object.extend("BeforeSaveChanged");

var aTestObject = new TestObject();
aTestObject.set("foo", "bar");
aTestObject.save()
.then(aTestObject => {
var aBeforeSaveUnchangedObject = new BeforeSaveUnchangedObject();
aBeforeSaveUnchangedObject.set("aTestObject", aTestObject);
expect(aBeforeSaveUnchangedObject.get("aTestObject").get("foo")).toEqual("bar");
return aBeforeSaveUnchangedObject.save();
})
.then(aBeforeSaveUnchangedObject => {
expect(aBeforeSaveUnchangedObject.get("aTestObject").get("foo")).toEqual("bar");

var aBeforeSaveChangedObject = new BeforeSaveChangedObject();
aBeforeSaveChangedObject.set("aTestObject", aTestObject);
expect(aBeforeSaveChangedObject.get("aTestObject").get("foo")).toEqual("bar");
return aBeforeSaveChangedObject.save();
})
.then(aBeforeSaveChangedObject => {
expect(aBeforeSaveChangedObject.get("aTestObject").get("foo")).toEqual("bar");
expect(aBeforeSaveChangedObject.get("foo")).toEqual("baz");
done();
});
});

it_exclude_dbs(['postgres'])('should fully delete objects when using `unset` with beforeSave (regression test for #1840)', done => {
var TestObject = Parse.Object.extend('TestObject');
var NoBeforeSaveObject = Parse.Object.extend('NoBeforeSave');
Expand Down
2 changes: 1 addition & 1 deletion spec/ParseUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ describe('Parse.User testing', () => {
},
json: true
}, (err, res, body) => {
expect(body.username).toEqual(user.username);
expect(body.username).toEqual('user');
expect(body.objectId).toEqual(originalUserId);
if (err) {
reject(err);
Expand Down
24 changes: 13 additions & 11 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ RestWrite.prototype.runBeforeTrigger = function() {
return triggers.maybeRunTrigger(triggers.Types.beforeSave, this.auth, updatedObject, originalObject, this.config);
}).then((response) => {
if (response && response.object) {
if (!_.isEqual(this.data, response.object)) {
this.storage.changedByTrigger = true;
}
this.storage.fieldsChangedByTrigger = _.reduce(response.object, (result, value, key) => {
if (!_.isEqual(this.data[key], value)) {
result.push(key);
}
return result;
}, []);
this.data = response.object;
// We should delete the objectId for an update write
if (this.query && this.query.objectId) {
Expand Down Expand Up @@ -792,9 +795,7 @@ RestWrite.prototype.runDatabaseOperation = function() {
return this.config.database.update(this.className, this.query, this.data, this.runOptions)
.then(response => {
response.updatedAt = this.updatedAt;
if (this.storage.changedByTrigger) {
this.updateResponseWithData(response, this.data);
}
this._updateResponseWithData(response, this.data);
this.response = { response };
});
} else {
Expand Down Expand Up @@ -850,9 +851,7 @@ RestWrite.prototype.runDatabaseOperation = function() {
if (this.responseShouldHaveUsername) {
response.username = this.data.username;
}
if (this.storage.changedByTrigger) {
this.updateResponseWithData(response, this.data);
}
this._updateResponseWithData(response, this.data);
this.response = {
status: 201,
response,
Expand Down Expand Up @@ -940,9 +939,12 @@ RestWrite.prototype.cleanUserAuthData = function() {
}
};

RestWrite.prototype.updateResponseWithData = function(response, data) {
RestWrite.prototype._updateResponseWithData = function(response, data) {
if (_.isEmpty(this.storage.fieldsChangedByTrigger)) {
return response;
}
let clientSupportsDelete = ClientSDK.supportsForwardDelete(this.clientSDK);
Object.keys(data).forEach(fieldName => {
this.storage.fieldsChangedByTrigger.forEach(fieldName => {
let dataValue = data[fieldName];
let responseValue = response[fieldName];

Expand Down