📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Remove captcha manuallyPassed column
Blue Mousea5af84a4ab9b
diff --git a/backend/apps/production/src/captcha/captcha.controller.ts b/backend/apps/production/src/captcha/captcha.controller.tsindex c9b42b049..e07a30eb8 100644--- a/backend/apps/production/src/captcha/captcha.controller.ts+++ b/backend/apps/production/src/captcha/captcha.controller.ts@@ -16,9 +16,9 @@ import { getIPFromHeaders } 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'-import { ManualDTO } from './dtos/manual.dto'-import { ValidateDTO } from './dtos/validate.dto'-import { GenerateDTO, DEFAULT_THEME } from './dtos/generate.dto'+import { VerifyDto } from './dtos/manual.dto'+import { ValidateDto } from './dtos/validate.dto'+import { GenerateDto, DEFAULT_THEME } from './dtos/generate.dto'dayjs.extend(utc)@@ -36,7 +36,7 @@ export class CaptchaController {@HttpCode(200)@UseGuards(BotDetectionGuard)@BotDetection()- async generateCaptcha(@Body() generateDTO: GenerateDTO): Promise<any> {+ async generateCaptcha(@Body() generateDTO: GenerateDto): Promise<any> {this.logger.log({ generateDTO }, 'POST /captcha/generate')const { theme = DEFAULT_THEME, pid } = generateDTO@@ -51,16 +51,16 @@ export class CaptchaController {@UseGuards(BotDetectionGuard)@BotDetection()async verify(- @Body() manualDTO: ManualDTO,+ @Body() verifyDto: VerifyDto,@Headers() headers,@Ip() reqIP,): Promise<any> {- this.logger.log({ manualDTO }, 'POST /captcha/verify-manual')+ this.logger.log({ manualDTO: verifyDto }, 'POST /captcha/verify')const { 'user-agent': userAgent } = headers// todo: add origin checks- const { code, hash, pid } = manualDTO+ const { code, hash, pid } = verifyDtoawait this.captchaService.validatePIDForCAPTCHA(pid)@@ -89,7 +89,13 @@ export class CaptchaController {const ip = getIPFromHeaders(headers) || reqIP || ''- await this.captchaService.logCaptchaPass(pid, userAgent, timestamp, ip)+ try {+ await this.captchaService.logCaptchaPass(pid, userAgent, timestamp, ip)+ } catch (reason) {+ this.logger.error(+ `[CaptchaController -> verify] Failed to log captcha pass: ${reason}`,+ )+ }return {success: true,@@ -102,7 +108,7 @@ export class CaptchaController {@Post('/validate')@HttpCode(200)- async validateToken(@Body() validateDTO: ValidateDTO): Promise<any> {+ async validateToken(@Body() validateDTO: ValidateDto): Promise<any> {this.logger.log({ validateDTO }, 'POST /captcha/validate')const { token, secret } = validateDTOdiff --git a/backend/apps/production/src/captcha/captcha.service.ts b/backend/apps/production/src/captcha/captcha.service.tsindex 422be9bdc..135e0334e 100644--- a/backend/apps/production/src/captcha/captcha.service.ts+++ b/backend/apps/production/src/captcha/captcha.service.ts@@ -71,7 +71,7 @@ export class CaptchaService {) {}// checks if captcha is enabled for pid- async _isCaptchaEnabledForPID(pid: string): Promise<boolean> {+ async _isCaptchaEnabledForPID(pid: string) {const project = await this.projectService.getRedisProject(pid)if (!project) {@@ -82,7 +82,7 @@ export class CaptchaService {}// validates pid, checks if captcha is enabled and throws an error otherwise- async validatePIDForCAPTCHA(pid: string): Promise<void> {+ async validatePIDForCAPTCHA(pid: string) {if (isDummyPID(pid)) {return}@@ -103,7 +103,7 @@ export class CaptchaService {userAgent: string,timestamp: number,ip: string,- ): Promise<void> {+ ) {const ua = UAParser(userAgent)const dv = ua.device.type || 'desktop'const br = ua.browser.name@@ -133,11 +133,7 @@ export class CaptchaService {)}- async generateToken(- pid: string,- captchaHash: string,- timestamp: number,- ): Promise<string> {+ async generateToken(pid: string, captchaHash: string, timestamp: number) {if (isDummyPID(pid)) {return this.generateDummyToken()}@@ -148,10 +144,7 @@ export class CaptchaService {throw new BadRequestException('Project not found')}- // @ts-ignore- const secretKey = project.captchaSecretKey-- if (!secretKey) {+ if (!project.captchaSecretKey) {throw new BadRequestException('No secret key generated for this project')}@@ -161,10 +154,10 @@ export class CaptchaService {pid,}- return encryptString(JSON.stringify(token), secretKey)+ return encryptString(JSON.stringify(token), project.captchaSecretKey)}- async validateToken(token: string, secretKey: string): Promise<object> {+ async validateToken(token: string, secretKey: string) {let parsedif (secretKey === DUMMY_SECRETS.ALWAYS_FAIL) {@@ -230,7 +223,7 @@ export class CaptchaService {}}- verifyCaptcha(text: string, captchaHash: string): boolean {+ verifyCaptcha(text: string, captchaHash: string) {return captchaHash === this.hashCaptcha(text)}}diff --git a/backend/apps/production/src/captcha/dtos/generate.dto.ts b/backend/apps/production/src/captcha/dtos/generate.dto.tsindex 18c74e3ae..17b7720aa 100644--- a/backend/apps/production/src/captcha/dtos/generate.dto.ts+++ b/backend/apps/production/src/captcha/dtos/generate.dto.ts@@ -3,7 +3,7 @@ import { IsOptional, IsNotEmpty, IsString } from 'class-validator'export const DEFAULT_THEME = 'light'-export class GenerateDTO {+export class GenerateDto {@ApiProperty({example: 'aUn1quEid-3',required: true,diff --git a/backend/apps/production/src/captcha/dtos/manual.dto.ts b/backend/apps/production/src/captcha/dtos/manual.dto.tsindex f3b19b8dc..7c7c1d206 100644--- a/backend/apps/production/src/captcha/dtos/manual.dto.ts+++ b/backend/apps/production/src/captcha/dtos/manual.dto.ts@@ -1,7 +1,7 @@import { ApiProperty } from '@nestjs/swagger'import { IsNotEmpty, IsString } from 'class-validator'-export class ManualDTO {+export class VerifyDto {@ApiProperty({example: 'aUn1quEid-3',required: true,diff --git a/backend/apps/production/src/captcha/dtos/validate.dto.ts b/backend/apps/production/src/captcha/dtos/validate.dto.tsindex cfc8fe619..7652c2100 100644--- a/backend/apps/production/src/captcha/dtos/validate.dto.ts+++ b/backend/apps/production/src/captcha/dtos/validate.dto.ts@@ -1,7 +1,7 @@import { ApiProperty } from '@nestjs/swagger'import { IsNotEmpty, IsString } from 'class-validator'-export class ValidateDTO {+export class ValidateDto {@ApiProperty({required: true,description: 'Catpcha pass token',diff --git a/backend/apps/production/src/captcha/utils/transformers.ts b/backend/apps/production/src/captcha/utils/transformers.tsindex 508db98e1..3a0d21d43 100644--- a/backend/apps/production/src/captcha/utils/transformers.ts+++ b/backend/apps/production/src/captcha/utils/transformers.ts@@ -17,7 +17,6 @@ export const captchaTransformer = (br: br || null,os: os || null,cc: cc || null,- manuallyPassed: 1,created: dayjs.utc(timestamp).format('YYYY-MM-DD HH:mm:ss'),}}diff --git a/backend/meta/clickhouse/generate-dummy-data.js b/backend/meta/clickhouse/generate-dummy-data.jsindex 02d9c416f..f864f886e 100644--- a/backend/meta/clickhouse/generate-dummy-data.js+++ b/backend/meta/clickhouse/generate-dummy-data.js@@ -215,7 +215,6 @@ const generateCaptchaData = async (pid, rowCount, from, to) => {faker.helpers.arrayElement(BROWSERS), // brfaker.helpers.arrayElement(OS), // osfaker.location.countryCode(), // cc- faker.number.int({ min: 0, max: 2 }) === 0 ? 1 : 0, // manuallyPassed; 33% chance of manuallyPassed record_.split(faker.date.between({ from, to })diff --git a/backend/migrations/clickhouse/2024_10_27.js b/backend/migrations/clickhouse/2024_10_27.jsnew file mode 100644index 000000000..227810e76--- /dev/null+++ b/backend/migrations/clickhouse/2024_10_27.js@@ -0,0 +1,8 @@+const { queriesRunner, dbName } = require('./setup')++const queries = [+ // Drop manuallyPassed column from captcha table+ `ALTER TABLE ${dbName}.captcha DROP COLUMN IF EXISTS manuallyPassed;`,+]++queriesRunner(queries)