Skip to content

fix(core,microservices): run transient global enhancers on controllers - #17993

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
johnnyhuirilef:fix/transient-global-enhancers
Oct 8, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
johnnyhuirilef:fix/transient-global-enhancers

Conversation

@johnnyhuirilef

Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

Issue Number: #17991

Closes #17991

Hi! πŸ‘‹ A global guard, pipe, interceptor or filter with transient scope never runs on a static controller. This includes the message handlers of microservice controllers. Nest creates it, but the handler runs without it. A transient global auth guard leaves every route on a static controller open.

The router and the microservice listener build the handlers of a static controller without an inquirer id. getGlobalMetadata() returns early in that case and never reads the transient enhancers. The injector also does not create a per-controller instance for a static controller.

I ran the repro on 12.1.2:

HTTP route + transient global guard that denies   ->  HTTP 200
TCP message + transient global guard that denies  ->  handler ran
RESULT: BUG: 2 of 5 cases ran the handler without the guard.

What is the new behavior?

The injector now loads the enhancers of a static controller with the controller as inquirer. The router and the microservice listener pass the controller id when they build its handlers. A transient global enhancer now gets one constructed instance per controller, the same as a transient dependency. Each controller keeps its instance across requests, and INQUIRER is the controller.

The same repro against a local build of this branch:

RESULT: FIXED: the guard denied the request in every case.

Does this PR introduce a breaking change?

  • Yes
  • No

Default and request-scoped global enhancers behave as before. Nothing exported changes.

Two behavior changes are worth a note:

  • A transient enhancer set with @UseGuards(), @UsePipes(), @UseInterceptors() or @UseFilters() on a static controller now gets one constructed instance per controller. Before, the controllers shared one instance, and its constructor never ran, so injected dependencies were undefined. The spec covers a method-level guard. I also checked a controller-level guard, pipe and interceptor by hand.
  • Nest still creates the startup instance of a transient global enhancer, as before. With this fix, it also creates one instance per static controller. So the constructor and onModuleInit run once more for each static controller. Three existing assertions in transient-scope.spec.ts count this extra instance (4 β†’ 5).

Other information

The specs use a real app with real HTTP requests and a real TCP microservice. They cover these cases:

  • A transient global guard with a dependency denies on HTTP and on TCP.
  • A transient global pipe, interceptor and filter apply on HTTP and on TCP.
  • Default and request-scoped global guards still deny.
  • Each controller gets its own instance of a transient global guard and keeps it across requests.
  • A method-level transient guard gets one constructed instance per controller.

One case is out of scope. Static GraphQL resolvers call ExternalContextCreator.create() without an inquirer id, so a transient global guard still does not run there. A fix there needs @nestjs/graphql to pass the resolver as inquirer, and it can also need a small change in core to load the enhancers for resolvers. I am happy to open an issue for it. πŸ™‚

@micalevisk micalevisk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address the comments left by github-advanced-security

A global guard, pipe, interceptor or filter with transient scope never
ran on a static controller. Nest created it, but the route and the
message handler ran without it. A transient global auth guard left every
such route open.

The router and the microservice listener build the handlers of a static
controller without an inquirer id. The global lookup returns early in
that case and never reads the transient enhancers. The injector also did
not create a per-controller instance for a static controller.

The injector now loads the enhancers of a static controller with the
controller as inquirer. The router and the listener pass the controller
id when they build its handlers. A transient global enhancer now gets
one instance per controller, the same as a transient dependency. Each
controller keeps its instance across requests.

A transient enhancer set on a controller or a method now gets one
constructed instance per controller too. Before, the controllers shared
one instance whose constructor never ran.

Default and request scoped global enhancers behave as before.

Closes nestjs#17991
@johnnyhuirilef
johnnyhuirilef force-pushed the fix/transient-global-enhancers branch from 1e57794 to 2465226 Compare October 2, 2026 21:27
@johnnyhuirilef

Copy link
Copy Markdown
Contributor Author

Thanks @micalevisk! πŸ™ Good catch.

The test route returned the name query param as the response body. I replaced it with a custom param decorator that returns a constant (hello), so no request value reaches the response now. The test still proves that the transient global pipe runs, because it expects hello-piped. Without the fix in router-explorer.ts, it gets hello.

@kamilmysliwiec
kamilmysliwiec merged commit 037b052 into nestjs:master Oct 8, 2026
4 checks passed
kamilmysliwiec added a commit that referenced this pull request Oct 16, 2026
@nestjs/graphql builds the handlers of a static resolver through
ExternalContextCreator.create() without an inquirer id, so the context
creators returned only the singleton global enhancers, and
Injector#loadProvider() never constructed the transient enhancers of a
static provider. A transient APP_GUARD never ran on such resolvers,
while it still guarded the REST routes of the same application, and a
transient APP_INTERCEPTOR was not applied.

As #17993 did for controllers, the bootstrap load of a static entry
provider now constructs its transient enhancers with the provider as
the inquirer, and ExternalContextCreator.create() defaults a missing
inquirer id to that provider's wrapper id. Transient global guards and
interceptors now run on default-scoped resolvers (a call the guard
denies is rejected), and a transient guard bound with @UseGuards() to
a resolver method gets a constructed instance.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-J6VYP0AI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global guards with transient scope never run on static controllers

4 participants