Stack

How to security test an Express API

Updated

In Express, authorization is a middleware you have to remember to attach to every router, and the router you write last is the one that forgets. Test by listing every route the app registers and requesting each one with no token, then with a low-privilege token and another user's id.

Where this class of bug actually lives in an Express app

  • Router ordering. app.use('/api', publicRouter) placed before the auth middleware serves the whole public router unauthenticated, and nothing warns you.
  • req.body spread into an update. User.update(req.body) lets a client set role, orgId or credits. This is the single most common Express bug.
  • Path parameters trusted as identity. req.params.userId in a handler that never compares it to req.user.id.
  • Error handler leakage. An unhandled rejection that returns a stack trace names your files, your ORM and sometimes your query.

What to try first

  1. Dump the route table — app._router.stack in a dev build, or express-list-endpoints — and request every path with no Authorization header.
  2. For each write endpoint, add "role":"admin" and "orgId":"<other>" to an otherwise valid body and diff the response.
  3. Swap the id in a /api/<resource>/:id path for one belonging to another account and compare the status code.
  4. Send a malformed body to trigger the error handler and read the response for a stack trace.

A reproduction you can keep

Mass assignment is the highest-yield check on an Express API. One extra field in a valid request proves it:

Mass assignment on a profile update
curl -sS -i -X PATCH https://staging.example.com/api/users/me \
  -H 'Authorization: Bearer ***' \
  -H 'Content-Type: application/json' \
  -d '{"name":"Alice","role":"admin","orgId":"org_victim"}'

# then confirm what stuck
curl -sS https://staging.example.com/api/users/me -H 'Authorization: Bearer ***'

# vulnerable: role now reads "admin"
# fixed: role unchanged, and the response does not echo the fields you sent

What "fixed" has to look like

The handler must whitelist the fields it accepts, so the fixed response is either a 400 on the unknown fields or a 200 with role and orgId unchanged when you read the record back. Confirm by re-reading, not by trusting the PATCH response.

Where people get this wrong

  • A guard applied with router.use() after the routes it should protect does nothing for those routes.
  • Zod or Joi validation that strips unknown keys fixes mass assignment, but only if the parsed result is what gets saved. Validation that runs and is then ignored is ceremony.
  • Rate limiting is not authorization. A 429 after 100 attempts is still 100 attempts.

What Offense does here

Offense runs an agent swarm against your app in a disposable sandbox, keeps the request, the identity and the vulnerable response as evidence, and replays the same attack after the fix so the retest is a control comparison rather than a green check. It does not read your source and it does not patch your code: the finding goes to your coding agent with the reproduction attached. The hosted workspace is invite-only — join the waitlist — and the free dependency audit in /install needs no account.

Questions this page answers

Why do some Express routes miss the auth middleware?

Because middleware only applies to routes registered after it on the same router. A public router mounted before the guard, or a route added to app instead of the guarded router, is simply unauthenticated.

Is <code>req.body</code> safe to pass to an ORM update?

No. It is whatever the client sent. Pick the fields explicitly; anything else is a mass-assignment bug.

Written by Offense Research, reviewed December 2, 2026. Correct something? Email imran@offenseresearch.com.