Repository navigation
Description
Summary
A SERVICE <fluree:ledger:...> block that targets a different ledger returns subject/object IRIs decoded against the requesting ledger's namespace table rather than the target's. The query succeeds — HTTP 200, a well-formed absolute IRI, no warning — but the IRI names the wrong thing whenever the two ledgers assigned the same namespace code to different prefixes.
I found this while verifying the review round on #1629 (that PR made GROUP_CONCAT emit IRIs where it previously dropped them, which is how the wrong value first became visible). It is not caused by that change, though: the plain SELECT below is wrong with no aggregate anywhere in the query.
Repro
Two ledgers, each holding one subject under a prefix unique to it. Namespace codes are allocated per-ledger in insertion order, so both prefixes land behind the same code — that is what makes the mis-decode observable rather than merely possible.
svc-alpha:main—http://alpha.example/a1, withhttp://alpha.example/tag"shared"svc-beta:main—http://beta.example/b1, withhttp://beta.example/tag"shared"
Dataset: svc-alpha:main as default graph, svc-beta:main as a named graph.
SELECT ?s WHERE { SERVICE <fluree:ledger:svc-beta:main> { ?s ?p "shared" } }| actual | http://alpha.example/b1 |
| expected | http://beta.example/b1 |
The local name (b1) is the beta subject; only the prefix is wrong. Against ledgers whose namespace tables happen to agree, the same query returns the right answer, so this is data-dependent and will not show up in a fixture where both sides share a prefix.
Scope
Confined to IRIs the SERVICE block newly binds, as they escape to output, aggregation, or rendering. Anything consuming such a term inherits the mis-decode — confirmed for DISTINCT, GROUP BY, GROUP_CONCAT, and a query pairing a local pattern with a SERVICE-bound variable:
SELECT ?s ... -> http://alpha.example/b1 (wrong)
SELECT DISTINCT ?s ... -> http://alpha.example/b1 (wrong)
SELECT ?s (COUNT(*) AS ?n) ... GROUP BY ?s
-> http://alpha.example/b1, 1 (wrong)
SELECT ?a ?s WHERE { ?a ?ap "shared" . SERVICE <...beta> { ?s ?p "shared" } }
-> http://alpha.example/a1, http://alpha.example/b1
(the local ?a is right, the SERVICE ?s is wrong)
To correct my own first reading of that last one: it is a wrong emitted IRI, not a wrong join. Joins across the boundary are unaffected, and I have since verified that in both orders — two different IRIs sharing a local name and namespace code do not conflate, and the same absolute IRI under deliberately different codes still matches, each agreeing with its GRAPH equivalent. service.rs seeds its inner tree from the parent row (SeedOperator::from_batch_row), so a term crosses the boundary by substitution and the target ledger re-encodes it — nothing compares a foreign SID.
Two shapes are correct, and they are worth stating because between them they locate the fix:
GRAPH <svc-beta:main> { ... }returnshttp://beta.example/b1. The named-graph form goes throughDatasetOperator, which stamps cross-ledger bindings on the way out (below).STR(?s)inside the SERVICE block returnshttp://beta.example/b1.eval_strreadsctx.active_snapshot.namespaces()and is evaluated per-row while the context is still the SERVICE target's, so it decodes against the right table. Only terms that escape the block as a raw SID are affected.
Root cause
DatasetOperator already solves this for its members. fluree-db-query/src/dataset_operator.rs:388 stamp_provenance runs each batch through stamp_binding (:428), converting Binding::Sid into Binding::IriMatch carrying the IRI decoded in its own ledger via sid_to_iri_match (:453); it errors rather than falling back, because multi-ledger equality is defined around IriMatch. It is gated on needs_provenance, set at :569.
The SERVICE operator has no equivalent step. fluree-db-query/src/service.rs:188-194 builds the inner tree against ctx.with_graph_ref(gref) — correctly, which is why STR() works — but the resulting rows are buffered as-is at :264/:343 and handed back to the parent with the target ledger's SIDs intact. The parent then decodes them against its own snapshot.
Severity
Silent wrong answers on a 200 response, on a cross-ledger read path. There is no error and nothing in the output that distinguishes a correct IRI from a mis-decoded one, so a caller cannot detect it without independently knowing the target ledger's namespace table. Data-dependent: it only bites when the two ledgers' namespace tables disagree on a code, which is the normal case for independently-created ledgers.
Note on the fix
Worth flagging that the obvious fix — stamping the SERVICE operator's rows the way dataset members are stamped — is not a drop-in. Converting the returned terms to IriMatch changes join, DISTINCT, and GROUP BY key identity for every SERVICE query, and stamp_binding hard-errors on EncodedSid while the SERVICE path does not disable the binary store the way dataset_operator.rs:616-620 does for its members. There is also the remote (non-fluree:ledger:) SERVICE case, which has no local namespace table at all, and which the two paths should probably converge on rather than diverge from. I will work the design before proposing anything.
Activity
stamp provenance on rows leaving a cross-ledger SERVICE