ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
refactor(security): one sensitive-keyword union for all four redaction sinks (plan 77 F3)
Juan Denisa66c4f816430
diff --git a/backend/app/middleware/audit.py b/backend/app/middleware/audit.pyindex d784dc35..2d4a9ac9 100644--- a/backend/app/middleware/audit.py+++ b/backend/app/middleware/audit.py@@ -15,24 +15,10 @@ MAX_BODY_BYTES = 32 * 1024MAX_STRING_LENGTH = 500MAX_LIST_ITEMS = 20MAX_DICT_ITEMS = 50-REDACTED = '[redacted]'--SENSITIVE_KEY_PARTS = (- 'password',- 'passwd',- 'secret',- 'token',- 'credential',- 'private',- 'certificate',- 'api_key',- 'apikey',- 'authorization',- 'cookie',- 'session',- 'totp',- 'otp',- 'csrf',+from app.utils.sensitive_data_filter import ( # noqa: F401 (re-export)+ REDACTED,+ SENSITIVE_KEY_PARTS,+ is_sensitive_key as _shared_is_sensitive_key,)NOISY_ENDPOINTS = {@@ -193,8 +179,7 @@ def _sanitize(value, key=None, depth=0):def _is_sensitive_key(key):- lowered = str(key).lower()- return any(part in lowered for part in SENSITIVE_KEY_PARTS)+ return _shared_is_sensitive_key(str(key))def _ip_address():diff --git a/backend/app/services/audit_service.py b/backend/app/services/audit_service.pyindex 36e976c2..9b68cace 100644--- a/backend/app/services/audit_service.py+++ b/backend/app/services/audit_service.py@@ -3,16 +3,10 @@ from flask import g, has_request_context, requestfrom app import dbfrom app.models import AuditLog-REDACTED = '[redacted]'-SENSITIVE_SETTING_PARTS = (- 'password',- 'secret',- 'token',- 'credential',- 'private',- 'certificate',- 'api_key',- 'apikey',+from app.utils.sensitive_data_filter import (+ REDACTED,+ SENSITIVE_KEY_PARTS as SENSITIVE_SETTING_PARTS,+ is_sensitive_key as _shared_is_sensitive_key,)@@ -132,8 +126,7 @@ class AuditService:@staticmethoddef is_sensitive_key(key):- lowered = str(key).lower()- return any(part in lowered for part in SENSITIVE_SETTING_PARTS)+ return _shared_is_sensitive_key(str(key))@staticmethoddef log_app_action(action, user_id, app_id, app_name=None, details=None):diff --git a/backend/app/services/telemetry_service.py b/backend/app/services/telemetry_service.pyindex 8d411ef5..c11794ee 100644--- a/backend/app/services/telemetry_service.py+++ b/backend/app/services/telemetry_service.py@@ -17,23 +17,10 @@ from app.models.system_event import SystemEventlogger = logging.getLogger(__name__)-REDACTED = '[redacted]'-SENSITIVE_KEY_PARTS = (- 'password',- 'passwd',- 'secret',- 'token',- 'credential',- 'private',- 'certificate',- 'api_key',- 'apikey',- 'authorization',- 'cookie',- 'session',- 'totp',- 'otp',- 'csrf',+from app.utils.sensitive_data_filter import ( # noqa: F401 (re-export)+ REDACTED,+ SENSITIVE_KEY_PARTS,+ is_sensitive_key as _shared_is_sensitive_key,)MAX_STRING_LENGTH = 2000@@ -307,8 +294,7 @@ def _sanitize_payload(value, key=None, depth=0):def _is_sensitive_key(key):- lowered = str(key).lower()- return any(part in lowered for part in SENSITIVE_KEY_PARTS)+ return _shared_is_sensitive_key(str(key))def _payload_too_large(payload):diff --git a/backend/app/utils/sensitive_data_filter.py b/backend/app/utils/sensitive_data_filter.pyindex 571f9ed5..92a80f0e 100644--- a/backend/app/utils/sensitive_data_filter.py+++ b/backend/app/utils/sensitive_data_filter.py@@ -22,26 +22,34 @@ MASK = '••••••••'REVEAL_SCOPE = 'secrets:read'# Substrings that mark a dict key as sensitive. Matched case-insensitively-# against the key name. Kept as a module-level constant so callers/tests can-# inspect or extend it.-SENSITIVE_KEY_PATTERNS = [+# against the key name. THE union of every sink's former private list+# (plan 77 F3): response filter, audit_service, middleware/audit (the only+# one that covered totp/otp/csrf/session), telemetry. A keyword added here+# is redacted by ALL sinks; adding one to a single sink again is ratcheted+# by tests/test_redaction_one_door.py.+SENSITIVE_KEY_PARTS = ('password','passwd',- 'secret',+ 'secret', # subsumes secret_key / client_secret'token','credential',- 'private_key',- 'privatekey',+ 'private', # subsumes private_key / privatekey+ 'certificate','api_key','apikey','key_hash','access_key',- 'secret_key',- 'client_secret',- 'session_key',- 'auth',+ 'auth', # subsumes authorization'passphrase',-]+ 'cookie',+ 'session', # subsumes session_key+ 'totp',+ 'otp',+ 'csrf',+)++# Backwards-compat alias (older callers/tests import the list name).+SENSITIVE_KEY_PATTERNS = list(SENSITIVE_KEY_PARTS)# Keys that *contain* a sensitive substring but are safe to keep (false# positives we explicitly allow through). e.g. "is_secret" is a boolean flag,@@ -58,9 +66,24 @@ _SAFE_KEY_EXCEPTIONS = {'authorized','authentication','authorization_url',+ # identifier/metadata keys the union parts would otherwise swallow:+ 'sessions',+ 'session_count',+ 'totp_enabled',+ 'otp_enabled',+ 'certificate', # public cert material/metadata, not a secret+ 'certificates',+ 'total_certificates',+ 'private_slug',+ 'private_url',+ 'private_url_enabled',+ 'private_wg_ip',+ 'private_server_name',+ 'private_server_id',+ 'private_pubkey', # a PUBLIC key}-_PATTERN_RE = re.compile('|'.join(re.escape(p) for p in SENSITIVE_KEY_PATTERNS), re.IGNORECASE)+_PATTERN_RE = re.compile('|'.join(re.escape(p) for p in SENSITIVE_KEY_PARTS), re.IGNORECASE)def is_sensitive_key(key):@@ -140,3 +163,27 @@ def _mask(value, sensitive):if sensitive and value is not None:return MASKreturn value+++# Mask used by the LOG sinks (audit middleware / audit_service / telemetry).+REDACTED = '[redacted]'+++def mask_payload(data, mask=REDACTED, max_depth=6):+ """Unconditionally redact sensitive values in ``data`` (no scope logic).++ The log-sink twin of :func:`mask_sensitive`: audit and telemetry writers+ must never reveal secrets regardless of the caller's scopes. Depth-capped+ against pathological nesting; scalars pass through.+ """+ if max_depth < 0:+ return mask+ if isinstance(data, dict):+ return {+ k: (mask if is_sensitive_key(k)+ else mask_payload(v, mask, max_depth - 1))+ for k, v in data.items()+ }+ if isinstance(data, (list, tuple)):+ return [mask_payload(v, mask, max_depth - 1) for v in data]+ return datadiff --git a/backend/tests/test_redaction_one_door.py b/backend/tests/test_redaction_one_door.pynew file mode 100644index 00000000..6daf634d--- /dev/null+++ b/backend/tests/test_redaction_one_door.py@@ -0,0 +1,70 @@+"""Plan 77 F3 — ONE sensitive-keyword list, four sinks.++utils/sensitive_data_filter.py owns SENSITIVE_KEY_PARTS and+is_sensitive_key(); the response filter, audit_service, middleware/audit and+telemetry all consume it. The ratchet asserts no sink re-grows a private+list, so a keyword added to the union is redacted everywhere at once.+"""+import re+from pathlib import Path++from app.utils import sensitive_data_filter as sdf++APP = Path(__file__).resolve().parents[1] / 'app'+++def test_union_covers_every_sinks_former_keywords():+ formerly_split = {+ # utils' originals+ 'password', 'passwd', 'secret', 'token', 'credential',+ 'api_key', 'apikey', 'key_hash', 'access_key', 'auth', 'passphrase',+ # middleware-only additions the other sinks used to miss+ 'private', 'certificate', 'cookie', 'session', 'totp', 'otp', 'csrf',+ }+ for part in formerly_split:+ assert any(part.find(u) != -1 or u.find(part) != -1+ for u in sdf.SENSITIVE_KEY_PARTS), part+ # every former keyword must actually trigger the shared predicate+ assert sdf.is_sensitive_key(f'x_{part}_value'), part+++def test_sinks_delegate_to_shared_predicate():+ from app.services import audit_service, telemetry_service+ from app.middleware import audit as audit_mw+ # csrf/totp were middleware-only; session_key was utils-only — every sink+ # must now agree on both.+ for key in ('csrf_token', 'totp_secret', 'session_key', 'x_cookie'):+ assert audit_service.AuditService.is_sensitive_key(key), key+ assert telemetry_service._is_sensitive_key(key), key+ assert audit_mw._is_sensitive_key(key), key+ assert sdf.is_sensitive_key(key), key+++def test_safe_exceptions_pass_through_everywhere():+ for key in ('totp_enabled', 'private_url', 'certificates', 'has_secret'):+ assert not sdf.is_sensitive_key(key), key+++def test_mask_payload_masks_recursively():+ data = {'name': 'ok', 'password': 'x', 'nested': [{'api_key': 'k'}],+ 'totp_enabled': True}+ out = sdf.mask_payload(data)+ assert out['name'] == 'ok'+ assert out['password'] == sdf.REDACTED+ assert out['nested'][0]['api_key'] == sdf.REDACTED+ assert out['totp_enabled'] is True+++def test_no_private_sensitive_lists_regrow():+ """Ratchet: no literal SENSITIVE list definitions outside the one door."""+ pattern = re.compile(r'^SENSITIVE\w*\s*=\s*[\(\[]', re.M)+ offenders = []+ for f in APP.rglob('*.py'):+ if f.name == 'sensitive_data_filter.py' or '__pycache__' in str(f):+ continue+ if pattern.search(f.read_text(encoding='utf-8', errors='replace')):+ offenders.append(str(f.relative_to(APP)))+ assert not offenders, (+ f"Private sensitive-keyword lists re-grown in {offenders} — extend "+ "utils/sensitive_data_filter.SENSITIVE_KEY_PARTS instead (plan 77 F3)."+ )