Repository navigation
fix(interpreter): allow rule-less packages over non-object data - #974
Open
snehabandla wants to merge 1 commit into
Open
snehabandla wants to merge 1 commit into
snehabandla wants to merge 1 commit into
Conversation
A package without rules whose path crosses a non-object value in base
data (for example data {"a": "str"} with `package a.b`) made every read
of data.a or data.a.b fail with `internal error: make: not an object`,
because the interpreter tried to create the package object inside the
base data value.
OPA treats such a package as non-conflicting: reads at or above the
non-object value return the base data, and reads below it still see
the empty package (data.a.b is {} in the example above).
- Skip creating the package object for rule-less packages when data
holds a non-object value at or above the package path, both in lazy
module evaluation and in Engine::eval_modules. Engine::eval_modules
no longer creates rule-less packages before evaluating rules, so a
rule that produces a non-object above such a package is not
reported as producing multiple outputs.
- When a data read below a non-object value in base data is undefined,
return the empty objects of the skipped packages, merged, including
reads through a variable index. Under a `with` override at or above
the package the read stays undefined. Non-object values produced by
rules are not treated this way: reads below them stay undefined.
- Packages with rules or functions over non-object base data still
fail; OPA rejects them at compile time. Reading a rule-less package
that would otherwise be returned as an empty object fails too when
such a package exists.
Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved package-synthesis and lookup defects can produce incorrect authorization results, while unchecked module scans can bypass configured memory and execution-time limits.
10 open issues
Preserve evaluated data paths across successive dynamic indexes · New Expose virtual package children to iteration or report an error · New Resolve imported data prefixes with remaining reference components · New Check resource limits during package scans and after each insertion · New Expose empty packages to root reads during rule evaluation · New Avoid synthesizing package fallback when a local data binding shadows root data · New Disable package fallback after a numeric index selects real array data · New Evaluate replacement expressions against the original override state · New Check rule-head ancestor paths for package conflicts · New Reject non-objects at the final package path for function-only packages · New
What changed in this PR
This PR brings interpreter package lookup closer to OPA when rule-less packages overlap non-object base data. Base values remain intact, while supported reads below them can expose synthesized empty packages.
Changes:
- Skip blocked package creation and synthesize package objects for supported data reads.
- Defer package creation during full-module evaluation and check conflicts before returning synthesized packages.
- Add regression coverage and document the interpreter fix.
| File | Description |
|---|---|
tests/interpreter/cases/data/package_over_non_object.yaml |
Covers non-object base data, merged packages, overrides, and conflicts. |
tests/interpreter/cases/data/empty_packages.yaml |
Checks existing empty-package behavior over object and missing data. |
src/tests/interpreter/mod.rs |
Checks that eval_rule rejects conflicting sibling rules beneath scalar data. |
src/interpreter.rs |
Adds package materialization checks and synthesized-package lookup. |
src/engine.rs |
Defers package creation until after rule evaluation. |
CHANGELOG.md |
Documents the interpreter behavior change. |
Review effort: Max
Comment on lines
+807
to
+810
| if let (Value::Undefined, Some(data_path)) = (&v, data_path) { | ||
| return self.package_value_below_non_object( | ||
| data_path.get(1..).unwrap_or_default(), | ||
| ); |
Comment on lines
+3283
to
+3285
| !module.policy.is_empty(), | ||
| )? { | ||
| continue; |
|
|
||
| fn lookup_data(&self, fields: &[&str]) -> Result<Value> { | ||
| match Self::get_value_chained(self.data.clone(), fields) { | ||
| Value::Undefined => self.package_value_below_non_object(fields), |
| if !Self::has_non_object_on_path(&self.init_data, fields) { | ||
| return Ok(value); | ||
| } | ||
| for module in self.compiled_policy.modules.iter() { |
Comment on lines
+1563
to
+1567
| Interpreter::ensure_package_value( | ||
| self.interpreter.get_data_mut(), | ||
| &path, | ||
| !m.policy.is_empty(), | ||
| )?; |
| .map(|component| (*component).to_string()), | ||
| ); | ||
| self.ensure_module_evaluated(&module_path)?; | ||
| data_path = Some(module_path); |
| v = obj[index].clone(); | ||
| } | ||
| return Ok(Self::get_value_chained(v, &path[..])); | ||
| let v = Self::get_value_chained(v, &path[..]); |
| let package: Vec<&str> = package.iter().map(String::as_str).collect(); | ||
| if package.len() >= fields.len() | ||
| && package.iter().zip(fields).all(|(p, f)| *p == f.as_ref()) | ||
| && !self.has_explicit_data_with_override_ancestor(&package)? |
Comment on lines
+3465
to
+3466
| if !module.policy.is_empty() | ||
| && Self::has_non_object_on_path(&self.init_data, &package) |
Comment on lines
+3940
to
+3943
| if !has_rules && Self::has_non_object_on_path(data, path) { | ||
| return Ok(false); | ||
| } | ||
| let vref = Self::make_or_get_value_mut(data, path)?; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Fix the interpreter failing with
internal error: make: not an objectwhen base data has a non-object value at or above the path of a package without rules. As in OPA, the base data value wins, and the empty package is still visible when read below that value.Details
With data
{"a": "str"}and a module containing onlypackage a.b, any read ofdata.aordata.a.bfailed, because the interpreter tried to create the package object inside the string. The same happened with{"a": {"b": "str"}}andpackage a.b.c.OPA 1.2.0 returns:
data.adata.a.bdata.a.b.c{"a":"str"}a.b"str"{}{"a":{"b":"str"}}a.b.c{"b":"str"}"str"{}Interpreter::ensure_package_valueskips creating the package object for a package without rules when data holds a non-object value at or above its path. Lazy module evaluation andEngine::eval_modules(used for the querydataand byeval_query_and_all_rules) both use it.Engine::eval_modulesno longer creates rule-less packages before evaluating rules (they are still created afterwards), sopackage awithb := "str"next to an emptypackage a.b.cno longer fails withcomplete rules should not produce multiple outputsthere.data.a.b,data.a["b"],data.a[x]) that are undefined and cross a non-object value in base data return the empty objects of the skipped packages, merged when there are several. Reads below a non-object produced by a rule stay undefined, as in OPA (data.a.b.cwithpackage a/b := input.xand an emptypackage a.b.c). A read under awithoverride at or above the package (e.g.with data.a as "x") stays undefined, as in OPA.conflicting rule for data path). If such a package exists, a read that would return a synthesized empty package fails as well instead of returning{}(this matters forEngine::eval_rule, which does not otherwise reach the conflict).Known limitations
These cases used to fail with the internal error and now return a result that still differs from OPA:
{"a":"str"}and empty packagesa.b.canda.b.d,some k; data.a[k]binds nothing andcount([1 | data.a[_]])is 0 (OPA:k = "b", 1).dataroot:data[k1][k2]withk1 = "a",k2 = "b"is undefined (OPA:{"c":{},"d":{}}).data[k1].banddata.a[k]work.complete rules should not produce multiple outputsineval_query, depending on module order.Compatibility
Policies that failed with the internal error now return the OPA result, except for the limitations above. Behaviour for object or missing base data is unchanged.
Testing
tests/interpreter/cases/data/package_over_non_object.yaml: rule-less packages over strings and arrays at several depths, merged packages, variable index reads, enumeration of a synthesized package, querydata,withoverrides of the path and of an unrelated path, non-object values produced by a complete rule and by a partial set rule, and non-match cases (object and missing base data, packages with rules or functions). Expected values were checked with OPA 1.2.0. The base data cases fail without the fix withinternal error: make: not an object.tests/interpreter/cases/data/empty_packages.yaml: rule-less packages over ordinary object data (querydata, direct reads, nesting under a package with rules, shadowing by base data, reads from another package, module order). Expected values were checked with OPA 1.2.0; these cases behave the same before and after the change, which removes the creation of rule-less packages before rules run inEngine::eval_modules.test_eval_rule_package_without_rules_beside_rules_below_base_string:Engine::eval_rulereports an error instead of{}when a package with rules sits below the same non-object value.cargo fmt --check;cargo clippy --all-targets --all-features -- -D warnings;cargo clippy --lib --no-default-features -- -D warnings;cargo test --all-features --lib(1041 passed, 2 ignored);cargo test --all-features --test mod(419 passed).cargo xtask pre-push: workspace and binding fmt and clippy, doc tests, no_std build,cargo test(default features and withrego-extensions), ACI and Kata suites, and the OPA conformance suite (2869 passed, 0 failed, 6 skipped).