diff --git a/orga/changelog/fix-rest-revive-validator.md b/orga/changelog/fix-rest-revive-validator.md new file mode 100644 index 00000000..109cab7e --- /dev/null +++ b/orga/changelog/fix-rest-revive-validator.md @@ -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) diff --git a/src/plugins/server/endpoint-rest.ts b/src/plugins/server/endpoint-rest.ts index abc4022b..6b7dc98f 100644 --- a/src/plugins/server/endpoint-rest.ts +++ b/src/plugins/server/endpoint-rest.ts @@ -265,9 +265,12 @@ export class RxServerRestEndpoint 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'); diff --git a/test/unit/endpoint-rest.test.ts b/test/unit/endpoint-rest.test.ts index a3d6e5d6..b57259c9 100644 --- a/test/unit/endpoint-rest.test.ts +++ b/test/unit/endpoint-rest.test.ts @@ -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('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();