Skip to content

Commit 1989c6c

Browse files
authored
fix: Only raise problematic if error when rule has no name set (#935)
If the rule has a name set, that means there isn't problematic use of the 'if' keyword without 'contains' so we can skip those. Signed-off-by: James Alseth <james@jalseth.me>
1 parent 6609893 commit 1989c6c

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

policy/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ func problematicIf(modules map[string]*ast.Module) error {
538538
// https://github.com/open-policy-agent/opa/issues/6509
539539
for _, module := range modules {
540540
for _, rule := range module.Rules {
541-
if rule.Head == nil || rule.Head.Value == nil || len(rule.Head.Reference) == 0 {
541+
if rule.Head == nil || rule.Head.Name != "" || rule.Head.Value == nil || len(rule.Head.Reference) == 0 {
542542
continue
543543
}
544544
refName := rule.Head.Reference[0].Value.String()

policy/engine_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ func TestProblematicIf(t *testing.T) {
326326
desc: "No rules",
327327
body: "",
328328
},
329+
{
330+
desc: "Bare deny",
331+
body: "deny { true }\n",
332+
},
329333
{
330334
desc: "Rule not using if statement",
331335
body: "deny[msg] {\n 1 == 1\nmsg := \"foo\"\n}\n",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
deny {
4+
true
5+
}

tests/problematic-if/test.bats

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
[[ "$output" =~ "1 test, 0 passed, 0 warnings, 1 failure, 0 exceptions" ]]
99
}
1010

11+
@test "Bare deny rule can be used without contains or if" {
12+
run $CONFTEST test --policy=policy/valid_bare_deny.rego data.yaml
13+
14+
[ "$status" -eq 0 ]
15+
echo $output
16+
[[ "$output" =~ "1 test, 1 passed, 0 warnings, 0 failures, 0 exceptions" ]]
17+
}
18+
1119
@test "Error is raised when if is used without contains" {
1220
run $CONFTEST test --policy=policy/invalid.rego data.yaml
1321

0 commit comments

Comments
 (0)