Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions infrastructure/lib/stacks/api-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,27 @@ 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',
autoDeploy: true,
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<string, typeof AUTH_ROUTE_THROTTLE> = {};

const authorizer = new HttpJwtAuthorizer('CognitoAuthorizer', props.userPool.userPoolProviderUrl, {
jwtAudience: [props.userPoolClient.userPoolClientId],
Expand Down Expand Up @@ -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 });
Expand Down
Loading