Skip to content

FIX REST revive of a deleted document skips the changeValidator stored-state check - #484

Merged
pubkey merged 2 commits into
masterfrom
claude/eloquent-hopper-6e78eb-rest-revive-validator
Nov 30, 2026
Merged

pubkey merged 2 commits into
masterfrom
claude/eloquent-hopper-6e78eb-rest-revive-validator

Conversation

@pubkey

@pubkey pubkey commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Bug

The REST /set and /patch routes treat a document that is deleted on the server as a new insert, because collection.findByIds() does not return deleted documents. They called the endpoint's changeValidator with assumedMasterState: undefined for it.

The documented readonly-fields validator (rx-server.md, "Readonly fields") returns true for inserts and compares the field with assumedMasterState for updates. So a client that is allowed to delete a document could change a readonly field: delete the document with /delete, then revive it with /set and a new value. The replication /push route already passes the stored document, deleted or not, as assumedMasterState.

Fix

For a revive, the changeValidator now gets the stored deleted document (passed through removeServerOnlyFields) as assumedMasterState, like the update path and the replication endpoint. Real inserts still get undefined.

A validator that blocks inserts with !change.assumedMasterState now treats a revive as a change of the stored document, the same as the replication endpoint does.

Test

test/unit/endpoint-rest.test.ts: should run the changeValidator against the stored state when a deleted document is revived. Without the fix, the revive with the changed readonly value is accepted.

Full unit suite: 108 passing. npm run check-types passes. An independent review confirmed the bug and the fix, including the shape of assumedMasterState, real inserts, the conflict retry, and the ownership check.

This PR touches the same /set insert branch as #483 (server-only fields on revive), but a different part of it. Both merge cleanly in either order.

Changelog: orga/changelog/fix-rest-revive-validator.md


Generated by Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

✅ Verify Test Reproduction: Tests FAILED without the fix (expected)

This confirms the changed tests correctly reproduce the bug that the source changes fix.

This workflow runs the changed tests without the source fix to verify they reproduce the bug.

Show output
=== test:unit output ===
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 test:unit
> npm run build && mocha --import=tsx ./test/unit.test.ts --bail --exit

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild
> npm run prebuild:babel && npm run prebuild:scripts

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild:babel
> rimraf -rf babel.config.js && cp node_modules/rxdb/babel.config.js babel.config.js

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild:scripts
> rimraf -rf scripts/generate-plugins-package-json.mjs && cp node_modules/rxdb/scripts/generate-plugins-package-json.mjs scripts/generate-plugins-package-json.mjs


> rxdb-server@0.0.0 build
> npm run prebuild && rimraf -rf ./dist && npm run build:plugins && concurrently "npm run build:cjs" "npm run build:es" "npm run build:types" --success all

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild
> npm run prebuild:babel && npm run prebuild:scripts

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild:babel
> rimraf -rf babel.config.js && cp node_modules/rxdb/babel.config.js babel.config.js

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 prebuild:scripts
> rimraf -rf scripts/generate-plugins-package-json.mjs && cp node_modules/rxdb/scripts/generate-plugins-package-json.mjs scripts/generate-plugins-package-json.mjs

npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.

> rxdb-server@0.0.0 build:plugins
> node ./scripts/generate-plugins-package-json.mjs

