41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
SENSITIVE_KEYS = {
|
||
|
|
"token", "signature", "app_key", "app_secret", "appKey", "appSecret",
|
||
|
|
"access_token", "accessToken", "api_key", "apiKey", "password", "secret",
|
||
|
|
}
|
||
|
|
|
||
|
|
OMIT_KEYS = {"msg_body", "msgBody", "binary_data", "binaryData"}
|
||
|
|
|
||
|
|
|
||
|
|
def sanitize_log(data: dict) -> dict:
|
||
|
|
result = {}
|
||
|
|
for key, value in data.items():
|
||
|
|
if key in OMIT_KEYS:
|
||
|
|
result[key] = "<OMITTED>"
|
||
|
|
continue
|
||
|
|
if key in SENSITIVE_KEYS:
|
||
|
|
result[key] = mask_value(value) if isinstance(value, str) else "<MASKED>"
|
||
|
|
continue
|
||
|
|
if isinstance(value, dict):
|
||
|
|
result[key] = sanitize_log(value)
|
||
|
|
else:
|
||
|
|
result[key] = value
|
||
|
|
return result
|
||
|
|
|
||
|
|
|
||
|
|
def mask_value(value: str, visible_start: int = 4, visible_end: int = 4) -> str:
|
||
|
|
if len(value) <= visible_start + visible_end:
|
||
|
|
return "*" * len(value)
|
||
|
|
return value[:visible_start] + "*" * min(len(value) - visible_start - visible_end, 8) + value[-visible_end:]
|
||
|
|
|
||
|
|
|
||
|
|
def is_debug_whitelist(peer_id: str, whitelist: list[str]) -> bool:
|
||
|
|
if not whitelist:
|
||
|
|
return False
|
||
|
|
return peer_id in whitelist or "*" in whitelist
|