Stack

How to security test a NestJS API

Updated

In NestJS, a controller inherits a guard only if you attach it, so the test is to walk every route and confirm the guard actually runs. Then check the @Public() exceptions, because a decorator added to unblock a webhook stays there forever.

Where this class of bug actually lives in a NestJS API

  • Controllers without @UseGuards. A global guard registered in APP_GUARD covers everything, a controller-level one covers that controller, and the one added without either is open.
  • @Public() exceptions. Webhooks and health checks need them. The route that borrows the decorator to skip a failing test does not.
  • DTOs without whitelist: true. Without it, ValidationPipe validates the fields it knows and passes the rest through to your service.
  • Param ids. @Param('id') reaches the service unfiltered, and the service usually does findOne(id).

What to try first

  1. List routes from the Nest route explorer log and request each with no token, watching for 200s.
  2. Enumerate @Public() in the codebase and test each one as an anonymous caller.
  3. POST extra fields to every DTO-backed endpoint and check whether they persist.
  4. Cross a tenant by swapping an id in a path or in a body.

A reproduction you can keep

The guard gap plus an id swap is the check. Nest returns different bodies for a real and a fake id, which makes the leak obvious:

Unguarded controller, foreign id
curl -sS -i https://staging.example.com/invoices/1003
curl -sS -i https://staging.example.com/invoices/99999999

# vulnerable: 200 with data for 1003, 404 for the fake id
# fixed: 401 for both with no token, 404 for both with a token

What "fixed" has to look like

Register the guard globally and use an explicit opt-out list, so a new controller is protected before anyone remembers to protect it. The fixed result is 401 with no token for every route not on the opt-out list, verified route by route.

Where people get this wrong

  • A guard that returns true when the request has no user is an authentication bypass that reads like a feature flag.
  • class-validator with forbidNonWhitelisted: false silently drops fields, which hides the bug instead of fixing it — the field still reaches the service if you read the raw body.
  • Microservice message handlers do not pass through HTTP guards at all.

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

Are NestJS routes protected by default?

No. Nothing is protected until a guard is registered globally, on the controller, or on the handler.

Does ValidationPipe stop mass assignment?

Only with whitelist: true and forbidNonWhitelisted: true, and only if the service uses the validated DTO rather than the raw body.

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