./plugins/server
./plugins/adapter-express
./plugins/replication-server
./plugins/client-rest
[1] npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[1] npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[2] npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[2] npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[0] npm warn Unknown env config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[0] npm warn Unknown project config "unsafe-perm". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
[2] 
[2] > rxdb-server@0.0.0 build:types
[2] > rimraf -rf ./dist/types && tsc --project ./config/tsconfig.types.json
[2] 
[1] 
[1] > rxdb-server@0.0.0 build:es
[1] > rimraf -rf dist/esm && cross-env NODE_ENV=es6 babel src --out-dir dist/esm --source-maps --extensions ".ts,.js" && echo '{ "type": "module", "sideEffects": false }' > dist/esm/package.json
[1] 
[0] 
[0] > rxdb-server@0.0.0 build:cjs
[0] > rimraf -rf dist/cjs && cross-env NODE_ENV=es5 babel src --out-dir dist/cjs --source-maps --extensions ".ts,.js"
[0] 
[0] @babel/plugin-transform-template-literals: The 'loose' option has been deprecated, use the `ignoreToPrimitiveHint` and `mutableTemplateObject` assumptions instead (https://babeljs.io/assumptions).
[0] @babel/plugin-transform-classes: The 'loose' option has been deprecated, use the 'setClassMethods', 'constantSuper', 'superIsCallableConstructor', 'noClassCalls', 'noClassPrototypeAssign', and 'noConstructorPrimitiveReturn' assumptions instead (https://babeljs.io/assumptions).
[1] @babel/plugin-transform-template-literals: The 'loose' option has been deprecated, use the `ignoreToPrimitiveHint` and `mutableTemplateObject` assumptions instead (https://babeljs.io/assumptions).
[2] npm run build:types exited with code 0
[1] Successfully compiled 15 files with Babel (616ms).
[1] npm run build:es exited with code 0
[0] Successfully compiled 15 files with Babel (724ms).
[0] npm run build:cjs exited with code 0
DEFAULT_STORAGE: undefined
# use RxStorage: memory


  server.test.ts
    init
-------------- RxDB dev-mode warning -------------------------------
you are seeing this because you use the RxDB dev-mode plugin https://rxdb.info/dev-mode.html?console=dev-mode 
This is great in development mode, because it will run many checks to ensure
that you use RxDB correct. If you see this in production mode,
you did something wrong because the dev-mode plugin will decrease the performance.

🤗 Hint: To get the most out of RxDB, check out the Premium Plugins
to get access to faster storages and more professional features: https://rxdb.info/premium/?console=dev-mode 

💬 Need help? The RxDB Discord is the fastest place to reach maintainers: https://rxdb.info/chat/?console=dev-mode 

You can disable this warning by calling disableWarnings() from the dev-mode plugin.
---------------------------------------------------------------------
      �[32m✔�[39m init
    basics
      �[32m✔�[39m should start end stop the server (77ms)
      �[32m✔�[39m should apply CORS (50ms)
      �[32m✔�[39m should not combine Access-Control-Allow-Origin: * with Access-Control-Allow-Credentials: true
      �[32m✔�[39m should add multiple endpoints
      �[32m✔�[39m should end open streams on close()
      �[32m✔�[39m should resolve close() after the database was closed
      �[32m✔�[39m should resolve when close() is called multiple times
      �[32m✔�[39m should reject start() when the port is already in use
    express adapter
      �[32m✔�[39m should work on an existing express app that does not parse JSON bodies
    .doesContainRegexQuerySelector()
      �[32m✔�[39m should return false
      �[32m✔�[39m should return true
    .mergeServerDocumentFieldsMonad()
      �[32m✔�[39m should merge the documents
      �[32m✔�[39m should not add server-only fields when serverDoc is undefined (new document)
      �[32m✔�[39m should return falsy clientDoc as-is without transforming it
      �[32m✔�[39m should not add the field when serverDoc lacks it (new-doc-on-server)
      �[32m✔�[39m should strip client-sent values for server-only fields missing on serverDoc
      �[32m✔�[39m should not produce extra keys when serverDoc is missing the field (no false conflict)

  endpoint-rest.test.ts
    basics
      �[32m✔�[39m should start end stop the server
      �[32m✔�[39m should work without auth handler
    /query
      �[32m✔�[39m should not allow a query when validUntil of the auth data has expired
      �[32m✔�[39m should return the correct query results
      �[32m✔�[39m should respect the auth header and queryModifier
      �[32m✔�[39m should not allow $regex queries
      �[32m✔�[39m should have access-control-allow-credentials set to true
    /query/observe
      �[32m✔�[39m should return the correct query results (72ms)
      �[32m✔�[39m should emit the newest result last when the authHandler has a varying latency (433ms)
      �[32m✔�[39m should disconnect an open stream when the auth becomes invalid (3047ms)
      �[32m✔�[39m should should automatically reconnect (3049ms)
      �[32m✔�[39m should respect the auth header and queryModifier
      �[32m✔�[39m should not allow $regex queries
      �[32m✔�[39m should work when the base64-encoded query contains URL-reserved characters
      �[32m✔�[39m should observe queries that contain characters outside of Latin-1 (70ms)
      �[32m✔�[39m should keep the Latin-1 query encoding of older clients (50ms)
      �[32m✔�[39m should not keep the stream open when the client closes it while the authHandler runs (2058ms)
      �[32m✔�[39m should stop observing on the server when the client unsubscribes (629ms)
      �[32m✔�[39m should emit an error when the auth of the open stream has expired (3049ms)
    /get
      �[32m✔�[39m should return the correct documents
      �[32m✔�[39m should respect the auth header and queryModifier
      �[32m✔�[39m should have access-control-allow-credentials set to true for /query/observe
    /set
      �[32m✔�[39m should write the documents
      �[32m✔�[39m should not accept if changeValidator says no
      �[32m✔�[39m should not accept new document inserts if changeValidator says no
      �[32m✔�[39m should throw an error via handleError when the server rejects a set
      �[32m✔�[39m should throw an error via handleError when the server rejects a delete
      �[32m✔�[39m should not allow overwriting a document the user does not own
      �[32m✔�[39m should not allow reviving a deleted document the user does not own (112ms)
      �[32m✔�[39m should allow reviving an own deleted document
      1) should run the changeValidator against the stored state when a deleted document is revived


  48 passing (13s)
  1 failing

  1) endpoint-rest.test.ts
       /set
         should run the changeValidator against the stored state when a deleted document is revived:
     Error: util.assertThrowsAsync(): Missing rejection with Error
      at assertThrows (node_modules/async-test-util/dist/lib/assert-throws.js:22:19)
      at Context.<anonymous> (test/unit/endpoint-rest.test.ts:1130:19)
      at process.processTicksAndRejections (node:internal/process/task_queues:104:5)





View full workflow run

@pubkey
pubkey merged commit 77f8d60 into master Nov 30, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants