Skip to content

fix(r2rml): a graph source reached through a GRAPH scope is filtered by its own view policy - #2314

Merged
bplatz merged 9 commits into
mainfrom
fix/graph-source-policy-in-graph-scope
Nov 12, 2026
Merged

bplatz merged 9 commits into
mainfrom
fix/graph-source-policy-in-graph-scope

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What was wrong

A graph scope runs in a context switched from the query's default graph, which kept that graph's view-policy
enforcer. An R2RML graph source (Iceberg or SQL) reached through a graph scope was therefore filtered by the default
graph's policy rather than its own:

  • as a named graph of a dataset (from + from-named / FROM NAMED + GRAPH): with no policy on the default
    graph the source was read unfiltered; with a request policy the source's model was not applied; and the source's
    policy targets were encoded against the default graph's namespaces;
  • named in a JSON-LD graph pattern of a query over one ledger, the same.

Present on main, v4.2.5 and v5.0.0-alpha.2.

What changes

  • The R2RML view-policy gate takes the enforcer and snapshot of the source's own graph when the dataset has one,
    and the context's otherwise. Where filtering is required and no enforcer resolves, the query is refused; the
    debug assertion that stood there becomes that error, so debug and release builds agree. The SQL lane shares the
    gate and propagates the refusal.
  • A graph scope reads a graph source only as a named graph of the query's dataset (from-named /
    FROM NAMED). A query over one ledger that reaches one through a graph scope without naming it there does not
    read it, governed source or not (the scope cannot tell them apart):
    • JSON-LD refuses with 400 err:db/InvalidQuery, whether the name is a constant in the graph pattern, a
      values or bind constant of its graph variable, or bound from data. The message names the source and the
      remedy: name it in from-named (SPARQL: FROM NAMED <iri>) and use GRAPH <iri> or VALUES ?g { <iri> }.
    • SPARQL with FROM and no FROM NAMED answers no rows, as for any graph outside its dataset (unchanged).
      The execution context records the graph whose policy it carries; callers that record none (an embedder running
      R2RML patterns without view policy) are unaffected.

An earlier revision of this PR instead rewrote the one-ledger JSON-LD query to carry the source as a named graph.
That cost a nameservice lookup per graph name on every JSON-LD query with a graph pattern, and kept JSON-LD apart
from SPARQL, so it was removed in review.

