bub icon indicating copy to clipboard operation
bub copied to clipboard

Telegram allowlist accepts @usernames, which are reassignable — docs teach it, reference says IDs

Open elfrost opened this issue 2 months ago • 1 comments

BUB_TELEGRAM_ALLOW_USERS is matched against the sender's @username as well as their numeric ID:

https://github.com/bubbuild/bub/blob/main/src/bub/channels/telegram.py#L232-L238

user = update.effective_user
sender_tokens = {str(user.id)}
if user.username:
    sender_tokens.add(user.username)
if self._allow_users and sender_tokens.isdisjoint(self._allow_users):
    await update.message.reply_text("Access denied.")
    return

Those two identifiers have different lifetimes. A Telegram user ID is immutable and permanently bound to one account. A username is rented: the owner can change it whenever they like, and a released username goes back into the pool for anyone to register (Telegram even has an official marketplace for them). So an allowlist entry that is a username authenticates whoever currently holds the handle, not the person you added.

The reference table gets this right — "Comma-separated allowlist of Telegram user IDs" (docs/reference/settings.mdx:136), matching the Field(description=...) in the code. But the docs an operator copies from show a username:

File Example
env.example BUB_TELEGRAM_ALLOW_USERS=123456789,my_username
docs/operate/channels/telegram.mdx:29 BUB_TELEGRAM_ALLOW_USERS=123456789,your_username
docs/operate/configure.mdx:47,75 allow_users: "123456789,your_username"
zh-cn mirrors of both same
docs/operate/deploy.mdx:30 BUB_TELEGRAM_ALLOW_USERS=123456789 ← numeric only

What it gates

_on_message is the only enforcement point for allow_users in the tree, and a message that passes it becomes a turn with the full default toolset: resolve_tool_names(names=None) returns set(REGISTRY), which includes bash (src/bub/builtin/tools.py:142) → asyncio.create_subprocess_shell. No approval prompt, no command allowlist. In the shipped docker-compose.yml that runs as root (no USER in the Dockerfile) with the workspace and ~/.agents mounted from the host.

Differential

Driving the shipped _on_message with synthetic updates, allowlist set to the exact string from env.example:

BUB_TELEGRAM_ALLOW_USERS = '123456789,your_username'

_on_message (telegram.py:226):
  operator, by numeric ID                       id=123456789  username=operator_handle -> ADMITTED
  operator, by username                         id=555000111  username=your_username   -> ADMITTED
  DIFFERENT ACCOUNT that acquired the handle    id=999999999  username=your_username   -> ADMITTED
  unrelated stranger                            id=424242424  username=someone_else    -> denied
  case-mismatched legitimate operator           id=555000111  username=Your_Username   -> denied

_on_start (telegram.py:218), same allowlist:
  unrelated stranger hits /start                id=424242424  username=someone_else    -> "Bub is online. Send text to start."

Row 3 is the issue: 999999999 shares nothing with the operator and is admitted on the string alone.

To be clear about scope — this is not a live compromise of every deployment. It needs an operator who enabled Telegram, used a username rather than an ID, and later let that username go. It is a documented config path that quietly turns a permanent credential into a temporary one, on the only remote boundary the runtime has. tests/test_configure.py covers allow_users: "1,2" (numeric), so the branch the tutorials teach isn't exercised by CI.

Two smaller things in the same twenty lines

Row 5 — the comparison is case-sensitive, Telegram usernames aren't. sender_tokens gets user.username in the account's stored case and is compared verbatim, so an operator who writes Your_Username against an account storing your_username is locked out of their own bot. Fails closed, so it's a correctness bug rather than a security one.

_on_start doesn't check allow_users. It applies _allow_chats and stops (:221), while _on_message applies both (:230, :236). With ALLOW_USERS set and ALLOW_CHATS unset — the configuration in deploy.mdx:30 — any user gets "Bub is online", which contradicts your documented rule in telegram.mdx:36 ("If BUB_TELEGRAM_ALLOW_USERS is set, messages from any other user receive Access denied.").

The fix is your call, because it has a compatibility cost

Rejecting non-numeric entries is the secure answer and it breaks every operator who followed env.example — their bot stops answering them, with no automatic migration, since the Bot API gives no username→ID lookup for an account that hasn't messaged the bot yet. So, two options and I don't think it's my call which:

  1. Strict — reject non-numeric entries at startup with an error naming them, and update the four docs. Secure, breaking.
  2. Compatible — keep accepting usernames, log a startup warning naming each non-numeric entry as reassignable, and lead the examples with IDs. Non-breaking, depends on the operator reading a log line. telegram.start already logs allow_users_count, so there's a natural place for it.

Happy to open a PR for whichever you pick. The /start alignment and the case-insensitive comparison are unambiguous either way and I can include them.


Found while reviewing open-source agent runtimes; write-up at https://elfrost.github.io/ai-patchlab/scans/bubbuild-bub.html

elfrost avatar Sep 13 '26 13:09 elfrost

Accepting usernames is mainly for convenience, and it is also used in other agents. Agent administrators are responsible for assessing security risks and handling them properly.

Therefore, we will not make this fix.

frostming avatar Sep 15 '26 03:09 frostming