Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast: Disallowing partial object rules to have other partial object rule within their immediate extent #5864

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ast/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,9 @@ func (c *Compiler) checkRuleConflicts() {
//
// data.p.q[r] { r := input.r } # data.p.q could be { "r": true }
// data.p.q.r.s { true }
//
// data.p[r] := x { r = input.key; x = input.bar }
// data.p.q[r] := x { r = input.key; x = input.bar }

// But this is allowed:
// data.p.q[r] = 1 { r := "r" }
Expand All @@ -899,6 +902,16 @@ func (c *Compiler) checkRuleConflicts() {
if r.Head.RuleKind() == SingleValue && len(node.Children) > 0 {
if len(ref) > 1 && !ref[len(ref)-1].IsGround() { // p.q[x] and p.q.s.t => check grandchildren
for _, c := range node.Children {
if len(c.Values) > 0 {
childRules := extractRules(c.Values)
for _, childRule := range childRules {
childRef := childRule.Ref()
if childRule.Head.RuleKind() == SingleValue && !childRef[len(childRef)-1].IsGround() {
// The child is a partial object rule, so it's effectively "generating" grandchildren.
singleValueConflicts = append(singleValueConflicts, childRule.Ref())
}
}
}
if len(c.Children) > 0 {
singleValueConflicts = node.flattenChildren()
Copy link
Contributor

Choose a reason for hiding this comment

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

Just from looking at the code, it looks like singleValueConflicts could be set in line 912, and end up being overwritten here. But I guess the two branches are mutually exclusive?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think they are 🤔 .
But you have a good point. I'll make this an append to be on the safe side. Don't think its worthwhile looking for duplicates, though.

break
Expand Down
36 changes: 36 additions & 0 deletions ast/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,42 @@ func TestCompilerCheckRuleConflictsDotsInRuleHeads(t *testing.T) {
`),
err: "rego_type_error: single-value rule data.pkg.p.q[r] conflicts with [data.pkg.p.q.r.s]",
},
{
note: "single-value partial object with other partial object rule overlap, unknown keys (regression test for #5855)",
modules: modules(
`package pkg
p[r] := x { r = input.key; x = input.bar }
p.q[r] := x { r = input.key; x = input.bar }
`),
err: "rego_type_error: single-value rule data.pkg.p[r] conflicts with [data.pkg.p.q[r]]",
},
{
note: "single-value partial object with other partial object (implicit 'true' value) rule overlap, unknown keys",
modules: modules(
`package pkg
p[r] := x { r = input.key; x = input.bar }
p.q[r] { r = input.key }
`),
err: "rego_type_error: single-value rule data.pkg.p[r] conflicts with [data.pkg.p.q[r]]",
},
{
note: "single-value partial object with multi-value rule (ref head) overlap, unknown key",
modules: modules(
`package pkg
import future.keywords
p[r] := x { r = input.key; x = input.bar }
p.q contains r { r = input.key }
`),
srenatus marked this conversation as resolved.
Show resolved Hide resolved
},
{
note: "single-value partial object with multi-value rule overlap, unknown key",
modules: modules(
`package pkg
p[r] := x { r = input.key; x = input.bar }
p.q { true }
`),
err: "rego_type_error: conflicting rules data.pkg.p found",
},
{
note: "single-value rule with known and unknown key",
modules: modules(
Expand Down