Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download platform/core/login_redact.py from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 7.85 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/platform/core/login_redact.py
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/platform/core/login_redact.py
-
curl -L -o login_redact.py https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/platform/core/login_redact.py
7.85 kB
| """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) | |