fix(guardrails): CONCAT before RAW, probe row newline kept, malformed urls handled, sa.text matched

This commit is contained in:
bchanot committed 2026-10-19 12:17:56 +02:00
1 parent 9275e0d82b
commit 63a88bc91d
5 files changed
+47 -15

No files matched your search

@@ -49,7 +49,8 @@ RAW = {
(".py",): re.compile(
r"\.objects\.raw\(\s*(?![\s'\"`)])|\.extra\(\s*(?![\s'\"`)])"),
}
SQLA_TEXT = re.compile(r"(?<![.\w])text\s*\(\s*(?![\s'\"`)])")
SQLA_TEXT = re.compile(
r"(?:(?<![.\w])|(?<=\bsa\.)|(?<=\bsqlalchemy\.))text\s*\(\s*(?![\s'\"`)])")
DYNAMIC_EXEC = re.compile(r"\bexecute\s+(?:'[^']*'|\w+)\s*\|\|", re.I)
POSTGREST = re.compile(
r"\.(?:or|filter|not|match|textSearch)\(\s*(?:`[^`\n]{0,200}\$\{"
@@ -135,14 +136,14 @@ def _shell_db(project, files):
rule = Rule(DOMAIN, "INJ-SHELL-DB-TOOL", "FAIL")
msg = "DB tool run through a shell with input: pass an argv"
php = _by_ext(files, (".php",))
other = [p for p in files if p not in php]
other = [p for p in files if p.suffix.lower() != ".php"]
return (hits(project, other, SHELL_DB, rule, msg)
+ hits(project, php, SHELL_DB_PHP, rule, msg))
def check(project):
files = code_files(project)
found = _raw_unsafe(project, files) + _concat(project, files)
found = _concat(project, files) + _raw_unsafe(project, files)
found += _dynamic_exec(project) + _postgrest(project, files)
found += _nosql(project, files) + _shell_db(project, files)
return dedupe_lines(found)
+18 -7
View File
@@ -75,7 +75,10 @@ class Probe:
def _stays_on_host(old_url, new_url):
"""Same host and port; the scheme may stay or upgrade, never downgrade."""
old, new = urlsplit(old_url), urlsplit(new_url)
try:
old, new = urlsplit(old_url), urlsplit(new_url)
except ValueError:
return False
return old.netloc == new.netloc and (
old.scheme == new.scheme or (old.scheme, new.scheme)
== ("http", "https"))
@@ -258,7 +261,10 @@ def probe_rate_limit(session, plan):
def _same_host(context, url):
"""Absolute url when it stays on context's host, else None."""
full = urljoin(context, url)
try:
full = urljoin(context, url)
except ValueError:
return None
return full if _stays_on_host(context, full) else None
@@ -447,9 +453,9 @@ def _printable(text):
def _probe_row(probe):
return _printable(
f" {probe.verdict:<8} {probe.domain:<9} {probe.id} "
f"{probe.target}\n {probe.message}")
return (f" {probe.verdict:<8} {probe.domain:<9} "
f"{_printable(probe.id)} {_printable(probe.target)}\n"
f" {_printable(probe.message)}")
def _summary(results):
@@ -474,8 +480,13 @@ def _render_json(plan, results):
def _http_url(value):
return isinstance(value, str) and urlsplit(value).scheme in (
"http", "https") and bool(urlsplit(value).netloc)
if not isinstance(value, str):
return False
try:
parts = urlsplit(value)
except ValueError:
return False
return parts.scheme in ("http", "https") and bool(parts.netloc)
def _load_plan(path):