From 9275e0d82b1ca70ad34e75d48c9fde4afab481bb Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 19 Oct 2026 11:59:40 +0200 Subject: [PATCH] fix(guardrails): Ruby where interpolation stays FAIL under CONCAT, PHP shell variables always count --- ...2026-10-18-guardrails-a11y-split-design.md | 4 ++-- lib/tests/guardrails-scan.test.sh | 6 +++++ skills/guardrails/references/checks.md | 6 ++--- skills/guardrails/scripts/checks/injection.py | 24 ++++++++++++------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/docs/superpowers/specs/2026-10-18-guardrails-a11y-split-design.md b/docs/superpowers/specs/2026-10-18-guardrails-a11y-split-design.md index 2a7d8ba..38fbbcb 100644 --- a/docs/superpowers/specs/2026-10-18-guardrails-a11y-split-design.md +++ b/docs/superpowers/specs/2026-10-18-guardrails-a11y-split-design.md @@ -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 | diff --git a/lib/tests/guardrails-scan.test.sh b/lib/tests/guardrails-scan.test.sh index c0ba2bb..4b46309 100644 --- a/lib/tests/guardrails-scan.test.sh +++ b/lib/tests/guardrails-scan.test.sh @@ -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' ' 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 \ diff --git a/skills/guardrails/references/checks.md b/skills/guardrails/references/checks.md index b4cb4f7..ee06c36 100644 --- a/skills/guardrails/references/checks.md +++ b/skills/guardrails/references/checks.md @@ -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 diff --git a/skills/guardrails/scripts/checks/injection.py b/skills/guardrails/scripts/checks/injection.py index 2458a6f..5cd7cf1 100644 --- a/skills/guardrails/scripts/checks/injection.py +++ b/skills/guardrails/scripts/checks/injection.py @@ -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"(?