feat(guardrails): exposure domain (EXP-*): console data, source maps (config, public dir, large files), hydrated state, shipped comments, data attributes, error bodies in routes, debug mode, wide responses
This commit is contained in:
1 parent
7500a618fe
commit
d46201c561
6 files changed
+380
-33
No files matched your search
@@ -72,12 +72,19 @@ def blank_markup_comments(text):
|
||||
return _blank(MARKUP_COMMENT, text)
|
||||
|
||||
|
||||
def _is_scannable(path):
|
||||
def _candidate(path):
|
||||
"""A regular, non-symlink, non-binary file the walker may list."""
|
||||
if path.name in SKIP_FILES or path.suffix.lower() in BINARY_EXTS:
|
||||
return False
|
||||
try:
|
||||
return (path.is_file() and not path.is_symlink()
|
||||
and path.stat().st_size <= MAX_BYTES)
|
||||
return path.is_file() and not path.is_symlink()
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
def _small(path):
|
||||
try:
|
||||
return path.stat().st_size <= MAX_BYTES
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
@@ -97,8 +104,8 @@ def _kept_dirs(root, base, dirs, prefixes):
|
||||
def _kept_files(root, base, names, prefixes):
|
||||
for name in names:
|
||||
path = Path(base) / name
|
||||
if _is_scannable(path) and not _excluded(root, path, prefixes):
|
||||
yield path
|
||||
if _candidate(path) and not _excluded(root, path, prefixes):
|
||||
yield path, _small(path)
|
||||
|
||||
|
||||
def _walk(root, prefixes):
|
||||
@@ -113,7 +120,9 @@ class Project:
|
||||
def __init__(self, root, exclude=()):
|
||||
self.root = Path(root).resolve()
|
||||
prefixes = [e.strip("/") for e in exclude if e.strip("/")]
|
||||
self.files = sorted(_walk(self.root, prefixes))
|
||||
listed = sorted(_walk(self.root, prefixes))
|
||||
self.files = [p for p, small in listed if small]
|
||||
self.large = [p for p, small in listed if not small]
|
||||
self._text = {}
|
||||
|
||||
def rel(self, path):
|
||||
@@ -202,3 +211,27 @@ def dedupe_lines(findings):
|
||||
seen.add((finding.path, finding.line))
|
||||
out.append(finding)
|
||||
return out
|
||||
|
||||
|
||||
SERVER_SEGMENTS = {
|
||||
"functions", "api", "server", "scripts", "tests", "test", "migrations",
|
||||
"seed", ".github", "supabase", "tools",
|
||||
}
|
||||
CLIENT_SEGMENTS = {
|
||||
"components", "public", "static", "client", "browser", "www", "islands",
|
||||
}
|
||||
CLIENT_EXTS = (".vue", ".svelte", ".html", ".htm")
|
||||
CLIENT_SCRIPT_EXTS = (".ts", ".tsx", ".js", ".jsx", ".mjs")
|
||||
|
||||
|
||||
def client_reachable(project, path):
|
||||
"""Heuristic: this file's text ends up in a browser bundle or page."""
|
||||
segments = set(project.segments(path))
|
||||
if segments & SERVER_SEGMENTS:
|
||||
return False
|
||||
if path.suffix.lower() in CLIENT_EXTS:
|
||||
return True
|
||||
if path.suffix.lower() not in CLIENT_SCRIPT_EXTS:
|
||||
return False
|
||||
head = project.text(path)[:300]
|
||||
return bool(segments & CLIENT_SEGMENTS) or "use client" in head
|
||||
@@ -0,0 +1,248 @@
|
||||
"""Exposure checks: what the browser can see that it should not."""
|
||||
import re
|
||||
|
||||
from . import passwords
|
||||
from .apis import ENV_NAME, ENV_SAMPLE, ignore_patterns, is_ignored_env
|
||||
from .apis import route_files
|
||||
from .common import Rule, client_reachable, code_files, dedupe_lines, hits
|
||||
|
||||
DOMAIN = "exposure"
|
||||
MARKUP_EXTS = (".html", ".htm", ".astro", ".php", ".ejs", ".hbs", ".twig",
|
||||
".njk", ".liquid")
|
||||
ATTR_EXTS = MARKUP_EXTS + (".jsx", ".tsx", ".vue", ".svelte")
|
||||
DEV_TOKEN = re.compile(
|
||||
r"(?:^|[._-])(?:dev|development|local|test|testing)(?:[._-]|$)", re.I)
|
||||
CONSOLE = re.compile(
|
||||
r"\bconsole\.(?:log|info|debug|dir|table)\s*\(([^\n]{0,300})")
|
||||
DATA_NAMES = re.compile(
|
||||
r"\b(?:users?|profile|session|token|account|customer|order|payment"
|
||||
r"|response|res|data|result|rows|body|headers|props|state|error|err"
|
||||
r"|json|payload|record|items?)\b", re.I)
|
||||
SENSITIVE = (
|
||||
r"(?:token|accessToken|refreshToken|secret|apiKey|api_key|password"
|
||||
r"|passwordHash|password_hash|serviceRole|service_role|privateKey"
|
||||
r"|private_key|ssn|iban|stripeSecret|clientSecret)")
|
||||
LOADER = re.compile(
|
||||
r"getServerSideProps|getStaticProps"
|
||||
r"|export\s+(?:async\s+)?function\s+(?:loader|load)\b"
|
||||
r"|export\s+const\s+(?:loader|load)\b|window\.__\w+__\s*=")
|
||||
TYPEISH = (r"(?:string|number|boolean|null|undefined|unknown|any|never"
|
||||
r"|[A-Z][\w<>\[\]|]*)")
|
||||
HYDRATED = re.compile(
|
||||
rf"\b{SENSITIVE}\s*:(?!\s*{TYPEISH}\s*(?:[;,}}]|$))", re.M)
|
||||
COMMENT = re.compile(r"<!--(.*?)-->", re.S)
|
||||
COMMENT_WORDS = re.compile(
|
||||
r"\b(?:todo|fixme|hack|xxx|debug|password|passwd|secret|token"
|
||||
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)
|
||||
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)
|
||||
SAFE_NAME = re.compile(r"csrf|xsrf|authenticity|nonce", re.I)
|
||||
SENDING = (
|
||||
r"(?:\bres\.(?:status|json|send|end|write)\b"
|
||||
r"|\breply\.(?:send|code|status)\b|\bctx\.body\b|\bResponse\("
|
||||
r"|Response\.json\(|\bjsonify\(|\bJSONResponse\(|\bHTTPException\("
|
||||
r"|\bdetail\s*=|\becho\b)")
|
||||
ERR = r"(?:err|error|e|exc|exception)"
|
||||
ERROR_STACK = re.compile(
|
||||
rf"{SENDING}[^\n]{{0,300}}?(?:\b{ERR}\.stack\b|traceback\.format_exc"
|
||||
rf"|getTraceAsString\(\))"
|
||||
rf"|\b(?:json|send)\s*\(\s*\{{[^}}\n]{{0,200}}\berror\s*:\s*{ERR}\s*[,}}]")
|
||||
ERROR_MESSAGE = re.compile(
|
||||
rf"{SENDING}[^\n]{{0,300}}?(?:\b{ERR}\.message\b|\bstr\({ERR}\)"
|
||||
rf"|\brepr\({ERR}\)|->getMessage\(\))")
|
||||
SETTINGS_NAME = re.compile(r"^(?:settings|config)", re.I)
|
||||
DEBUG_SETTINGS = re.compile(r"^DEBUG\s*=\s*True\b", re.M)
|
||||
DEBUG_ENV = re.compile(
|
||||
r"^\s*(?:APP_DEBUG\s*=\s*true|FLASK_DEBUG\s*=\s*1)\b", re.M | re.I)
|
||||
DEBUG_CODE = re.compile(
|
||||
r"app\.debug\s*=\s*True|app\.run\([^)\n]{0,200}debug\s*=\s*True")
|
||||
DEBUG_HANDLER = re.compile(
|
||||
r"^(?![^\n]*(?:development|NODE_ENV))[^\n]*app\.use\(\s*errorhandler\(\)",
|
||||
re.M)
|
||||
DEBUG_RAILS = re.compile(r"consider_all_requests_local\s*=\s*true")
|
||||
NEXT_MAPS = re.compile(r"productionBrowserSourceMaps\s*:\s*true")
|
||||
VITE_MAPS = re.compile(r"\bsourcemap\s*:\s*(?:true|['\"]inline['\"])")
|
||||
WEBPACK_DEVTOOL = re.compile(
|
||||
r"\bdevtool\s*:\s*['\"](?:source-map|eval[\w-]*|inline[\w-]*"
|
||||
r"|cheap[\w-]*)['\"]")
|
||||
ENV_MAPS = re.compile(r"^\s*GENERATE_SOURCEMAP\s*=\s*true\b", re.M | re.I)
|
||||
ANGULAR_MAPS = re.compile(r"\"sourceMap\"\s*:\s*true")
|
||||
PUBLIC_DIRS = {"public", "static", "assets", "www"}
|
||||
VENDORED = re.compile(r"\.min\.|vendor|bootstrap|jquery", re.I)
|
||||
USERS = (r"(?:users|profiles|accounts|customers|members|employees"
|
||||
r"|patients|clients)")
|
||||
WIDE = re.compile(
|
||||
rf"\.from\(\s*['\"]{USERS}['\"]\s*\)\s*\.select\(\s*['\"]\*['\"]\s*\)"
|
||||
rf"|select\s+\*\s+from\s+{USERS}\b"
|
||||
r"|\b(?:user|profile|account|customer|member)\.(?:findUnique|findFirst"
|
||||
r"|findMany)\(\s*\{(?![^\n]{0,300}\b(?:select|omit)\s*:)[^\n]{0,300}\}"
|
||||
r"\s*\)", re.I)
|
||||
|
||||
|
||||
def applies(project):
|
||||
return bool(project.with_ext(".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs",
|
||||
".py", ".php", ".rb", ".vue", ".svelte",
|
||||
".astro", *MARKUP_EXTS))
|
||||
|
||||
|
||||
def _named(project, prefix):
|
||||
return [p for p in project.files if p.name.startswith(prefix)]
|
||||
|
||||
|
||||
def _client(project, path):
|
||||
"""client_reachable, plus Next pages and a root App component."""
|
||||
if client_reachable(project, path):
|
||||
return True
|
||||
segments = project.segments(path)
|
||||
return (path.suffix.lower() in (".tsx", ".jsx") and "api" not in segments
|
||||
and ("pages" in segments or path.stem == "App"))
|
||||
|
||||
|
||||
def _console_data(project, files):
|
||||
rule = Rule(DOMAIN, "EXP-CONSOLE-DATA", "WARN")
|
||||
out = []
|
||||
for path in (p for p in files if _client(project, p)):
|
||||
for m in CONSOLE.finditer(project.text(path)):
|
||||
if passwords.LOGGED_NAME.search(m.group(1)):
|
||||
continue
|
||||
if DATA_NAMES.search(passwords.log_args(m.group(1))):
|
||||
out.append(project.found(
|
||||
rule, path, m.start(),
|
||||
"data logged to the browser console"))
|
||||
return out
|
||||
|
||||
|
||||
def _map_config(project):
|
||||
fail = Rule(DOMAIN, "EXP-SOURCEMAP-PUBLIC", "FAIL")
|
||||
warn = Rule(DOMAIN, "EXP-SOURCEMAP-PUBLIC", "WARN")
|
||||
out = hits(project, _named(project, "next.config"), NEXT_MAPS, fail,
|
||||
"production source maps enabled")
|
||||
out += hits(project, _named(project, "vite.config"), VITE_MAPS, fail,
|
||||
"build source maps enabled: use 'hidden' or false")
|
||||
prod = _named(project, "webpack.prod")
|
||||
other = [p for p in _named(project, "webpack") if p not in prod]
|
||||
out += hits(project, prod, WEBPACK_DEVTOOL, fail,
|
||||
"devtool emits readable source maps in production")
|
||||
out += hits(project, other, WEBPACK_DEVTOOL, warn,
|
||||
"devtool emits readable source maps: confirm not in prod")
|
||||
return out + hits(project, project.named("angular.json"), ANGULAR_MAPS,
|
||||
warn, "sourceMap true: confirm not the production build")
|
||||
|
||||
|
||||
def _env_files(project):
|
||||
"""Committed env files that are not samples and not dev/local/test."""
|
||||
patterns = ignore_patterns(project)
|
||||
return [p for p in project.files if ENV_NAME.fullmatch(p.name)
|
||||
and not p.name.endswith(ENV_SAMPLE)
|
||||
and not DEV_TOKEN.search(p.name[4:] or "x")
|
||||
and not is_ignored_env(p, patterns)]
|
||||
|
||||
|
||||
def _public_map(project, path):
|
||||
return (set(project.segments(path)) & PUBLIC_DIRS
|
||||
and not VENDORED.search(project.rel(path)))
|
||||
|
||||
|
||||
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)]
|
||||
return out + [project.found(rule, p, 0, "source map in a public directory")
|
||||
for p in small + large]
|
||||
|
||||
|
||||
def _hydrated(project, files):
|
||||
loaders = [p for p in files if LOADER.search(project.text(p))]
|
||||
return hits(project, loaders, HYDRATED,
|
||||
Rule(DOMAIN, "EXP-HYDRATED-SENSITIVE", "WARN"),
|
||||
"sensitive key in server-to-client state")
|
||||
|
||||
|
||||
def _markup_comments(project):
|
||||
rule = Rule(DOMAIN, "EXP-MARKUP-COMMENT", "WARN")
|
||||
return [project.found(rule, path, m.start(),
|
||||
"HTML comment shipped to the browser carries "
|
||||
"internal notes")
|
||||
for path in project.with_ext(*MARKUP_EXTS)
|
||||
for m in COMMENT.finditer(project.text(path))
|
||||
if COMMENT_WORDS.search(m.group(1))]
|
||||
|
||||
|
||||
def _hidden_sensitive(tag):
|
||||
if not re.search(r"type\s*=\s*[\"']?hidden\b", tag, re.I):
|
||||
return False
|
||||
name = re.search(r"name\s*=\s*[\"']([^\"']+)", tag, re.I)
|
||||
return bool(name and SENSITIVE_NAME.search(name.group(1))
|
||||
and not SAFE_NAME.search(name.group(1)))
|
||||
|
||||
|
||||
def _data_attrs(project):
|
||||
rule = Rule(DOMAIN, "EXP-DATA-ATTR-SENSITIVE", "WARN")
|
||||
out = []
|
||||
for path in project.with_ext(*ATTR_EXTS):
|
||||
text = project.text(path)
|
||||
out += [project.found(rule, path, m.start(),
|
||||
"sensitive value in a data-* attribute")
|
||||
for m in DATA_ATTR.finditer(text)
|
||||
if not SAFE_NAME.search(m.group(1))]
|
||||
out += [project.found(rule, path, m.start(),
|
||||
"sensitive value in a hidden input")
|
||||
for m in HIDDEN_INPUT.finditer(text)
|
||||
if _hidden_sensitive(m.group(0))]
|
||||
return out
|
||||
|
||||
|
||||
def _errors(project, files):
|
||||
server = [p for p in files if not client_reachable(project, p)]
|
||||
stack = hits(project, server, ERROR_STACK,
|
||||
Rule(DOMAIN, "EXP-ERROR-STACK", "FAIL"),
|
||||
"stack trace or error object sent to the client")
|
||||
message = hits(project, server, ERROR_MESSAGE,
|
||||
Rule(DOMAIN, "EXP-ERROR-MESSAGE", "WARN"),
|
||||
"raw exception message sent to the client")
|
||||
return dedupe_lines(stack + message)
|
||||
|
||||
|
||||
def _settings_modules(project, files):
|
||||
return [p for p in files if p.suffix == ".py"
|
||||
and (SETTINGS_NAME.match(p.name)
|
||||
or "settings" in project.segments(p))
|
||||
and not DEV_TOKEN.search(p.stem)]
|
||||
|
||||
|
||||
def _debug(project, files):
|
||||
rule = Rule(DOMAIN, "EXP-DEBUG-ON", "FAIL")
|
||||
out = hits(project, _settings_modules(project, files), DEBUG_SETTINGS,
|
||||
rule, "DEBUG = True in a settings module")
|
||||
out += hits(project, _env_files(project), DEBUG_ENV, rule,
|
||||
"debug mode on in a committed env file")
|
||||
out += hits(project, files, DEBUG_CODE, rule,
|
||||
"debug mode enabled in code")
|
||||
out += hits(project, files, DEBUG_HANDLER, rule,
|
||||
"dev error handler mounted without an environment guard")
|
||||
return out + hits(project, project.named("production.rb"), DEBUG_RAILS,
|
||||
rule, "full error pages enabled in production")
|
||||
|
||||
|
||||
def _response_wide(project):
|
||||
return hits(project, route_files(project), WIDE,
|
||||
Rule(DOMAIN, "EXP-RESPONSE-WIDE", "WARN"),
|
||||
"whole rows of a user table returned: select the fields")
|
||||
|
||||
|
||||
def check(project):
|
||||
files = code_files(project)
|
||||
found = _console_data(project, files) + _map_config(project)
|
||||
found += _map_files(project) + _hydrated(project, files)
|
||||
found += _markup_comments(project) + _data_attrs(project)
|
||||
found += _errors(project, files) + _debug(project, files)
|
||||
return found + _response_wide(project)
|
||||
@@ -98,7 +98,7 @@ def _weak_hash(project, files):
|
||||
]
|
||||
|
||||
|
||||
def _log_args(args):
|
||||
def log_args(args):
|
||||
"""Call arguments with string literals reduced to their interpolations."""
|
||||
return STRING.sub(
|
||||
lambda m: " ".join(re.findall(r"\{([^}]*)\}", m.group())), args)
|
||||
@@ -110,7 +110,7 @@ def _logged(project, files):
|
||||
Rule(DOMAIN, "API-PASSWORD-LOGGED", "FAIL"), path, m.start(),
|
||||
"password reaches a log: never log credentials")
|
||||
for path, m in project.matches(LOG_CALL, files)
|
||||
if LOGGED_NAME.search(_log_args(m.group(1)))
|
||||
if LOGGED_NAME.search(log_args(m.group(1)))
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,8 @@ import re
|
||||
from dataclasses import dataclass
|
||||
|
||||
from .apis import ignore_patterns, is_ignored_env
|
||||
from .common import Rule, SOURCE_EXTS, blank_comments
|
||||
from .common import CLIENT_EXTS, Rule, SOURCE_EXTS, blank_comments
|
||||
from .common import client_reachable
|
||||
|
||||
DOMAIN = "supabase"
|
||||
_NAME = r'(?:"?\w+"?\.)?"?\w+"?'
|
||||
@@ -35,15 +36,6 @@ ANON_ROLES = {"anon", "public"}
|
||||
PRIVILEGED_ROLES = {"service_role", "postgres", "supabase_admin"}
|
||||
|
||||
SERVICE_REF = r"service_role|SERVICE_ROLE|SUPABASE_SECRET_KEY|sb_secret_"
|
||||
SERVER_SEGMENTS = {
|
||||
"functions", "api", "server", "scripts", "tests", "test", "migrations",
|
||||
"seed", ".github", "supabase", "tools",
|
||||
}
|
||||
CLIENT_SEGMENTS = {
|
||||
"components", "public", "static", "client", "browser", "www", "islands",
|
||||
}
|
||||
CLIENT_EXTS = (".vue", ".svelte", ".html", ".htm")
|
||||
SCRIPT_EXTS = (".ts", ".tsx", ".js", ".jsx", ".mjs")
|
||||
JWT = r"eyJ[\w-]{8,}\.(eyJ[\w-]{8,})\.[\w-]{8,}"
|
||||
MANIFESTS = (
|
||||
"package.json", "Cargo.toml", "requirements.txt", "pyproject.toml",
|
||||
@@ -286,18 +278,6 @@ def _config(project):
|
||||
return out
|
||||
|
||||
|
||||
def _client_reachable(project, path):
|
||||
segments = set(project.segments(path))
|
||||
if segments & SERVER_SEGMENTS:
|
||||
return False
|
||||
if path.suffix.lower() in CLIENT_EXTS:
|
||||
return True
|
||||
if path.suffix.lower() not in SCRIPT_EXTS:
|
||||
return False
|
||||
head = project.text(path)[:300]
|
||||
return bool(segments & CLIENT_SEGMENTS) or "use client" in head
|
||||
|
||||
|
||||
def _client_leaks(project):
|
||||
files = project.with_ext(*SOURCE_EXTS, *CLIENT_EXTS)
|
||||
return [
|
||||
@@ -306,7 +286,7 @@ def _client_leaks(project):
|
||||
"service role or secret key referenced in client-reachable "
|
||||
"code: it bypasses RLS for anyone who reads the bundle")
|
||||
for path, m in project.matches(SERVICE_REF, files, 0)
|
||||
if _client_reachable(project, path)
|
||||
if client_reachable(project, path)
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@ See scanner.py for the modes and exit codes.
|
||||
import sys
|
||||
|
||||
import scanner
|
||||
from checks import a11y, apis, functions, hosting, injection, messaging, supabase
|
||||
from checks import (
|
||||
a11y, apis, exposure, functions, hosting, injection, messaging, supabase,
|
||||
)
|
||||
|
||||
CATALOGUE = scanner.Catalogue(
|
||||
name="guardrails",
|
||||
@@ -18,11 +20,12 @@ CATALOGUE = scanner.Catalogue(
|
||||
"messaging": messaging,
|
||||
"functions": functions,
|
||||
"injection": injection,
|
||||
"exposure": exposure,
|
||||
},
|
||||
rule_domains={
|
||||
"API": "api", "HOST": "hosting", "A11Y": "a11y", "SB": "supabase",
|
||||
"MSG": "messaging", "FN": "functions",
|
||||
"INJ": "injection",
|
||||
"INJ": "injection", "EXP": "exposure",
|
||||
},
|
||||
manual_rules=frozenset({
|
||||
"HOST-WAF-DDOS", "HOST-SPEND-CAP", "SB-RLS-LIVE",
|
||||
|
||||
Reference in new issue
Block a user