feat(guardrails): injection domain (INJ-*): per-language SQL concat and raw escapes, dynamic EXECUTE, PostgREST, NoSQL, shell into DB tools; shared code_files/hits/dedupe helpers

This commit is contained in:
bchanot committed 2026-10-19 00:32:06 +02:00
1 parent 988038cce3
commit 7500a618fe
4 files changed
+235 -1

No files matched your search

+52
View File
@@ -52,6 +52,7 @@ SQL=supabase/migrations/0001_init.sql
TSX=src/components/ContactForm.tsx
NOTIFY=supabase/functions/notify/index.ts
SMS=supabase/functions/sms/index.ts
API=src/pages/api/notes.ts
echo "== clean fixture"
out=$(scan "$GOOD"); rc=$?
@@ -175,6 +176,32 @@ mutant firebase-loop WARN FN-SELF-TRIGGER \
mutant key-body FAIL API-SECRET-LITERAL \
'printf -- "-----BEGIN %s KEY-----\n%s\n" PRIVATE "$(head -c 40 /dev/zero | tr "\0" A)" > src/key.ts'
echo "== injection"
mutant sql-template FAIL INJ-SQL-CONCAT \
"printf '%s\n' 'const q = \`select id from notes where owner = \${owner}\`;' >> $API"
mutant sql-concat FAIL INJ-SQL-CONCAT \
"printf '%s\n' 'const q = \"select id from notes where owner = \" + owner;' >> $API"
mutant sql-fstring FAIL INJ-SQL-CONCAT \
"printf '%s\n' 'cur.execute(f\"delete from notes where id = {note_id}\")' > src/lib/cleanup.py"
mutant sql-php-interpolation FAIL INJ-SQL-CONCAT \
"printf '%s\n' '<?php \$r = \$db->query(\"select id from notes where owner = \$owner\");' > src/lib/notes.php"
mutant sql-py-concat FAIL INJ-SQL-CONCAT \
"printf '%s\n' 'cur.execute(\"select id from notes where owner = \" + owner)' > src/lib/cleanup.py"
mutant sql-quoted-value FAIL INJ-SQL-CONCAT \
"printf '%s\n' \$'const q = \"select id from notes where owner = \\'\" + owner + \"\\'\";' >> $API"
mutant sql-php-dot FAIL INJ-SQL-CONCAT \
"printf '%s\n' \$'<?php \$r = \$db->query(\"select id from notes where owner = \\'\" . \$owner . \"\\'\");' > src/lib/notes.php"
mutant sql-raw-unsafe FAIL INJ-SQL-RAW-UNSAFE \
"printf '%s\n' 'const rows = await prisma.\$queryRawUnsafe(statement);' >> $API"
mutant sql-dynamic-exec WARN INJ-SQL-DYNAMIC-EXEC \
"printf '%s\n' \"execute 'select count(*) from ' || tbl into n;\" >> $SQL"
mutant postgrest-filter WARN INJ-POSTGREST-FILTER \
"printf '%s\n' 'const { data } = await supabase.from(\"notes\").select().or(\`owner.eq.\${who}\`);' >> $API"
mutant nosql-operator WARN INJ-NOSQL-OPERATOR \
"printf '%s\n' 'const doc = await Notes.findOne({ owner: req.body.owner });' >> $API"
mutant shell-db-tool FAIL INJ-SHELL-DB-TOOL \
"printf '%s\n' 'execSync(\`pg_dump --table=\${table} app > backup.sql\`);' > src/lib/backup.ts"
echo "== benign patterns stay silent"
benign() { # benign <label> <snippet>: a legitimate pattern raises nothing
local label="$1" snippet="$2" dir="$WORK/$1"
@@ -228,6 +255,31 @@ ENVKEY='printf "SUPABASE_SERVICE_ROLE_KEY=%s.%s.signaturepart\n" "$(b64 "{\"alg\
benign env-worker-ignored "printf '.env.*\n' >> .gitignore; $ENVKEY"
mutant env-worker-tracked FAIL SB-SERVICE-ROLE-LITERAL "$ENVKEY"
benign parameterized-query \
"printf '%s\n' 'const r = await db.query(\"select id from notes where owner = \$1\", [owner]);' >> $API"
benign tagged-sql-template \
"printf '%s\n' 'const r = await sql\`select id from notes where owner = \${owner}\`;' >> $API"
benign typed-sql-tag \
"printf '%s\n' 'const r = await sql<Note[]>\`select id from notes where owner = \${owner}\`;' >> $API"
benign express-raw-webhook \
"printf '%s\n' 'app.post(\"/hook\", express.raw({ type: \"application/json\" }), onHook);' >> $API"
benign ui-copy-select \
"printf '%s\n' 'const hint = \`Select a plan from the list, \${name}\`;' >> $TSX"
benign ui-copy-delete \
"printf '%s\n' 'const label = \"Delete from favorites \" + item.title;' >> $TSX"
benign jquery-not \
"printf '%s\n' 'const rows = \$(\".row\").not(\".row-\" + id);' >> $TSX"
benign argv-db-tool \
"printf '%s\n' 'spawnSync(\"pg_dump\", [\"--table\", table, \"app\"]);' > src/lib/backup.ts"
benign format-dynamic-exec \
"printf '%s\n' \"execute format('select count(*) from %I', tbl) into n;\" >> $SQL"
benign test-path-sql \
"mkdir -p src/__tests__; printf '%s\n' 'const q = \`select id from notes where owner = \${owner}\`;' > src/__tests__/notes.test.ts"
benign minified-bundle \
"printf '%s\n' 'const q=\`select id from notes where owner = \${o}\`;' > src/lib/app.min.js"
benign long-line-bundle \
"head -c 6000 /dev/zero | tr '\\0' x > src/lib/bundle.js; printf '%s\n' ' const q=\`select id from notes where owner = \${o}\`;' >> src/lib/bundle.js"
echo "== transactional versus marketing senders"
solo tx-only src/contact.ts 'import nodemailer from "nodemailer";'
check "transactional-only mail exits 0" "$LAST_RC" 0
@@ -161,3 +161,44 @@ def non_doc_files(project):
"""Files that can hold behavior: everything except docs and samples."""
skip = (".md", ".mdx", ".txt", ".example", ".sample", ".template")
return [p for p in project.files if not p.name.endswith(skip)]
TEST_PATH = re.compile(
r"\.(?:test|spec|e2e|stories)\.|(?:^|/)(?:tests?|__tests__|spec|e2e"
r"|fixtures?|mocks?)/", re.I)
BUILT_SEGMENTS = {"public", "static", "www", "assets"}
MINIFIED_NAME = re.compile(r"\.min\.|\.bundle\.", re.I)
LONG_LINE = 5000
def _minified(project, path):
if MINIFIED_NAME.search(path.name):
return True
return any(len(line) > LONG_LINE
for line in project.text(path).splitlines())
def code_files(project, exts=SOURCE_EXTS):
"""Hand-written source: no tests, fixtures, built dirs, minified files."""
return [p for p in non_doc_files(project)
if p.suffix.lower() in exts
and not TEST_PATH.search(project.rel(p))
and not set(project.segments(p)) & BUILT_SEGMENTS
and not _minified(project, p)]
def hits(project, files, regex, rule, message):
"""One finding per match of a compiled regex over the given files."""
return [project.found(rule, path, m.start(), message)
for path in files
for m in regex.finditer(project.text(path))]
def dedupe_lines(findings):
"""One finding per (path, line): the first emitted wins."""
seen, out = set(), []
for finding in findings:
if (finding.path, finding.line) not in seen:
seen.add((finding.path, finding.line))
out.append(finding)
return out
@@ -0,0 +1,139 @@
"""Injection checks: SQL built from input, raw ORM escapes, dynamic SQL,
PostgREST filters, NoSQL operators, a shell into a DB tool."""
import re
from .common import Rule, blank_comments, code_files, dedupe_lines, hits
DOMAIN = "injection"
JS_EXTS = (".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".vue", ".svelte",
".astro")
SQL_EXTS = (".sql", ".pgsql", ".plpgsql")
GAP = r"[^'\"`]{0,200}"
STMT = (
rf"(?:select\b{GAP}\bfrom\b{GAP}\b(?:where|join|limit|order\s+by"
rf"|group\s+by)\b|select\s+\*\s+from\b"
rf"|insert\s+into\s+[\w.\"]+{GAP}\b(?:values|select)\b"
r"|insert\s+into\s+[\w.\"]+\s*\(|update\s+[\w.\"]+\s+set\b"
r"|delete\s+from\s+[\w.\"]+\s+where\b"
r"|\bwhere\s+[\w.\"]+\s*(?:=|like\b|in\b|<|>)"
r"|create\s+(?:table|index)\s|drop\s+(?:table|index)\s"
r"|alter\s+table\s|truncate\s+table\s)")
DQ = r"[^\"\n]{0,200}"
SQ = r"[^'\n]{0,200}"
def _quoted(tail):
"""A double- or single-quoted SQL statement string followed by tail."""
return rf"\"{DQ}?{STMT}{DQ}\"{tail}|'{SQ}?{STMT}{SQ}'{tail}"
CONCAT = {
JS_EXTS: re.compile(
rf"(?<![\w$)\]>])`[^`]{{0,400}}?{STMT}[^`]{{0,400}}?\$\{{|"
+ _quoted(r"\s*\+\s*[\w$(]"), re.I),
(".py",): re.compile(
rf"\bf['\"][^'\"\n]{{0,200}}?{STMT}|"
+ _quoted(r"\s*(?:%\s*[\w(]|\.format\(|\+\s*[\w(])"), re.I),
(".php",): re.compile(
rf"\"{DQ}?{STMT}{DQ}\$[A-Za-z_]\w*|"
+ _quoted(r"\s*\.\s*\$[A-Za-z_]"), re.I),
(".rb",): re.compile(rf"\"{DQ}?{STMT}{DQ}#\{{", re.I),
(".rs",): re.compile(rf"format!\(\s*\"{DQ}?{STMT}", re.I),
(".go",): re.compile(rf"fmt\.Sprintf\(\s*\"{DQ}?{STMT}", re.I),
}
RAW = {
JS_EXTS: re.compile(
r"(?:\$queryRawUnsafe|\$executeRawUnsafe|\bknex\.raw"
r"|\bsequelize\.query|\b[Ss]equelize\.literal)\s*\(\s*(?![\s'\"`)])"),
(".py",): re.compile(
r"\.objects\.raw\(\s*(?![\s'\"`)])|\.extra\(\s*(?![\s'\"`)])"),
(".rb",): re.compile(r"\.where\(\s*\"[^\"\n]{0,200}#\{"),
}
SQLA_TEXT = re.compile(r"\btext\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}\$\{"
r"|['\"][^'\"\n]{0,200}['\"]\s*\+\s*[\w$])")
NOSQL = re.compile(
r"\$where\s*:\s*(?:['\"`]|\w)"
r"|\.(?:find|findOne|findOneAndUpdate|findOneAndDelete|updateOne"
r"|updateMany|deleteOne|deleteMany|countDocuments|replaceOne)\(\s*"
r"(?:req\.(?:body|query|params)\b"
r"|\{\s*\w+\s*:\s*req\.(?:body|query|params)\.\w+\s*[,}])")
SHELL_DB = re.compile(
r"\b(?:exec|execSync|spawn|spawnSync|system|popen|subprocess\.\w+"
r"|os\.system|Command::new|shell_exec|passthru|proc_open)\s*\("
r"[^\n]{0,300}?\b(?:psql|mysql|mysqldump|pg_dump|pg_restore|mongosh"
r"|mongo|mongodump|redis-cli|sqlite3|clickhouse-client)\b[^\n]{0,300}?"
r"(?:\$\{|\+\s*[\w$]|#\{|\{\w+\}|%s|\$[A-Za-z_])")
def applies(project):
return bool(project.with_ext(*JS_EXTS, ".py", ".php", ".rb", ".rs",
".go", *SQL_EXTS))
def _by_ext(files, exts):
return [p for p in files if p.suffix.lower() in exts]
def _per_language(project, files, table, rule, message):
out = []
for exts, regex in table.items():
out += hits(project, _by_ext(files, exts), regex, rule, message)
return out
def _raw_unsafe(project, files):
rule = Rule(DOMAIN, "INJ-SQL-RAW-UNSAFE", "FAIL")
msg = "raw SQL escape fed a non-literal: bind the values"
sqla = [p for p in _by_ext(files, (".py",))
if "sqlalchemy" in project.text(p)]
return (_per_language(project, files, RAW, rule, msg)
+ hits(project, sqla, SQLA_TEXT, rule, msg))
def _concat(project, files):
return _per_language(
project, files, CONCAT, Rule(DOMAIN, "INJ-SQL-CONCAT", "FAIL"),
"SQL built from an expression: use placeholders")
def _dynamic_exec(project):
rule = Rule(DOMAIN, "INJ-SQL-DYNAMIC-EXEC", "WARN")
out = []
for path in code_files(project, SQL_EXTS):
text = blank_comments(project.text(path))
for m in DYNAMIC_EXEC.finditer(text):
out.append(project.found(
rule, path, m.start(),
"EXECUTE built with ||: use format() with %I/%L and USING"))
return out
def _postgrest(project, files):
client = [p for p in _by_ext(files, JS_EXTS)
if "supabase" in project.text(p)]
return hits(project, client, POSTGREST,
Rule(DOMAIN, "INJ-POSTGREST-FILTER", "WARN"),
"PostgREST filter string built from a value: use .eq()")
def _nosql(project, files):
return hits(project, _by_ext(files, JS_EXTS), NOSQL,
Rule(DOMAIN, "INJ-NOSQL-OPERATOR", "WARN"),
"filter taken from the request: allowlist fields, cast")
def _shell_db(project, files):
return hits(project, files, SHELL_DB,
Rule(DOMAIN, "INJ-SHELL-DB-TOOL", "FAIL"),
"DB tool run through a shell with input: pass an argv")
def check(project):
files = code_files(project)
found = _raw_unsafe(project, files) + _concat(project, files)
found += _dynamic_exec(project) + _postgrest(project, files)
found += _nosql(project, files) + _shell_db(project, files)
return dedupe_lines(found)
+3 -1
View File
@@ -6,7 +6,7 @@ See scanner.py for the modes and exit codes.
import sys
import scanner
from checks import a11y, apis, functions, hosting, messaging, supabase
from checks import a11y, apis, functions, hosting, injection, messaging, supabase
CATALOGUE = scanner.Catalogue(
name="guardrails",
@@ -17,10 +17,12 @@ CATALOGUE = scanner.Catalogue(
"supabase": supabase,
"messaging": messaging,
"functions": functions,
"injection": injection,
},
rule_domains={
"API": "api", "HOST": "hosting", "A11Y": "a11y", "SB": "supabase",
"MSG": "messaging", "FN": "functions",
"INJ": "injection",
},
manual_rules=frozenset({
"HOST-WAF-DDOS", "HOST-SPEND-CAP", "SB-RLS-LIVE",