feat(guardrails): exposure probe (source maps served or inline, error bodies, sensitive keys) that fails closed; every plan path validated; fetch keeps error bodies

This commit is contained in:
bchanot committed 2026-10-19 00:42:13 +02:00
1 parent 6e79d182e3
commit 8b6472b8c4
2 files changed
+316 -10

No files matched your search

+92 -3
View File
@@ -39,9 +39,9 @@ class H(BaseHTTPRequestHandler):
def do_OPTIONS(self):
origin = self.headers.get("Origin", "")
self._send(204, b"", {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Credentials": "true"})
upper = ("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")
names = tuple(n.lower() for n in upper) if self.path == "/lower" else upper
self._send(204, b"", {names[0]: origin, names[1]: "true"})
def do_GET(self):
path = self.path.split("?")[0]
@@ -58,6 +58,29 @@ class H(BaseHTTPRequestHandler):
return self._send(429 if HITS["rl"] > 5 else 200, b"")
if path == "/nolimit":
return self._send(200, b"")
if path == "/":
return self._send(200, b'<html><script src="/app.js"></script>'
b'<script src="/clean.js"></script>'
b'<script src=/inline.js></script>'
b'<script src="https://cdn.invalid/x.js"></script>'
b'</html>')
if path == "/app.js":
return self._send(200, b"console.log(1);\n//# sourceMappingURL=app.js.map\n")
if path == "/app.js.map":
return self._send(200, b'{"version":3,"sources":["src/app.ts"],"mappings":""}')
if path == "/clean.js":
return self._send(200, b"console.log(2);\n")
if path == "/inline.js":
return self._send(200, b"console.log(3);\n//# sourceMappingURL="
b"data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==\n")
if path == "/boom":
return self._send(500, b"Traceback (most recent call last):\n"
b' File "app.py", line 12, in handler\n')
if path == "/api/me":
return self._send(200, b'{"id": 1, "email": "t@t.io", '
b'"password_hash": "x", "profile": {"api_key": "y"}, '
b'"next_page_token": "abc", "hashtags": []}',
{"Content-Type": "application/json"})
return self._send(404, b"")
@@ -146,6 +169,72 @@ printf '{"base_url": "%s", "rate_limit": {"path": "/x", "method": "DELETE"}}\n'
probe --authorized "$WORK/bad.json" >/dev/null 2>&1
check "a write method in the plan exits 2" "$?" 2
echo "== exposure probes"
cat > "$WORK/exposure.json" <<EOF
{"base_url": "$BASE",
"routes": [{"path": "/api/me"}, {"path": "/api/guarded"}, {"path": "/api/private"}],
"exposure": {"error_routes": ["/boom"]}}
EOF
OUT="$(probe --authorized "$WORK/exposure.json")"
check "a served source map is OPEN" \
"$(verdict "$OUT" 'sourcemap:/app.js')" OPEN
check "an inline data: source map is OPEN" \
"$(verdict "$OUT" 'sourcemap:/inline.js')" OPEN
check "a script with no map is CLOSED" \
"$(verdict "$OUT" 'sourcemap:/clean.js')" CLOSED
check "a foreign-host script is never probed" \
"$(printf '%s\n' "$OUT" | grep -c 'cdn.invalid')" 0
check "a traceback in an error body is OPEN" \
"$(verdict "$OUT" 'error-body:/boom')" OPEN
check "the random 404 path is CLOSED" \
"$(verdict "$OUT" 'error-body:/guardrails-probe-')" CLOSED
check "sensitive keys in a JSON response are OPEN" \
"$(verdict "$OUT" 'fields:/api/me')" OPEN
check "the sensitive key names are listed, never the values" \
"$(printf '%s\n' "$OUT" | grep -A1 'fields:/api/me' | grep -c 'api_key, password_hash$')" 1
check "fields on a 401 is SKIPPED, never CLOSED" \
"$(verdict "$OUT" 'fields:/api/guarded')" SKIPPED
check "fields on a non-JSON body is SKIPPED" \
"$(verdict "$OUT" 'fields:/api/private')" SKIPPED
cat > "$WORK/unreachable.json" <<EOF
{"base_url": "http://127.0.0.1:1", "exposure": {}}
EOF
OUT="$(probe --authorized "$WORK/unreachable.json")"
check "an unreachable base page gives a SKIPPED sourcemap row" \
"$(verdict "$OUT" 'sourcemap')" SKIPPED
printf '{"base_url": "%s", "exposure": {"error_routes": ["boom"]}}\n' "$BASE" > "$WORK/badexp.json"
probe --authorized "$WORK/badexp.json" >/dev/null 2>&1
check "an error route not starting with / exits 2" "$?" 2
printf '{"base_url": "%s", "exposure": []}\n' "$BASE" > "$WORK/badexp2.json"
probe --authorized "$WORK/badexp2.json" >/dev/null 2>&1
check "a non-object exposure block exits 2" "$?" 2
printf '{"base_url": "%s", "exposure": {"scripts": "/app.js"}}\n' "$BASE" > "$WORK/badexp3.json"
probe --authorized "$WORK/badexp3.json" >/dev/null 2>&1
check "a scripts string instead of a list exits 2" "$?" 2
printf '{"base_url": "%s", "routes": [{"path": ".evil.invalid/x"}]}\n' "$BASE" > "$WORK/badroute.json"
probe --authorized "$WORK/badroute.json" >/dev/null 2>&1
check "a route path that could leave the host exits 2" "$?" 2
printf '{"base_url": "%s", "routes": ["/x"]}\n' "$BASE" > "$WORK/badroute2.json"
probe --authorized "$WORK/badroute2.json" >/dev/null 2>&1
check "a route that is not an object exits 2" "$?" 2
printf '{"base_url": "%s", "exposure": true}\n' "$BASE" > "$WORK/badexp4.json"
probe --authorized "$WORK/badexp4.json" >/dev/null 2>&1
check "a boolean exposure block exits 2 without a traceback" "$?" 2
printf '{"base_url": "%s", "exposure": {"scripts": ["/does-not-exist.js"]}}\n' "$BASE" > "$WORK/stale.json"
OUT="$(probe --authorized "$WORK/stale.json")"
check "a listed script that answers 404 is SKIPPED, never CLOSED" \
"$(verdict "$OUT" 'sourcemap:/does-not-exist.js')" SKIPPED
printf '{"base_url": "%s", "exposure": {"scripts": []}}\n' "$BASE" > "$WORK/noscript.json"
OUT="$(probe --authorized "$WORK/noscript.json")"
check "an empty scripts list still gives a SKIPPED sourcemap row" \
"$(verdict "$OUT" 'sourcemap')" SKIPPED
cat > "$WORK/lower.json" <<EOF
{"base_url": "$BASE", "cors": true, "cors_path": "/lower"}
EOF
OUT="$(probe --authorized "$WORK/lower.json")"
check "lowercase CORS header names are still read" \
"$(verdict "$OUT" 'cors-reflect')" OPEN
echo "== JSON output"
check "json reports the open count" \
"$(probe --authorized --json "$WORK/plan.json" \
+224 -7
View File
@@ -5,12 +5,15 @@ The static scan shows a protection is absent in the code. This observes what
a running target the user owns and names actually returns. It sends GET, HEAD
and OPTIONS only; a login burst is opt-in in the plan. It never writes data,
never uses a service_role key, never follows a redirect to another host, and
stops at a fixed request budget.
stops at a fixed request budget. With an exposure block in the plan it
also reads what the page's scripts, error bodies and JSON responses give
away; a target it could not observe is ERROR or SKIPPED, never CLOSED.
Input is a plan JSON (built by the skill from the static findings) plus
secrets from the environment, never the command line:
GUARDRAILS_ANON_KEY Supabase anon/publishable key for the anon-read probe
GUARDRAILS_JWT2 a second test user's token for the cross-user probe
(also sent as bearer by the fields probe when set)
A verdict is a factual observation, not a conclusion: the skill reads it
against the static findings. OPEN means the request returned data or a 200,
@@ -20,23 +23,36 @@ OPEN, 2 usage error.
import argparse
import json
import os
import re
import sys
import urllib.error
import urllib.request
from dataclasses import asdict, dataclass
from urllib.parse import quote, urlsplit
from urllib.parse import quote, urljoin, urlsplit
READ_METHODS = {"GET", "HEAD", "OPTIONS"}
FAKE_ORIGIN = "https://guardrails-probe.invalid"
DEFAULT_BUDGET = 200
BURST_CAP = 25
BODY_CAP = 1_000_000
SCRIPT_CAP = 10
SCRIPT_SRC = re.compile(r"<script[^>]+src=[\"']?([^\"'\s>]+)", re.I)
MAP_COMMENT = re.compile(r"//[#@]\s*sourceMappingURL=(\S+)")
TRACE_MARKERS = re.compile(
rb"Traceback \(most recent call last\)|\bat [\w.$<> ]+ \([^)\n]*:\d+:\d+\)"
rb"|File \"[^\"\n]+\", line \d+|SQLSTATE\[|syntax error at or near"
rb"|node_modules/|/var/www/|Stack trace:|#\d+ /[\w/.-]+\.php\(\d+\)")
SENSITIVE_KEY = re.compile(
r"(?:^|_)(?:password|passwd|pwd|salt|secret|api_?key|service_?role"
r"|private_?key|(?:access|refresh|id|auth|api|session)_token|jwt|ssn"
r"|iban|cvv|card_?number)(?:$|_)")
IDENT = re.compile(r"[A-Za-z_][\w-]{0,31}") # used with fullmatch
@dataclass(frozen=True)
class Response:
status: int # None on a transport error
headers: dict
headers: object # http.client.HTTPMessage: .get() ignores case
body: bytes
error: str = ""
@@ -71,8 +87,15 @@ class Session:
self.budget = budget
def _error_body(exc):
try:
return exc.read(BODY_CAP)
except (OSError, ValueError, AttributeError):
return b""
def _response_from(resp):
return Response(resp.status, dict(resp.headers), resp.read(BODY_CAP))
return Response(resp.status, resp.headers, resp.read(BODY_CAP))
def fetch(session, method, url, headers=None, body=None):
@@ -85,7 +108,7 @@ def fetch(session, method, url, headers=None, body=None):
with session.opener.open(req, timeout=session.timeout) as resp:
return _response_from(resp)
except urllib.error.HTTPError as exc:
return Response(exc.code, dict(exc.headers), b"")
return Response(exc.code, exc.headers, _error_body(exc))
except (urllib.error.URLError, OSError, ValueError) as exc:
return Response(None, {}, b"", str(exc))
@@ -219,8 +242,169 @@ def probe_rate_limit(session, plan):
return [_rate_verdict(target, codes)]
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
def _page_scripts(session, base):
"""(urls, note): same-host scripts of the base page, or why none."""
page = fetch(session, "GET", base + "/")
if page.error:
return [], f"base page unreachable: {page.error}"
text = page.body.decode("utf-8", "ignore")
urls = [u for u in (_same_host(base + "/", s)
for s in SCRIPT_SRC.findall(text)) if u]
urls = list(dict.fromkeys(urls))[:SCRIPT_CAP]
return urls, "" if urls else "no same-host script on the base page"
def _map_candidates(script_url, resp):
urls = [script_url + ".map"]
header = resp.headers.get("sourcemap") or resp.headers.get("x-sourcemap")
if header:
urls.append(header)
tail = resp.body[-600:].decode("utf-8", "ignore")
comment = MAP_COMMENT.search(tail)
if comment:
urls.append(comment.group(1))
return list(dict.fromkeys(urls))
def _map_verdict(session, script_url, candidates):
"""(verdict, target, note) over the candidate map urls, fail closed."""
for url in candidates:
if url.startswith("data:"):
return "OPEN", script_url, "inline source map in the script"
target = _same_host(script_url, url)
if not target:
return "SKIPPED", url, "map url on another host, not fetched"
resp = fetch(session, "GET", target)
if resp.error or resp.status is None:
return "ERROR", target, resp.error or "no response"
if resp.status == 200 and b'"sources"' in resp.body:
return "OPEN", target, "source map served with its sources"
return "CLOSED", script_url, "no source map served for this script"
def _sourcemap_probe(session, script_url):
pid = f"sourcemap:{urlsplit(script_url).path}"
resp = fetch(session, "GET", script_url)
if resp.error:
return Probe("exposure", pid, "ERROR", script_url, resp.error)
if resp.status != 200:
return Probe("exposure", pid, "SKIPPED", script_url,
f"HTTP {resp.status}: script not served, map not read")
if len(resp.body) >= BODY_CAP:
return Probe("exposure", pid, "SKIPPED", script_url,
"script body truncated: its map comment was not read")
verdict, target, note = _map_verdict(
session, script_url, _map_candidates(script_url, resp))
return Probe("exposure", pid, verdict, target, note)
def _sourcemaps(session, base, cfg):
listed = cfg.get("scripts")
if listed is None:
urls, note = _page_scripts(session, base)
else:
urls = [base + s for s in listed][:SCRIPT_CAP]
note = "" if urls else "no script listed in exposure.scripts"
if note:
return [Probe("exposure", "sourcemap", "SKIPPED", base, note)]
return [_sourcemap_probe(session, u) for u in urls]
def _error_probe(session, base, path):
target = base + path
resp = fetch(session, "GET", target)
return _verdict(
("exposure", f"error-body:{path}", target), resp,
bool(TRACE_MARKERS.search(resp.body)),
("error body carries a stack trace or an internal path",
"error body carries no trace marker"))
def _error_paths(cfg):
random_path = "/guardrails-probe-" + os.urandom(6).hex()
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 _snake(key):
return re.sub(r"(?<=[a-z0-9])(?=[A-Z])", "_", key).replace("-", "_").lower()
def _sensitive_keys(data):
return sorted({k for k in _keys(data) if SENSITIVE_KEY.search(_snake(k))})
def _key_note(keys):
"""Identifier-shaped key names only; anything else is counted."""
shown = [k for k in keys if IDENT.fullmatch(k)]
hidden = len(keys) - len(shown)
return ", ".join(shown) + (f" (+{hidden} unprintable)" if hidden else "")
def _json_body(resp):
try:
return json.loads(resp.body), True
except ValueError:
return None, False
def _fields_verdict(resp):
if resp.error:
return "ERROR", resp.error
data, is_json = _json_body(resp)
if resp.status != 200 or not is_json:
return "SKIPPED", f"HTTP {resp.status}: no JSON body to read"
keys = _sensitive_keys(data)
if keys:
return "OPEN", "response carries sensitive keys: " + _key_note(keys)
return "CLOSED", "no sensitive key in the response"
def _bearer():
token = os.environ.get("GUARDRAILS_JWT2")
return {"Authorization": f"Bearer {token}"} if token else None
def _fields_probe(session, base, route):
target = base + route["path"]
verdict, note = _fields_verdict(fetch(session, "GET", target, _bearer()))
return Probe("exposure", f"fields:{route['path']}", verdict, target, note)
def probe_exposure(session, plan):
cfg = plan.get("exposure")
if cfg is None:
return []
base = plan["base_url"].rstrip("/")
out = _sourcemaps(session, base, cfg)
out += [_error_probe(session, base, p) for p in _error_paths(cfg)]
out += [_fields_probe(session, base, r) for r in plan.get("routes", [])]
return out
PROBES = (probe_supabase, probe_cross_user, probe_routes,
probe_cors, probe_rate_limit)
probe_cors, probe_exposure, probe_rate_limit)
def run_probes(session, plan):
@@ -271,11 +455,44 @@ def _load_plan(path):
return plan, ""
def _obj(plan, key):
value = plan.get(key)
return value if isinstance(value, dict) else {}
def _plan_paths(plan):
"""Every path the probe would append to base_url (None when malformed)."""
routes = plan.get("routes") or []
cfg, supa, rate = _obj(plan, "exposure"), _obj(plan, "supabase"), \
_obj(plan, "rate_limit")
return ([r.get("path") if isinstance(r, dict) else None for r in routes]
+ list(supa.get("cross_user_paths") or [])
+ [plan.get("cors_path", "/"), rate.get("path", "/")]
+ list(cfg.get("scripts") or [])
+ list(cfg.get("error_routes") or []))
def _exposure_error(cfg):
if cfg is None:
return ""
if not isinstance(cfg, dict):
return "exposure must be an object"
for key in ("scripts", "error_routes"):
if cfg.get(key) is not None and not isinstance(cfg[key], list):
return f"exposure.{key} must be a list"
return ""
def _plan_error(plan):
method = plan.get("rate_limit", {}).get("method", "GET").upper()
if method not in READ_METHODS and method != "POST":
return f"rate_limit method {method} is not a probe method"
return ""
error = _exposure_error(plan.get("exposure"))
if error:
return error
bad = [p for p in _plan_paths(plan)
if not isinstance(p, str) or not p.startswith("/")]
return f"plan path {bad[0]!r} must start with /" if bad else ""
def parse_args(argv):