Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: skip log redaction rules that cannot match a line (#16356)

Daniel Han633231e7d9dd
diff --git a/studio/backend/tests/test_log_redaction_gates.py b/studio/backend/tests/test_log_redaction_gates.py
new file mode 100644
index 00000000000..dcdfc9b239d
--- /dev/null
+++ b/studio/backend/tests/test_log_redaction_gates.py
@@ -0,0 +1,247 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The literal gates in redact_log_text skip only rules that cannot match, so the output must equal running every rule."""
+
+from __future__ import annotations
+
+import random
+import re
+
+import pytest
+
+from utils import log_redaction as lr
+
+
+def _reference(text: str) -> str:
+ """Every rule, ungated, in the same order as redact_log_text."""
+ if not text:
+ return text
+ if lr._ANSI_INTRODUCER_RE.search(text):
+ text = lr._strip_ansi(text)
+ for pattern, replacement in lr._PATTERNS:
+ text = pattern.sub(replacement, text)
+ text = lr._AUTH_HEADER_RE.sub(lr._redact_shaped, text)
+ text = lr._SCHEME_RE.sub(lr._redact_shaped, text)
+ text = lr._COOKIE_RE.sub(lr._redact_cookie, text)
+ text = lr._KV_RE.sub(lr._redact_kv, text)
+ text = lr._FLAG_RE.sub(lr._redact_kv, text)
+ try:
+ from utils.native_path_leases import redact_native_paths
+ text = redact_native_paths(text)
+ except Exception:
+ pass
+ return text
+
+
+_FRAGMENTS = [
+ "hf_",
+ "hf_oauth_",
+ "sk-",
+ "sk-proj-",
+ "sk-ant-api03-",
+ "sk-or-v1-",
+ "gsk_",
+ "xai-",
+ "ghp_",
+ "gho_",
+ "ghu_",
+ "ghs_",
+ "ghr_",
+ "github_pat_",
+ "glpat-",
+ "xoxb-",
+ "xoxp-",
+ "ya29.",
+ "AIza",
+ "AKIA",
+ "ASIA",
+ "eyJ",
+ ".",
+ "://",
+ "user:pw@",
+ "@",
+ "?",
+ "&",
+ "token=",
+ "api_key=",
+ "apikey=",
+ "sig=",
+ "signature=",
+ "X-Amz-Signature=",
+ "x-amz-credential=",
+ "x-amz-security-token=",
+ "access_token=",
+ "Authorization",
+ "authorization: ",
+ "Proxy-Authorization",
+ "Bearer ",
+ "bearer ",
+ "Basic ",
+ "Digest ",
+ "Token ",
+ "Cookie: ",
+ "set-cookie=",
+ "api_key",
+ "API-KEY",
+ "x-api-key",
+ "hf_token",
+ "HF-TOKEN",
+ "refresh-token",
+ "client_secret",
+ "aws_secret_access_key",
+ "aws_session_token",
+ "wandb_token",
+ "hub_token",
+ "secretAccessKey",
+ "password",
+ "passwd",
+ "PASSWORD",
+ "secret",
+ "--hf-token",
+ "--password",
+ "--api_key",
+ "--",
+ "-",
+ ":",
+ "=",
+ '"',
+ "'",
+ "\\",
+ " ",
+ "\t",
+ ",",
+ "}",
+ "]",
+ "{",
+ "\n",
+ "\r",
+ # Characters IGNORECASE folds onto ASCII letters: lowercasing alone would miss these.
+ "K",
+ "ſ",
+ "İ",
+ "ı",
+ "paſſword",
+ "Key",
+ "toKen",
+ "é",
+ "█",
+ "\x1b[31m",
+ "\x1b]0;t\x07",
+ "\x9b1m",
+ "\x1b",
+ "0123456789",
+ "ABCDEFGHIJKLMNOPQRST",
+ "abcdefghijklmnopqrstuvwxyz0123",
+ "AbCdEfGh12",
+ "n_tokens = 4096",
+ "token_id=128009",
+ "eos_token_id",
+ "checkpoint-sk-9f8a",
+]
+# One real-shaped secret per alternative of every gated rule: a gate missing a literal leaves its sample clear.
+_SHAPED_SECRETS = [
+ "hf_abcdefghijklmnopqrstuvwx",
+ "hf_oauth_abcdefghijklmnopqrstuvwx",
+ "sk-abcdefghijklmnop1234",
+ "sk-proj-abcdefghijklmnop1234",
+ *(
+ prefix + "abcdefghijklmnop1234"
+ for prefix in (
+ "gsk_",
+ "xai-",
+ "ghp_",
+ "gho_",
+ "ghu_",
+ "ghs_",
+ "ghr_",
+ "github_pat_",
+ "glpat-",
+ "ya29.",
+ )
+ ),
+ *("xox" + c + "-abcdefghijklmnop1234" for c in "abpsr"),
+ "AIzaabcdefghijklmnopqrstuvwxyz0123456",
+ "AKIAABCDEFGHIJKLMNOP",
+ "ASIAABCDEFGHIJKLMNOP",
+ "eyJabcdefghijk.abcdefghijk.abcdef",
+ "https://h/x?sig=abcdef",
+]
+_FRAGMENTS += _SHAPED_SECRETS
+_ALNUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-./=+~"
+
+
+def _random_line(rng: random.Random) -> str:
+ parts = []
+ for _ in range(rng.randint(1, 14)):
+ roll = rng.random()
+ if roll < 0.6:
+ parts.append(rng.choice(_FRAGMENTS))
+ elif roll < 0.9:
+ parts.append("".join(rng.choice(_ALNUM) for _ in range(rng.randint(1, 40))))
+ else:
+ parts.append(chr(rng.randint(1, 0x2FFF)))
+ line = "".join(parts)
+ if rng.random() < 0.3:
+ line = "".join(c.upper() if rng.random() < 0.5 else c.lower() for c in line)
+ return line
+
+
[email protected]("seed", range(4))
+def test_gated_output_equals_every_rule(seed):
+ rng = random.Random(seed)
+ redacted = 0
+ for _ in range(20_000):
+ line = _random_line(rng)
+ expected = _reference(line)
+ assert lr.redact_log_text(line) == expected, repr(line)
+ redacted += expected != line
+ assert redacted > 2_000
+
+
+ "line",
+ [
+ "paſſword=abcdef12",
+ "api_Key=hunter22hunter",
+ "██ HF_TOKEN=hf_abcdefghijklmnopqrstuvwx",
+ "AUTHORIZATION: Bearer abcdef1234567890",
+ "Cookie: session=abcdef123",
+ "--PASSWORD hunter22",
+ "https://u:p@host/x?X-Amz-Signature=abc",
+ "line one\npassword = 'hunter22'\nCookie: a=b",
+ "plain training step 12 loss=0.91",
+ "",
+ ],
+)
+def test_edge_lines_match_reference(line):
+ assert lr.redact_log_text(line) == _reference(line)
+
+
+def test_non_ascii_key_still_redacted():
+ assert lr.redact_log_text("paſſword=abcdef12") == "paſſword=" + lr.REDACTED
+
+
+def test_every_secret_key_has_a_gate_word():
+ for key in lr._SECRET_KEYS.split("|"):
+ assert any(word in key for word in lr._SECRET_KEY_WORDS), key
+
+
+def test_every_pattern_gate_literal_is_in_its_rule():
+ assert len(lr._PATTERN_LITERALS) == len(lr._PATTERNS) == len(lr._PATTERN_GATES)
+ for literals, (pattern, _) in zip(lr._PATTERN_LITERALS, lr._PATTERNS):
+ if literals is None:
+ continue
+ for literal in literals:
+ assert literal in pattern.pattern or literal.replace(".", r"\.") in pattern.pattern, (
+ literal,
+ pattern.pattern,
+ )
+ # A case-insensitive rule could match the literal in another case, which the plain substring gate misses.
+ assert not pattern.flags & re.IGNORECASE or literal.upper() == literal.lower(), literal
+
+
[email protected]("secret", _SHAPED_SECRETS)
+def test_every_gated_alternative_is_redacted(secret):
+ line = f"log {secret} end"
+ assert lr.redact_log_text(line) == _reference(line) != line
diff --git a/studio/backend/utils/log_redaction.py b/studio/backend/utils/log_redaction.py
index c8240078b8e..2778bfc0751 100644
--- a/studio/backend/utils/log_redaction.py
+++ b/studio/backend/utils/log_redaction.py
@@ -207,6 +207,59 @@ def _redact_cookie(match: re.Match[str]) -> str:
return f"{match.group('key')}{match.group('sep')}{REDACTED}{tail}"
+# Per _PATTERNS rule, literals every match contains (None = always run). Those rules are case-sensitive, so substring tests are exact.
+_PATTERN_LITERALS: tuple[tuple[str, ...] | None, ...] = (
+ ("hf_",),
+ ("sk-",),
+ (
+ "gsk_",
+ "xai-",
+ "ghp_",
+ "gho_",
+ "ghu_",
+ "ghs_",
+ "ghr_",
+ "github_pat_",
+ "glpat-",
+ "xox",
+ "ya29.",
+ ),
+ ("AIza",),
+ ("AKIA", "ASIA"),
+ ("eyJ",),
+ None,
+ ("=",),
+)
+_PATTERN_GATES: tuple[re.Pattern[str] | None, ...] = tuple(
+ None if literals is None else re.compile("|".join(re.escape(literal) for literal in literals))
+ for literals in _PATTERN_LITERALS
+)
+if len(_PATTERN_GATES) != len(_PATTERNS):
+ raise RuntimeError("every _PATTERNS rule needs an entry in _PATTERN_GATES")
+
+_SECRET_KEY_WORDS = ("authorization", "key", "token", "secret", "pass")
+if not all(any(word in key for word in _SECRET_KEY_WORDS) for key in _SECRET_KEYS.split("|")):
+ raise RuntimeError("every _SECRET_KEYS alternative needs a word in _SECRET_KEY_WORDS")
+# Off ASCII, IGNORECASE folds e.g. the Kelvin sign onto "k", so the engine decides.
+_FOLDED_GATE_RES = {
+ words: re.compile("(?i)" + "|".join(words))
+ for words in (("authorization",), ("bearer",), ("cookie",), _SECRET_KEY_WORDS)
+}
+
+
+def _has_folded(text: str, lowered: str | None, words: tuple[str, ...]) -> bool:
+ if lowered is None:
+ return _FOLDED_GATE_RES[words].search(text) is not None
+ for word in words:
+ if word in lowered:
+ return True
+ return False
+
+
+def _lowered(text: str) -> str | None:
+ return text.lower() if text.isascii() else None
+
+
def redact_log_text(text: str) -> str:
"""Mask credentials. Idempotent, and a no-op on ordinary log content."""
if not text:
@@ -214,14 +267,25 @@ def redact_log_text(text: str) -> str:
# Nothing anchored below survives an escape between a key and its value, so strip first, guarded by one introducer scan: ordinary content is untouched.
if _ANSI_INTRODUCER_RE.search(text):
text = _strip_ansi(text)
- for pattern, replacement in _PATTERNS:
- text = pattern.sub(replacement, text)
+ for (pattern, replacement), gate in zip(_PATTERNS, _PATTERN_GATES):
+ if gate is None or gate.search(text) is not None:
+ text = pattern.sub(replacement, text)
+ lowered = _lowered(text)
# Before the key/value rules: _KV_RE captures "Basic" from "Authorization: Basic dXNlcjpwdw==", masking the scheme and leaving the credential clear.
- text = _AUTH_HEADER_RE.sub(_redact_shaped, text)
- text = _SCHEME_RE.sub(_redact_shaped, text)
- text = _COOKIE_RE.sub(_redact_cookie, text)
- text = _KV_RE.sub(_redact_kv, text)
- text = _FLAG_RE.sub(_redact_kv, text)
+ if _has_folded(text, lowered, ("authorization",)):
+ text = _AUTH_HEADER_RE.sub(_redact_shaped, text)
+ lowered = _lowered(text)
+ if _has_folded(text, lowered, ("bearer",)):
+ text = _SCHEME_RE.sub(_redact_shaped, text)
+ lowered = _lowered(text)
+ if _has_folded(text, lowered, ("cookie",)):
+ text = _COOKIE_RE.sub(_redact_cookie, text)
+ lowered = _lowered(text)
+ if (":" in text or "=" in text) and _has_folded(text, lowered, _SECRET_KEY_WORDS):
+ text = _KV_RE.sub(_redact_kv, text)
+ lowered = _lowered(text)
+ if "--" in text and _has_folded(text, lowered, _SECRET_KEY_WORDS):
+ text = _FLAG_RE.sub(_redact_kv, text)
try:
from utils.native_path_leases import redact_native_paths
text = redact_native_paths(text)