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:
1 parent
6e79d182e3
commit
8b6472b8c4
2 files changed
+316
-10
No files matched your search
@@ -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):
|
||||
|
||||
Reference in new issue
Block a user