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!

fix potential security issues

Blue Mousee979d43ed086
diff --git a/backend/apps/cloud/src/captcha/captcha.controller.ts b/backend/apps/cloud/src/captcha/captcha.controller.ts
index 1531e4299..dcc8906a9 100644
--- a/backend/apps/cloud/src/captcha/captcha.controller.ts
+++ b/backend/apps/cloud/src/captcha/captcha.controller.ts
@@ -12,7 +12,7 @@ import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import { AppLoggerService } from '../logger/logger.service'
-import { getIPFromHeaders } from '../common/utils'
+import { getIPFromHeaders, checkRateLimit } from '../common/utils'
import { CaptchaService, DUMMY_PIDS } from './captcha.service'
import { BotDetectionGuard } from '../common/guards/bot-detection.guard'
import { BotDetection } from '../common/decorators/bot-detection.decorator'
@@ -22,6 +22,13 @@ import { GenerateDto } from './dtos/generate.dto'
dayjs.extend(utc)
+// Rate limit: 30 requests per IP per minute for challenge generation
+const CAPTCHA_GENERATE_RL_REQUESTS_IP = 30
+const CAPTCHA_GENERATE_RL_TIMEOUT = 60 // 1 minute
+
+// Rate limit: 100 requests per project per minute for challenge generation
+const CAPTCHA_GENERATE_RL_REQUESTS_PID = 100
+
@Controller({
version: '1',
path: 'captcha',
@@ -36,10 +43,29 @@ export class CaptchaController {
@HttpCode(200)
@UseGuards(BotDetectionGuard)
@BotDetection()
- async generateCaptcha(@Body() generateDTO: GenerateDto): Promise<any> {
+ async generateCaptcha(
+ @Body() generateDTO: GenerateDto,
+ @Headers() headers,
+ @Ip() reqIP,
+ ): Promise<any> {
this.logger.log({ generateDTO }, 'POST /captcha/generate')
const { pid } = generateDTO
+ const ip = getIPFromHeaders(headers) || reqIP || ''
+
+ // Rate limit by IP and PID to prevent abuse
+ await checkRateLimit(
+ ip,
+ 'captcha-generate',
+ CAPTCHA_GENERATE_RL_REQUESTS_IP,
+ CAPTCHA_GENERATE_RL_TIMEOUT,
+ )
+ await checkRateLimit(
+ pid,
+ 'captcha-generate',
+ CAPTCHA_GENERATE_RL_REQUESTS_PID,
+ CAPTCHA_GENERATE_RL_TIMEOUT,
+ )
await this.captchaService.validatePIDForCAPTCHA(pid)
diff --git a/backend/apps/cloud/src/captcha/captcha.service.ts b/backend/apps/cloud/src/captcha/captcha.service.ts
index 394acbcee..4fb8c054d 100644
--- a/backend/apps/cloud/src/captcha/captcha.service.ts
+++ b/backend/apps/cloud/src/captcha/captcha.service.ts
@@ -294,6 +294,16 @@ export class CaptchaService {
difficulty = parseInt(storedData, 10)
}
+ // Validate difficulty to prevent bypass via NaN or invalid values
+ if (
+ typeof difficulty !== 'number' ||
+ !Number.isFinite(difficulty) ||
+ difficulty < 1 ||
+ difficulty > 6
+ ) {
+ throw new BadRequestException('Invalid challenge difficulty')
+ }
+
// If the challenge was bound to a specific project, verify it matches
if (storedPid !== undefined && storedPid !== pid) {
throw new BadRequestException(