telegram: enforce TELEGRAM_ALLOWED_USERS on DMs (mirror DingTalk allowlist)#8
Open
zenprocess wants to merge 1 commit into
Open
Conversation
…wlist) The existing TELEGRAM_ALLOWED_USERS env var gated only callback buttons (_is_callback_user_authorized at lines 1632, 1680). _should_process_message short-circuited DMs unconditionally with 'if not self._is_group_chat(message): return True' — bypassing any allowlist check. This change moves the user-id check to the top of _should_process_message, reusing the existing _is_callback_user_authorized static method, so the allowlist gates both DM and group messages. Empty env preserves the legacy permissive default; backwards compatible. Mirrors the dingtalk.py:402-422 + 546 pattern that already does this for DingTalk. Verified: 5/5 unit-test cases (allowed/disallowed × DM/group × backcompat).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TELEGRAM_ALLOWED_USERScurrently gates only callback buttons via_is_callback_user_authorized(gateway/platforms/telegram.pylines 1632, 1680). DMs are not gated —_should_process_messageshort-circuits non-group chats unconditionally at line 2367:This means an operator who sets
TELEGRAM_ALLOWED_USERS=<id>expecting it to lock the bot's DM surface is silently exposed — any Telegram user who knows the bot's @-handle can DM it freely.Fix
Move the user-id check to the top of
_should_process_message, reusing the existing static method_is_callback_user_authorizedso the allowlist gates both DMs and groups. 9 insertions, 1 deletion.Backwards compatibility
_is_callback_user_authorizedalready returnsTruewhenTELEGRAM_ALLOWED_USERSis empty or unset — the legacy permissive default is preserved for every existing deployment that hasn't opted into the allowlist.Prior art
This mirrors the pattern
dingtalk.pyalready uses:_load_allowed_users+_is_user_allowed(lines 402-422)DingTalk gates both DM and group; Telegram now does the same.
Verification
Unit-tested locally with mocked
Messageobjects:Also live-tested end-to-end via PYTHONPATH-shadow against
hermes-agent 0.11.0(Atomic Hermes 0.1.36): patched gateway started cleanly, polling worked, allowlist enforced on inbound messages.Notes
_is_callback_user_authorizedalready acceptsuser_id: strand parses the CSV env var; calling it from_should_process_messagerequires no new helper.TELEGRAM_ALLOWED_USERSfor callbacks now get DM enforcement for free.