fix(config): guard WECOM/BlueBubbles port env vars against malformed values - #49416
fix(config): guard WECOM/BlueBubbles port env vars against malformed values#49416vanthinh6886 wants to merge 1 commit into
Conversation
|
Nice defensive coding. Guarding port env vars against malformed values prevents a class of runtime crashes that are surprisingly common in containerized/k8s deployments where env vars get templated from ConfigMaps. What this PR likely prevents:
Best practice pattern: def parse_port(env_var: str, default: int = 8080) -> int:
val = os.getenv(env_var, "").strip()
if not val:
return default
try:
port = int(val)
if not (1 <= port <= 65535):
raise ValueError(f"Port out of range: {port}")
return port
except ValueError as e:
logger.warning(f"Invalid {env_var}={val!r}, using default {default}: {e}")
return defaultIf WECOM and BlueBubbles are notification channels, crashing on startup (before the fix) would block all notifications. Good catch. |
|
Duplicate of #48748 — identical diff (same author): both replace the two bare |
…values WECOM_CALLBACK_PORT and BLUEBUBBLES_WEBHOOK_PORT use bare int(os.getenv(...)) which raises ValueError if the env var contains a non-numeric string (e.g. "abc" or ""). Replace with env_int() from utils which gracefully falls back to the default value on malformed input.
37e7cc6 to
7810e1c
Compare
|
Closing as superseded by #49558, which landed the canonical fix for this whole bug class. #49558 adds We went with the Thanks for spotting and driving the fix on this — it's all in main now via: |
Summary
WECOM_CALLBACK_PORTandBLUEBUBBLES_WEBHOOK_PORTingateway/config.pyuse bareint(os.getenv(...))which raisesValueErrorif the env var contains a non-numeric string (e.g."","abc"). This crashes the gateway on startup.Fix
Replace with
env_int()fromutils.pywhich gracefully falls back to the default value on malformed input. This is the same pattern already used throughout the rest of the codebase.Changes
gateway/config.py: importenv_intfromutils, replace 2 bareint(os.getenv(...))callsTest Plan