Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Added rate limiter for API key

Yevhenii K29f31a3a6383
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index c1e267771..82074d4d6 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -1,5 +1,5 @@
import {
- Controller, Query, Req, Body, Param, Get, Post, Put, Delete, HttpCode, BadRequestException, UseGuards, MethodNotAllowedException, ConflictException,
+ Controller, Query, Req, Body, Param, Get, Post, Put, Delete, HttpCode, BadRequestException, UseGuards, MethodNotAllowedException, ConflictException, Headers, Ip
} from '@nestjs/common'
import { Request } from 'express'
import { ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger'
@@ -36,6 +36,7 @@ import { AuthService } from '../auth/auth.service'
import { LetterTemplate } from 'src/mailer/letter'
import { AppLoggerService } from 'src/logger/logger.service'
import { UserProfileDTO } from './dto/user.dto'
+import { checkRateLimit } from 'src/common/utils'
dayjs.extend(utc)
@@ -98,11 +99,15 @@ export class UserController {
@UseGuards(RolesGuard)
@UseGuards(SelfhostedGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
- async generateApiKey(@CurrentUserId() userId: string): Promise<{
+ async generateApiKey(@CurrentUserId() userId: string, @Headers() headers, @Ip() reqIP): Promise<{
apiKey: string
}> {
this.logger.log({ userId }, 'POST /user/api-key')
+ const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''
+
+ await checkRateLimit(ip, 'generate-api-key', 5, 3600)
+
const user = await this.userService.findOne(userId)
if (!_isNull(user.apiKey)) {