fix(guardrails): Ruby where interpolation stays FAIL under CONCAT, PHP shell variables always count
This commit is contained in:
1 parent
c0fdbec3c1
commit
9275e0d82b
4 files changed
+27
-13
No files matched your search
@@ -72,8 +72,8 @@ when two rules match the same line the first in this order wins.
|
||||
|
||||
| Rule | Sev | Catches |
|
||||
|---|---|---|
|
||||
| INJ-SQL-RAW-UNSAFE | WARN | a raw escape fed a non-literal first argument: `$queryRawUnsafe(`, `$executeRawUnsafe(`, `knex.raw(`, `.raw(`, `.extra(`, `sequelize.query(`, `Sequelize.literal(`, `text(` when `sqlalchemy` is imported; ActiveRecord `.where("… #{`; WARN: a variable may hold a constant |
|
||||
| INJ-SQL-CONCAT | FAIL | a SQL statement string built from an expression: untagged template literal with `${`, quoted SQL followed by `+ identifier`, Python f-string, `%` operator or `.format(`, PHP `"… $var"` or `. $var`, Ruby `#{}`, Rust `format!(`, Go `fmt.Sprintf(` |
|
||||
| INJ-SQL-RAW-UNSAFE | WARN | a raw escape fed a non-literal first argument: `$queryRawUnsafe(`, `$executeRawUnsafe(`, `knex.raw(`, `.raw(`, `.extra(`, `sequelize.query(`, `Sequelize.literal(`, `text(` when `sqlalchemy` is imported; WARN: a variable may hold a constant |
|
||||
| INJ-SQL-CONCAT | FAIL | a SQL statement string built from an expression: untagged template literal with `${`, quoted SQL followed by `+ identifier`, Python f-string, `%` operator or `.format(`, PHP `"… $var"` or `. $var`, Ruby `#{}` (including ActiveRecord `.where("… #{`), Rust `format!(`, Go `fmt.Sprintf(` |
|
||||
| INJ-SQL-DYNAMIC-EXEC | WARN | plpgsql `EXECUTE` whose statement uses `\|\|` and is not `format(…)` (SQL files, comments blanked) |
|
||||
| INJ-POSTGREST-FILTER | WARN | supabase-js `.or(`, `.filter(`, `.not(`, `.match(`, `.textSearch(` with a template `${` or a `+` concatenation |
|
||||
| INJ-NOSQL-OPERATOR | WARN | Mongo `$where:` with a string, or `find`/`findOne`/`update*`/`delete*`/`countDocuments` whose filter is `req.body`, `req.query`, `req.params` or one field taken straight from them |
|
||||
|
||||
@@ -187,6 +187,10 @@ mutant shell-db-os-system FAIL INJ-SHELL-DB-TOOL \
|
||||
"printf '%s\n' 'os.system(\"psql \" + url)' > src/lib/backup.py"
|
||||
mutant shell-db-template FAIL INJ-SHELL-DB-TOOL \
|
||||
"printf '%s\n' 'exec(\`psql \${db}\`);' > src/lib/backup.ts"
|
||||
mutant sql-ruby-where-interp FAIL INJ-SQL-CONCAT \
|
||||
"printf '%s\n' 'User.where(\"name = #{name}\")' > src/lib/users.rb"
|
||||
mutant shell-db-php-upper-var FAIL INJ-SHELL-DB-TOOL \
|
||||
"printf '%s\n' '<?php shell_exec(\"mysqldump --host=\$Host app\");' > src/lib/dump.php"
|
||||
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 \
|
||||
@@ -326,6 +330,8 @@ benign shell-db-argv-python \
|
||||
"printf '%s\n' 'subprocess.run([\"psql\", f\"--dbname={url}\"])' > src/lib/backup.py"
|
||||
benign shell-db-env-ref \
|
||||
"printf '%s\n' 'execSync(\"psql \$DATABASE_URL -f schema.sql\");' > src/lib/backup.ts"
|
||||
benign ruby-where-placeholder \
|
||||
"printf '%s\n' 'User.where(\"name = ?\", name)' 'User.where(name: value)' > src/lib/users.rb"
|
||||
benign console-public-minified \
|
||||
"mkdir -p public/js; printf '%s\n' 'console.log(user);' > public/js/app.min.js"
|
||||
benign console-plain-string \
|
||||
|
||||
@@ -34,12 +34,12 @@ silent by design.
|
||||
|
||||
| Rule | Sev | Catches | Fix |
|
||||
|---|---|---|---|
|
||||
| INJ-SQL-RAW-UNSAFE | WARN | `$queryRawUnsafe`, `$executeRawUnsafe`, `knex.raw`, `sequelize.query`, `Sequelize.literal`, Django `.objects.raw()` / `.extra()`, SQLAlchemy `text()` fed a variable (a variable may hold a constant); ActiveRecord `where("… #{}")` | show it is a constant with bound values, else bind them; or the tagged `$queryRaw` |
|
||||
| INJ-SQL-CONCAT | FAIL | a SQL statement (select … from … where, insert into, update … set, delete … where, create/drop/alter) built with `${}`, `+`, an f-string, `%`, `.format`, PHP `$var`, Ruby `#{}`, `format!`, `Sprintf` | placeholders and a parameter list |
|
||||
| INJ-SQL-RAW-UNSAFE | WARN | `$queryRawUnsafe`, `$executeRawUnsafe`, `knex.raw`, `sequelize.query`, `Sequelize.literal`, Django `.objects.raw()` / `.extra()`, SQLAlchemy `text()` fed a variable (a variable may hold a constant)| show it is a constant with bound values, else bind them; or the tagged `$queryRaw` |
|
||||
| INJ-SQL-CONCAT | FAIL | a SQL statement (select … from … where, insert into, update … set, delete … where, create/drop/alter) built with `${}`, `+`, an f-string, `%`, `.format`, PHP `$var`, Ruby `#{}` (ActiveRecord `where("… #{}")` included), `format!`, `Sprintf` | placeholders and a parameter list |
|
||||
| INJ-SQL-DYNAMIC-EXEC | WARN | plpgsql `EXECUTE '…' \|\| x` | `format('… %I %L', …)` and `USING` |
|
||||
| INJ-POSTGREST-FILTER | WARN | supabase-js `.or()`, `.filter()`, `.not()`, `.match()`, `.textSearch()` with an interpolated string, in a file that uses supabase | `.eq()` and friends, validated values |
|
||||
| INJ-NOSQL-OPERATOR | WARN | Mongo `$where` with a string; a filter taken from `req.body`, `req.query` or `req.params` | allowlist the fields, cast to string, `sanitizeFilter` |
|
||||
| INJ-SHELL-DB-TOOL | FAIL | a shell call naming psql, mysql, mysqldump, pg_dump, mongosh, redis-cli, sqlite3… with an interpolated argument (an argv array and a bare `$ENV_VAR` stay silent) | an argv array, no shell, validated input |
|
||||
| INJ-SHELL-DB-TOOL | FAIL | a shell call naming psql, mysql, mysqldump, pg_dump, mongosh, redis-cli, sqlite3… with an interpolated argument (an argv array and a bare `$ENV_VAR` stay silent; in PHP every `$name` counts) | an argv array, no shell, validated input |
|
||||
|
||||
## exposure
|
||||
|
||||
|
||||
@@ -37,7 +37,8 @@ CONCAT = {
|
||||
(".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),
|
||||
(".rb",): re.compile(
|
||||
rf"\"{DQ}?{STMT}{DQ}#\{{|\.where\(\s*\"{DQ}#\{{", re.I),
|
||||
(".rs",): re.compile(rf"format!\(\s*\"{DQ}?{STMT}", re.I),
|
||||
(".go",): re.compile(rf"fmt\.Sprintf\(\s*\"{DQ}?{STMT}", re.I),
|
||||
}
|
||||
@@ -47,7 +48,6 @@ 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"(?<![.\w])text\s*\(\s*(?![\s'\"`)])")
|
||||
DYNAMIC_EXEC = re.compile(r"\bexecute\s+(?:'[^']*'|\w+)\s*\|\|", re.I)
|
||||
@@ -60,12 +60,17 @@ NOSQL = re.compile(
|
||||
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(
|
||||
SHELL_DB_HEAD = (
|
||||
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(?![\"']\s*[,\]])"
|
||||
r"[^\n]{0,300}?(?:\$\{|\+\s*[\w$]|#\{|\{\w+\}|%s|\$[a-z_])")
|
||||
r"|mongo|mongodump|redis-cli|sqlite3|clickhouse-client)"
|
||||
r"\b(?![\"']\s*[,\]])[^\n]{0,300}?")
|
||||
SHELL_INTERP = r"(?:\$\{|\+\s*[\w$]|#\{|\{\w+\}|%s|"
|
||||
# An uppercase $NAME is a shell env reference, except in PHP where every
|
||||
# $name is a variable.
|
||||
SHELL_DB = re.compile(SHELL_DB_HEAD + SHELL_INTERP + r"\$[a-z_])")
|
||||
SHELL_DB_PHP = re.compile(SHELL_DB_HEAD + SHELL_INTERP + r"\$[A-Za-z_])")
|
||||
|
||||
|
||||
def applies(project):
|
||||
@@ -127,9 +132,12 @@ def _nosql(project, files):
|
||||
|
||||
|
||||
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")
|
||||
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]
|
||||
return (hits(project, other, SHELL_DB, rule, msg)
|
||||
+ hits(project, php, SHELL_DB_PHP, rule, msg))
|
||||
|
||||
|
||||
def check(project):
|
||||
|
||||
Reference in new issue
Block a user