diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-14 16:28:34 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-10-19 16:29:26 +0000 |
| commit | 6b5e21a6650efbdf528e970dfd567f0cb492e9b3 (patch) | |
| tree | b78d0cb9bbcfb342d0ca951796ecc40306e50731 /plugins/security-guidance/tests/test_pattern_false_positives.py | |
| parent | ea10b213686c90bb1984b2016937310e05336cf3 (diff) | |
security-guidance: stop pattern warnings on benign code and prosemhegazy/sg-pattern-false-positives
Eight rules matched their trigger without its surrounding syntax, or fired
in prose files: Playwright's page.$eval, identifiers starting with
Function, innerHTML/outerHTML comparisons, ET.parse in JS, and TLS,
subprocess, XML and script-tag examples in Markdown, text and reST files.
Each rule still matches its real target.
Diffstat (limited to 'plugins/security-guidance/tests/test_pattern_false_positives.py')
| -rw-r--r-- | plugins/security-guidance/tests/test_pattern_false_positives.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/plugins/security-guidance/tests/test_pattern_false_positives.py b/plugins/security-guidance/tests/test_pattern_false_positives.py new file mode 100644 index 00000000..d4d3091f --- /dev/null +++ b/plugins/security-guidance/tests/test_pattern_false_positives.py @@ -0,0 +1,63 @@ +import pytest + +import security_reminder_hook as hook + + +def fired(path, content): + return {rule for rule, _ in hook.check_patterns(path, content)} + + +QUIET = [ + ("t.js", "await page.$$eval('.btn', ns => ns.length)\n"), + ("t.js", "await page.$eval('#id', e => e.tagName)\n"), + ("t.js", "const r = new FunctionRegistry()\n"), + ("t.tsx", "const c = new FunctionComponent()\n"), + ("t.js", "if (el.innerHTML === expected) { pass() }\n"), + ("t.js", "if (el.innerHTML == expected) { pass() }\n"), + ("t.js", "expect(el.outerHTML === golden).toBe(true)\n"), + ("doc.md", "Never pass verify=False to requests.\n"), + ("README.md", 'Add `<script src="https://cdn.x/l.js"></script>` to your page.\n'), + ("t.js", "const doc = ET.parse(payload)\n"), + ("guide.md", "```python\nsubprocess.run(cmd, shell=True)\n```\n"), + ("notes.rst", "tree = ET.parse(path)\n"), + ("README.MD", "Never pass verify=False to requests.\n"), + ("guide.markdown", "Never pass verify=False to requests.\n"), +] + +CAUGHT = [ + ("c.js", "const v = eval(userInput)\n", "eval_injection"), + ("c.py", "v = eval(user_input)\n", "eval_injection"), + ("c.js", "const f = new Function('a', body)\n", "new_function_injection"), + ("c.js", "const f = new Function ('a', body)\n", "new_function_injection"), + ("c.js", "const f = new\n Function(body)\n", "new_function_injection"), + ("c.ts", "const f = new Function<any>(body)\n", "new_function_injection"), + ("c.js", "new Function", "new_function_injection"), + ("c.js", "$scope.$eval(attrs.expr)\n", "eval_injection"), + ("c.js", "scope.$eval(userInput)\n", "eval_injection"), + ("page.mdx", '<script src="https://cdn.x/l.js"></script>\n', "script_src_without_sri"), + ("page.mdx", "export const a = new https.Agent({ rejectUnauthorized: false })\n", "tls_verification_disabled"), + ("c.js", "el.innerHTML = html\n", "innerHTML_xss"), + ("c.js", "el.innerHTML=html\n", "innerHTML_xss"), + ("c.js", "el.outerHTML = html\n", "outerHTML_xss"), + ("c.js", "el.outerHTML=html\n", "outerHTML_xss"), + ("c.py", "requests.get(url, verify=False)\n", "tls_verification_disabled"), + ("c.go", "tr := &tls.Config{InsecureSkipVerify: true}\n", "tls_verification_disabled"), + ("compose.yaml", "environment:\n - NODE_TLS_REJECT_UNAUTHORIZED=0\n", "tls_verification_disabled"), + ("agent.yml", "rejectUnauthorized: false\n", "tls_verification_disabled"), + ("index.html", '<script src="https://cdn.x/l.js"></script>\n', "script_src_without_sri"), + ("c.py", "tree = ET.parse(path)\n", "xml_unsafe_parse"), + ("tool", "d = minidom.parseString(s)\n", "xml_unsafe_parse"), + ("c.py", "subprocess.run(cmd, shell=True)\n", "python_subprocess_shell"), + ("tool", "subprocess.run(cmd, shell=True)\n", "python_subprocess_shell"), + ("ci.yml", "run: python -c 'subprocess.run(cmd, shell=True)'\n", "python_subprocess_shell"), +] + + +@pytest.mark.parametrize("path,content", QUIET) +def test_benign_code_gets_no_warning(path, content): + assert fired(path, content) == set() + + +@pytest.mark.parametrize("path,content,rule", CAUGHT) +def test_real_use_still_warns(path, content, rule): + assert rule in fired(path, content) |
