"""Hide credentials in DOM and accessibility snapshot strings.""" import html import json import re from urllib.parse import quote, quote_plus _HIDDEN = "[hidden]" _INPUT = re.compile(r"]|'[^']*'|\"[^\"]*\")*>", re.IGNORECASE | re.DOTALL) _ATTRIBUTE = re.compile( r"(?P[^\s=/>]+)(?:\s*=\s*(?:\"(?P[^\"]*)\"|'(?P[^']*)'|(?P[^\s>]+)))?" ) _VALUE = re.compile( r"(?P(?:\b(?:value|defaultvalue|data-value|aria-valuetext|aria-valuenow)\b|" r"[\"'](?:value|defaultvalue|data-value|aria-valuetext|aria-valuenow)[\"'])\s*[:=]\s*)" r"(?:\"(?P(?:\\.|[^\"\\])*)\"|'(?P(?:\\.|[^'\\])*)'|(?P[^\r\n\]}]*))", re.IGNORECASE, ) _PASSWORD = re.compile(r"password", re.IGNORECASE) _ONE_TIME = re.compile( r"\b(?:one[\s_-]*time[\s_-]*(?:code|password)|otp\d*|totp\d*|passcode|" r"(?:verification|security|authentication|auth|login|2fa|mfa)[\s_-]*code|code)\b", re.IGNORECASE, ) _FIELD = re.compile(r"\b(?:input|textbox|text[\s_-]*field|edit(?:able)?|password)\b", re.IGNORECASE) _VALUE_KEYS = frozenset({"value", "defaultvalue", "data-value", "aria-valuetext", "aria-valuenow", "text"}) _CONTAINERS = frozenset({"attributes", "properties", "props", "state"}) _LABEL_KEYS = frozenset({"type", "autocomplete", "name", "id", "label", "aria-label", "placeholder", "role"}) def _sensitive(label): label = re.sub(r"([a-z])([A-Z])", r"\1 \2", label).replace("_", " ") return bool(_PASSWORD.search(label) or _ONE_TIME.search(label)) def _replace_ranges(text, spans): merged = [] for start, end in sorted(spans): if merged and start < merged[-1][1]: merged[-1] = (merged[-1][0], max(end, merged[-1][1])) else: merged.append((start, end)) for start, end in reversed(merged): text = text[:start] + _HIDDEN + text[end:] return text def _html_input(match): tag = match.group() attributes = list(_ATTRIBUTE.finditer(tag, 6, len(tag) - 1)) labels, spans = [], [] for attribute in attributes: name = attribute.group("name").lower() field = next((key for key in ("double", "single", "bare") if attribute.group(key) is not None), None) if field is None: continue value = attribute.group(field) if name in _LABEL_KEYS: labels.append(html.unescape(value)) if name in _VALUE_KEYS: spans.append(attribute.span(field)) return _replace_ranges(tag, spans) if _sensitive(" ".join(labels)) else tag def _descriptor(line): quote, bracket = None, 0 escaped = False for index, char in enumerate(line): if escaped: escaped = False elif char == "\\" and quote is not None: escaped = True elif quote is not None: if char == quote: quote = None elif char in ("'", '"'): quote = char elif char == "[": bracket += 1 elif char == "]": bracket = max(0, bracket - 1) elif bracket == 0 and (char == ":" or (char == "=" and line[:index].rstrip().endswith(('"', "'", "]")))): return index return None def _snapshot_line(line): if re.search(r" block_indent: ending = "\r\n" if line.endswith("\r\n") else "\n" if line.endswith("\n") else "" lines.append(line[:indent] + _HIDDEN + ending) continue if line.strip(): block_indent = None redacted = _snapshot_line(line) if redacted != line and re.search(r":\s*[|>][-+]?\s*$", line): block_indent = indent lines.append(redacted) return "".join(lines) def scrub_for_model(snapshot): """Hide password and one-time-code values without a supplied secret list.""" if not isinstance(snapshot, str): raise TypeError("Snapshot must be text.") if snapshot.lstrip().startswith(("{", "[")): try: parsed = json.loads(snapshot) except (ValueError, RecursionError): pass else: return json.dumps(_json_snapshot(parsed), ensure_ascii=False) return _snapshot_text(snapshot) def scrub(text, secrets=()): """Hide snapshot credential values and all overlapping occurrences of supplied secrets.""" text = scrub_for_model(text) if secrets is None: return text if isinstance(secrets, str): secrets = (secrets,) spans = [] for secret in secrets: if not isinstance(secret, str): raise TypeError("Secrets must be text.") if not secret: continue variants = {secret, json.dumps(secret, ensure_ascii=False)[1:-1], html.escape(secret, quote=True), quote(secret, safe=""), quote_plus(secret)} for variant in variants: start = text.find(variant) while start >= 0: spans.append((start, start + len(variant))) start = text.find(variant, start + 1) return _replace_ranges(text, spans)