Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.
Rust ai-nativedescription-logicsformal-verificationinfrastructure-as-codeisabelle-languageknowledge-graphleanlean4mcpmcp-serverontologyowlowl2reasoningrustshaclsparqltheorem-proving
Refuse a sub-SELECT that cannot receive the pre-binding (#193)
Fabio Rovai081c56178b3c
diff --git a/src/shacl.rs b/src/shacl.rsindex 7bea1b09..f703500f 100644--- a/src/shacl.rs+++ b/src/shacl.rs@@ -2730,8 +2730,25 @@ fn prebinding_violation(query: &str, var: &str) -> Option<String> {Some(w) => &upper[at..at + w],None => &upper[at..],};- if !head.contains('*') && !marks.iter().any(|m| head.contains(&m.to_ascii_uppercase())) {- return Some("a sub-SELECT that does not project the pre-bound variable".to_string());+ // `SELECT *` used to be accepted here and must not be. A+ // subquery's `*` projects the variables that subquery BINDS, and+ // the pre-bound variable is bound outside it, so the substitution+ // does not reach inward and the inner reference is simply unbound.+ // The W3C suite settles it: `sparql/pre-binding/pre-binding-006`+ // is exactly `{ SELECT * WHERE { FILTER ($this = ...) } }` and its+ // expected result is `sht:Failure`, a refusal rather than a+ // verdict.+ //+ // Accepting it produced a FALSE CLEAN rather than a wrong count.+ // The query found no solution because `$this` was unbound, the run+ // reported `conforms: true` with an empty `skipped_constraints`,+ // and nothing in the report said a constraint had gone+ // unevaluated (#192).+ if !marks.iter().any(|m| head.contains(&m.to_ascii_uppercase())) {+ return Some(+ "a sub-SELECT that does not project the pre-bound variable, and `SELECT *` does not project it because a subquery's `*` is what that subquery binds"+ .to_string(),+ );}from = at + 6;}diff --git a/tests/shacl_subquery_prebinding_test.rs b/tests/shacl_subquery_prebinding_test.rsnew file mode 100644index 00000000..267ffe1f--- /dev/null+++ b/tests/shacl_subquery_prebinding_test.rs@@ -0,0 +1,114 @@+//! **Issue #192.** A false clean: `$this` inside a sub-SELECT.+//!+//! `prebinding_violation` already refused `MINUS`, `SERVICE`, a `VALUES` over+//! the pre-bound variable, and a sub-SELECT that does not project it. But it+//! EXEMPTED `SELECT *`, and that exemption is wrong.+//!+//! A subquery's `*` projects the variables that subquery BINDS. The pre-bound+//! variable is bound outside it, so the substitution does not reach inward and+//! the inner reference is simply unbound. The query then finds no solution,+//! and a SPARQL constraint with no solution is a constraint that HELD.+//!+//! So the run reported `conforms: true` with an empty `skipped_constraints`,+//! about a constraint it had never evaluated. That is a false clean, and the+//! quiet kind: nothing in the report said anything had gone unchecked.+//!+//! The W3C suite settles the question rather than this file deciding it.+//! `sparql/pre-binding/pre-binding-006` is exactly this query shape and its+//! expected result is `sht:Failure`, a refusal rather than a verdict.+//! pySHACL 0.40.1 raises `ValidationFailure` on it, which is the same answer.++use open_ontologies::graph::GraphStore;+use open_ontologies::shacl::ShaclValidator;+use std::sync::Arc;++const DATA: &str = r#"+ @prefix ex: <http://example.org/> .+ ex:InvalidResource a ex:Thing .+ ex:ValidResource a ex:Thing .+"#;++/// `$this` reached only from inside a nested SELECT that projects `*`.+const SUBQUERY: &str = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:SubqueryShape a sh:NodeShape ;+ sh:targetNode ex:InvalidResource , ex:ValidResource ;+ sh:sparql [+ sh:select """+ SELECT $this+ WHERE {+ { SELECT * WHERE { FILTER ($this = <http://example.org/InvalidResource>) } }+ }"""+ ] .+"#;++/// The same constraint with the FILTER at the top level. This is W3C+/// `pre-binding-001`, whose expected result is one violation on+/// `ex:InvalidResource`, and it is the control: without it, a fix that refused+/// every SPARQL constraint would pass the test above and look correct.+const PLAIN: &str = r#"+ @prefix sh: <http://www.w3.org/ns/shacl#> .+ @prefix ex: <http://example.org/> .+ ex:PlainShape a sh:NodeShape ;+ sh:targetNode ex:InvalidResource , ex:ValidResource ;+ sh:sparql [+ sh:select """+ SELECT $this+ WHERE { FILTER ($this = <http://example.org/InvalidResource>) }"""+ ] .+"#;++fn report(shapes: &str) -> serde_json::Value {+ let graph = Arc::new(GraphStore::new());+ graph.load_turtle(DATA, None).expect("data must parse");+ let json = ShaclValidator::validate(&graph, shapes).expect("validate");+ serde_json::from_str(&json).expect("report is JSON")+}++#[test]+fn a_subquery_that_cannot_receive_the_binding_is_refused_and_not_called_conforming() {+ let r = report(SUBQUERY);+ assert!(+ r["conforms"].is_null(),+ "the validator cannot evaluate this constraint, so it must withhold the verdict. \+ Reporting true states that a constraint held when it was never checked, which is the \+ one failure mode a validator used as an assurance gate must not have: {r}"+ );+ let skipped = r["skipped_constraints"].as_array().expect("skipped_constraints");+ // One entry per constraint-evaluation SITE, so a shape with two target+ // nodes produces two. That granularity is documented on `onto_shacl`.+ assert_eq!(+ skipped.len(),+ 2,+ "the refusal must be VISIBLE, once per evaluation site. A null verdict with nothing in \+ skipped_constraints leaves a reader unable to tell a refusal from a clean run: {r}"+ );+ for entry in skipped {+ assert!(+ entry["reason"].as_str().is_some_and(|s| s.contains("sub-SELECT")),+ "the reason must name the construct, so the author can fix the query: {entry}"+ );+ }+}++#[test]+fn the_same_constraint_at_the_top_level_is_still_evaluated() {+ let r = report(PLAIN);+ assert_eq!(+ r["conforms"],+ serde_json::json!(false),+ "pre-binding works at the top level and must keep working; a fix that refused every \+ SPARQL constraint would satisfy the test above and be worthless: {r}"+ );+ assert_eq!(r["violation_count"], serde_json::json!(1), "exactly ex:InvalidResource: {r}");+ assert_eq!(+ r["violations"][0]["focus_node"],+ serde_json::json!("http://example.org/InvalidResource"),+ "and the violation is on the node the FILTER selects: {r}"+ );+ // The key is omitted entirely when nothing was skipped, so absent and+ // empty both mean the same thing and either is acceptable.+ let skipped = r["skipped_constraints"].as_array().map(|s| s.len()).unwrap_or(0);+ assert_eq!(skipped, 0, "nothing should be skipped here: {r}");+}