Repository navigation
fix(r2rml): a graph source reached through a GRAPH scope is filtered by its own view policy - #2314
Conversation
…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.
…by its own policy
…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.
bplatz
left a comment
There was a problem hiding this comment.
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.
| let Ok(id) = fluree_db_core::LedgerId::parse(&name) else { | ||
| continue; | ||
| }; | ||
| let is_source = self |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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" }]] }The un-normalized name goes from |
…cy-in-graph-scope
…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.
|
Filed the debug-build panic (a graph source named without its branch) as #2362. |
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.
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:
from+from-named/FROM NAMED+GRAPH): with no policy on the defaultgraph 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;
graphpattern of a query over one ledger, the same.Present on main, v4.2.5 and v5.0.0-alpha.2.
What changes
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.
from-named/FROM NAMED). A query over one ledger that reaches one through a graph scope without naming it there does notread it, governed source or not (the scope cannot tell them apart):
400 err:db/InvalidQuery, whether the name is a constant in thegraphpattern, avaluesorbindconstant of its graph variable, or bound from data. The message names the source and theremedy: name it in from-named (SPARQL:
FROM NAMED <iri>) and useGRAPH <iri>orVALUES ?g { <iri> }.FROMand noFROM NAMEDanswers 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
graphpattern, and kept JSON-LD apartfrom SPARQL, so it was removed in review.
Breaking changes
model's).
graphpattern (or binds its graph variable toone) without
from-namedis refused (400). On v4.2.5 it read the source, under the ledger's policy. Add thesource to
from-named.ExecutionContextandContextConfiggainpolicy_source: Option<Arc<str>>(ContextConfigderivesDefault).Tests
it_iceberg_policy, each against the source queried directly with the same options, on an Iceberg source and a SQLsource over the same rows, JSON-LD and SPARQL:
values-bound graph variable, governed or not: eachanswers under its own policy;
valuesconstant, name bound from data) refused with 400and the remedy text; SPARQL
FROM+GRAPHandFROM+VALUES ?ganswer no rows;narrows that further;
GRAPH ?gover named graph sources (one governed, one not) answers each under its own policy, with?gbound 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), wasm32clippy, 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.