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!

Add missing captcha secret key gen endpoints

Blue Mouse18119890f094
diff --git a/backend/apps/community/src/common/utils.ts b/backend/apps/community/src/common/utils.ts
index 159ec8b75..2394b2804 100644
--- a/backend/apps/community/src/common/utils.ts
+++ b/backend/apps/community/src/common/utils.ts
@@ -2,6 +2,7 @@ import { NotFoundException, HttpException } from '@nestjs/common'
import path from 'path'
import fs from 'fs'
import crypto from 'crypto'
+import randomstring from 'randomstring'
import { CityResponse, Reader } from 'maxmind'
import timezones from 'countries-and-timezones'
import _join from 'lodash/join'
@@ -109,6 +110,9 @@ export const generateRandomId = (alphabet: string, size: number) => {
return id
}
+export const generateRandomString = (length: number): string =>
+ randomstring.generate(length)
+
const RATE_LIMIT_REQUESTS_AMOUNT = 3
const RATE_LIMIT_TIMEOUT = 86400 // 24 hours
@@ -123,6 +127,7 @@ const ALLOWED_KEYS = [
'passwordHash',
'botsProtectionLevel',
'websiteUrl',
+ 'captchaSecretKey',
]
const CLICKHOUSE_PROJECT_UPDATABLE_KEYS = [...ALLOWED_KEYS, 'adminId']
diff --git a/backend/apps/community/src/project/project.controller.ts b/backend/apps/community/src/project/project.controller.ts
index 8b1efecd3..3ce2642e3 100644
--- a/backend/apps/community/src/project/project.controller.ts
+++ b/backend/apps/community/src/project/project.controller.ts
@@ -64,7 +64,7 @@ import {
AnnotationUpdateDTO,
} from './dto'
import { AppLoggerService } from '../logger/logger.service'
-import { isValidPID } from '../common/constants'
+import { isValidPID, CAPTCHA_SECRET_KEY_LENGTH } from '../common/constants'
import { clickhouse } from '../common/integrations/clickhouse'
import {
getProjectsClickhouse,
@@ -100,6 +100,7 @@ import {
getPinnedProjectsClickhouse,
pinProjectClickhouse,
unpinProjectClickhouse,
+ generateRandomString,
} from '../common/utils'
import { Funnel } from './entity/funnel.entity'
import { ProjectViewEntity } from './entity/project-view.entity'
@@ -589,6 +590,70 @@ export class ProjectController {
}
}
+ @ApiBearerAuth()
+ @Post('/secret-gen/:pid')
+ @HttpCode(200)
+ @Auth()
+ @ApiResponse({ status: 200, description: 'A regenerated CAPTCHA secret key' })
+ async secretGen(
+ @Param('pid') pid: string,
+ @CurrentUserId() uid: string,
+ ): Promise<any> {
+ this.logger.log({ uid, pid }, 'POST /project/secret-gen/:pid')
+
+ if (!isValidPID(pid)) {
+ throw new BadRequestException(
+ 'The provided Project ID (pid) is incorrect',
+ )
+ }
+
+ const project = await this.projectService.getFullProject(pid)
+
+ if (_isEmpty(project)) {
+ throw new NotFoundException('Project was not found in the database')
+ }
+
+ this.projectService.allowedToManage(project, uid)
+
+ const secret = generateRandomString(CAPTCHA_SECRET_KEY_LENGTH)
+
+ await updateProjectClickhouse({ id: pid, captchaSecretKey: secret })
+
+ await deleteProjectRedis(pid)
+
+ return secret
+ }
+
+ @ApiBearerAuth()
+ @Delete('/secret-gen/:pid')
+ @HttpCode(200)
+ @Auth()
+ @ApiResponse({ status: 200, description: 'CAPTCHA secret key deleted' })
+ async deleteSecretKey(
+ @Param('pid') pid: string,
+ @CurrentUserId() uid: string,
+ ): Promise<void> {
+ this.logger.log({ uid, pid }, 'DELETE /project/secret-gen/:pid')
+
+ if (!isValidPID(pid)) {
+ throw new BadRequestException(
+ 'The provided Project ID (pid) is incorrect',
+ )
+ }
+
+ const project = await this.projectService.getFullProject(pid)
+
+ if (_isEmpty(project)) {
+ throw new NotFoundException('Project was not found in the database')
+ }
+
+ this.projectService.allowedToManage(project, uid)
+
+ await updateProjectClickhouse({ id: pid, captchaSecretKey: null })
+
+ await deleteProjectRedis(pid)
+ }
+
@Post('/funnel')
@ApiResponse({ status: 201 })
@Auth(true)