Skip to content

fix(dpp)!: refuse immutableAllowSetting on a deletableDocument reference (PV14) - #4983

Merged
QuantumExplorer merged 1 commit into
v4.2-devfrom
claude/allow-setting-deletable-reference
Sep 25, 2026
Merged

QuantumExplorer merged 1 commit into
v4.2-devfrom
claude/allow-setting-deletable-reference

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

A document type (protocol version 14, schema generation 3) can list properties under immutable (frozen after creation, presence included) and, among those, under immutableAllowSetting (an absent immutable property may be set once, then it is frozen). The replace state validation lets an immutable property change in exactly two cases:

  1. a first-time set: the property is in immutableAllowSetting and the stored document has no value for it (added_data_fields);
  2. clearing a deletableDocument reference by id whose target has been deleted. This exception exists because every replace re-validates a deletableDocument reference, so a document whose immutable reference went dead could otherwise never be replaced again.

On one property the two apply one after the other, and together they repoint a reference that is supposed to be frozen:

"post": {
  "type": "object",
  "documentsMutable": true,
  "properties": {
    "author": { "type": "string", "maxLength": 63, "position": 0 },
    "pinnedId": {
      "type": "array", "byteArray": true, "minItems": 32, "maxItems": 32,
      "contentMediaType": "application/x.dash.dpp.identifier",
      "refersTo": { "type": "deletableDocument", "documentType": "draft" },
      "position": 1
    }
  },
  "required": ["author"],
  "immutable": ["author", "pinnedId"],
  "immutableAllowSetting": ["pinnedId"],
  "additionalProperties": false
}

Before:

  1. The contract registers.
  2. A replace sets pinnedId to draft A: accepted, a first-time set.
  3. Draft A is deleted.
  4. A replace removes pinnedId: accepted, a dead deletableDocument reference may be cleared. The stored post now has no pinnedId.
  5. A replace sets pinnedId to draft B: accepted, since the stored post has no value for it, so this counts as a first-time set.

The "immutable" pinnedId now points at draft B.

What was done?

The combination is now refused at contract registration. Under full validation, the generation-3 document type parser refuses an immutableAllowSetting entry whose property is a deletableDocument reference by id. That is the only form the dead-reference clear covers. Every other deletableDocument form (a typed array of them, one inside an object, one through a lookup, alone or in an expression) is already refused on any immutable property, and an immutableAllowSetting entry must also be immutable. So after this change, no deletableDocument reference can be set once.

After, the same schema fails to register with a BasicError::ContractError(InvalidContractStructure) consensus error:

document type "post" lists "pinnedId" in `immutableAllowSetting`, but it is a deletableDocument reference: a replace may clear it once its target is deleted, and the next replace could then set it to another document. Use a permanentDocument reference, or leave it out of immutableAllowSetting

"immutable": ["author", "pinnedId"] without immutableAllowSetting still registers, and it keeps the clear as its one way out once the target is deleted. immutableAllowSetting on a permanentDocument or identity reference still registers too, because those targets are never deleted and such a reference is never cleared.

Why refuse at registration instead of refusing the clear at write time: every replace re-validates a deletableDocument reference. If the clear were refused, a document whose frozen reference went dead could never be replaced again, which is the problem the clear was added to solve.

