File size: 7,852 Bytes
8145bee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | """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"<input\b(?:[^'\"<>]|'[^']*'|\"[^\"]*\")*>", re.IGNORECASE | re.DOTALL)
_ATTRIBUTE = re.compile(
r"(?P<name>[^\s=/>]+)(?:\s*=\s*(?:\"(?P<double>[^\"]*)\"|'(?P<single>[^']*)'|(?P<bare>[^\s>]+)))?"
)
_VALUE = re.compile(
r"(?P<prefix>(?:\b(?:value|defaultvalue|data-value|aria-valuetext|aria-valuenow)\b|"
r"[\"'](?:value|defaultvalue|data-value|aria-valuetext|aria-valuenow)[\"'])\s*[:=]\s*)"
r"(?:\"(?P<double>(?:\\.|[^\"\\])*)\"|'(?P<single>(?:\\.|[^'\\])*)'|(?P<bare>[^\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"<input\b", line, re.IGNORECASE):
return line
# Field labels decide sensitivity before value text can affect the decision.
matches = list(_VALUE.finditer(line))
delimiter = _descriptor(line)
descriptor = line if matches or delimiter is None else line[:delimiter]
descriptor = _VALUE.sub(lambda match: match.group("prefix") + _HIDDEN, descriptor)
if not (_FIELD.search(descriptor) and _sensitive(descriptor)):
return line
spans = []
for match in matches:
field = next(key for key in ("double", "single", "bare") if match.group(key) is not None)
spans.append(match.span(field))
if spans:
return _replace_ranges(line, spans)
if delimiter is not None:
suffix = line[delimiter + 1:]
if suffix.strip():
leading = len(suffix) - len(suffix.lstrip())
ending = "\r\n" if line.endswith("\r\n") else "\n" if line.endswith("\n") else ""
return line[:delimiter + 1 + leading] + _HIDDEN + ending
return line
def _labels(node):
if isinstance(node, list):
if len(node) == 2 and isinstance(node[0], str) and node[0].lower() in _LABEL_KEYS and isinstance(node[1], str):
return [node[1]]
return [label for item in node for label in _labels(item)]
if not isinstance(node, dict):
return []
labels = [value for key, value in node.items() if key.lower() in _LABEL_KEYS and isinstance(value, str)]
normalized = {key.lower(): value for key, value in node.items()}
if isinstance(normalized.get("name"), str) and normalized["name"].lower() in _LABEL_KEYS and isinstance(normalized.get("value"), str):
labels.append(normalized["value"])
for key, value in node.items():
if key.lower() in _CONTAINERS:
labels.extend(_labels(value))
return labels
def _json_snapshot(node, inherited=False):
if isinstance(node, list):
if inherited and len(node) == 2 and isinstance(node[0], str) and node[0].lower() in _VALUE_KEYS:
return [node[0], _HIDDEN]
return [_json_snapshot(item, inherited) for item in node]
if isinstance(node, str):
return _snapshot_text(node)
if not isinstance(node, dict):
return node
sensitive = inherited or _sensitive(" ".join(_labels(node)))
return {
key: (_HIDDEN if sensitive and key.lower() in _VALUE_KEYS
else _json_snapshot(value, sensitive and key.lower() in _CONTAINERS))
for key, value in node.items()
}
def _snapshot_text(text):
text = _INPUT.sub(_html_input, text)
lines, block_indent = [], None
for line in text.splitlines(keepends=True):
indent = len(line) - len(line.lstrip())
if block_indent is not None and line.strip() and indent > 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)
|