|
| 1 | +package regal.rules.bugs["invalid-regexp_test"] |
| 2 | + |
| 3 | +import data.regal.ast |
| 4 | +import data.regal.capabilities |
| 5 | +import data.regal.config |
| 6 | +import data.regal.util |
| 7 | + |
| 8 | +import data.regal.rules.bugs["invalid-regexp"] as rule |
| 9 | + |
| 10 | +test_fail_invalid_regexp if { |
| 11 | + r := rule.report with input as ast.policy("r := regex.match(`(`, input.string)") |
| 12 | + with config.capabilities as capabilities.provided |
| 13 | + |
| 14 | + r == {{ |
| 15 | + "category": "bugs", |
| 16 | + "description": "Invalid regular expression", |
| 17 | + "level": "error", |
| 18 | + "location": { |
| 19 | + "col": 18, |
| 20 | + "end": { |
| 21 | + "col": 21, |
| 22 | + "row": 3, |
| 23 | + }, |
| 24 | + "file": "policy.rego", |
| 25 | + "row": 3, |
| 26 | + "text": "r := regex.match(`(`, input.string)", |
| 27 | + }, |
| 28 | + "related_resources": [{ |
| 29 | + "description": "documentation", |
| 30 | + "ref": "https://www.openpolicyagent.org/projects/regal/rules/bugs/invalid-regexp", |
| 31 | + }], |
| 32 | + "title": "invalid-regexp", |
| 33 | + }} |
| 34 | +} |
| 35 | + |
| 36 | +test_fail_invalid_regexp_for_pattern_function[call] if { |
| 37 | + some call in [ |
| 38 | + "regex.find_all_string_submatch_n(`(`, input.x, 1)", |
| 39 | + "regex.find_n(`(`, input.x, 1)", |
| 40 | + "regex.globs_match(`(`, `[abc]`)", |
| 41 | + "regex.globs_match(`[abc]`, `(`)", |
| 42 | + "regex.is_valid(`(`)", |
| 43 | + "regex.match(`(`, input.x)", |
| 44 | + "regex.replace(input.x, `(`, `y`)", |
| 45 | + "regex.split(`(`, input.x)", |
| 46 | + "regex.template_match(`(`, input.x, `<`, `>`)", |
| 47 | + ] |
| 48 | + |
| 49 | + r := rule.report with input as ast.policy($"r := {call}") |
| 50 | + with config.capabilities as capabilities.provided |
| 51 | + |
| 52 | + util.single_set_item(r).location.text == $"r := {call}" |
| 53 | +} |
| 54 | + |
| 55 | +test_pass_valid_regexp if { |
| 56 | + r := rule.report with input as ast.policy("r := regex.match(`[abc]`, input.string)") |
| 57 | + with config.capabilities as capabilities.provided |
| 58 | + |
| 59 | + r == set() |
| 60 | +} |
0 commit comments