Security Audits
Case Study: How a Missing Middleware Caused a $12k API Bill in AI-Generated Code
An AI document summarizer hit 500 users overnight. 48 hours later the founder woke up to a $12,400 OpenAI bill. Here is what went wrong, and the 15 lines that would have prevented it.
CodeRescue.Help Engineering · · Stack: Cursor, Node.js, Express, OpenAI API
The Problem
A founder launched an AI document summarizer built with Cursor. The app worked smoothly, gained traction on Reddit, and hit 500 active users overnight.
The architecture was simple: a React frontend, one Express backend, and a single route, POST /api/generate, that sent each uploaded document to GPT-4 and returned a summary.
48 hours later, the founder woke up to a $12,400 OpenAI API bill.
The Vulnerability
The AI coding assistant had produced a working generation route, but it left out everything that protects it in production:
- No authentication. The route accepted requests from anyone who found the URL in the frontend bundle.
- No rate limiting. There was no limit per user, per IP, or overall.
- No spending guardrails. No
max_tokens, no per-user quota, and no hard cap on the provider account.
A bot discovered the unauthenticated endpoint and looped 100,000 heavy GPT-4 queries through the API. Every one of them was billed to the founder.
This is the pattern we see again and again in AI-generated code: the happy path is complete, the abuse path does not exist.
The Fix
This could have been prevented with about 15 lines of middleware: require a signed-in user, limit how often each user can call the expensive route, and cap the size of each request.
import express from 'express';
+import rateLimit from 'express-rate-limit';
+import { requireAuth } from './auth.js';
const app = express();
+const generateLimiter = rateLimit({
+ windowMs: 60 * 1000, // 1 minute
+ limit: 10, // 10 generations per user per minute
+ keyGenerator: (req) => req.user.id,
+ standardHeaders: 'draft-7',
+});
+
-app.post('/api/generate', async (req, res) => {
+app.post('/api/generate', requireAuth, generateLimiter, async (req, res) => {
const summary = await openai.chat.completions.create({
model: 'gpt-4',
+ max_tokens: 1024,
messages: buildPrompt(req.body.document),
});
res.json({ summary });
});
On top of the code change, we recommend two settings outside the codebase:
- A hard monthly spending limit and usage alerts in the AI provider’s dashboard.
- A per-user token quota stored with the account, so one heavy user cannot drain the budget either.
When we perform our Health Check, we don’t just run automated tools. We manually review your routes, infrastructure configs, and API secrets to spot exactly these vulnerabilities before launch.