Skip to content

fix: the rate limiter must stop the request, not relabel it - #128

Merged
jrosskopf merged 1 commit into
mainfrom
fix/rate-limiter-actually-limits
Sep 19, 2026
Merged

jrosskopf merged 1 commit into
mainfrom
fix/rate-limiter-actually-limits

Conversation

@jrosskopf

@jrosskopf jrosskopf commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

The bug

RateLimitMiddleware set a 429 and returned, with a comment asserting Crow would handle completion:

// Set 429 response - don't call end() as Crow handles that
res.code = 429;
...
// Don't call res.end() - let Crow's middleware chain handle completion
return;

Crow's chain short-circuits only on res.is_completed() (crow/middleware.h:151), and only res.end() sets that flag. So the request carried on: auth ran, the SQL executed, and the result set was appended to the 429 body.

Measured against the shipped binary

Endpoint limit of 2 per 60s:

request 1: 200  {"data":[{"marker":"leaked-row"}]}
request 2: 200  {"data":[{"marker":"leaked-row"}]}
request 3: 429  Rate limit exceeded. Try again later.{"data":[{"marker":"leaked-row"}]}
request 4: 429  Rate limit exceeded. Try again later.{"data":[{"marker":"leaked-row"}]}

The limiter did neither of the two things it exists for:

  • It shed no load. Every rejected request still paid for auth and the full query, so the limiter offered no protection against exactly the traffic it was configured to stop.
  • It withheld no data. It returned the rows it had just refused, in the body of the refusal.

After the fix, requests 3 and 4 return the message alone and the query never runs.

The fix

res.end(). One line.

Testing

Integration tests against the real binary, deliberately not a unit test of the middleware: the defect lives in the interaction between flAPI's middleware and Crow's chain, and a middleware unit test passes against the broken code.

Four cases — a rejected request returns no rows; the limit holds for subsequent requests; requests within the limit are untouched (end() is only on the rejection branch); and the 429 still carries Retry-After and X-RateLimit-Limit per RFC 6585, since end() must not cost the headers set before it.

765 unit, 338 standalone integration, all green. No existing test modified.

Filed, not fixed here

#127 — the global rate_limit: block is documented (4 keys in CONFIG_REFERENCE.md), parsed, and read by nothing. Measured: a global limit of 2 lets request 3 and 4 straight through with no 429 at all. An operator configuring deployment-wide rate limiting today gets none. Making it live starts returning 429s to deployments that are currently unlimited, so it needs an owner decision and a release note rather than riding along here.

Found by an agent-crew review of the request hot path; every claim above verified against a running server.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

RateLimitMiddleware set a 429 and returned, with a comment saying Crow
would handle completion. Crow's middleware chain short-circuits only on
res.is_completed() (crow/middleware.h:151), and only res.end() sets that
flag - so the request carried on. Auth ran, the SQL executed, and the
result set was appended to the 429 body.

Measured against the shipped binary, endpoint limit of 2:

  request 1: 200  {"data":[{"marker":"leaked-row"}]}
  request 2: 200  {"data":[{"marker":"leaked-row"}]}
  request 3: 429  Rate limit exceeded. Try again later.{"data":[{"marker":"leaked-row"}]}
  request 4: 429  Rate limit exceeded. Try again later.{"data":[{"marker":"leaked-row"}]}

So the limiter did none of the two things it exists for: it did not shed
load - every rejected request still paid for auth and the full query - and
it did not withhold the data, handing back the rows it had just refused in
the body of the refusal.

After: requests 3 and 4 return the message alone, and the query never runs.

The fix is res.end(). The tests are an integration suite rather than a
unit test of the middleware, deliberately: the defect lives in the
interaction between flAPI's middleware and Crow's chain, and a unit test
of the middleware in isolation passes against the broken code.

Four cases: a rejected request returns no rows, the limit holds for
subsequent requests, requests within the limit are untouched, and the 429
still carries Retry-After and X-RateLimit-Limit (RFC 6585) - end() must
not cost the headers set before it.

Found by an agent-crew review of the request hot path and verified against
a running server. A separate defect found while confirming it - the global
rate_limit block is documented, parsed, and read by nothing - is filed as
#127 rather than fixed here, because making it live starts returning 429s
to deployments that are unlimited today.

765 unit tests, 338 standalone integration tests.
@jrosskopf
jrosskopf merged commit 252ebaa into main Sep 19, 2026
22 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.

1 participant