forked from bchanot/claude
fix(guardrails): probe map and fields verdicts fail closed, console rule sees public/, raw escape WARN, argv silent, plan validation
This commit is contained in:
1 parent
6c0006bce3
commit
c0fdbec3c1
8 files changed
+206
-44
No files matched your search
@@ -34,12 +34,12 @@ silent by design.
|
||||
|
||||
| Rule | Sev | Catches | Fix |
|
||||
|---|---|---|---|
|
||||
| INJ-SQL-RAW-UNSAFE | FAIL | `$queryRawUnsafe`, `$executeRawUnsafe`, `knex.raw`, `sequelize.query`, `Sequelize.literal`, Django `.objects.raw()` / `.extra()`, SQLAlchemy `text()` fed a variable; ActiveRecord `where("… #{}")` | bind the values, or the tagged `$queryRaw` |
|
||||
| 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-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, 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) | an argv array, no shell, validated input |
|
||||
|
||||
## exposure
|
||||
|
||||
@@ -50,8 +50,8 @@ silent by design.
|
||||
| EXP-HYDRATED-SENSITIVE | WARN | a token, secret, key, password or ssn-like key returned by `getServerSideProps`, `getStaticProps`, a Remix `loader`, a SvelteKit `load` or set on `window.__STATE__`; a TS type member is skipped | return only the fields the page renders |
|
||||
| EXP-MARKUP-COMMENT | WARN | `<!-- -->` shipped to the browser (html, astro, php, ejs, hbs, twig, njk, liquid) with todo, fixme, debug, password, token, internal, staging, admin… | delete it, or move it to a code comment the build strips |
|
||||
| EXP-DATA-ATTR-SENSITIVE | WARN | `data-*` attribute or hidden input named secret, api-key, password, jwt, private, credential (csrf, xsrf, authenticity, nonce excluded) | keep it server-side, send an opaque id |
|
||||
| EXP-ERROR-STACK | FAIL | in a route file: `err.stack`, the error object, `traceback.format_exc()` or `getTraceAsString()` in a response | generic message out, full error in the server log |
|
||||
| EXP-ERROR-MESSAGE | WARN | in a route file: `err.message`, `str(e)`, `$e->getMessage()` sent as is | map to a safe message; validation errors are fine, DB errors are not |
|
||||
| EXP-ERROR-STACK | FAIL | in any server file: `err.stack`, the error object, `traceback.format_exc()` or `getTraceAsString()` in a response | generic message out, full error in the server log |
|
||||
| EXP-ERROR-MESSAGE | WARN | in any server file: `err.message`, `str(e)`, `$e->getMessage()` sent as is | map to a safe message; validation errors are fine, DB errors are not |
|
||||
| EXP-DEBUG-ON | FAIL | `DEBUG = True` in a settings module (not dev/local/test), `APP_DEBUG=true` / `FLASK_DEBUG=1` in a committed non-dev env file, `app.debug = True`, `consider_all_requests_local = true`, `errorhandler()` mounted without an environment guard | env-driven, false in production |
|
||||
| EXP-RESPONSE-WIDE | WARN | a route returning `select *` / `select('*')` on a users-like table, or a single-line Prisma `findUnique/findFirst/findMany` without `select` or `omit` | an explicit field list |
|
||||
|
||||
|
||||
@@ -188,12 +188,16 @@ def _minified(project, path):
|
||||
for line in project.text(path).splitlines())
|
||||
|
||||
|
||||
def code_files(project, exts=SOURCE_EXTS):
|
||||
"""Hand-written source: no tests, fixtures, built dirs, minified files."""
|
||||
def code_files(project, exts=SOURCE_EXTS, keep_built=False):
|
||||
"""Hand-written source: no tests, fixtures, built dirs, minified files.
|
||||
|
||||
keep_built keeps public/, static/, www/ and assets/: they also hold the
|
||||
hand-written browser scripts of server-rendered stacks."""
|
||||
skipped = set() if keep_built else BUILT_SEGMENTS
|
||||
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 set(project.segments(p)) & skipped
|
||||
and not _minified(project, p)]
|
||||
|
||||
|
||||
|
||||
@@ -261,7 +261,8 @@ def _response_wide(project):
|
||||
|
||||
def check(project):
|
||||
files = code_files(project)
|
||||
found = _console_data(project, files) + _map_config(project)
|
||||
console = code_files(project, keep_built=True)
|
||||
found = _console_data(project, console) + _map_config(project)
|
||||
found += _map_files(project) + _hydrated(project, files)
|
||||
found += _markup_comments(project) + _data_attrs(project)
|
||||
found += _errors(project, files) + _debug(project, files)
|
||||
|
||||
@@ -49,7 +49,7 @@ RAW = {
|
||||
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'\"`)])")
|
||||
SQLA_TEXT = re.compile(r"(?<![.\w])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}\$\{"
|
||||
@@ -64,8 +64,8 @@ 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_])")
|
||||
r"|mongo|mongodump|redis-cli|sqlite3|clickhouse-client)\b(?![\"']\s*[,\]])"
|
||||
r"[^\n]{0,300}?(?:\$\{|\+\s*[\w$]|#\{|\{\w+\}|%s|\$[a-z_])")
|
||||
|
||||
|
||||
def applies(project):
|
||||
@@ -85,8 +85,9 @@ def _per_language(project, files, table, rule, message):
|
||||
|
||||
|
||||
def _raw_unsafe(project, files):
|
||||
rule = Rule(DOMAIN, "INJ-SQL-RAW-UNSAFE", "FAIL")
|
||||
msg = "raw SQL escape fed a non-literal: bind the values"
|
||||
rule = Rule(DOMAIN, "INJ-SQL-RAW-UNSAFE", "WARN")
|
||||
msg = ("raw SQL escape fed a non-literal: show it is a constant "
|
||||
"with bound values, else bind them")
|
||||
sqla = [p for p in _by_ext(files, (".py",))
|
||||
if "sqlalchemy" in project.text(p)]
|
||||
return (_per_language(project, files, RAW, rule, msg)
|
||||
|
||||
@@ -21,6 +21,7 @@ CLOSED means it was refused or empty. Exit codes: 0 no OPEN, 1 at least one
|
||||
OPEN, 2 usage error.
|
||||
"""
|
||||
import argparse
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
@@ -37,7 +38,8 @@ BURST_CAP = 25
|
||||
BODY_CAP = 1_000_000
|
||||
SCRIPT_CAP = 10
|
||||
SCRIPT_SRC = re.compile(r"<script[^>]{1,300}src=[\"']?([^\"'\s>]+)", re.I)
|
||||
MAP_COMMENT = re.compile(r"//[#@]\s*sourceMappingURL=(\S+)")
|
||||
MAP_COMMENT = re.compile(
|
||||
r"^[ \t]*//[#@][ \t]*sourceMappingURL=(\S+)", re.M)
|
||||
TRACE_MARKERS = re.compile(
|
||||
rb"Traceback \(most recent call last\)"
|
||||
rb"|\bat [\w.$<> ]{1,80} \([^)\n]{0,200}:\d+:\d+\)"
|
||||
@@ -71,11 +73,19 @@ class Probe:
|
||||
return asdict(self)
|
||||
|
||||
|
||||
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)
|
||||
return old.netloc == new.netloc and (
|
||||
old.scheme == new.scheme or (old.scheme, new.scheme)
|
||||
== ("http", "https"))
|
||||
|
||||
|
||||
class _SameHost(urllib.request.HTTPRedirectHandler):
|
||||
"""Follow a same-host redirect, drop one that leaves the host."""
|
||||
|
||||
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
||||
if urlsplit(newurl).netloc != urlsplit(req.full_url).netloc:
|
||||
if not _stays_on_host(req.full_url, newurl):
|
||||
return None
|
||||
return super().redirect_request(req, fp, code, msg, headers, newurl)
|
||||
|
||||
@@ -111,8 +121,10 @@ def fetch(session, method, url, headers=None, body=None):
|
||||
return _response_from(resp)
|
||||
except urllib.error.HTTPError as exc:
|
||||
return Response(exc.code, exc.headers, _error_body(exc))
|
||||
except (urllib.error.URLError, OSError, ValueError) as exc:
|
||||
return Response(None, {}, b"", str(exc))
|
||||
except (urllib.error.URLError, OSError, ValueError,
|
||||
http.client.HTTPException) as exc:
|
||||
reason = str(exc)
|
||||
return Response(None, {}, b"", reason)
|
||||
|
||||
|
||||
def _verdict(ctx, response, is_open, msgs):
|
||||
@@ -247,7 +259,7 @@ 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)
|
||||
return full if urlsplit(full).netloc == urlsplit(context).netloc else None
|
||||
return full if _stays_on_host(context, full) else None
|
||||
|
||||
|
||||
def _page_scripts(session, base):
|
||||
@@ -268,9 +280,9 @@ def _map_candidates(script_url, resp):
|
||||
header = resp.headers.get("sourcemap") or resp.headers.get("x-sourcemap")
|
||||
if header:
|
||||
urls.append(header)
|
||||
comment = MAP_COMMENT.search(resp.body.decode("utf-8", "ignore"))
|
||||
if comment:
|
||||
urls.append(comment.group(1))
|
||||
comments = MAP_COMMENT.findall(resp.body.decode("utf-8", "ignore"))
|
||||
if comments:
|
||||
urls.append(comments[-1])
|
||||
return list(dict.fromkeys(urls))
|
||||
|
||||
|
||||
@@ -287,6 +299,9 @@ def _map_verdict(session, script_url, candidates):
|
||||
resp = fetch(session, "GET", target)
|
||||
if resp.error or resp.status is None:
|
||||
return "ERROR", target, resp.error or "no response"
|
||||
if 300 <= resp.status < 400:
|
||||
return ("SKIPPED", target, f"HTTP {resp.status}: map redirect "
|
||||
"dropped (off host or downgraded), not followed")
|
||||
if resp.status == 403 or (resp.status or 0) >= 500:
|
||||
return ("SKIPPED", target,
|
||||
f"HTTP {resp.status}: map not readable, not confirmed")
|
||||
@@ -341,20 +356,17 @@ def _error_paths(cfg):
|
||||
return [random_path] + list(cfg.get("error_routes") or [])
|
||||
|
||||
|
||||
def _keys(data, depth=0):
|
||||
if depth > 4:
|
||||
return []
|
||||
if isinstance(data, dict):
|
||||
found = [k for k in data if isinstance(k, str)]
|
||||
for value in data.values():
|
||||
found += _keys(value, depth + 1)
|
||||
return found
|
||||
if isinstance(data, list):
|
||||
found = []
|
||||
for value in data[:5]:
|
||||
found += _keys(value, depth + 1)
|
||||
return found
|
||||
return []
|
||||
def _keys(data):
|
||||
"""Every string key of the whole body, walked without recursion."""
|
||||
found, stack = [], [data]
|
||||
while stack:
|
||||
node = stack.pop()
|
||||
if isinstance(node, dict):
|
||||
found += [k for k in node if isinstance(k, str)]
|
||||
stack += node.values()
|
||||
elif isinstance(node, list):
|
||||
stack += node
|
||||
return found
|
||||
|
||||
|
||||
def _snake(key):
|
||||
@@ -429,9 +441,15 @@ def _tally(results):
|
||||
for v in ("OPEN", "CLOSED", "SKIPPED", "ERROR")}
|
||||
|
||||
|
||||
def _printable(text):
|
||||
"""Control characters of a remote or plan string never reach the tty."""
|
||||
return re.sub(r"[\x00-\x1f\x7f-\x9f]", "?", str(text))
|
||||
|
||||
|
||||
def _probe_row(probe):
|
||||
return (f" {probe.verdict:<8} {probe.domain:<9} {probe.id} "
|
||||
f"{probe.target}\n {probe.message}")
|
||||
return _printable(
|
||||
f" {probe.verdict:<8} {probe.domain:<9} {probe.id} "
|
||||
f"{probe.target}\n {probe.message}")
|
||||
|
||||
|
||||
def _summary(results):
|
||||
@@ -442,7 +460,7 @@ def _summary(results):
|
||||
|
||||
def _render_text(plan, results):
|
||||
body = [_probe_row(p) for p in results]
|
||||
head = [f"guardrails live probe: {plan['base_url']}",
|
||||
head = [_printable(f"guardrails live probe: {plan['base_url']}"),
|
||||
f"probes: {len(results)} (read-only, bounded budget)"]
|
||||
return "\n".join(head + [""] + body + [_summary(results)])
|
||||
|
||||
@@ -455,13 +473,18 @@ def _render_json(plan, results):
|
||||
}, indent=2)
|
||||
|
||||
|
||||
def _http_url(value):
|
||||
return isinstance(value, str) and urlsplit(value).scheme in (
|
||||
"http", "https") and bool(urlsplit(value).netloc)
|
||||
|
||||
|
||||
def _load_plan(path):
|
||||
try:
|
||||
plan = json.loads(open(path, encoding="utf-8").read())
|
||||
except (OSError, ValueError) as exc:
|
||||
return None, f"cannot read plan: {exc}"
|
||||
if not isinstance(plan, dict) or not plan.get("base_url"):
|
||||
return None, "plan needs an object with a base_url"
|
||||
if not isinstance(plan, dict) or not _http_url(plan.get("base_url")):
|
||||
return None, "plan needs an object with an http(s) base_url"
|
||||
return plan, ""
|
||||
|
||||
|
||||
@@ -494,8 +517,11 @@ def _exposure_error(cfg):
|
||||
|
||||
|
||||
def _shape_error(plan):
|
||||
if "rate_limit" in plan and not isinstance(plan["rate_limit"], dict):
|
||||
rate = plan.get("rate_limit", {})
|
||||
if not isinstance(rate, dict):
|
||||
return "rate_limit must be an object"
|
||||
if rate and not isinstance(rate.get("path"), str):
|
||||
return "rate_limit.path must be a string"
|
||||
if plan.get("routes") is not None and \
|
||||
not isinstance(plan["routes"], list):
|
||||
return "routes must be a list"
|
||||
@@ -539,7 +565,8 @@ def main(argv):
|
||||
return 2
|
||||
if not args.authorized:
|
||||
print("refused: pass --authorized to confirm you own "
|
||||
f"{plan['base_url']} and may probe it", file=sys.stderr)
|
||||
f"{_printable(plan['base_url'])} and may probe it",
|
||||
file=sys.stderr)
|
||||
return 2
|
||||
results = run_probes(Session(), plan)
|
||||
render = _render_json if args.json else _render_text
|
||||
|
||||
Reference in new issue
Block a user