Skip to content
Open
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
272 changes: 272 additions & 0 deletions spec/ParseInstallation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,265 @@ describe('Installations', () => {
});
});

it('update fails to clear installationId via Delete op', done => {
Comment thread
AdrianCurtin marked this conversation as resolved.
Resolved
const installId = '12345678-abcd-abcd-abcd-123456789abc';
const input = {
installationId: installId,
deviceType: 'ios',
};
rest
.create(config, auth.nobody(config), '_Installation', input)
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
.then(results => {
expect(results.length).toEqual(1);
return rest.update(
config,
auth.nobody(config),
'_Installation',
{ objectId: results[0].objectId },
{ installationId: { __op: 'Delete' } }
);
})
.then(() => {
fail('Updating the installation should have failed.');
done();
})
.catch(error => {
expect(error.code).toEqual(136);
done();
});
});

it('update fails to clear installationId via null', done => {
Comment thread
AdrianCurtin marked this conversation as resolved.
Resolved
const installId = '12345678-abcd-abcd-abcd-123456789abc';
const input = {
installationId: installId,
deviceType: 'ios',
};
rest
.create(config, auth.nobody(config), '_Installation', input)
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
.then(results => {
expect(results.length).toEqual(1);
return rest.update(
config,
auth.nobody(config),
'_Installation',
{ objectId: results[0].objectId },
{ installationId: null }
);
})
.then(() => {
fail('Updating the installation should have failed.');
done();
})
.catch(error => {
expect(error.code).toEqual(136);
done();
});
});

it('update fails to clear installationId via empty string', done => {
const installId = '12345678-abcd-abcd-abcd-123456789abc';
const input = {
installationId: installId,
deviceType: 'ios',
};
rest
.create(config, auth.nobody(config), '_Installation', input)
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
.then(results => {
expect(results.length).toEqual(1);
return rest.update(
config,
auth.nobody(config),
'_Installation',
{ objectId: results[0].objectId },
{ installationId: '' }
);
})
.then(() => {
fail('Updating the installation should have failed.');
done();
})
.catch(error => {
expect(error.code).toEqual(136);
done();
});
});

it('create fails when installationId is the Delete op (no real ID provided)', done => {
const input = {
installationId: { __op: 'Delete' },
deviceType: 'ios',
};
rest
.create(config, auth.nobody(config), '_Installation', input)
.then(() => {
fail('Creating the installation should have failed.');
done();
})
.catch(error => {
expect(error.code).toEqual(135);
done();
});
});

it('create fails when installationId is null (no real ID provided)', done => {
Comment thread
AdrianCurtin marked this conversation as resolved.
Resolved
const input = {
installationId: null,
deviceType: 'ios',
};
rest
.create(config, auth.nobody(config), '_Installation', input)
.then(() => {
fail('Creating the installation should have failed.');
done();
})
.catch(error => {
expect(error.code).toEqual(135);
done();
});
Comment thread
AdrianCurtin marked this conversation as resolved.
Resolved
});

it('create fails when installationId is a non-string object', done => {
const input = {
installationId: { foo: 'bar' },
deviceType: 'ios',
};
rest
.create(config, auth.nobody(config), '_Installation', input)
.then(() => {
fail('Creating the installation should have failed.');
done();
})
.catch(error => {
expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE);
done();
});
});

it('create fails when installationId is a number', done => {
const input = {
installationId: 12345,
deviceType: 'ios',
};
rest
.create(config, auth.nobody(config), '_Installation', input)
.then(() => {
fail('Creating the installation should have failed.');
done();
})
.catch(error => {
expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE);
done();
});
});

it('create fails when installationId is an array', done => {
const input = {
installationId: ['abc'],
deviceType: 'ios',
};
rest
.create(config, auth.nobody(config), '_Installation', input)
.then(() => {
fail('Creating the installation should have failed.');
done();
})
.catch(error => {
expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE);
done();
});
});

it('update fails when installationId is a non-string object', done => {
Comment thread
AdrianCurtin marked this conversation as resolved.
Resolved
const installId = '12345678-abcd-abcd-abcd-123456789abc';
const input = {
installationId: installId,
deviceType: 'ios',
};
rest
.create(config, auth.nobody(config), '_Installation', input)
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
.then(results => {
expect(results.length).toEqual(1);
return rest.update(
config,
auth.nobody(config),
'_Installation',
{ objectId: results[0].objectId },
{ installationId: { foo: 'bar' } }
);
})
.then(() => {
fail('Updating the installation should have failed.');
done();
})
.catch(error => {
expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE);
done();
});
});

