# -*- coding: utf-8 -*- """Audit trail for settings changes — append-only events, no data mutation.""" from __future__ import annotations from typing import Any _SENSITIVE_KEYS = frozenset( { "admin_password_hash", "readonly_password_hash", "shorts_oauth_client_secret", "tiktok_oauth_client_secret", "dy_cookie", "yt_studio_cookies_by_profile", "shorts_upload_socks_profiles", "tiktok_upload_socks_profiles", } ) def _mask_value(key: str, value: Any) -> Any: if key in _SENSITIVE_KEYS: return "***" if key == "shorts_oauth_profiles" and isinstance(value, list): out = [] for row in value: if not isinstance(row, dict): continue r = dict(row) if r.get("client_secret"): r["client_secret"] = "***" out.append(r) return out if isinstance(value, str) and len(value) > 120: return value[:80] + "…" return value def log_settings_patch(patch: dict[str, Any], *, actor: str = "") -> None: if not patch: return try: from . import db if not db.get_settings_map().get("settings_audit_enabled", True): return except Exception: pass safe = {k: _mask_value(k, v) for k, v in patch.items() if v is not None} if not safe: return keys = ", ".join(sorted(safe.keys())[:12]) more = len(safe) - 12 msg = f"设置变更:{keys}" + (f" 等 {more} 项" if more > 0 else "") try: from . import db db.add_event( kind="settings_audit", level="info", message=msg, meta={"actor": actor or "system", "patch": safe}, ) except Exception: pass