Breaking changes

  • A graph source reached through a graph scope is now filtered by its own view policy (the request's and its
    model's).
  • A JSON-LD query over one ledger that names a graph source in a graph pattern (or binds its graph variable to
    one) without from-named is refused (400). On v4.2.5 it read the source, under the ledger's policy. Add the
    source to from-named.
  • ExecutionContext and ContextConfig gain policy_source: Option<Arc<str>> (ContextConfig derives Default).

Tests

it_iceberg_policy, each against the source queried directly with the same options, on an Iceberg source and a SQL
source over the same rows, JSON-LD and SPARQL:

  • a governed source as a dataset's named graph, with and without a request policy on the default graph;
  • a source in the dataset reached by name or through a values-bound graph variable, governed or not: each
    answers under its own policy;
  • a source outside the dataset: JSON-LD (constant name, values constant, name bound from data) refused with 400
    and the remedy text; SPARQL FROM + GRAPH and FROM + VALUES ?g answer no rows;
  • an ungoverned source beside a default graph under a request policy keeps every row that policy leaves it;
  • a root caller reads a governed source unfiltered;
  • a request selecting policy by class is filtered by both its options and the source's model, and a request policy
    narrows that further;
  • an unbound GRAPH ?g over named graph sources (one governed, one not) answers each under its own policy, with
    ?g bound to the source.

Revert checks: without the named-graph enforcer the dataset tests fail (and the existing
dataset_mode_enforces_per_graph); without the refusal the outside-the-dataset tests fail.

Gates: fmt (workspace and testsuite-sparql), CI clippy (--all --all-features --all-targets -D warnings), wasm32
clippy, W3C suites, query / policy / api libs, grp_query, grp_query_sparql, grp_policy, grp_graphsource,
it_graph_source_r2rml, it_iceberg_local_fs, it_r2rml_untyped_columns, it_fused_aggregate_routing,
it_sql_pushdown_lane, it_iceberg_policy.

…by its own view policy

A graph scope runs in a context switched from the default graph, which
keeps that graph's policy enforcer. The view-policy gate of an R2RML scan
read the context's enforcer, so a graph source that was a named graph of a
dataset was filtered by the default graph's policy rather than its own.
Where the default graph had none, no gate was built and the scan read the
source unfiltered (a debug build stopped on an assertion instead); where
the request carried a policy, the source's model was not applied. The
source's targets were also encoded against the default graph's namespaces,
while its policy was built against its own.

The gate now takes the enforcer and the snapshot of the source's own graph
when the dataset has one, and the context's otherwise. Where filtering is
required and no enforcer resolves for the source, the query is refused
rather than read unfiltered; the assertion becomes that error, so debug and
release builds agree. The SQL lane, which shares the gate, propagates the
refusal instead of treating a missing gate as allow-all.
…es in a graph pattern through the dataset

A JSON-LD query over one ledger can name a graph source in a graph pattern
without listing it in from-named. It ran on the single-ledger path, where
the graph scope reaches the source with the ledger's view policy in force,
not the source's own.

Such a query now reads every graph source it names by IRI in a graph
pattern, anywhere in the query, as a named graph of a dataset, as it would
with the source in from-named: the source's own policy filters it. A name
that is not a graph source (a graph of the ledger, a variable) leaves the
query on the single-ledger path, so an ungoverned source answers as before.
…aset is refused

A graph variable can bind to a graph source's name in a query whose
dataset does not include the source, so no named graph carries the
source's own policy, and the context's policy is that of the graph the
query reads. The execution context now records the graph source its policy
was built for (the query's primary, when it is one), and the view-policy
gate refuses a scan of a source in its own graph scope whose context policy
was not built for it, naming the source and from-named. An ungoverned
source reached that way is refused too: the scope cannot tell the two
apart. Naming the source in from-named, or by IRI in a JSON-LD graph
pattern, reads it with its own policy.
…olicy per graph

The refusal of a graph scope that reaches a graph source outside the
dataset fired on every context with no recorded policy source, including
callers that run R2RML patterns with no view policy at all (an embedder
executing a ledger query with its own providers). A context now records
the graph whose view policy it carries: the query's primary, a ledger or a
graph source, for view and dataset queries. The refusal applies when that
graph is another one; a caller that records none is not affected.
…ant reads it under its own policy

A JSON-LD query over one ledger that gives a graph pattern's variable a
graph source's name as a values or bind constant now reads that source as
a named graph too, as one naming it in the pattern does: the source's own
view policy filters it, and an ungoverned source answers as before. Only a
graph variable bound from data, which no query text names, still reaches a
source outside the dataset; that is refused as a query error (400), naming
the source and how to read it (from-named, then GRAPH or VALUES).

Tests, each against the source queried directly with the same options, on
an Iceberg source and a SQL source: a graph variable bound by values, for
governed and ungoverned sources; one bound from data, refused with the
remedy; an ungoverned source beside a default graph under a request policy
keeps the rows that policy leaves; a root caller reads a governed source
unfiltered; a request selecting policy by class is filtered by both its
options and the source's model, a request policy narrowing further; and an
unbound graph variable over named graph sources answers each under its own
policy, bound to its name.
@aaj3f aaj3f added bug Something isn't working as expected breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section P0 Drop everything: data correctness / security area:query Query execution, planning, fast paths, overlay, result formatting labels Nov 11, 2026

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The fix is right: the gate takes the source's own dataset entry (enforcer and snapshot) through the name resolver, every API call site sets policy_source, and anything the JSON reroute misses lands on the refusal rather than an unfiltered read. Approving, but please look at the inline comment before merging.

Also: please add a test for the SPARQL one-ledger case. FROM <ledger> … GRAPH <source> without FROM NAMED, and the VALUES ?g { <source> } form, both return zero rows on this branch (I ran them), which is correct — but it is the SPARQL twin of the JSON-LD shape this PR reroutes and nothing pins it.

Comment thread fluree-db-api/src/query/connection.rs Outdated
let Ok(id) = fluree_db_core::LedgerId::parse(&name) else {
continue;
};
let is_source = self

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This adds a nameservice read to queries that have nothing to do with graph sources. Every graph pattern name that parses as a ledger id is looked up, on every JSON-LD query over one ledger. A full IRI is skipped (LedgerId::parse rejects name://…), but a compact IRI like ex:g1 or a bare name is not, so a native ledger using named graphs pays one uncached lookup per distinct name per query — a network round trip on a remote or proxied nameservice. The walk runs on the raw JSON before context expansion, so it can't tell the name is one of the ledger's own graphs.

Please skip the lookup for the ledger's own registered graphs, or cache the answer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Addressed in 37f96c4, by removing the reroute rather than skipping or caching the lookup. A graph scope now reads a graph source only when the query names it in from-named / FROM NAMED, so no query pays a nameservice lookup for its graph names. A JSON-LD query over one ledger that names a source without from-named gets the 400 with the remedy, where SPARQL with FROM and no FROM NAMED answers no rows. That is a breaking change for that JSON-LD shape and is listed as one in the description. The same commit adds the SPARQL test asked for in the review.

@bplatz

bplatz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Separate from this PR, found while probing it: a JSON-LD graph pattern that names a graph source without its branch panics in debug builds.

{ "from": "native-people:main", "select": ["?s", "?n"],
  "where": [["graph", "local-people", { "@id": "?s", "ex:name": "?n" }]] }
panicked at fluree-db-core/src/address_path.rs:30:5:
Non-canonical ledger id 'local-people' reached a storage path: expected 'name:branch'.

The un-normalized name goes from GraphOperator::execute_in_graph → r2rml_mapping_for_rewrite → FlureeR2rmlProvider::compiled_mapping → Fluree::content_store. It reproduces on main too, with and without from-named, so it predates this PR; release builds apply the default branch instead of panicking. Worth a look, and perhaps an issue so it is tracked.

…of the dataset

A JSON-LD query over one ledger that named a graph source in a graph
pattern was rewritten to carry it as a named graph, which cost a
nameservice lookup for every graph name of every such query and kept
JSON-LD apart from SPARQL, where a source outside the dataset is never
read. The rewrite is gone: JSON-LD refuses the scope with the 400 that
names the remedy (from-named), for a constant name and a values or bind
constant as for a name bound from data, governed source or not. SPARQL
with FROM and no FROM NAMED answers no rows, now pinned by a test.
@bplatz

bplatz commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Filed the debug-build panic (a graph source named without its branch) as #2362.

@bplatz
bplatz merged commit 4b717f1 into main Nov 12, 2026
19 checks passed
@bplatz
bplatz deleted the fix/graph-source-policy-in-graph-scope branch November 12, 2026 03:53
bplatz added a commit that referenced this pull request Nov 12, 2026
Brings in #2157, #2226, #2297, #2314 and #2331.

Conflicts:

- eval/helpers.rs (#2226): the filter verdict cache key takes main's store
  instance id and next's u32 graph id, and main's test builds its stores
  with next's `BinaryIndexStore::empty()`.
- grp_transact.rs lists both sides' test modules.
- #2157 changed `retract` and `drop_branch` on every nameservice backend,
  and the nested-ledger prefixes a sweep excludes. next replaced those
  operations with the fenced record store, so the nameservice, sweep and
  drop-test hunks take next's side here. The next commit carries the fix
  to next's operations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:query Query execution, planning, fast paths, overlay, result formatting breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section bug Something isn't working as expected P0 Drop everything: data correctness / security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants