fix(guardrails): bound DATA_ATTR, SCRIPT_SRC, TRACE_MARKERS and the comment blankers, SENSITIVE_KEY aligned with the spec, 5xx map SKIPPED, base64 inline maps only, RecursionError and supabase shape guarded, large .map head read
This commit is contained in:
1 parent
0de2e8a48a
commit
5643e9dbd4
7 files changed
+82
-15
No files matched your search
@@ -114,7 +114,7 @@ defaults). Still GET only, same host, shared budget, `--authorized`.
|
||||
|---|---|
|
||||
| `sourcemap:<script path>` | for a script of the base page (`<script src>` on the same host, 10 at most, or the plan's `exposure.scripts`): `<url>.map`, the `SourceMap`/`X-SourceMap` header target or the trailing `sourceMappingURL` target answers 200 with `"sources"` |
|
||||
| `error-body:<path>` | the GET of `/guardrails-probe-<random>` or of a path in `exposure.error_routes` returns a body with a trace marker: `Traceback (most recent call last)`, `at … (file:N:N)`, `File "…", line N`, `SQLSTATE[`, `syntax error at or near`, `node_modules/`, `/var/www/`, `Stack trace:`, `#N /…php(N)` |
|
||||
| `fields:<path>` | the GET of a plan route returns JSON 200 whose keys (depth 4, 5 list items) match password, passwd, pwd, hash, salt, secret, token, api_key, apikey, service_role, private, ssn, iban, cvv, card_number. Only key names are printed, never values. Sent with `GUARDRAILS_JWT2` as bearer when set, anonymous otherwise |
|
||||
| `fields:<path>` | the GET of a plan route returns JSON 200 whose keys (depth 4, 5 list items) match password, passwd, pwd, hash, salt, secret, token, api_key, apikey, service_role, private_key, ssn, iban, cvv, card_number. Only key names are printed, never values. Sent with `GUARDRAILS_JWT2` as bearer when set, anonymous otherwise |
|
||||
|
||||
One shared change, flip-tested: `fetch` keeps the body of a 4xx/5xx
|
||||
response (today it returns `b""`); headers stay the `HTTPMessage`, whose
|
||||
|
||||
@@ -63,6 +63,7 @@ class H(BaseHTTPRequestHandler):
|
||||
b'<script src="/clean.js"></script>'
|
||||
b'<script src=/inline.js></script>'
|
||||
b'<script src="/biginline.js"></script>'
|
||||
b'<script src="/err.js"></script>'
|
||||
b'<script src="https://cdn.invalid/x.js"></script>'
|
||||
b'</html>')
|
||||
if path == "/app.js":
|
||||
@@ -77,6 +78,13 @@ class H(BaseHTTPRequestHandler):
|
||||
if path == "/biginline.js":
|
||||
return self._send(200, b"console.log(4);\n//# sourceMappingURL="
|
||||
b"data:application/json;base64," + b"A" * 20000)
|
||||
if path == "/err.js":
|
||||
return self._send(200, b"console.log(5);\n//# sourceMappingURL=err.js.map\n")
|
||||
if path == "/err.js.map":
|
||||
return self._send(503, b"")
|
||||
if path == "/api/session":
|
||||
return self._send(200, b'{"id": 2, "token": "t", "profile": {"private": false}}',
|
||||
{"Content-Type": "application/json"})
|
||||
if path == "/boom":
|
||||
return self._send(500, b"Traceback (most recent call last):\n"
|
||||
b' File "app.py", line 12, in handler\n')
|
||||
@@ -176,7 +184,8 @@ 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"}],
|
||||
"routes": [{"path": "/api/me"}, {"path": "/api/guarded"}, {"path": "/api/private"},
|
||||
{"path": "/api/session"}],
|
||||
"exposure": {"error_routes": ["/boom"]}}
|
||||
EOF
|
||||
OUT="$(probe --authorized "$WORK/exposure.json")"
|
||||
@@ -197,7 +206,13 @@ check "the random 404 path is 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
|
||||
"$(printf '%s\n' "$OUT" | grep -A1 'fields:/api/me' | grep -c 'api_key, next_page_token, password_hash$')" 1
|
||||
check "a bare token key is OPEN, a bare private key is not" \
|
||||
"$(verdict "$OUT" 'fields:/api/session')" OPEN
|
||||
check "the session key list is token only" \
|
||||
"$(printf '%s\n' "$OUT" | grep -A1 'fields:/api/session' | grep -c 'token$')" 1
|
||||
check "a 503 map answer is SKIPPED" \
|
||||
"$(verdict "$OUT" 'sourcemap:/err.js')" SKIPPED
|
||||
check "fields on a 401 is SKIPPED, never CLOSED" \
|
||||
"$(verdict "$OUT" 'fields:/api/guarded')" SKIPPED
|
||||
check "fields on a non-JSON body is SKIPPED" \
|
||||
@@ -244,6 +259,9 @@ printf '{"base_url": "%s", "supabase": {"url": "%s", "cross_user_paths": "/x"}}\
|
||||
"$BASE" "$BASE" > "$WORK/badcu.json"
|
||||
probe --authorized "$WORK/badcu.json" >/dev/null 2>&1
|
||||
check "a string cross_user_paths exits 2" "$?" 2
|
||||
printf '{"base_url": "%s", "supabase": 5}\n' "$BASE" > "$WORK/badsb.json"
|
||||
probe --authorized "$WORK/badsb.json" >/dev/null 2>&1
|
||||
check "a non-object supabase block exits 2" "$?" 2
|
||||
cat > "$WORK/lower.json" <<EOF
|
||||
{"base_url": "$BASE", "cors": true, "cors_path": "/lower"}
|
||||
EOF
|
||||
|
||||
@@ -202,7 +202,7 @@ mutant sourcemap-webpack-warn WARN EXP-SOURCEMAP-PUBLIC \
|
||||
mutant sourcemap-public-dir FAIL EXP-SOURCEMAP-PUBLIC \
|
||||
"mkdir -p public/assets; printf '%s\n' '{\"version\":3,\"sources\":[\"src/app.ts\"],\"mappings\":\"AAAA\"}' > public/assets/app.js.map"
|
||||
mutant sourcemap-large-map FAIL EXP-SOURCEMAP-PUBLIC \
|
||||
"mkdir -p public; head -c 1100000 /dev/zero | tr '\\0' a > public/app.js.map"
|
||||
"mkdir -p public; { printf '%s' '{\"version\":3,\"sources\":[\"src/app.ts\"],\"mappings\":\"'; head -c 1100000 /dev/zero | tr '\\0' A; printf '%s\n' '\"}'; } > public/app.js.map"
|
||||
mutant hydrated-sensitive WARN EXP-HYDRATED-SENSITIVE \
|
||||
"printf '%s\n' 'export async function getServerSideProps() { return { props: { accessToken: process.env.CRM_TOKEN } }; }' > src/pages/account.tsx"
|
||||
mutant markup-comment WARN EXP-MARKUP-COMMENT \
|
||||
@@ -241,6 +241,8 @@ benign() { # benign <label> <snippet>: a legitimate pattern raises nothing
|
||||
check "$label raises no FAIL or WARN" \
|
||||
"$(printf '%s\n' "$out" | grep -c ' 0 FAIL, 0 WARN')" 1
|
||||
}
|
||||
benign large-level-map \
|
||||
"mkdir -p public/levels; head -c 1100000 /dev/zero | tr '\\0' x > public/levels/big.map"
|
||||
benign helper-under-api \
|
||||
"mkdir -p src/pages/api/helpers; printf '%s\n' 'export const fmt = () => 1;' > src/pages/api/helpers/format.ts"
|
||||
benign mailer-test-file \
|
||||
@@ -462,6 +464,33 @@ out=$(scan "$WORK/linked")
|
||||
check "symlinked directory is not followed" \
|
||||
"$(printf '%s\n' "$out" | grep -c '(0 files)')" 1
|
||||
|
||||
echo "== regex timing"
|
||||
FAST="$(cd "$ROOT" && python3 -B - <<'PY'
|
||||
import importlib.util, sys, time
|
||||
sys.path.insert(0, "skills/guardrails/scripts")
|
||||
from checks import common, exposure
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"probe", "skills/guardrails/scripts/probe.py")
|
||||
probe = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(probe)
|
||||
cases = [
|
||||
(exposure.DATA_ATTR, "data-secret" * 20000),
|
||||
(common.MARKUP_COMMENT, "<!--" * 50000),
|
||||
(common.SQL_COMMENT, "/*" * 100000),
|
||||
(probe.SCRIPT_SRC, "<script " * 25000),
|
||||
(probe.TRACE_MARKERS, b"at a ( " * 28000),
|
||||
]
|
||||
slow = 0
|
||||
for pattern, text in cases:
|
||||
start = time.perf_counter()
|
||||
pattern.search(text)
|
||||
slow += time.perf_counter() - start >= 1.0
|
||||
print("REGEX-FAST" if not slow else "REGEX-SLOW")
|
||||
PY
|
||||
)"
|
||||
check "adversarial inputs finish each regex under one second" \
|
||||
"$(printf '%s\n' "$FAST" | grep -c 'REGEX-FAST')" 1
|
||||
|
||||
echo "---"
|
||||
echo "passed: $PASS failed: $FAIL"
|
||||
[ "$FAIL" -eq 0 ]
|
||||
@@ -220,7 +220,7 @@ The fields probe sends `GUARDRAILS_JWT2` as a bearer when it is set. Every path
|
||||
| rate-limit | `rate-limit` | the burst saw no 429 | HOST-NO-RATE-LIMIT |
|
||||
| sourcemap | `sourcemap:<script>` | a source map with sources answered 200 for a page script, or the script carries an inline map | EXP-SOURCEMAP-PUBLIC |
|
||||
| error-body | `error-body:<path>` | a 404 or a named error route returned a stack trace or an internal path | EXP-ERROR-STACK, EXP-DEBUG-ON |
|
||||
| fields | `fields:<path>` | a JSON 200 carried a password, salt, token, key or secret-like key (identifier-shaped names printed, never values) | EXP-RESPONSE-WIDE, EXP-HYDRATED-SENSITIVE |
|
||||
| fields | `fields:<path>` | a JSON 200 carried a password, salt, token, hash, key or secret-like key (`private` only as private_key; identifier-shaped names printed, never values) | EXP-RESPONSE-WIDE, EXP-HYDRATED-SENSITIVE |
|
||||
|
||||
A verdict is an observation, not a verdict on the project. OPEN is a returned
|
||||
200 or returned rows; CLOSED is a refusal or an empty body; SKIPPED is a
|
||||
|
||||
@@ -54,9 +54,10 @@ def redact(value):
|
||||
return value[:6] + "..."
|
||||
|
||||
|
||||
SQL_COMMENT = re.compile(r"--[^\n]*|/\*.*?\*/", re.S)
|
||||
SQL_COMMENT = re.compile(r"--[^\n]*|/\*[\s\S]{0,4000}?\*/")
|
||||
MARKUP_COMMENT = re.compile(
|
||||
r"<!--.*?-->|/\*.*?\*/|(?:^|(?<=\s))//[^\n]*", re.S | re.M)
|
||||
r"<!--(?:[^<]|<(?!!--)){0,4000}?-->|/\*[\s\S]{0,4000}?\*/"
|
||||
r"|(?:^|(?<=\s))//[^\n]*", re.M)
|
||||
|
||||
|
||||
def _blank(pattern, text):
|
||||
|
||||
@@ -36,8 +36,8 @@ COMMENT_WORDS = re.compile(
|
||||
r"|api[-_ ]?key|internal|staging|localhost|admin|remove before"
|
||||
r"|do not commit|don'?t commit|temporary)\b", re.I)
|
||||
DATA_ATTR = re.compile(
|
||||
r"\b(data-[\w-]*(?:secret|api-?_?key|password|passwd|jwt|private"
|
||||
r"|credential|service-?role)[\w-]*)\s*=", re.I)
|
||||
r"\b(data-[\w-]{0,60}(?:secret|api-?_?key|password|passwd|jwt|private"
|
||||
r"|credential|service-?role)[\w-]{0,60})\s*=", re.I)
|
||||
HIDDEN_INPUT = re.compile(r"<input\b[^>\n]{0,300}>", re.I)
|
||||
SENSITIVE_NAME = re.compile(
|
||||
r"secret|api[-_]?key|password|passwd|jwt|private|credential", re.I)
|
||||
@@ -147,14 +147,24 @@ def _public_map(project, path):
|
||||
and not VENDORED.search(project.rel(path)))
|
||||
|
||||
|
||||
def _large_map(project, path):
|
||||
"""A large public .map whose first 4 KB already hold a mappings key."""
|
||||
if path.suffix.lower() != ".map" or not _public_map(project, path):
|
||||
return False
|
||||
try:
|
||||
with path.open("rb") as handle:
|
||||
return b'"mappings"' in handle.read(4096)
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
def _map_files(project):
|
||||
rule = Rule(DOMAIN, "EXP-SOURCEMAP-PUBLIC", "FAIL")
|
||||
out = hits(project, _env_files(project), ENV_MAPS, rule,
|
||||
"GENERATE_SOURCEMAP=true ships source maps")
|
||||
small = [p for p in project.with_ext(".map")
|
||||
if _public_map(project, p) and '"mappings"' in project.text(p)]
|
||||
large = [p for p in project.large
|
||||
if p.suffix.lower() == ".map" and _public_map(project, p)]
|
||||
large = [p for p in project.large if _large_map(project, p)]
|
||||
return out + [project.found(rule, p, 0, "source map in a public directory")
|
||||
for p in small + large]
|
||||
|
||||
|
||||
@@ -36,15 +36,16 @@ DEFAULT_BUDGET = 200
|
||||
BURST_CAP = 25
|
||||
BODY_CAP = 1_000_000
|
||||
SCRIPT_CAP = 10
|
||||
SCRIPT_SRC = re.compile(r"<script[^>]+src=[\"']?([^\"'\s>]+)", re.I)
|
||||
SCRIPT_SRC = re.compile(r"<script[^>]{1,300}src=[\"']?([^\"'\s>]+)", re.I)
|
||||
MAP_COMMENT = re.compile(r"//[#@]\s*sourceMappingURL=(\S+)")
|
||||
TRACE_MARKERS = re.compile(
|
||||
rb"Traceback \(most recent call last\)"
|
||||
rb"|\bat [\w.$<> ]{1,80} \([^)\n]*:\d+:\d+\)"
|
||||
rb"|\bat [\w.$<> ]{1,80} \([^)\n]{0,200}:\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"(?:^|_)(?:password|passwd|pwd|salt|secret|token|hash|api_?key"
|
||||
r"|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
|
||||
@@ -277,6 +278,8 @@ 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:"):
|
||||
if "base64," not in url:
|
||||
continue
|
||||
return "OPEN", script_url, "inline source map in the script"
|
||||
target = _same_host(script_url, url)
|
||||
if not target:
|
||||
@@ -284,6 +287,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 == 403 or (resp.status or 0) >= 500:
|
||||
return ("SKIPPED", target,
|
||||
f"HTTP {resp.status}: map not readable, not confirmed")
|
||||
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")
|
||||
@@ -369,7 +375,7 @@ def _key_note(keys):
|
||||
def _json_body(resp):
|
||||
try:
|
||||
return json.loads(resp.body), True
|
||||
except ValueError:
|
||||
except (ValueError, RecursionError):
|
||||
return None, False
|
||||
|
||||
|
||||
@@ -493,6 +499,9 @@ def _shape_error(plan):
|
||||
if plan.get("routes") is not None and \
|
||||
not isinstance(plan["routes"], list):
|
||||
return "routes must be a list"
|
||||
if plan.get("supabase") is not None and \
|
||||
not isinstance(plan["supabase"], dict):
|
||||
return "supabase must be an object"
|
||||
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"
|
||||
|
||||
Reference in new issue
Block a user