Skip to content
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
1 change: 1 addition & 0 deletions orga/changelog/fix-rest-revive-validator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- FIX the REST `/set` and `/patch` routes called the `changeValidator` without an `assumedMasterState` when a client revived a deleted document, so a client could change a read-only field by deleting and reviving the document [#484](https://github.com/pubkey/rxdb-server/pull/484)
5 changes: 4 additions & 1 deletion src/plugins/server/endpoint-rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,12 @@ export class RxServerRestEndpoint<ServerAppType, AuthType, RxDocType> implements
// inconsistent with the replication endpoint and the
// documented contract that the validator gates all
// changes.
// A deleted document that is revived is a change of the stored
// document, so the changeValidator gets the stored state.
const deletedStoredDoc = storedDocs.find(d => (d as any)[primaryPath] === id);
const isAllowed = this.changeValidator(authData, {
newDocumentState: removeServerOnlyFields(docData as any),
assumedMasterState: undefined
assumedMasterState: removeServerOnlyFields(deletedStoredDoc as any)
});
if (!isAllowed) {
adapter.closeConnection(res, 403, 'Forbidden');
Expand Down
51 changes: 51 additions & 0 deletions test/unit/endpoint-rest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,57 @@ describe('endpoint-rest.test.ts', () => {

await col.database.close();
});
it('should run the changeValidator against the stored state when a deleted document is revived', async () => {
const col = await humansCollection.create(0);
const ownDoc = await col.insert(schemaObjects.humanData('readonly-deleted-doc', 30, headers.userid));
await ownDoc.remove();

const port = await nextPort();
const server = await createRxServer({
adapter: TEST_SERVER_ADAPTER,
database: col.database,
authHandler,
port
});
const endpoint = await server.addRestEndpoint({
name: randomToken(10),
collection: col,
queryModifier,
// the age can only be set on inserts, not changed later
changeValidator: (_authData, change) => {
if (!change.assumedMasterState) {
return true;
}
return change.newDocumentState.age === change.assumedMasterState.age;
}
});
await server.start();
const client = createRestClient<HumanDocumentType>('http://localhost:' + port + '/' + endpoint.urlPath, headers);

await assertThrows(
() => client.set([{
passportId: 'readonly-deleted-doc',
firstName: headers.userid,
lastName: 'revived',
age: 99
}]),
Error,
'Forbidden'
);
const docAfter = await col.findOne('readonly-deleted-doc').exec();
assert.strictEqual(docAfter, null);

await client.set([{
passportId: 'readonly-deleted-doc',
firstName: headers.userid,
lastName: 'revived',
age: 30
}]);
const revivedDoc = await col.findOne('readonly-deleted-doc').exec(true);
assert.strictEqual(revivedDoc.lastName, 'revived');

await col.database.close();
});
it('should not write any document when one document of the request is not allowed', async () => {
const col = await humansCollection.create(0);
const port = await nextPort();
Expand Down
Loading