Skip to content

fix(traffic): check a bot sender's secret before reading its body, and rate-limit unknown secrets by IP - #1222

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/bot-ingest-auth-first
Oct 20, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/bot-ingest-auth-first

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

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.js mounted express.raw({ limit: '20mb' }) on /bots/ingest before everything else. A caller without a secret could therefore make the server take in up to 20 MB before the 401.
  • The route is now mounted before the app's JSON and form parsers, and runs in this order:
    1. botIngestLimiter
    2. sender: the secret lookup, which answers 401 for an unknown secret
    3. the raw body read
    4. the handler
  • An unknown secret now gets its 401 without the body being read. Node discards the unread bytes; it doesn't buffer them.

2. Made-up secrets each got their own bucket.

  • botIngestKey hashed any Bearer string. A flood of random secrets therefore got a fresh bucket per request, plus a lookup each.
  • Only a secret the route has matched to a live connection now earns the bucket bot:<connection id>.
  • An unknown secret, or no secret, counts against the caller's IP (anonymousIpKey).
  • This uses the same idea as the dashboard's verified tokens (verified-tokens.js). It is refactored into a small verifiedCache() with a separate instance for bot senders, so a bot secret never earns a dashboard user's bucket.
  • Each accepted request marks the secret again, so a sender that keeps sending keeps its bucket. A sender's first request in 30 minutes counts against its IP.

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 feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Unit tests: cd server && npx vitest run src/routes/bot-ingest.test.js src/middleware/rate-limiter.test.js. They cover:

  • 401 rather than 413 for an unknown secret with a 21 MB body;
  • a 500 on a failed lookup, without reading the body;
  • a matched secret taking its connection's bucket;
  • made-up secrets sharing the IP bucket;
  • a bot secret never getting a dashboard user's bucket.

Locally, I mounted the branch's route alone on :8081 against the dev DB (no schedulers) and sent real requests:

Request Result
No secret 401
Made-up secret with a 21 MB body 401 (it was 413 before)
Two made-up secrets from one address RateLimit-Remaining 597, then 596: one shared bucket
A temporary real connection, 1st request 200, counted in the IP bucket (595)
The same connection, 2nd request its own bucket (599)

The temporary connection was deleted afterwards.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): web not touched
  • yarn typecheck passes (run from web/): web not touched
  • yarn format:check passes (run from web/): web not touched
  • Changes are focused — one concern per PR

Server: npm run format, npm run lint and npm test (1823 passed) all pass.

🤖 Generated with Claude Code

…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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk This is the first of the AI Traffic follow-ups. It covers two of #1179's notes:

  • Limiter key: an unknown secret now shares the caller's IP bucket.
  • Buffered body: the secret is checked before the body is read.

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.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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's flush ignores the 429, so the batch is lost.
  • Loading the live secret_hash set, refreshed periodically, would give a real secret its own bucket on the first request. A smaller max for ip: keys would also cut the lookup cost.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Server tests pass on main with #1223 (1,832). The shared-egress note above can be a follow-up.

@gkhngyk
gkhngyk merged commit a661f62 into ansvisor:main Oct 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants