Repository navigation
fix(channels): rate-limit sign-in code emails on Telegram, WhatsApp and Slack (abilityai/trinity-enterprise#1253) - #4543
Conversation
…nd Slack
Telegram `/login <email>`, WhatsApp `/login <email>` and the Slack
require_email email step sent a verification email on every request.
Commands reach handle_command straight from the transports, so the
router's per-sender message limit never saw them.
services/channel_login_limit.py caps code requests over 10 minutes:
5 per sender ({channel}:{sender_id}), 3 per address (shared across
channels) and 20 per Telegram bot / WhatsApp binding / Slack workspace,
counted with rate_limiter.check keyed on the input. Adapters call it
after the email-format check and before the ent#849 membership branch.
Over the limit the bot says so, mints no code and sends no email.
The cap never reads email_login_codes: the web login writes rows only
for whitelisted addresses, so a reply derived from them would reveal
whitelist membership. Logs carry a hash of the sender id (a WhatsApp
sender id is a phone number).
tests/unit/conftest.py empties the limiter's in-process buckets before
each test; several files send /login from the same fake sender.
Fixes Abilityai/trinity-enterprise#1253
Co-Authored-By: Claude Opus 5.5 <[email protected]>
AndriiPasternak31
left a comment
There was a problem hiding this comment.
Requesting changes: an existing integration test will go red under the new cap, and the per-address budget isn't shared with web/MCP login, which the issue asks for.
1. tests/test_whatsapp_integration.py exceeds the new per-sender cap.
The module sends /login <email> six times from the same _TEST_USER_PHONE on one module-scoped binding (lines 295, 310, 332, 358, 384, 414). The emails differ, but the sender cap is 5 per 10 minutes (channel_login_limit.py, PER_SENDER = 5), so the sixth request (in test_logout_clears_verified_email) is suppressed, and the test then times out waiting for the pending-login key. The bucket lives in Redis, so re-running the module within 10 minutes fails earlier. Each test needs its own from_phone.
2. The address budget isn't shared with web/MCP.
Web and MCP count rows in email_login_codes (auth.py:642, mcp_auth_service.py:127); the channel cap is a separate Redis bucket. Channel codes count against web, but not the other way round: after 3 web codes, the channels can still send 3 more, and Slack codes write no row at all. Not reading web rows (to avoid leaking whether an address is whitelisted) doesn't rule out a shared bucket. Web and MCP could charge the same rate_limiter key before their whitelist check, and since the web reply is uniform that leaks nothing. If the split is intended, please get it agreed on ent#1253 and fix the new test module's docstring, which says "shared with web + MCP login".
3. A request refused by a later cap still uses up the earlier ones.
rate_limiter.check records the hit when it passes, so a request that clears the sender and address caps but is refused by the bot cap has still spent the sender's and the address's budget. The module docstring promises only the other direction. While a bot is flooded (the accepted 4-accounts case), a legitimate user's retries can exhaust their address budget across every bot and Slack for the following 10 minutes. Fix with a reorder or a refund. rate_limiter can't check without counting.
4. A named victim can now be locked out on every channel. Please list this in the accepted risks.
One Telegram account, within its sender cap of 5, can send /login [email protected] 3 times every 10 minutes and keep that address locked out of Slack verification and Telegram/WhatsApp /login on every agent. Any per-address cap allows this. Slack previously had no per-address cap, so this is new there.
Smaller items
test_web_code_rows_do_not_change_the_replyonly catches an import placed inside the function; a module-levelfrom database import dbwould slip pastpatch("database.db").- The in-process fallback in
rate_limiternever removes keys, and here they are user-supplied (emails, sender ids). That only matters while Redis is down. - The "Current consumers" list in
docs/memory/architecture/security.md:262isn't updated.
Looks right: all three channels are covered, the cap runs before the ent#849 branch so members and outsiders get the same reply, emails are normalised, and every Redis command used is allowed by the backend ACL.
The module sent /login six times from one number, past the per-sender cap of 5 per 10 minutes, and the cap lives in Redis, so re-runs within the window failed sooner. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…channels - Web login and MCP inline auth charge the per-address rate_limiter bucket the channels use (login_code_email:<email>), before their whitelist / known-address check, so the budget is one budget and its refusal reveals no membership. - A channel request refused by a later cap no longer spends the earlier ones: rate_limiter.refund removes the recorded hit. - The in-process fallback is capped at 10,000 keys; keys here are user input. - Accepted risk documented: one sender can keep a named address from receiving a code on every surface (module docstring, security.md, flow doc). - Current consumers list in security.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Smaller: row-read test patches the shared |
AndriiPasternak31
left a comment
There was a problem hiding this comment.
Re-checked 80a7cd746 + 78c64b460. All four points are fixed:
- Each WhatsApp test gets its own sender.
- Web and MCP charge the shared key before the whitelist / known-address check. Moving either charge after that check fails tests.
- The refund works: a request refused by any cap is charged to none.
- The lockout risk is documented.
The web reply stays uniform with the new charge, and refund only adds ZREM.
Still blocking: an unauthenticated caller can fill Redis memory through the web login. This comes from the new shared key.
EmailLoginRequest.emailis a plainstrwith no length limit (db_models.py:768), and nothing inrequest_email_codechecks its length.- nginx accepts bodies up to 25 MB (
nginx-server.conf:20), and the route has no per-IP limit. address_allowedbuilds the key straight from the input:login_code_email:{email}(channel_login_limit.py), TTL 601 s. A 5 MB "email" gives a 5 MB key.- Repeated 25 MB requests stack up for 10 minutes each. Redis has no
maxmemory(docker-compose.yml:552) and also holds the event bus and secrets, so this takes the platform down, not just login. - MCP (
max_length=254) and the channels (>254 rejected) are already bounded, so the web route is the only open path. - Fix: hash the normalised email in the key, or refuse anything over 254 chars before charging.
Smaller:
- PR body is stale. It still describes the first design: "never reads
email_login_codes… count against the web cap", "not charged to the next". It doesn't mention the shared web/MCP charge,refundor the lockout risk, and the Changes list omitsauth.py,mcp_auth_service.pyandrate_limiter.py. The squash commit will carry that text. - In-process fallback while Redis is down. Eviction drops the oldest-created key (
rate_limiter.py:157-158), and the anonymous web route can push 10k distinct addresses through in seconds. That wipes every long-lived bucket on that worker: the MCP global cap, webhook limits, the channel sender/bot caps. Bounding the key size (above) helps with memory but not with this. - Log line.
auth.py:637logs the anonymous email with%s, so newlines and very long lines get through.%rmatches the rest of the PR. - Test gap. The autouse fixture forces
_get_redis=None, so the Redis branch ofrefundisn't unit-tested.
…llback - The per-address limiter key holds a sha256 of the stripped, lowercased address, so its size is fixed for any input. Web, MCP and the channels build it in one function and share the same key for the same address. - The web login model caps email at 254 characters; a longer address gets the route's usual 422 before any budget is charged. - The in-process fallback caps each key prefix at 10k keys and evicts the oldest key of the same prefix, so a flood of login addresses while Redis is down cannot evict another limiter's bucket. - Log the anonymous address with %r on the web and MCP suppression lines. - Unit test for check + refund through the Redis branch (fakeredis). Refs Abilityai/trinity-enterprise#1253 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Fixed in
|
AndriiPasternak31
left a comment
There was a problem hiding this comment.
Approving 83cd3efc5. The Redis blocker is fixed: every surface builds the key through _address_key (sha256 of the stripped, lowercased address), and the web model caps the email at 254 characters, so a 5 MB email now gets a 422 before any charge and writes no key. The other claims hold:
- Normalisation is identical across web, MCP and the channels.
- Per-prefix fallback caps keep a login-address flood from evicting other buckets.
- The suppression lines use
%r. - The fakeredis refund test goes red if the Redis branch is broken.
Two wording nits in the PR body and the module docstring:
- "Caps sign-in code requests on every surface that mints one": the portal (
client_portal/service.py:393), public-link verification (routers/public.py:458) and account bind (users.py:116) mint codes outside the shared budget. The portal has its own 5/300 s cap. - "MCP and the channels already reject over 254" (
channel_login_limit.py:19): Slack's_extract_email(slack_adapter.py:618-620) has no length cap. With the key hashed this can't reach Redis anymore; the only effect is a very long log line on refusal.
Summary
Telegram
/login <email>, WhatsApp/login <email>and the Slackrequire_emailstep each emailed a sign-in code on every request, with no cap. This PR caps sign-in code requests on every surface that mints one.Design
services/channel_login_limit.py, all counted with the sharedrate_limiter:channel_login_sender:{channel}:{sender_id}(Slack{team}:{user});login_code_email:<sha256 of the stripped, lowercased address>. It is built in one function (_address_key), so every surface charges the same key for the same address, and its size is fixed whatever the caller sends.routers/auth.py) and MCP inline auth (services/mcp_auth_service.py) calladdress_allowedbefore their whitelist / known-address check, so the budget counts every address alike and its refusal says nothing about membership. The web reply stays the uniform generic body; the MCP send runs after its 202. The channels run the check before the ent#849 membership branch. The cap never readsemail_login_codes.rate_limiter.refund(key, result)removes the hit an allowedcheckrecorded. Channel caps run sender, then address, then bot; when one refuses, the caps already passed are refunded, so a refused request is charged to none.EmailLoginRequest.emailis capped at 254 characters; a longer address gets the route's usual 422 before any budget is charged. MCP and the channels already reject over 254.awaiting_email.Accepted risks
/loginon one bot for 10 minutes.Changes
src/backend/services/channel_login_limit.py(new): caps, hashed address key,address_allowed,login_code_request_allowedwith refund.src/backend/services/rate_limiter.py:RateLimitResult.member,refund(), per-prefix bounded in-process fallback.src/backend/routers/auth.py: web login charges the shared address budget before the whitelist check.src/backend/services/mcp_auth_service.py: MCP inline auth charges it before the known-address check.src/backend/db_models.py:EmailLoginRequest.emailmax length 254.src/backend/adapters/{telegram,whatsapp,slack}_adapter.py: calllogin_code_request_allowedbefore minting a code.tests/unit/test_ent1253_channel_login_rate_limit.py(new),tests/unit/test_848_mcp_inline_auth.py,tests/unit/conftest.py(clears limiter buckets per test),tests/test_whatsapp_integration.py(one sender per/logintest).docs/memory/architecture/security.md(rate limiting),docs/memory/feature-flows/unified-channel-access-control.md.Test Plan
cd tests && python -m pytest unit/test_ent1253_channel_login_rate_limit.py unit/test_848_mcp_inline_auth.py unit/test_186_enumeration_uniformity.py unit/test_ent849_email_code_policy.py unit/test_rate_limiter.py -q -p no:randomly" [email protected]","[email protected] ","[email protected]".check+refundthrough the Redis branch (fakeredis).Fixes abilityai/trinity-enterprise#1253
🤖 Generated with Claude Code