diff --git a/infrastructure/lib/stacks/api-stack.ts b/infrastructure/lib/stacks/api-stack.ts index 9b07c7a..01c1952 100644 --- a/infrastructure/lib/stacks/api-stack.ts +++ b/infrastructure/lib/stacks/api-stack.ts @@ -64,9 +64,9 @@ export class ApiStack extends Stack { }); // Baseline throttle protects every public/unauthenticated route from - // simple volumetric abuse; the six /auth/* routes get a much tighter - // limit since they're the highest-value target for credential - // stuffing, signup spam, and password-reset enumeration. + // simple volumetric abuse; these routes get a much tighter limit + // since they're the highest-value target for credential stuffing, + // signup spam, password-reset enumeration, and contact-form spam. const defaultStage = new HttpStage(this, 'DefaultStage', { httpApi: this.httpApi, stageName: '$default', @@ -74,22 +74,17 @@ export class ApiStack extends Stack { throttle: { rateLimit: 50, burstLimit: 100 }, }); - // Per-route throttle overrides aren't exposed on the HttpStage L2 - // (only the stage-wide `throttle` default is), so this drops to the - // L1 `RouteSettings` CFN property directly. Keep these keys in sync - // with the `/auth/*` entries in the `routes` array below. + const THROTTLED_ROUTE_KEYS = new Set([ + 'POST /auth/signup', + 'POST /auth/confirm-signup', + 'POST /auth/login', + 'POST /auth/refresh', + 'POST /auth/forgot-password', + 'POST /auth/reset-password', + 'POST /contact', + ]); const AUTH_ROUTE_THROTTLE = { ThrottlingRateLimit: 5, ThrottlingBurstLimit: 10 }; - (defaultStage.node.defaultChild as CfnStage).addPropertyOverride('RouteSettings', { - 'POST /auth/signup': AUTH_ROUTE_THROTTLE, - 'POST /auth/confirm-signup': AUTH_ROUTE_THROTTLE, - 'POST /auth/login': AUTH_ROUTE_THROTTLE, - 'POST /auth/refresh': AUTH_ROUTE_THROTTLE, - 'POST /auth/forgot-password': AUTH_ROUTE_THROTTLE, - 'POST /auth/reset-password': AUTH_ROUTE_THROTTLE, - // Same tight limit as /auth/* — the other highest-value target for - // spam since it's unauthenticated and triggers an outbound email. - 'POST /contact': AUTH_ROUTE_THROTTLE, - }); + const routeSettings: Record = {}; const authorizer = new HttpJwtAuthorizer('CognitoAuthorizer', props.userPool.userPoolProviderUrl, { jwtAudience: [props.userPoolClient.userPoolClientId], @@ -149,14 +144,30 @@ export class ApiStack extends Stack { this.applyGrants(fn, route, props); this.functions[route.id] = fn; - this.httpApi.addRoutes({ + const httpRoutes = this.httpApi.addRoutes({ path: route.path, methods: [route.method], integration: new HttpLambdaIntegration(`${route.id}Integration`, fn), authorizer: route.authenticated ? authorizer : undefined, }); + + const routeKey = `${route.method} ${route.path}`; + if (THROTTLED_ROUTE_KEYS.has(routeKey)) { + routeSettings[routeKey] = AUTH_ROUTE_THROTTLE; + // RouteSettings (set below) references these routes by key, which + // CloudFormation validates against routes that already exist — + // without this, the Stage can be created/updated before its + // Route resources, and deployment fails with "Unable to find + // Route by key ... within the provided RouteSettings". + httpRoutes.forEach((r) => defaultStage.node.addDependency(r)); + } } + // Per-route throttle overrides aren't exposed on the HttpStage L2 + // (only the stage-wide `throttle` default is), so this drops to the + // L1 `RouteSettings` CFN property directly. + (defaultStage.node.defaultChild as CfnStage).addPropertyOverride('RouteSettings', routeSettings); + new CfnOutput(this, 'ApiUrl', { value: this.httpApi.apiEndpoint }); new CfnOutput(this, 'UserPoolId', { value: props.userPool.userPoolId }); new CfnOutput(this, 'UserPoolClientId', { value: props.userPoolClient.userPoolClientId });