Skip to content
engineering workbench.by StudyPort Academy
All errors
logic · DEBUG LIBRARY

A missing rate limit lets an endpoint be hammered

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

An endpoint with no limit accepts every call that reaches it. A script can repeat that call as fast as the network allows. For a cheap read, the damage is load on the process and the database. For a route that calls a paid model, each accepted request can spend money, so the same flood is a cost blowout and not only a slow server. The handler can be correct on one manual request and still be unsafe to leave open. Nothing in the happy-path tells you the route will keep saying yes. A counter that runs after the model call, or after the insert, still does the expensive work and then complains. The refusal arrived too late to matter.

Minimal reproduction

Before

app.post('/summaries', async (req, res) => {
  const summary = await complete(req.body.text);
  res.json({ summary: summary });
});

The failure is silent or crashes.

The fix

Count the call, or the estimated cost, before the expensive line. A small in-memory window can return 429 once a caller passes a request limit, and an AI route should also refuse when the next call would pass a budget. Put that branch above fetch and above the write. Return 429 with a stable error body and do not call the provider on that path. Log the refusal with the route name so a structured log and the Debug Library describe the same event. A generic limit on every route is still worth having. It does not replace a budget on the route whose yes costs money. Re-test by starting the bucket at the limit and confirming the provider was not contacted.

After

app.post('/summaries', async (req, res) => {
  if (spent + cost > limit) return res.status(429).json({ error: 'AI budget exceeded' });
  const summary = await complete(req.body.text);
  res.json({ summary: summary });
});

The check happens before the damage.