diff --git a/lib/tests/guardrails-probe.test.sh b/lib/tests/guardrails-probe.test.sh index a4f988a..0b0587c 100644 --- a/lib/tests/guardrails-probe.test.sh +++ b/lib/tests/guardrails-probe.test.sh @@ -62,6 +62,7 @@ class H(BaseHTTPRequestHandler): return self._send(200, b'' b'' b'' + b'' b'' b'') if path == "/app.js": @@ -73,6 +74,9 @@ class H(BaseHTTPRequestHandler): if path == "/inline.js": return self._send(200, b"console.log(3);\n//# sourceMappingURL=" b"data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==\n") + if path == "/biginline.js": + return self._send(200, b"console.log(4);\n//# sourceMappingURL=" + b"data:application/json;base64," + b"A" * 20000) if path == "/boom": return self._send(500, b"Traceback (most recent call last):\n" b' File "app.py", line 12, in handler\n') @@ -180,6 +184,8 @@ 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 large inline source map is OPEN" \ + "$(verdict "$OUT" 'sourcemap:/biginline.js')" OPEN check "a script with no map is CLOSED" \ "$(verdict "$OUT" 'sourcemap:/clean.js')" CLOSED check "a foreign-host script is never probed" \ @@ -228,6 +234,16 @@ printf '{"base_url": "%s", "exposure": {"scripts": []}}\n' "$BASE" > "$WORK/nosc OUT="$(probe --authorized "$WORK/noscript.json")" check "an empty scripts list still gives a SKIPPED sourcemap row" \ "$(verdict "$OUT" 'sourcemap')" SKIPPED +printf '{"base_url": "%s", "rate_limit": null}\n' "$BASE" > "$WORK/badrl.json" +probe --authorized "$WORK/badrl.json" >/dev/null 2>&1 +check "a null rate_limit exits 2" "$?" 2 +printf '{"base_url": "%s", "routes": 5}\n' "$BASE" > "$WORK/badroutes.json" +probe --authorized "$WORK/badroutes.json" >/dev/null 2>&1 +check "a non-list routes exits 2" "$?" 2 +printf '{"base_url": "%s", "supabase": {"url": "%s", "cross_user_paths": "/x"}}\n' \ + "$BASE" "$BASE" > "$WORK/badcu.json" +probe --authorized "$WORK/badcu.json" >/dev/null 2>&1 +check "a string cross_user_paths exits 2" "$?" 2 cat > "$WORK/lower.json" <]+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"Traceback \(most recent call last\)" + rb"|\bat [\w.$<> ]{1,80} \([^)\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( @@ -190,7 +191,7 @@ def _route_probe(session, base, route): def probe_routes(session, plan): base = plan["base_url"].rstrip("/") - routes = [r for r in plan.get("routes", []) if not r.get("public")] + routes = [r for r in plan.get("routes") or [] if not r.get("public")] return [_route_probe(session, base, r) for r in routes] @@ -261,12 +262,12 @@ def _page_scripts(session, base): def _map_candidates(script_url, resp): - urls = [script_url + ".map"] + bare = urlsplit(script_url)._replace(query="", fragment="").geturl() + urls = [bare + ".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) + comment = MAP_COMMENT.search(resp.body.decode("utf-8", "ignore")) if comment: urls.append(comment.group(1)) return list(dict.fromkeys(urls)) @@ -283,6 +284,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 resp.status == 200 and len(resp.body) >= BODY_CAP: + return ("SKIPPED", target, "map answered 200 but its body is " + "over 1 MB: sources not confirmed, fetch it by hand") 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" @@ -399,7 +403,7 @@ def probe_exposure(session, plan): 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", [])] + out += [_fields_probe(session, base, r) for r in plan.get("routes") or []] return out @@ -483,11 +487,23 @@ def _exposure_error(cfg): return "" +def _shape_error(plan): + if "rate_limit" in plan and not isinstance(plan["rate_limit"], dict): + return "rate_limit must be an object" + if plan.get("routes") is not None and \ + not isinstance(plan["routes"], list): + return "routes must be a list" + paths = _obj(plan, "supabase").get("cross_user_paths") + if paths is not None and not isinstance(paths, list): + return "supabase.cross_user_paths must be a list" + return "" + + def _plan_error(plan): - method = plan.get("rate_limit", {}).get("method", "GET").upper() + method = str(_obj(plan, "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" - error = _exposure_error(plan.get("exposure")) + error = _exposure_error(plan.get("exposure")) or _shape_error(plan) if error: return error bad = [p for p in _plan_paths(plan)