Repository navigation
Description
Follow-on from the same claude-code audit session as #1781 (the @json merge duplicate) — this half reproduces with byte-identical values and a plain in-ledger shape, so it's its own defect, not a variant of the identity bug.
What happens
A normal transaction that would violate an attached SHACL shape is rejected at commit time. A branch merge that produces the identical violating state commits silently. Repro against 4.1.6 (full script below):
- Install
ex:ConfigShape—sh:targetNode ex:config,sh:property { sh:path ex:items; sh:maxCount 1 }— and seed one value A. - Control: plain
insertof a secondex:itemsvalue onmain→ correctly rejected:
Path: ex:items
Constraint: http://www.w3.org/ns/shacl#MaxCountConstraintComponent
- Branch
featreplaces A→B;mainreplaces A→C;fluree branch merge feat(defaulttake-both, whose documented semantics are "both values coexist"):
Merged 'feat' into 'main' (t=4, 1 commits copied, 1 conflicts).
$ fluree query ... # main now has TWO ex:items values
['[{"name":"beta","qty":2}]', '[{"name":"gamma","qty":3}]']
$ fluree validate demo
Violation: http://example.org/config
path: http://example.org/items
component: MaxCountConstraintComponent
message: Expected at most 1 value(s) but found 2
Conforms: false — 1 violation(s), 0 warning(s), 0 info (1 shape(s) checked)
So the merge manufactures exactly the state the control transaction was refused for, reports success, and the violation sits in the ledger until someone happens to run fluree validate. In the originating field case the shape was a sh:maxCount 1 and the violation was only discovered much later, which is the worst version of this: constraints that are supposed to be invariants become advisory the moment a branch operation is involved.
Why: merge/rebase/revert never call validation at all
The normal write path routes every staged view through SHACL — apply_shacl_policy_to_staged_view → validate_view_with_shacl (fluree-db-api/src/tx.rs:951, tx.rs:1242, wired into the txn flow at tx.rs:1496). The branch operations build their commits on a parallel path that has no hook: build_merge_general stages the resolved flakes straight into StagedLedger::new and build_commit (fluree-db-api/src/merge.rs:534-581), and there is not a single reference to shacl/validation in merge.rs, rebase.rs, or revert.rs. So this isn't a policy decision expressed in code — rebase replays and revert commits are equally unvalidated, which the same audit confirmed by reading, though the repro above only exercises merge.
One thing worth settling explicitly: docs/security/cross-ledger-policy.md-adjacent docs say commit replay intentionally skips SHACL for cross-ledger shape sources (the originating case did use f:shapesSource + f:ledger, which is what sent us looking there first). The repro above uses an in-ledger shape, so that carve-out isn't what's happening — merge simply has no validation step for any shapes. If the intent is that the cross-ledger carve-out also covers branch operations, that seems worth (a) saying somewhere and (b) not extending to in-ledger shapes, which have no replay-ordering excuse.
Fix direction
I think the merge (and rebase/revert) build phase should validate the staged view the same way tx.rs does before apply — fail the operation like a normal transaction would, which composes sensibly with --strategy abort and gives take-both a backstop for exactly the maxCount case where "both values coexist" is self-contradictory with the schema. If a hard fail is judged too aggressive for merge UX, minimally the violations belong in the MergeReport so the caller learns the merged state doesn't conform — silence is the one behavior that seems clearly wrong. Happy to talk through where the hook should live; build_merge_general already holds the staged view at merge.rs:540 right before build_commit, which looks like the natural seam.
Full repro script (fluree 4.1.6, local mode)
#!/bin/bash
set -u
mkdir -p /tmp/shacl-merge-repro && cd /tmp/shacl-merge-repro
fluree init >/dev/null 2>&1; fluree create demo >/dev/null 2>&1
fluree insert -l demo -e '{"@context": {"sh": "http://www.w3.org/ns/shacl#", "ex": "http://example.org/"}, "@id": "ex:ConfigShape", "@type": "sh:NodeShape", "sh:targetNode": {"@id": "ex:config"}, "sh:property": {"@id": "ex:ConfigShapeItems", "sh:path": {"@id": "ex:items"}, "sh:maxCount": 1}}'
fluree insert -l demo -e '{"@context": {"ex": "http://example.org/"}, "@id": "ex:config", "ex:items": {"@value": [{"name": "alpha", "qty": 1}], "@type": "@json"}}'
# CONTROL — a 2nd value via plain insert is rejected (MaxCountConstraintComponent):
fluree insert -l demo -e '{"@context": {"ex": "http://example.org/"}, "@id": "ex:config", "ex:items": {"@value": [{"name": "delta", "qty": 4}], "@type": "@json"}}'
# Now the same violating state via merge:
fluree branch create feat -l demo
fluree update -l demo:feat -e '{"@context": {"ex": "http://example.org/"}, "where": [{"@id": "ex:config", "ex:items": "?old"}], "delete": [{"@id": "ex:config", "ex:items": "?old"}], "insert": [{"@id": "ex:config", "ex:items": {"@value": [{"name": "beta", "qty": 2}], "@type": "@json"}}]}'
fluree update -l demo -e '{"@context": {"ex": "http://example.org/"}, "where": [{"@id": "ex:config", "ex:items": "?old"}], "delete": [{"@id": "ex:config", "ex:items": "?old"}], "insert": [{"@id": "ex:config", "ex:items": {"@value": [{"name": "gamma", "qty": 3}], "@type": "@json"}}]}'
fluree branch merge feat -l demo # succeeds: "1 commits copied, 1 conflicts"
fluree query -l demo -e 'SELECT ?o WHERE { <http://example.org/config> <http://example.org/items> ?o }' --format json # 2 values
fluree validate demo # Conforms: false — maxCount 2Versions: fluree CLI 4.1.6 (local mode, macOS); source refs against main @ 1aae89018.
Activity