From 68bf8df57e86f2e55db0c83495d91bfe4a6fb52d Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 13:35:53 +0000 Subject: [PATCH 1/2] Pass the stored deleted document to the changeValidator on REST revives Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs --- orga/changelog/fix-rest-revive-validator.md | 1 + src/plugins/server/endpoint-rest.ts | 5 +- test/unit/endpoint-rest.test.ts | 51 +++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 orga/changelog/fix-rest-revive-validator.md diff --git a/orga/changelog/fix-rest-revive-validator.md b/orga/changelog/fix-rest-revive-validator.md new file mode 100644 index 00000000..ddd71070 --- /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 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(); From 41201f73096b0a9f21cbaf51161174571c473ce3 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 13:36:59 +0000 Subject: [PATCH 2/2] Add PR link to changelog entry Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs --- orga/changelog/fix-rest-revive-validator.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orga/changelog/fix-rest-revive-validator.md b/orga/changelog/fix-rest-revive-validator.md index ddd71070..109cab7e 100644 --- a/orga/changelog/fix-rest-revive-validator.md +++ b/orga/changelog/fix-rest-revive-validator.md @@ -1 +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 +- 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)