| 126 | # =========================================================================== |
| 127 | |
| 128 | class TestCompileRules: |
| 129 | def test_re_compile_not_flagged_py515(self): |
| 130 | """re.compile() is regex, not Python code execution — no PY515.""" |
| 131 | code = """ |
| 132 | import re |
| 133 | tag_re = re.compile(r'({%.*?%}|{{.*?}}|{#.*?#})') |
| 134 | hidden_settings = re.compile('API|AUTH|TOKEN|KEY|SECRET', flags=re.I) |
| 135 | """ |
| 136 | assert findings_for_rule(code, "PY515") == [], \ |
| 137 | "PY515 should not fire for re.compile()" |
| 138 | |
| 139 | def test_re_compile_not_flagged_shell645(self): |
| 140 | """re.compile() must not trigger SHELL645.""" |
| 141 | code = """ |
| 142 | import re |
| 143 | pattern = re.compile(r'[a-z]+') |
| 144 | """ |
| 145 | assert findings_for_rule(code, "SHELL645") == [], \ |
| 146 | "SHELL645 should not fire for re.compile()" |
| 147 | |
| 148 | def test_re_compile_not_flagged_shell670(self): |
| 149 | """re.compile() must not trigger SHELL670.""" |
| 150 | code = """ |
| 151 | import re |
| 152 | validator_re = re.compile(r'^[A-Z_]+$') |
| 153 | """ |
| 154 | assert findings_for_rule(code, "SHELL670") == [], \ |
| 155 | "SHELL670 should not fire for re.compile()" |
| 156 | |
| 157 | # True positives |
| 158 | def test_bare_compile_or_exec_flagged(self): |
| 159 | """exec(compile(user_code, ...)) IS dangerous — PY305 (exec) or compile rules must fire.""" |
| 160 | code = "user_code = get_input()\nexec(compile(user_code, '<string>', 'exec'))\n" |
| 161 | findings = run_pyspector(code, filename="runner.py") |
| 162 | # PY305 (exec), PY515/SHELL645/SHELL670 (compile), SEC501 — any confirms danger |
| 163 | danger_rules = {"PY515", "SHELL645", "SHELL670", "PY305", "SEC501"} |
| 164 | triggered = {f["rule_id"] for f in findings} & danger_rules |
| 165 | assert triggered, \ |
| 166 | f"At least one danger rule should fire for exec(compile(user_code)), got: {findings}" |
| 167 | |
| 168 | |
| 169 | # =========================================================================== |
nothing calls this directly
no outgoing calls
no test coverage detected