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

Extend redundant-existence-check to fail redundant ref checks #949

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 20 additions & 2 deletions bundle/regal/rules/bugs/redundant_existence_check.rego
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import rego.v1
import data.regal.ast
import data.regal.result

# METADATA
# description: check rule bodies for redundant existence checks
report contains violation if {
some rule_index, rule in input.rules
some expr_index, expr in ast.exprs[rule_index]
Expand All @@ -18,12 +20,28 @@ report contains violation if {
ast.static_ref(expr.terms)

ref_str := ast.ref_to_string(expr.terms.value)

next_expr := rule.body[expr_index + 1]

some term in next_expr.terms

ast.ref_to_string(term.value) == ref_str

violation := result.fail(rego.metadata.chain(), result.location(expr))
violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(expr))
}

# METADATA
# description: check for redundant existence checks in rule head assignment
report contains violation if {
some rule_index, rule in input.rules

rule.head.value.type == "ref"

ref_str := ast.ref_to_string(rule.head.value.value)

some expr in ast.exprs[rule_index]

expr.terms.type == "ref"
ast.ref_to_string(expr.terms.value) == ref_str

violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(expr.terms))
}
21 changes: 20 additions & 1 deletion bundle/regal/rules/bugs/redundant_existence_check_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test_fail_redundant_existence_check if {
"category": "bugs",
"description": "Redundant existence check",
"level": "error",
"location": {"col": 3, "file": "policy.rego", "row": 7, "text": "\t\tinput.foo"},
"location": {"col": 3, "file": "policy.rego", "row": 7, "text": "\t\tinput.foo", "end": {"col": 12, "row": 7}},
"related_resources": [{
"description": "documentation",
"ref": config.docs.resolve_url("$baseUrl/$category/redundant-existence-check", "bugs"),
Expand Down Expand Up @@ -47,3 +47,22 @@ test_success_not_redundant_existence_check_with_cancels if {
r := rule.report with input as module
r == set()
}

test_fail_redundant_existence_check_head_assignment_of_ref if {
module := ast.with_rego_v1(`
redundant := input.foo if {
input.foo
}`)
r := rule.report with input as module
r == {{
"category": "bugs",
"description": "Redundant existence check",
"level": "error",
"location": {"col": 3, "file": "policy.rego", "row": 7, "text": "\t\tinput.foo", "end": {"col": 12, "row": 7}},
"related_resources": [{
"description": "documentation",
"ref": config.docs.resolve_url("$baseUrl/$category/redundant-existence-check", "bugs"),
}],
"title": "redundant-existence-check",
}}
}