Skip to content

fix: guard float(os.getenv) against ValueError in gateway platform adapters - #17967

Closed
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/env-float-valueerror-guard
Closed

fix: guard float(os.getenv) against ValueError in gateway platform adapters#17967
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/env-float-valueerror-guard

Conversation

@vominh1919

Copy link
Copy Markdown
Contributor

Problem

Several gateway platform adapters cast environment variables directly via float(os.getenv(...)) without try/except. If any env var is set to a non-numeric string (e.g., HERMES_TELEGRAM_TEXT_BATCH_DELAY_SECONDS=abc), the platform adapter crashes with ValueError during __init__, preventing the entire gateway from starting.

Affected files and variables:

  • telegram.py: MEDIA_BATCH_DELAY_SECONDS, TEXT_BATCH_DELAY_SECONDS, TEXT_BATCH_SPLIT_DELAY_SECONDS (3 instances)
  • discord.py: TEXT_BATCH_DELAY_SECONDS, TEXT_BATCH_SPLIT_DELAY_SECONDS (2 instances)
  • wecom.py: TEXT_BATCH_DELAY_SECONDS, TEXT_BATCH_SPLIT_DELAY_SECONDS (2 instances)

Note: telegram.py already has a _env_float() helper (line 776) with proper try/except handling, but it was not used in __init__.

Fix

Each float(os.getenv(...)) call is wrapped in try/except (ValueError, TypeError) with the original default value as fallback.

Diff: +28 lines, -7 lines across 3 files

Before vs After

Scenario Before After
Valid env var Works Works (identical)
Env var set to "abc" ValueError crash → gateway won't start Falls back to default value
Env var set to None string TypeError crash Falls back to default value

Impact

  • Severity: MEDIUM — gateway startup crash from misconfigured env var
  • Scope: Telegram, Discord, WeCom platform adapters
  • Risk: Minimal — only adds error handling, no behavioral change on valid input

…apters

Several gateway platform adapters cast environment variables directly
via float(os.getenv(...)) without try/except. If any env var is set to
a non-numeric string (e.g., HERMES_TELEGRAM_TEXT_BATCH_DELAY_SECONDS=
"abc"), the platform adapter crashes with ValueError during __init__,
preventing the entire gateway from starting.

Affected files and variables:
- telegram.py: MEDIA_BATCH_DELAY_SECONDS, TEXT_BATCH_DELAY_SECONDS,
  TEXT_BATCH_SPLIT_DELAY_SECONDS (3 instances)
- discord.py: TEXT_BATCH_DELAY_SECONDS, TEXT_BATCH_SPLIT_DELAY_SECONDS
  (2 instances)
- wecom.py: TEXT_BATCH_DELAY_SECONDS, TEXT_BATCH_SPLIT_DELAY_SECONDS
  (2 instances)

Each call is now wrapped in try/except (ValueError, TypeError) with
the original default value as fallback. Note: telegram.py already has
a _env_float() helper (line 776) with this exact pattern, but it was
not used in __init__. This fix makes the __init__ paths consistent
with that helper.
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter platform/discord Discord bot adapter platform/wecom WeCom / WeChat Work adapter labels Apr 30, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #17905 (merged) — same root cause: unguarded float() env var casts in gateway platform adapters. #17905 already addressed this.

@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #17905 (merged) — same root cause: unguarded float() env var casts in gateway platform adapters.

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the defensive gateway hardening. This is already implemented on current main, so this PR is redundant.

  • utils.py:421 defines env_float(), which falls back on malformed values instead of propagating ValueError / TypeError.
  • The live Discord and WeCom adapters use it for the reported batch-delay settings at plugins/platforms/discord/adapter.py:829 and plugins/platforms/wecom/adapter.py:190.
  • Telegram uses env_float() for media batching (plugins/platforms/telegram/adapter.py:546) and the stricter _env_float_clamped() path for text batching (plugins/platforms/telegram/adapter.py:558), which also rejects non-finite values.
  • Commit a7dd98c8609c0d944e3c5dd0c5b9ee31dd99eb29 implemented the broader malformed numeric-env guard; the legacy gateway/platforms/{telegram,discord,wecom}.py paths in this diff were subsequently migrated to bundled platform plugins by 5600105478ffde29d7566b45421b100eaa29c4ef.

The earlier discussion linked #17905; current adapter coverage is independently present in the later broader safeguard above. Automated hermes-sweeper review.

@teknium1 teknium1 closed this Jul 12, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P3 Low — cosmetic, nice to have platform/discord Discord bot adapter platform/telegram Telegram bot adapter platform/wecom WeCom / WeChat Work adapter sweeper:implemented-on-main Sweeper: behavior already present on current main type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants