diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index bab64c6d7..316d1bc43 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -4,6 +4,7 @@ import { ScheduleModule } from '@nestjs/schedule'; import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler'; import { AICoreModule } from './ai-core/ai-core.module.js'; import { AppController } from './app.controller.js'; +import { IRequestWithCognitoInfo } from './authorization/cognito-decoded.interface.js'; import { GlobalDatabaseContext } from './common/application/global-database-context.js'; import { BaseType, UseCaseType } from './common/data-injection.tokens.js'; import { AIModule } from './entities/ai/ai.module.js'; @@ -65,6 +66,14 @@ import { GetHelloUseCase } from './use-cases-app/get-hello.use.case.js'; limit: 200, }, ], + // Skip rate limiting for internal service-to-service calls: they are + // gated by SaaSAuthMiddleware (microservice JWT), which flags the request. + // All such calls share the satellites' IPs, so a per-IP limit would + // throttle them collectively; auth already bounds who can make them. + skipIf: (context) => { + const req = context.switchToHttp().getRequest(); + return req.isMicroserviceRequest === true; + }, }), CedarAuthorizationModule, AICoreModule, diff --git a/backend/src/authorization/cognito-decoded.interface.ts b/backend/src/authorization/cognito-decoded.interface.ts index 49ba057dc..6c7350e80 100644 --- a/backend/src/authorization/cognito-decoded.interface.ts +++ b/backend/src/authorization/cognito-decoded.interface.ts @@ -29,4 +29,10 @@ export interface IRequestWithCognitoInfo extends Omit; params: Record; decoded: Partial; + /** + * Set by `SaaSAuthMiddleware` once a request has passed microservice-JWT auth. + * The global throttler's `skipIf` reads this to exempt internal service-to-service + * calls from rate limiting (they all originate from the same satellite IPs). + */ + isMicroserviceRequest?: boolean; } diff --git a/backend/src/authorization/saas-auth.middleware.ts b/backend/src/authorization/saas-auth.middleware.ts index cceb0e8c5..682ac6d45 100644 --- a/backend/src/authorization/saas-auth.middleware.ts +++ b/backend/src/authorization/saas-auth.middleware.ts @@ -27,6 +27,9 @@ export class SaaSAuthMiddleware implements NestMiddleware { } req.decoded = data; + // Mark the request as an internal service-to-service call so the global + // throttler skips it (see ThrottlerModule `skipIf` in app.module). + req.isMicroserviceRequest = true; next(); } catch (_e) { throw new UnauthorizedException(Messages.AUTHORIZATION_REJECTED);