From 2c63bbe27ae07f1b6985643b2d8235a7967d8971 Mon Sep 17 00:00:00 2001 From: Frowtek Date: Tue, 5 May 2026 15:19:41 +0300 Subject: [PATCH] fix(gateway): harden malformed env port overrides --- gateway/config.py | 4 ++-- tests/gateway/test_config.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/gateway/config.py b/gateway/config.py index fa64b9046d4d8..94831ea625768 100644 --- a/gateway/config.py +++ b/gateway/config.py @@ -1413,7 +1413,7 @@ def _apply_env_overrides(config: GatewayConfig) -> None: "token": os.getenv("WECOM_CALLBACK_TOKEN", ""), "encoding_aes_key": os.getenv("WECOM_CALLBACK_ENCODING_AES_KEY", ""), "host": os.getenv("WECOM_CALLBACK_HOST", "0.0.0.0"), - "port": int(os.getenv("WECOM_CALLBACK_PORT", "8645")), + "port": _coerce_int(os.getenv("WECOM_CALLBACK_PORT"), 8645), }) # Weixin (personal WeChat via iLink Bot API) @@ -1469,7 +1469,7 @@ def _apply_env_overrides(config: GatewayConfig) -> None: "server_url": bluebubbles_server_url.rstrip("/"), "password": bluebubbles_password, "webhook_host": os.getenv("BLUEBUBBLES_WEBHOOK_HOST", "127.0.0.1"), - "webhook_port": int(os.getenv("BLUEBUBBLES_WEBHOOK_PORT", "8645")), + "webhook_port": _coerce_int(os.getenv("BLUEBUBBLES_WEBHOOK_PORT"), 8645), "webhook_path": os.getenv("BLUEBUBBLES_WEBHOOK_PATH", "/bluebubbles-webhook"), "send_read_receipts": os.getenv("BLUEBUBBLES_SEND_READ_RECEIPTS", "true").lower() in ("true", "1", "yes"), }) diff --git a/tests/gateway/test_config.py b/tests/gateway/test_config.py index 3df2a7d50b9df..d843d377c4383 100644 --- a/tests/gateway/test_config.py +++ b/tests/gateway/test_config.py @@ -609,3 +609,27 @@ def test_existing_platform_configs_accept_home_channel_env_overrides(self): home = config.platforms[platform].home_channel assert home is not None, f"{platform.value}: home_channel should not be None" assert (home.chat_id, home.name) == expected, platform.value + + +class TestMalformedEnvPortOverrides: + def test_invalid_bluebubbles_webhook_port_falls_back_to_default(self, monkeypatch): + monkeypatch.setenv("BLUEBUBBLES_SERVER_URL", "http://localhost:1234") + monkeypatch.setenv("BLUEBUBBLES_PASSWORD", "secret") + monkeypatch.setenv("BLUEBUBBLES_WEBHOOK_PORT", "not-a-port") + + config = GatewayConfig() + + _apply_env_overrides(config) + + assert config.platforms[Platform.BLUEBUBBLES].extra["webhook_port"] == 8645 + + def test_invalid_wecom_callback_port_falls_back_to_default(self, monkeypatch): + monkeypatch.setenv("WECOM_CALLBACK_CORP_ID", "corp-id") + monkeypatch.setenv("WECOM_CALLBACK_CORP_SECRET", "corp-secret") + monkeypatch.setenv("WECOM_CALLBACK_PORT", "not-a-port") + + config = GatewayConfig() + + _apply_env_overrides(config) + + assert config.platforms[Platform.WECOM_CALLBACK].extra["port"] == 8645