"""Map the attack surface of a Python or JS/TS codebase with regex heuristics. Standard library only. It finds routes and handlers (and whether an auth or ownership check shows up near them), SQL built with string formatting, shell/eval/deserialisation sinks, file paths built from input, outbound HTTP with non-literal URLs, CORS/CSRF/cookie settings, webhook handlers without signature checks, LLM tool use, and more. Every hit is a lead to read, not a finding. Lines that use a variable taken from the request are tagged [input]. Run: python3 surface.py [--json] [--only CATEGORY,...] """ import json, os, re, sys from collections import defaultdict from pathlib import Path PY = {".py"} JS = {".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs"} SKIP_DIRS = {".git", "node_modules", ".venv", "venv", "env", "__pycache__", "dist", "build", ".next", ".nuxt", "coverage", "site-packages", ".tox", ".mypy_cache", ".pytest_cache", "vendor", "out"} MAX_BYTES = 1_000_000 # (category, languages, regex, hint). "both" means py and js. RULES = [ # injection ("sql-format", "py", r"""f["']{1,3}[^"']*((?i:select\s[^"'`]*\sfrom|insert\s+into|update\s+\w+\s+set|delete\s+from)|\bWHERE\b|\bORDER\s+BY\b)[^"']*\{""", "SQL built with an f-string. Use placeholders (?, %s, :name) and pass values separately."), ("sql-format", "py", r"""["'][^"']*((?i:select\s[^"'`]*\sfrom|insert\s+into|update\s+\w+\s+set|delete\s+from)|\bWHERE\b|\bORDER\s+BY\b)[^"']*["']\s*(%\s*[\w(]|\.format\(|\+\s*\w)""", "SQL built with % / .format / +. Use placeholders."), ("sql-format", "js", r"""(?]+>|:\w+|\{\w+\}|\[\w+\]|\(\\d\+\)|\([^)]*\)") WEBHOOK_VERIFY = re.compile(r"hmac|signature|Signature|constructEvent|verify|svix|timingSafeEqual|compare_digest", re.I) LLM_IN_BODY = re.compile(r"generate(Text|Content|Object)\(|streamText\(|messages\.create\(|chat\.completions|responses\.create\(|\bllm\b|\bcomplete\(|ask_?model|callModel|runAgent|agent\.run", re.I) RATELIMIT = re.compile(r"limiter|rate_?limit|ratelimit|RateLimit|express-rate-limit|slowapi|throttl|upstash/ratelimit", re.I) SENSITIVE_ROUTE = re.compile(r"login|signin|sign-in|signup|register|reset|forgot|otp|verify|token|invite|password|magic", re.I) INPUT_SRC = re.compile(r"""request\.|req\.(query|params|body|headers|cookies)|\.query\.get\(|args\.get\(|form\.get\(|get_json\(|\.json\(\)|searchParams|params\[|query\[|body\[|formData\(|input\(""") ASSIGN = re.compile(r"""^\s*(?:const |let |var )?\s*\{?\s*([A-Za-z_]\w*(?:\s*,\s*[A-Za-z_]\w*)*)\s*\}?(?:\s*:\s*[\w<>\[\]|. ]{1,60}?)?\s*=(?!=)(.+)$""") def lang_of(p): return "py" if p.suffix in PY else "js" if p.suffix in JS else None def walk(root): for dirpath, dirs, files in os.walk(root): dirs[:] = [d for d in dirs if d not in SKIP_DIRS and not d.startswith(".")] for f in files: p = Path(dirpath) / f if lang_of(p) and p.stat().st_size < MAX_BYTES and not f.endswith((".min.js", ".d.ts")): yield p def tainted_names(lines): """Variable names assigned from something request-shaped, plus handler parameters. Crude on purpose.""" names = set() for ln in lines: m = ASSIGN.match(ln) if m and INPUT_SRC.search(m.group(2)): for n in re.split(r"[,\s]+", m.group(1)): if n and n.isidentifier() and n not in {"const", "let", "var", "self"}: names.add(n) return names def block_after(lines, i, lang): """Rough handler body: for Python the indented block of the next def, for JS the next 30 lines.""" if lang == "js": return lines[i:i + 30] j = i while j < len(lines) and not re.match(r"\s*(async\s+)?def\s", lines[j]): j += 1 if j >= len(lines): return lines[i:i + 30] indent = len(lines[j]) - len(lines[j].lstrip()) body = [lines[j]] for ln in lines[j + 1:]: if ln.strip() and len(ln) - len(ln.lstrip()) <= indent: break body.append(ln) return lines[i:j] + body def handler_params(body): for ln in body: m = re.match(r"\s*(?:async\s+)?def\s+\w+\(([^)]*)\)", ln) if m: return {a.split(":")[0].split("=")[0].strip() for a in m.group(1).split(",")} - {"self", "request", "req", "ctx", ""} return set() def scan(root, only=None): root = Path(root).resolve() hits = defaultdict(list) routes, notes = [], [] project_text_flags = defaultdict(set) compiled = [(c, l, re.compile(rx, re.I if c in {"weak-random", "secrets-in-logs", "timing-compare"} else 0), h) for c, l, rx, h in RULES] for p in walk(root): rel = str(p.relative_to(root)) lang = lang_of(p) try: text = p.read_text(errors="replace") except OSError: continue lines = text.splitlines() taint = tainted_names(lines) file_level_auth = bool(re.search(r"\.use\([^)]*(auth|Auth|session|requireUser|clerk)|before_request|middleware", text)) if "BaseHTTPRequestHandler" in text or "createServer(" in text: notes.append(f"{rel}: hand-rolled HTTP routing. Check every branch for auth, there is no framework doing it.") if RATELIMIT.search(text): project_text_flags["ratelimit"].add(rel) for i, ln in enumerate(lines): s = ln.strip() if len(ln) > 2000 or s.startswith(("#", "//", "*", "/*")): continue # routes for rx, kind in ((ROUTE_PY, "py"), (ROUTE_DJANGO, "django"), (ROUTE_JS, "js"), (ROUTE_NEXT, "next")): if (kind in {"py", "django"} and lang != "py") or (kind in {"js", "next"} and lang != "js"): continue m = rx.search(ln) if not m: continue if kind == "next": path = "/" + "/".join(x for x in p.parent.relative_to(root).parts if x not in {"app", "src", "pages"}) + f" [{m.group(3)}]" elif kind == "js" and m.group(2) == "use": continue else: path = m.group(2) if kind in {"py", "django"} else m.group(3) body = block_after(lines, i, "js" if kind in {"js", "next"} else "py") body_text = "\n".join(body) taint |= handler_params(body) has_auth = bool(AUTH_MARK.search(body_text)) or file_level_auth has_param = bool(PARAM.search(re.sub(r" \[[A-Z]+\]$", "", path))) flags = [] if not has_auth: flags.append("NO-AUTH-SEEN") if has_param and not OWNER_MARK.search(body_text): flags.append("IDOR?") if re.search(r"webhook|/hook", path, re.I) and not WEBHOOK_VERIFY.search(body_text): flags.append("WEBHOOK-UNVERIFIED?") if SENSITIVE_ROUTE.search(path): flags.append("ABUSE-TARGET") if LLM_IN_BODY.search(body_text) and not has_auth: flags.append("LLM-COST") routes.append({"file": rel, "line": i + 1, "route": path, "flags": flags}) # rules for cat, l, rx, hint in compiled: if only and cat not in only: continue if l != "both" and l != lang: continue if rx.search(ln): if cat == "path-traversal" and not (taint and re.search(r"\b(" + "|".join(map(re.escape, taint)) + r")\b", ln)) and not INPUT_SRC.search(ln): continue # file ops are everywhere; only keep ones touching input used = sorted(n for n in taint if re.search(r"\b" + re.escape(n) + r"\b", ln)) if taint else [] tag = "[input] " if used or INPUT_SRC.search(ln) else "" hits[cat].append({"file": rel, "line": i + 1, "code": s[:160], "input": bool(tag), "hint": hint}) if cat in {"llm-call", "llm-tools"}: project_text_flags[cat].add(rel) if cat in {"command-exec", "outbound-http", "code-eval"}: project_text_flags["dangerous-" + cat].add(rel) if routes and not project_text_flags["ratelimit"] and any("ABUSE-TARGET" in r["flags"] for r in routes): notes.append("No rate limiting library or code found, and there are login/signup/reset style routes.") if project_text_flags["llm-tools"]: danger = project_text_flags["dangerous-outbound-http"] | project_text_flags["dangerous-command-exec"] | project_text_flags["dangerous-code-eval"] if danger: notes.append("LLM tools exist and the code also makes outbound requests or runs commands (" + ", ".join(sorted(danger)[:5]) + "). Check whether a tool can reach these with model-chosen arguments: " "that is the prompt injection to exfiltration or code execution path.") return root, routes, hits, notes ORDER = ["sql-format", "command-exec", "code-eval", "deserialise", "path-traversal", "outbound-http", "template-xss", "jwt", "mass-assignment", "open-redirect", "cors", "csrf-cookie", "timing-compare", "weak-random", "weak-crypto", "client-secret", "secrets-in-logs", "error-leak", "upload", "debug-tls", "llm-tools", "llm-call"] def main(): args = [a for a in sys.argv[1:] if not a.startswith("--")] only = None if "--only" in sys.argv: only = set(sys.argv[sys.argv.index("--only") + 1].split(",")) args = [a for a in args if a != sys.argv[sys.argv.index("--only") + 1]] root, routes, hits, notes = scan(args[0] if args else ".", only) if "--json" in sys.argv: print(json.dumps({"routes": routes, "hits": hits, "notes": notes}, indent=2)) return print(f"Attack surface of {root.name}/ (heuristic leads: read each one before calling it a finding)\n") print(f"ROUTES ({len(routes)})") for r in routes: print(f" {r['file']}:{r['line']} {r['route']} {' '.join(r['flags'])}") if routes: print(" flags: NO-AUTH-SEEN = no auth call near the handler; IDOR? = takes an id but no owner/tenant check seen;") print(" WEBHOOK-UNVERIFIED? = no signature check seen; ABUSE-TARGET = needs rate limiting;") print(" LLM-COST = anyone can make you pay for model calls (needs auth, rate limit and a spend cap)") for cat in ORDER: if not hits.get(cat): continue items = sorted(hits[cat], key=lambda h: (not h["input"], h["file"], h["line"])) print(f"\n{cat.upper()} ({len(items)}) {items[0]['hint']}") for h in items[:40]: print(f" {'[input] ' if h['input'] else ''}{h['file']}:{h['line']} {h['code']}") if len(items) > 40: print(f" ... {len(items) - 40} more (use --json)") if notes: print("\nNOTES") for n in notes: print(" - " + n) if __name__ == "__main__": main()