Details:

  • The check lives in validate_no_immutable_deletable_element_references (document type parser generation 3, try_from_schema/v3/mod.rs), at the branch that lets the single top-level reference through. It sits next to the related refusals and reuses their error, DataContractError::InvalidContractStructure through consensus_or_protocol_data_contract_error. No new consensus error was needed.
  • The check runs under full validation only, like the related refusals. A stored contract is re-parsed without it and stays readable.
  • Contract updates are covered by the same check. validate_update 1 lets immutableAllowSetting gain a property that becomes immutable in the same update, so an update could reach this combination. However, the update transition parses the whole new contract with full validation (DataContract::try_from_platform_versioned) before the update rules run, and that parse now refuses it.
  • The meta-schema cannot express a rule that crosses two keywords, so this is a parser-only rule, like the other immutable rules.
  • The replace state validation comment (document replace state validation 1) now explains why the two exceptions can no longer apply to one property, and why the clear was kept.
  • Documentation: the book chapter on documents (the immutable-properties rules, the enforcement paragraph, and the typed array references bullet) and item 31 of the protocol version 14 notes in v14.rs describe the restriction.
  • No system contract uses immutableAllowSetting (checked packages/*-contract and the dpp fixtures), so no system contract is affected.
  • The wasm-dpp2, js-evo-sdk, Swift and Kotlin surfaces only report what a registered contract declares, so they need no change.

How Has This Been Tested?

New tests in packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v3/immutable_tests.rs, in the module deletable_document_reference. All four use PlatformVersion::latest():

  • should_refuse_allow_setting_on_a_deletable_document_reference: the schema above is refused under full validation with the consensus InvalidContractStructure error and the message above. The non-validating (stored contract) parse still records the lists as declared.
  • should_admit_an_immutable_deletable_document_reference_that_is_not_settable: an immutable deletableDocument reference with no immutableAllowSetting entry still registers.
  • should_admit_allow_setting_on_a_reference_that_is_never_cleared: immutableAllowSetting on a permanentDocument reference and on an identity reference still registers.
  • should_refuse_an_update_making_a_deletable_document_reference_settable_once: a contract registers with pinnedId mutable. Version 2 makes it immutable and lists it under immutableAllowSetting. DataContract::validate_update alone accepts that pair, so the update path really reaches it. The full-validation parse the update transition performs refuses it with the same error.
cargo test -p dpp immutable_tests::deletable_document_reference
cargo test -p dpp immutable_tests
cargo test -p dpp should_refuse_an_immutable_typed_array_of_deletable_document_references
cargo test -p dpp validate_update
cargo test -p drive-abci batch::tests::document::deletable_document_reference
cargo test -p drive-abci batch::tests::document::immutable

Results, run locally:

  • cargo test -p dpp --lib immutable_tests: 26 passed; cargo test -p dpp --lib validate_update: 116 passed; cargo test -p dpp --lib reference: 147 passed.
  • Mutation check: with the new refusal disabled, should_refuse_allow_setting_on_a_deletable_document_reference and should_refuse_an_update_making_a_deletable_document_reference_settable_once fail and the two admission tests still pass.
  • cargo test -p drive-abci --lib immutable (14 passed) and cargo test -p drive-abci --lib deletable (15 passed).
  • cargo clippy -p dpp --all-features --all-targets -- -D warnings and cargo fmt --all -- --check are clean.

The drive-abci change is comment-only. Its immutable and deletableDocument reference tests are listed because they exercise both exceptions, and none of their contracts combines them.

Breaking Changes

Consensus change at protocol version 14 only, which is unreleased. A contract that lists a deletableDocument reference by id under immutableAllowSetting can no longer be registered, and no update can introduce one. Earlier protocol versions do not know either keyword.

On a devnet already running protocol version 14 from a 4.2 beta, a contract registered with this combination stays readable, because stored contracts are parsed without the registration rules. Any update to that contract will now be refused, since an update re-parses the whole contract.

In-place changes to shipped generations

None. The edited parser, document type parser generation 3 (try_from_schema: 3, selected only by CONTRACT_VERSIONS_V6), is reached only from protocol version 14, which is unreleased. The only change to document replace state validation 1 (selected only by DRIVE_ABCI_VALIDATION_VERSIONS_V10, protocol version 14) is a comment.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed
  • If I added or changed GroveDB structure, I described it in the area's structure.rs, regenerated grovedb-structure.json, and checked the structure viewer link posted on this pull request

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

PR Hygiene · 39a76d0

  • Bots — coderabbitai skipped after its own rate limit · thepastaclaw requested changes — dismiss the review or push a fix
  • Self-review — post /self-reviewed once the bots are done
  • Within your 5 open PRs
  • Build green
  • Approvals — you own every area touched; none needed

When every box is checked the PR Hygiene check passes and this can merge.

…nce (PV14)

A replace may change an immutable property in two cases: a first-time
set of an immutableAllowSetting property the stored document lacks, and
clearing a deletableDocument reference whose target has been deleted.
On one property they applied in sequence (set to A, A deleted, clear,
set to B) and repointed the frozen reference.

Generation 3 of the document type parser now refuses, under full
validation, an immutableAllowSetting entry that is a deletableDocument
reference by id, the only form the clear covers. A contract update
re-parses the whole contract the same way, so updates are covered too.
The clear is kept: every replace re-validates the reference, so without
it such a document could never be replaced once its target is deleted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added this to the v4.2.0 milestone Sep 24, 2026
@coderabbitai

coderabbitai Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository: dashpay/platform/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: fcf254d7-2635-4aae-a8df-742fe3aa027a

📥 Commits

Reviewing files that changed from the base of the PR and between ac3d94e and 39a76d0.

📒 Files selected for processing (5)
  • book/src/data-model/documents.md
  • packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v3/immutable_tests.rs
  • packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v3/mod.rs
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/document/document_replace_transition_action/state_v1/mod.rs
  • packages/rs-platform-version/src/version/v14.rs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Sep 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📖 Book Preview built successfully.

Download the preview from the workflow artifacts.
To view locally: download the artifact, unzip, and open index.html.

Updated at 2026-09-24T14:09:57.920Z

@thepastaclaw

thepastaclaw commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

⛔ Final review complete — 1 blocking finding(s) (commit 39a76d0) · triage: critical · Phase 2 only (queue backlog)

@github-actions github-actions Bot added the bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. label Sep 24, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final validation — Phase 2 only (queue backlog)

The parser change correctly rejects the unsafe combination for newly registered contracts and contract updates. However, PV14 contracts created before this fix are intentionally reparsed with full validation disabled, so their immutable deletableDocument declarations remain active and the existing replace-state exceptions still allow the reference to be cleared and later repointed. This leaves the stated security invariant unenforced for already deployed PV14 contracts.

🔴 1 blocking

1 finding(s) not shown inline (the lines are not part of this PR's diff)

🔴 Blocking: Pre-existing PV14 contracts can bypass the new immutable-reference protection
packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v3/mod.rs:517-523

The new restriction runs only when full_validation is true. The stored-contract path deliberately reparses existing contracts with full_validation = false, and the added test explicitly confirms that this preserves immutableAllowSetting on the unsafe reference. Consequently, a contract registered by the pre-fix PV14 code remains usable after upgrade with the vulnerable declaration. The replace validator still reads that declaration at document_replace_transition_action/state_v1/mod.rs:85-107: it allows clearing the dead reference, and after the property is absent it allows the next replacement to set it again as an added_data_field. Thus the immutable reference can still be repointed from document A to document B on an already deployed PV14 contract, despite the PR's stated goal that no such reference can be set once. The compatibility decision to keep these contracts readable needs a corresponding migration/invalidation plan or write-time handling that prevents the second set for legacy contracts.

source: gpt-6-astra (phase2-reviewer: security-auditor)

Review provenance

Source: reviewer 1: gpt-6-astra (agent: phase2-reviewer, role: general); reviewer 2: gpt-6-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 3: gpt-6-astra (agent: phase2-reviewer, role: platform-versioning); reviewer 4: gpt-6-astra (agent: phase2-reviewer, role: rust-quality); reviewer 5: gpt-6-astra (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6-astra (agent: astra-verifier, role: final-verifier)

  • Triage: critical by gpt-6-astra (effort low) — The diff changes consensus validation rules for document contract registration and replace-transition behavior in try_from_schema/v3/mod.rs and document_replace_transition_action/state_v1/mod.rs, which is a critical consensus surface despite being a focused fix.
  • Phase 1 reviewers: not run (skipped for throughput: 18 PRs queued, above the 10 limit)
  • Fresh verifier: gpt-6-astra — final-verifier; agent astra-verifier
  • Phase 2 reviewers: gpt-6-astra — general (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — architecture-layering (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — platform-versioning (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — rust-quality (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — security-auditor (completed, effort xhigh); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v3/mod.rs`:
- [BLOCKING] packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v3/mod.rs:517-523: Pre-existing PV14 contracts can bypass the new immutable-reference protection
  The new restriction runs only when `full_validation` is true. The stored-contract path deliberately reparses existing contracts with `full_validation = false`, and the added test explicitly confirms that this preserves `immutableAllowSetting` on the unsafe reference. Consequently, a contract registered by the pre-fix PV14 code remains usable after upgrade with the vulnerable declaration. The replace validator still reads that declaration at `document_replace_transition_action/state_v1/mod.rs:85-107`: it allows clearing the dead reference, and after the property is absent it allows the next replacement to set it again as an `added_data_field`. Thus the immutable reference can still be repointed from document A to document B on an already deployed PV14 contract, despite the PR's stated goal that no such reference can be set once. The compatibility decision to keep these contracts readable needs a corresponding migration/invalidation plan or write-time handling that prevents the second set for legacy contracts.

@github-actions

Copy link
Copy Markdown
Contributor

Your move: thepastaclaw requested changes on this head; dismiss the review or push a fix.
Full checklist in the description.

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed and removed waiting-bots Waiting for the review bots to report on this head labels Sep 24, 2026
@QuantumExplorer
QuantumExplorer merged commit 3d7554a into v4.2-dev Sep 25, 2026
38 of 39 checks passed
@QuantumExplorer
QuantumExplorer deleted the claude/allow-setting-deletable-reference branch September 25, 2026 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. waiting-self-review Waiting for the author to post /self-reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants