Skip to content

fix(interpreter): allow rule-less packages over non-object data - #974

Open
snehabandla wants to merge 1 commit into
microsoft:mainfrom
snehabandla:fix/interpreter-package-over-non-object-data
Open

snehabandla wants to merge 1 commit into
microsoft:mainfrom
snehabandla:fix/interpreter-package-over-non-object-data

Conversation

@snehabandla

Copy link
Copy Markdown
Collaborator

Summary

Fix the interpreter failing with internal error: make: not an object when 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 only package a.b, any read of data.a or data.a.b failed, because the interpreter tried to create the package object inside the string. The same happened with {"a": {"b": "str"}} and package a.b.c.

OPA 1.2.0 returns:

data package data.a data.a.b data.a.b.c
{"a":"str"} a.b "str" {} undefined
{"a":{"b":"str"}} a.b.c {"b":"str"} "str" {}
  • Interpreter::ensure_package_value skips creating the package object for a package without rules when data holds a non-object value at or above its path. Lazy module evaluation and Engine::eval_modules (used for the query data and by eval_query_and_all_rules) both use it. Engine::eval_modules no longer creates rule-less packages before evaluating rules (they are still created afterwards), so package a with b := "str" next to an empty package a.b.c no longer fails with complete rules should not produce multiple outputs there.
  • Data reads (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.c with package a / b := input.x and an empty package a.b.c). A read under a with override at or above the package (e.g. with data.a as "x") stays undefined, as in OPA.
  • Packages with rules or functions over non-object base data still fail (OPA rejects them at compile time with 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 for Engine::eval_rule, which does not otherwise reach the conflict).
  • The RVM already matches OPA here with fix(rvm): match OPA for undefined rules and subpackages in packages #967 (open).

Known limitations

These cases used to fail with the internal error and now return a result that still differs from OPA:

  • Iterating the non-object: with data {"a":"str"} and empty packages a.b.c and a.b.d, some k; data.a[k] binds nothing and count([1 | data.a[_]]) is 0 (OPA: k = "b", 1).
  • Two dynamic indexes from the data root: data[k1][k2] with k1 = "a", k2 = "b" is undefined (OPA: {"c":{},"d":{}}). data[k1].b and data.a[k] work.
  • A rule-less package listed before a module whose rule produces a non-object above it can still fail with complete rules should not produce multiple outputs in eval_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

  • New 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, query data, with overrides 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 with internal error: make: not an object.
  • New tests/interpreter/cases/data/empty_packages.yaml: rule-less packages over ordinary object data (query data, 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 in Engine::eval_modules.
  • New unit test test_eval_rule_package_without_rules_beside_rules_below_base_string: Engine::eval_rule reports 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 with rego-extensions), ACI and Kata suites, and the OPA conformance suite (2869 passed, 0 failed, 6 skipped).

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]>

Copilot AI 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.

🟡 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
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 thread src/interpreter.rs
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 thread src/interpreter.rs
Comment on lines +3283 to +3285
!module.policy.is_empty(),
)? {
continue;
Comment thread src/interpreter.rs

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),
Comment thread src/interpreter.rs
if !Self::has_non_object_on_path(&self.init_data, fields) {
return Ok(value);
}
for module in self.compiled_policy.modules.iter() {
Comment thread src/engine.rs
Comment on lines +1563 to +1567
Interpreter::ensure_package_value(
self.interpreter.get_data_mut(),
&path,
!m.policy.is_empty(),
)?;
Comment thread src/interpreter.rs
.map(|component| (*component).to_string()),
);
self.ensure_module_evaluated(&module_path)?;
data_path = Some(module_path);
Comment thread src/interpreter.rs
v = obj[index].clone();
}
return Ok(Self::get_value_chained(v, &path[..]));
let v = Self::get_value_chained(v, &path[..]);
Comment thread src/interpreter.rs
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 thread src/interpreter.rs
Comment on lines +3465 to +3466
if !module.policy.is_empty()
&& Self::has_non_object_on_path(&self.init_data, &package)
Comment thread src/interpreter.rs
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)?;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants