Repository navigation
fix(traffic): check a bot sender's secret before reading its body, and rate-limit unknown secrets by IP - #1222
Merged
Conversation
…d rate-limit unknown secrets by IP /bots/ingest read up to 20 MB into memory before the limiter and the secret check, so a caller without a secret could make the server take in the whole body. The route is now mounted before the app's body parsers and runs limiter, then the secret lookup, then the raw body read. The limiter keyed every Bearer string by its hash, so made-up secrets got a fresh bucket, and a lookup, per request. Only a secret the route has matched to a live connection now earns the connection's bucket (a cache like the dashboard's verified tokens, kept apart from it); an unknown one counts against the caller's IP. Follow-up to ansvisor#1179's review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
Author
|
@gkhngyk This is the first of the AI Traffic follow-ups. It covers two of #1179's notes:
Server only, no migration. The other two #1179 notes (the partial-failure recount and the batched prunes) need a migration. I'll send them next, as a separate PR. |
This was referenced Oct 19, 2026
Merged
gkhngyk
reviewed
Oct 20, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
The secret is now checked before the body is read. The router sits before the global body parsers, and the 401/500 paths never read the body.
One trade-off for a follow-up:
- Shared Cloudflare egress. A real secret gets its connection's bucket only after one accepted request, and that cache lives in memory for 30 minutes.
- So after a deploy or restart, and for a Worker that sends less than once per 30 minutes, the first request lands in the IP bucket. Those IPs are shared across Cloudflare customers.
- Anyone posting made-up
abot_secrets from the same egress can hold that bucket at 600 a minute. The Worker'sflushignores the 429, so the batch is lost. - Loading the live
secret_hashset, refreshed periodically, would give a real secret its own bucket on the first request. A smallermaxforip:keys would also cut the lookup cost.
gkhngyk
added a commit
to cihan-gyk/ansvisor
that referenced
this pull request
Oct 20, 2026
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
Two follow-ups from #1179's review: the limiter key and the buffered body.
1. The body was read before the secret was checked.
server.jsmountedexpress.raw({ limit: '20mb' })on/bots/ingestbefore everything else. A caller without a secret could therefore make the server take in up to 20 MB before the 401.botIngestLimitersender: the secret lookup, which answers 401 for an unknown secret2. Made-up secrets each got their own bucket.
botIngestKeyhashed any Bearer string. A flood of random secrets therefore got a fresh bucket per request, plus a lookup each.bot:<connection id>.anonymousIpKey).verified-tokens.js). It is refactored into a smallverifiedCache()with a separate instance for bot senders, so a bot secret never earns a dashboard user's bucket.Server only; no migration.
The other two #1179 notes, the partial-failure recount and the unbatched prunes, need a migration. They come in a separate PR.
Related issue
Follow-up to #1179 (review of 2026-10-19 17:39 UTC).
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Unit tests:
cd server && npx vitest run src/routes/bot-ingest.test.js src/middleware/rate-limiter.test.js. They cover:Locally, I mounted the branch's route alone on :8081 against the dev DB (no schedulers) and sent real requests:
RateLimit-Remaining597, then 596: one shared bucketThe temporary connection was deleted afterwards.
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/): web not touchedyarn typecheckpasses (run fromweb/): web not touchedyarn format:checkpasses (run fromweb/): web not touchedServer:
npm run format,npm run lintandnpm test(1823 passed) all pass.🤖 Generated with Claude Code