-
Notifications
You must be signed in to change notification settings - Fork 0
feat: rate limit authentication endpoints #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,10 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { Module, DynamicModule } from '@nestjs/common'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { APP_GUARD } from '@nestjs/core'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { TypeOrmModule } from '@nestjs/typeorm'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { ConfigModule, ConfigService } from '@nestjs/config'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { CacheModule } from '@nestjs/cache-manager'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { ScheduleModule } from '@nestjs/schedule'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { redisStore } from 'cache-manager-redis-yet'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { UsersModule } from './modules/users/users.module'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { AuthModule } from './modules/auth/auth.module'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -36,6 +38,17 @@ if (!isTest) { | |||||||||||||||||||||||||||||||||||||||||||||||
| ConfigModule.forRoot({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| isGlobal: true, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ThrottlerModule.forRootAsync({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| imports: [ConfigModule], | ||||||||||||||||||||||||||||||||||||||||||||||||
| inject: [ConfigService], | ||||||||||||||||||||||||||||||||||||||||||||||||
| useFactory: (configService: ConfigService) => [ | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| name: 'default', | ||||||||||||||||||||||||||||||||||||||||||||||||
| ttl: configService.get<number>('THROTTLE_TTL', 60000), | ||||||||||||||||||||||||||||||||||||||||||||||||
| limit: configService.get<number>('THROTTLE_LIMIT', 100), | ||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+50
|
||||||||||||||||||||||||||||||||||||||||||||||||
| useFactory: (configService: ConfigService) => [ | |
| { | |
| name: 'default', | |
| ttl: configService.get<number>('THROTTLE_TTL', 60000), | |
| limit: configService.get<number>('THROTTLE_LIMIT', 100), | |
| }, | |
| ], | |
| useFactory: (configService: ConfigService) => { | |
| const throttleTtl = Number( | |
| configService.get<string | number>('THROTTLE_TTL', 60000), | |
| ); | |
| const throttleLimit = Number( | |
| configService.get<string | number>('THROTTLE_LIMIT', 100), | |
| ); | |
| return [ | |
| { | |
| name: 'default', | |
| ttl: Number.isFinite(throttleTtl) ? throttleTtl : 60000, | |
| limit: Number.isFinite(throttleLimit) ? throttleLimit : 100, | |
| }, | |
| ]; | |
| }, |
Copilot
AI
Apr 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the default ThrottlerGuard without proxy/trust configuration means the rate-limit key is typically derived from req.ip. If this service is deployed behind a reverse proxy/load balancer, all clients may appear to come from the proxy IP unless trust proxy/X-Forwarded-For handling is configured. Consider using a custom ThrottlerGuard (override tracker) and/or ensuring the underlying HTTP adapter is configured to trust the proxy in production.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import { | |
| ApiBody, | ||
| ApiBearerAuth, | ||
| } from '@nestjs/swagger'; | ||
| import { Throttle } from '@nestjs/throttler'; | ||
| import { AuthService } from './auth.service'; | ||
| import { LocalAuthGuard } from './local-auth.guard'; | ||
| import { JwtAuthGuard } from './jwt-auth.guard'; | ||
|
|
@@ -43,6 +44,12 @@ export class AuthController { | |
| }) | ||
| @ApiResponse({ status: 200, description: 'Successfully logged in' }) | ||
| @ApiResponse({ status: 401, description: 'Invalid credentials' }) | ||
| @Throttle({ | ||
| default: { | ||
| ttl: parseInt(process.env['AUTH_LOGIN_THROTTLE_TTL'] ?? '60000'), | ||
| limit: parseInt(process.env['AUTH_LOGIN_THROTTLE_LIMIT'] ?? '10'), | ||
| }, | ||
|
Comment on lines
+47
to
+51
|
||
| }) | ||
| @UseGuards(LocalAuthGuard) | ||
| @Post('login') | ||
| async login(@Request() req: ExpressRequest) { | ||
|
Comment on lines
+47
to
55
|
||
|
|
@@ -52,7 +59,12 @@ export class AuthController { | |
| @ApiOperation({ summary: 'Register new user' }) | ||
| @ApiResponse({ status: 201, description: 'User successfully registered' }) | ||
| @ApiResponse({ status: 400, description: 'Invalid input data' }) | ||
| // Registration Route: No guards required | ||
| @Throttle({ | ||
| default: { | ||
| ttl: parseInt(process.env['AUTH_REGISTER_THROTTLE_TTL'] ?? '60000'), | ||
| limit: parseInt(process.env['AUTH_REGISTER_THROTTLE_LIMIT'] ?? '5'), | ||
| }, | ||
|
Comment on lines
+62
to
+66
|
||
| }) | ||
| @Post('register') | ||
| async register(@Body() userDto: UserDto) { | ||
| return this.authService.register(userDto); | ||
|
|
@@ -88,6 +100,12 @@ export class AuthController { | |
| description: | ||
| 'If an account with that email exists, a password reset link has been sent', | ||
| }) | ||
| @Throttle({ | ||
| default: { | ||
| ttl: parseInt(process.env['AUTH_FORGOT_THROTTLE_TTL'] ?? '60000'), | ||
| limit: parseInt(process.env['AUTH_FORGOT_THROTTLE_LIMIT'] ?? '5'), | ||
| }, | ||
|
Comment on lines
+103
to
+107
|
||
| }) | ||
| @Post('forgot-password') | ||
| async forgotPassword(@Body() forgotPasswordDto: ForgotPasswordDto) { | ||
| return this.authService.requestPasswordReset(forgotPasswordDto.email); | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example config calls these values “TTL in milliseconds”. To avoid misconfiguration, consider making the unit expectation unambiguous (e.g., rename vars to *_TTL_MS or adjust the comment to match the throttler’s expected units) and ensure the values used in AppModule/AuthController are consistent with that unit.