LeakyVibe

Guides · Updated 2026-11-19

Is my /api error response leaking a stack trace? How to check and fix it

Short answer: Check it: call one of your API routes with a wrong input and look at the response. If you see lines like "at handler (/var/task/...:12:5)", node_modules paths, a Python Traceback or a raw database error, your API is sending its internals to anyone who asks. It's not a break-in by itself, but it hands a stranger your file paths, libraries and sometimes query text or config values. The fix: catch errors in one place, return a short generic message (with an id) to the client, and log the details only on the server.

Does your app have this problem? Free scan, 15 sec, no sign-up

or watch it scan our demo app ▶

What a leaked stack trace looks like

A stack trace is the list of functions and files your server was running when it crashed. In a response it often looks like this:

{
  "error": "Cannot read properties of undefined (reading 'id')",
  "stack": "TypeError: Cannot read properties of undefined (reading 'id')\n    at handler (/var/task/.next/server/app/api/orders/route.js:1:2345)\n    at /var/task/node_modules/..."
}
  • Node.js: lines starting with "at" followed by a file path and :line:column, or paths containing node_modules/.
  • Serverless hosts: paths like /var/task/ (AWS Lambda-style functions).
  • Python: "Traceback (most recent call last)". Java: "Exception in thread".
  • Database/ORM errors passed through as-is, for example a Prisma error with the model and field names, or a raw SQL error that shows part of the query.

Why it matters

OWASP's Error Handling Cheat Sheet explains that attacks start with reconnaissance: collecting names and versions of your server, frameworks and libraries. Unhandled errors make that step easy. They can also point at injection points, for example a database error that shows how your input was put into a query.

  • File paths and folder names show how your project is built and which routes exist.
  • Library names (and sometimes versions) let someone look up known bugs for exactly what you run.
  • Error messages from your database or ORM can reveal table and column names.
  • Some apps put the whole error object, request or config into the response, which can include tokens or connection strings.

How it ends up in a vibe-coded app

A common pattern we see: the AI tool writes a try/catch that returns the error to the browser so it's easy to debug, like res.status(500).json({ error: err.message, stack: err.stack }) or return Response.json({ error: String(err) }). That's handy while building and nobody removes it before launch.

  • Express: the built-in error handler writes the stack trace to the client unless NODE_ENV is set to production. Express's docs say the body is err.stack outside production and plain HTML of the status message in production. A server started without NODE_ENV=production sends stack traces.
  • Next.js: errors thrown in Server Components are already hidden in production. The error page gets a generic message plus a digest id you can match in your server logs. But a Route Handler (app/api/.../route.ts) that catches an error and returns err.message or err.stack itself sends whatever you put in the response. Next.js can't hide that for you.
  • Debug or "verbose" modes left on in production in other frameworks do the same.

2-minute check

  1. Open your live site in a private window with DevTools (F12) > Network open.
  2. Use the app normally and note the requests to /api/... (or your Supabase Edge Function / serverless function URLs).
  3. Send one of them bad input: open the URL directly in the address bar, remove a required field, or put text where an id is expected (for example /api/orders/abc).
  4. Read the response body. A short message like {"error":"Something went wrong","id":"..."} is fine. File paths, "at ..." lines, node_modules, Traceback or a raw SQL/ORM error is a leak.
  5. Also open https://your-site/api in the browser. Some apps crash there and show the trace.

Or paste your URL in the free scan at the top of this page. Among its passive checks, it requests your site's /api path and flags the response if it contains stack-trace patterns (Node "at ...(file:line:col)" lines, node_modules/, /var/task/, Python Traceback, Java exceptions, Prisma errors). It only looks at that one path, so still test your real routes by hand.

The fix

  1. Catch errors in one place. In Express, add a final error-handling middleware (four arguments: err, req, res, next) and run with NODE_ENV=production. In Next.js Route Handlers, wrap each handler (or use a small shared helper) so every catch returns the same safe response.
  2. Return a generic message and a status code to the client: 400 for bad input you expected, 401/403 for auth, 404 for not found, 500 for real bugs. Add a random error id so you can find the log line. Never include err.stack, the raw error object, request headers or config.
  3. Log the full details on the server only (console.error on Vercel/Netlify ends up in your function logs, or send them to your error tracker).
  4. Treat expected errors as normal responses. Next.js's docs recommend returning expected errors (failed validation, failed requests) as values instead of throwing.
  5. Re-run the 2-minute check on the live site after deploying.
// app/api/orders/route.ts (Next.js) - safe pattern
export async function POST(req: Request) {
  try {
    // ... your logic
    return Response.json({ ok: true });
  } catch (err) {
    const id = crypto.randomUUID();
    console.error("orders POST failed", id, err); // details stay in server logs
    return Response.json({ error: "Something went wrong", id }, { status: 500 });
  }
}

A prompt you can give your AI tool

My API routes return error details (err.message, err.stack or raw database errors) to the browser. Find every API route, server action and serverless/edge function that sends an error object, message or stack to the client. Replace them with one shared error helper that logs the full error server-side with a random id and returns only a generic JSON message, the id and the right status code (400/401/403/404/500). Expected validation errors should return a short, safe message. Make sure production runs with NODE_ENV=production. Don't change business logic. List the files you changed.

Sources

Official sources (checked 2026-11-19): OWASP Cheat Sheet Series, "Error Handling Cheat Sheet" (https://cheatsheetseries.owasp.org/cheatsheets/Error_Handling_Cheat_Sheet.html): unhandled errors help reconnaissance and can reveal injection points; use a global error handler that returns a generic response and logs details server-side. Express, "Error Handling" (https://expressjs.com/en/guide/error-handling.html): the default error handler writes the stack trace to the client except in production (NODE_ENV=production). Next.js, "error.js" (https://nextjs.org/docs/app/api-reference/file-conventions/error): errors from Server Components show a generic message with a digest in production. Next.js, "Error Handling" (https://nextjs.org/docs/app/getting-started/error-handling): model expected errors as return values.

Check your app for this now

Free scan · about 15 seconds · no sign-up

Passive, read-only checks. Only scan apps you own or have permission to test.