Comment thread
AdrianCurtin marked this conversation as resolved.
Resolved
it('master key cannot clear installationId via Delete op', done => {
const installId = '12345678-abcd-abcd-abcd-123456789abc';
const input = {
installationId: installId,
deviceType: 'ios',
};
rest
.create(config, auth.master(config), '_Installation', input)
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
.then(results => {
expect(results.length).toEqual(1);
return rest.update(
config,
auth.master(config),
'_Installation',
{ objectId: results[0].objectId },
{ installationId: { __op: 'Delete' } }
);
})
.then(() => {
fail('Master key clearing of installationId should have been rejected.');
done();
})
.catch(error => {
expect(error.code).toEqual(136);
done();
});
Comment thread
AdrianCurtin marked this conversation as resolved.
Resolved
});

it('master key cannot clear installationId via null', done => {
const installId = '12345678-abcd-abcd-abcd-123456789abc';
const input = {
installationId: installId,
deviceType: 'ios',
};
rest
.create(config, auth.master(config), '_Installation', input)
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
.then(results => {
expect(results.length).toEqual(1);
return rest.update(
config,
auth.master(config),
'_Installation',
{ objectId: results[0].objectId },
{ installationId: null }
);
})
.then(() => {
fail('Master key clearing of installationId via null should have been rejected.');
done();
})
.catch(error => {
expect(error.code).toEqual(136);
done();
});
});

Comment thread
AdrianCurtin marked this conversation as resolved.
Resolved
it('update fails to change deviceType', done => {
const installId = '12345678-abcd-abcd-abcd-123456789abc';
let input = {
Expand Down Expand Up @@ -1502,6 +1761,19 @@ describe('Installations', () => {
expect(rows[0].deviceToken).toBe(deviceToken);
});

it('answers like a nonexistent installation when the installationId is cleared', async () => {
const owner = await signUp();
const objectId = await createOwnedInstallation(owner, baseFields);

expectNotFound(await update(objectId, { installationId: null }));
expectNotFound(await update(objectId, { installationId: '' }));
expectNotFound(await update(objectId, { installationId: { __op: 'Delete' } }));

const rows = await findRows();
expect(rows.length).toBe(1);
expect(rows[0].installationId).toBe(installationId);
});

it('answers like a nonexistent installation for an authenticated user without write access', async () => {
const owner = await signUp();
const other = await signUp();
Expand Down
24 changes: 23 additions & 1 deletion src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,20 @@ RestWrite.prototype.handleInstallation = function () {
return;
}

// installationId is the row's primary identity (used by the SDK auth header to bind a
// client request to its row), so it may not be cleared via null, '' or { __op: 'Delete' }.
// The value is dropped here so the lookup below does not use it. On create, the "must
// specify ID" guard below then rejects the write unless another ID is supplied. On
// update, the rejection is deferred to the sanity checks so that it follows the
// caller's write permission check, like the guard against changing installationId.
const clearingInstallationId =
this.data.installationId === null ||
this.data.installationId === '' ||
(typeof this.data.installationId === 'object' && this.data.installationId.__op === 'Delete');
if (clearingInstallationId) {
delete this.data.installationId;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// The deduplication below embeds these client-supplied values directly into database
// queries that delete or update rows with master privileges, and it runs before
// `validateSchema`, so their types must be enforced here: a non-string value would
Expand Down Expand Up @@ -1490,7 +1504,13 @@ RestWrite.prototype.handleInstallation = function () {
}

// Updating _Installation but not updating anything critical
if (this.query && !this.data.deviceToken && !installationId && !this.data.deviceType) {
if (
this.query &&
!this.data.deviceToken &&
!installationId &&
!this.data.deviceType &&
!clearingInstallationId
) {
return;
}

Expand Down Expand Up @@ -1572,6 +1592,8 @@ RestWrite.prototype.handleInstallation = function () {
let error;
if (!objectIdMatch) {
error = new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found for update.');
} else if (clearingInstallationId) {
error = new Parse.Error(136, 'installationId may not be changed or cleared in this operation');
} else if (
this.data.installationId &&
objectIdMatch.installationId &&
Expand Down