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
fix(notifications): redact transport errors before they reach the log
Juan Denisf10c04ddc344
diff --git a/backend/app/models/chat_webhook.py b/backend/app/models/chat_webhook.pyindex 42dffbb9..178dd512 100644--- a/backend/app/models/chat_webhook.py+++ b/backend/app/models/chat_webhook.py@@ -49,9 +49,14 @@ class ChatWebhookConnection(db.Model):def raw_credentials(self):try:- return json.loads(self.credentials_json) if self.credentials_json else {}+ value = json.loads(self.credentials_json) if self.credentials_json else {}except (json.JSONDecodeError, TypeError):return {}+ # A hand-edited or corrupt row can hold valid JSON of the wrong shape+ # (``[]``, a string, ``null``). Every caller assumes a mapping, so+ # normalise here rather than at each call site -- same guard as+ # ``categories()`` below.+ return value if isinstance(value, dict) else {}def credentials(self):"""Decrypted credentials for use at send/test time (never serialized)."""diff --git a/backend/app/services/chat_webhook_service.py b/backend/app/services/chat_webhook_service.pyindex cec9e801..8ecd1202 100644--- a/backend/app/services/chat_webhook_service.py+++ b/backend/app/services/chat_webhook_service.py@@ -16,6 +16,7 @@ import hashlibimport hmacimport jsonimport logging+import refrom datetime import datetimeimport requests@@ -30,6 +31,31 @@ logger = logging.getLogger(__name__)SIGNATURE_HEADER = 'X-ServerKit-Signature'_REQUEST_TIMEOUT = 15+# Split a URL into scheme+host and everything after it.+_URL_RE = re.compile(r'(https?://[^/\s\'"]+)([^\s\'"]*)')+# Telegram bot tokens look like ``123456789:AA...`` and appear in bare error text+# as well as inside the API URL.+_BOT_TOKEN_RE = re.compile(r'\b\d{6,}:[A-Za-z0-9_-]{20,}\b')+++def _redact_reason(text):+ """Reduce a transport error to something safe to write to the log.++ Every mainstream chat provider puts the credential in the URL *path* -- the+ Discord/Slack/Teams webhook token and the Telegram bot token are all path+ segments -- and ``requests`` embeds the full URL in its exception text. The+ host is useful for diagnosis and is not a secret, so keep it and drop the+ rest. ``to_dict()`` already masks ``destination``, so logging the raw string+ would leak past the API's own masking.+ """+ if not text:+ return None+ text = _BOT_TOKEN_RE.sub('<token>', str(text))+ return _URL_RE.sub(+ lambda m: m.group(1) + ('/<redacted>' if m.group(2) not in ('', '/') else m.group(2)),+ text,+ )+# Legacy notifications.json section -> (connection kind, destination key in the# saved config). telegram maps to chat_id; the chat flavors carry a webhook_url._LEGACY_MAP = [@@ -302,13 +328,14 @@ class ChatWebhookService:if success:return {'success': True, 'message': 'Test notification sent'}- # The reason stays server-side: transport errors embed the full webhook- # URL (secret path segment included) and Telegram bot tokens.+ # The reason stays server-side AND redacted: transport errors embed the+ # full webhook URL (secret path segment included) and Telegram bot+ # tokens, and the panel serves the log files back over /api/logs.logger.warning('Chat connection test failed (id=%s, kind=%s, reason=%s)',conn.id,conn.kind,- getattr(delivery_result, 'error', None),+ _redact_reason(getattr(delivery_result, 'error', None)),)return {'success': False, 'error': 'Test notification failed'}diff --git a/backend/tests/BASELINE_COUNT b/backend/tests/BASELINE_COUNTindex 42606260..110b5c51 100644--- a/backend/tests/BASELINE_COUNT+++ b/backend/tests/BASELINE_COUNT@@ -1 +1 @@-3379+3382diff --git a/backend/tests/test_notification_chat_connections.py b/backend/tests/test_notification_chat_connections.pyindex 0840ca40..645aaa1e 100644--- a/backend/tests/test_notification_chat_connections.py+++ b/backend/tests/test_notification_chat_connections.py@@ -7,6 +7,7 @@ payload, the consumer delivery path, and legacy-import idempotency.import hashlibimport hmacimport json+import loggingimport pytestfrom werkzeug.security import generate_password_hash@@ -165,6 +166,28 @@ class TestCrud:with pytest.raises(ValueError, match='default endpoint'):ChatWebhookService.update(conn.id, {'is_default': False})+ def test_corrupt_non_dict_credentials_do_not_500(self, app):+ """A row holding well-formed JSON of the wrong shape stays serialisable.++ Every caller of raw_credentials() assumes a mapping, so a hand-edited+ ``[]`` used to blow up to_dict() -- i.e. the list endpoint -- before+ update() was even reachable.+ """+ conn = ChatWebhookService.add({+ 'kind': 'webhook',+ 'name': 'Corrupt',+ 'url': 'https://hooks.example/ops',+ })+ conn.credentials_json = '[]'+ db.session.commit()++ assert conn.raw_credentials() == {}+ assert conn.credentials() == {}+ assert conn.to_dict()['destination'] == ''+ # and the write path reports a controlled validation error, not a 500+ with pytest.raises(ValueError, match='requires a url'):+ ChatWebhookService.update(conn.id, {'secret': 'x'})+def test_update_clears_explicitly_empty_optional_credential(self, app):conn = ChatWebhookService.add({'kind': 'telegram',@@ -419,6 +442,50 @@ class TestConnectionTesting:}assert secret_url not in json.dumps(result)+ def test_connection_test_keeps_secrets_out_of_the_log(self, app, monkeypatch, caplog):+ """The failure reason is logged for diagnosis, but redacted first.++ Providers put the credential in the URL *path* and requests embeds the+ whole URL in its exception text, so an unredacted log line would leak+ past to_dict()'s destination masking -- and /api/logs serves those files+ back to the panel.+ """+ def fail_post(url, *args, **kwargs):+ raise RuntimeError(f'failed POST {url}')++ monkeypatch.setattr(+ 'app.services.chat_webhook_service.requests.post', fail_post,+ )+ conn = ChatWebhookService.add({+ 'kind': 'webhook',+ 'name': 'Ops',+ 'url': 'https://hooks.example/services/T000/B000/super-secret-token',+ })++ with caplog.at_level(logging.WARNING,+ logger='app.services.chat_webhook_service'):+ ChatWebhookService.test(conn.id)++ logged = '\n'.join(r.getMessage() for r in caplog.records)+ assert 'super-secret-token' not in logged+ assert 'T000' not in logged+ # the host survives -- it is the part that makes the log worth keeping+ assert 'hooks.example' in logged+ assert '<redacted>' in logged++ def test_redact_reason_strips_telegram_bot_tokens(self, app):+ from app.services.chat_webhook_service import _redact_reason++ token = '123456789:AAHkq3Bv7pQwErTyUiOpAsDfGhJkLzXcVbN'+ reason = f'404 Client Error for url: https://api.telegram.org/bot{token}/sendMessage'++ redacted = _redact_reason(reason)++ assert token not in redacted+ assert 'AAHkq3Bv7pQwErTyUiOpAsDfGhJkLzXcVbN' not in redacted+ assert 'api.telegram.org' in redacted+ assert _redact_reason(None) is None+def test_connection_test_returns_none_for_unknown_id(self, app):assert ChatWebhookService.test(999999) is None