Skip to content
NestRS

Security

Two concerns, two layers — authentication answers who is calling, authorization answers what they may do, and the framework wires both transparently.

Security in nestrs splits into two concerns wired by two composable layers. Authentication establishes who the caller is. Authorization declares what they may do, then enforces it on every read, every by-id write, and every response body a unit’s posture names before it leaves the process. Bind the two guards on the controller, write the one line that says what each unit reads, and the framework does the rest: a unit naming nothing reads nothing a grant guards, so nothing fails open.

Here for the first auth task? Add login and protect a route walks the whole happy path — issue a JWT on POST /login, bind the two guards, curl the round-trip — before any of the reference below.

The authn layer leans on jsonwebtoken, argon2, and the oauth2 crate. The authz layer is the framework’s own ability-based engine, with row-level filtering bridged into SeaORM.

Concern Layer Who answers it
Who is calling? Authentication A Strategy turns the request into a principal, or says why it could not; the guard answers the 401
What may they do? Authorization An Ability declares allowed actions; the framework applies it to queries, by-id loads, and response bodies

Each layer ships its own guard. Bind them in order — the authn guard runs first and attaches the principal; the authz guard reads it and installs the ambient Ability.

crates/features/src/users/http/controller.rs (from the demo)
#[controller(path = "/users")]
#[use_guards(AuthnGuard, AuthzGuard)]
pub struct UsersController {
#[inject]
svc: Arc<UsersService>,
}

That’s the whole opt-in surface for HTTP. GraphQL, WebSockets and MCP run the same two guards at their own request, and every operation it carries reads the ability they attached — see Endpoint guards.

  • Every read is filtered. The Repo gateway every service uses joins the ambient ability’s condition_for into the SQL — a member querying Users::find() gets WHERE org_id = $caller_org_id appended.
  • Every by-id write checks access. #[authorize(A, bind = S)] calls Service::access(action, id); missing rows return 404, denied rows return 403 — see By-id binding.
  • Every wire row is judged. The exposed type a service builds from a row, User::from(&row), judges it for the caller whose ability is installed there: a column their grant withholds reads null, a row their grants refuse answers nobody, and only the columns #[expose] lists exist on the wire, so an unrestricted grant cannot leak an unexposed column. A value your body copies a column into is its own visible line.

The wiring sketch for an app that serves all three transports:

apps/api/src/module.rs
#[module(
imports = [
SeaOrmDatabaseModule,
UsersHttpModule,
UsersGraphqlModule,
UsersWsModule,
],
)]
pub struct ApiModule;

Each adapter imports what its guards need — UsersHttpModule and UsersWsModule import AuthzModule, UsersGraphqlModule the AuthzGraphqlModule that declares /graphql’s guards — so the root lists the edges it serves, and the access graph checks each import where it is used.

  • Threat model — what these layers cover and what they don’t (explanation — read before going to production, not needed for your first feature).
  • Guards — the primitive every security layer builds on.
  • Throttler — rate limiting as a guard, composes with AuthnGuard.