29 lines
1.1 KiB
Python
29 lines
1.1 KiB
Python
# aider-watch-client secret redactor | 2026-04-20 @ Asia/Taipei
|
||
"""與 server secret_redactor.py 同步。Mac 端進 buffer 前先遮罩(defense in depth)。"""
|
||
from __future__ import annotations
|
||
import re
|
||
from typing import Any
|
||
|
||
_PATTERNS: list[tuple[re.Pattern[str], str]] = [
|
||
(re.compile(r"sk-or-v1-[A-Za-z0-9]{36,}"), "openrouter"),
|
||
(re.compile(r"sk-ant-api\d{2}-[A-Za-z0-9_\-]{12,}"), "anthropic"),
|
||
(re.compile(r"sk-[A-Za-z0-9]{40,}"), "openai"),
|
||
(re.compile(r"ghp_[A-Za-z0-9]{36}"), "github"),
|
||
(re.compile(r"AIza[0-9A-Za-z_\-]{35}"), "google"),
|
||
(re.compile(r"\b\d{8,10}:[A-Za-z0-9_\-]{35}\b"), "telegram"),
|
||
(re.compile(r"AKIA[0-9A-Z]{16}"), "aws"),
|
||
]
|
||
|
||
|
||
def redact(obj: Any) -> Any:
|
||
if isinstance(obj, str):
|
||
s = obj
|
||
for pat, kind in _PATTERNS:
|
||
s = pat.sub(f"<redacted:{kind}>", s)
|
||
return s
|
||
if isinstance(obj, dict):
|
||
return {k: redact(v) for k, v in obj.items()}
|
||
if isinstance(obj, list):
|
||
return [redact(x) for x in obj]
|
||
return obj
|