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
@@ -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