Skip to content
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
110 changes: 70 additions & 40 deletions .claude/skills/deep-code-review/templates/semgrep-taint/py.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,46 +316,31 @@ rules:
- pattern: httpx.$M($S, ...)
- pattern: urllib.request.urlopen($S, ...)
- pattern: urlopen($S, ...)
- pattern-not-inside: |
if $G not in $A:
raise ...
...
<... requests.$M(<... $G ...>, ...) ...>
- pattern-not-inside: |
if $G not in $A:
raise ...
...
<... httpx.$M(<... $G ...>, ...) ...>
- pattern-not-inside: |
if $G not in $A:
raise ...
...
<... urllib.request.urlopen(<... $G ...>, ...) ...>
- pattern-not-inside: |
if $G not in $A:
raise ...
...
<... urlopen(<... $G ...>, ...) ...>
- pattern-not-inside: |
if $G not in $A:
return ...
...
<... requests.$M(<... $G ...>, ...) ...>
- pattern-not-inside: |
if $G not in $A:
return ...
...
<... httpx.$M(<... $G ...>, ...) ...>
- pattern-not-inside: |
if $G not in $A:
return ...
...
<... urllib.request.urlopen(<... $G ...>, ...) ...>
- pattern-not-inside: |
if $G not in $A:
return ...
...
<... urlopen(<... $G ...>, ...) ...>
# Allowlist guard: only when $G is the URL argument itself (not inside an f-string or concat) and the container is not a string literal (substring test).
- pattern-not-inside:
patterns:
- pattern-either:
- pattern: |
if $G not in $A:
raise ...
...
<... $F($G, ...) ...>
- pattern: |
if $G not in $A:
return ...
...
<... $F($G, ...) ...>
- pattern: |
if $G not in $A:
abort(...)
...
<... $F($G, ...) ...>
- metavariable-regex:
metavariable: $A
regex: ^(?![rbfRBF]{0,2}["'])
- metavariable-regex:
metavariable: $F
regex: ^(?:requests\.\w+|httpx\.\w+|urllib\.request\.urlopen|urlopen)$
- focus-metavariable: $S
- id: perun.py.deserialize
mode: taint
Expand Down Expand Up @@ -444,7 +429,52 @@ rules:
- pattern-either:
- pattern: redirect($S, ...)
- pattern: HttpResponseRedirect($S, ...)
# Relative-path guard: reassigning to a constant when the value is not a single-slash path.
- pattern-not-inside: |
if not $G.startswith("/") or $G.startswith("//") or $G.startswith("/\\"):
$G = ...
...
<... redirect(<... $G ...>, ...) ...>
- pattern-not-inside: |
if not $G.startswith("/") or $G.startswith("//") or $G.startswith("/\\"):
$G = ...
...
<... HttpResponseRedirect(<... $G ...>, ...) ...>
- pattern-not-inside: |
if not url_has_allowed_host_and_scheme($G, ...):
$G = ...
...
<... redirect(<... $G ...>, ...) ...>
- pattern-not-inside: |
if not url_has_allowed_host_and_scheme($G, ...):
$G = ...
...
<... HttpResponseRedirect(<... $G ...>, ...) ...>
- pattern-not-inside: |
if not url_has_allowed_host_and_scheme($G, ...):
return ...
...
<... redirect(<... $G ...>, ...) ...>
- pattern-not-inside: |
if not url_has_allowed_host_and_scheme($G, ...):
return ...
...
<... HttpResponseRedirect(<... $G ...>, ...) ...>
- focus-metavariable: $S
- id: perun.py.xxe-lxml
languages: [python]
severity: ERROR
message: lxml XMLParser resolves external entities or allows network access (CWE-611, XXE). Use resolve_entities=False and no_network=True.
pattern-either:
# ponytail: only explicit unsafe flags are flagged; a bare XMLParser() (lxml before 5 defaults to resolve_entities=True) is not.
- pattern: $P.XMLParser(..., resolve_entities=True, ...)
- pattern: XMLParser(..., resolve_entities=True, ...)
- pattern: $P.XMLParser(..., load_dtd=True, ...)
- pattern: XMLParser(..., load_dtd=True, ...)
- pattern: $P.XMLParser(..., huge_tree=True, ...)
- pattern: XMLParser(..., huge_tree=True, ...)
- pattern: $P.XMLParser(..., no_network=False, ...)
- pattern: XMLParser(..., no_network=False, ...)
- id: perun.py.hardcoded-credential-compare
languages: [python]
severity: WARNING
Expand Down
8 changes: 7 additions & 1 deletion bench/tool-only/answer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@
"vuln/requirements.txt:1": "CWE-1104",
"vuln/Dockerfile:2": "CWE-250",
"vuln/.github/workflows/inject.yml:7": "CWE-94",
"vuln/main.tf:3": "CWE-732"
"vuln/main.tf:3": "CWE-732",
"vuln/xss.js:5": "CWE-79",
"vuln/ssrf.py:9": "CWE-918",
"vuln/xxe.py:5": "CWE-611",
"vuln/redirect.py:8": "CWE-601",
"vuln/tlsverify.py:5": "CWE-295",
"vuln/jwtverify.py:5": "CWE-347"
}
5 changes: 5 additions & 0 deletions bench/tool-only/clean/jwtverify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import jwt


def read_claims(token, key):
return jwt.decode(token, key, algorithms=['HS256'])
11 changes: 11 additions & 0 deletions bench/tool-only/clean/redirect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask import Flask, redirect, request

app = Flask(__name__)


@app.route('/login')
def login():
nxt = request.args.get('next', '/')
if not nxt.startswith('/') or nxt.startswith('//') or nxt.startswith('/\\'):
nxt = '/'
return redirect(nxt)
13 changes: 13 additions & 0 deletions bench/tool-only/clean/ssrf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import requests
from flask import Flask, abort, request

app = Flask(__name__)
ALLOWED = {'https://api.example.com/status'}


@app.route('/fetch')
def fetch():
url = request.args['url']
if url not in ALLOWED:
abort(400)
return requests.get(url, timeout=5).text
5 changes: 5 additions & 0 deletions bench/tool-only/clean/tlsverify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import requests


def get_status():
return requests.get('https://api.example.com/status', timeout=5).text
7 changes: 7 additions & 0 deletions bench/tool-only/clean/xss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const express = require('express');
const escapeHtml = require('escape-html');
const app = express();

app.get('/hello', (req, res) => {
res.type('text/plain').send('Hello ' + escapeHtml(String(req.query.name)));
});
6 changes: 6 additions & 0 deletions bench/tool-only/clean/xxe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lxml import etree


def parse(path):
parser = etree.XMLParser(resolve_entities=False, no_network=True)
return etree.parse(path, parser)
5 changes: 5 additions & 0 deletions bench/tool-only/vuln/jwtverify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import jwt


def read_claims(token, key):
return jwt.decode(token, key, options={'verify_signature': False})
8 changes: 8 additions & 0 deletions bench/tool-only/vuln/redirect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask import Flask, redirect, request

app = Flask(__name__)


@app.route('/login')
def login():
return redirect(request.args.get('next', '/'))
9 changes: 9 additions & 0 deletions bench/tool-only/vuln/ssrf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import requests
from flask import Flask, request

app = Flask(__name__)


@app.route('/fetch')
def fetch():
return requests.get(request.args['url'], timeout=5).text
5 changes: 5 additions & 0 deletions bench/tool-only/vuln/tlsverify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import requests


def get_status():
return requests.get('https://api.example.com/status', verify=False, timeout=5).text
6 changes: 6 additions & 0 deletions bench/tool-only/vuln/xss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const express = require('express');
const app = express();

app.get('/hello', (req, res) => {
res.send('<h1>Hello ' + req.query.name + '</h1>');
});
6 changes: 6 additions & 0 deletions bench/tool-only/vuln/xxe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lxml import etree


def parse(path):
parser = etree.XMLParser(resolve_entities=True, no_network=False)
return etree.parse(path, parser)
2 changes: 1 addition & 1 deletion changelog.d/bench-tool-only.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
### Added
- bench/tool-only: 11 seeded fictional CWE fixtures with clean twins and an answer key, scored by scripts/bench_tool_only.py (per-CWE and overall recall, false positives on the clean twins; fails below the frozen recall floor or above the frozen false-positive ceiling; SKIPs when semgrep, gitleaks or trivy is absent). Runs in the real-hub CI job after the tools install.
- bench/tool-only: 17 seeded fictional CWE fixtures (wave 19 adds CWE-79, 918, 611, 601, 295, 347) with clean twins and an answer key, scored by scripts/bench_tool_only.py (per-CWE and overall recall, false positives on the clean twins; fails below the frozen recall floor or above the frozen false-positive ceiling; SKIPs when semgrep, gitleaks or trivy is absent). Runs in the real-hub CI job after the tools install.
2 changes: 1 addition & 1 deletion scripts/bench_tool_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
CORE = ("semgrep", "gitleaks", "trivy", "bandit", "ruff", "git", "jq")
FLOOR_RECALL = 1.0 # frozen: 11/11 measured in the CI real-hub job after the CWE-22 taint rule stopped firing on send_from_directory's file name
DEP_FILE = "vuln/requirements.txt" # only fixture where a file-level (line 0) row counts
MAX_FALSE_POSITIVES = 26 # frozen: 26 noise rows on the clean twins measured in the CI real-hub job (full hub: bandit, ruff, trivy, checkov, devskim, semgrep); a local run with fewer tools sees fewer
MAX_FALSE_POSITIVES = 32 # frozen: 32 noise rows on the clean twins measured in the CI real-hub job (full hub: bandit, ruff, trivy, checkov, devskim, semgrep); a local run with fewer tools sees fewer


def run(truth_path=ROOT / "bench/tool-only/answer.json", fixtures=ROOT / "bench/tool-only"):
Expand Down
2 changes: 2 additions & 0 deletions scripts/ci-tests.d/semgrep-py-guards.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Bundled semgrep rules in py.yml: ssrf abort allowlist guard, open-redirect relative-path/url_has_allowed_host_and_scheme guard, xxe-lxml (annotated ruleid/ok fixtures; live when semgrep is installed, SKIP otherwise).
python3 scripts/test_semgrep_py_guards.py
Loading
Loading