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!

improved code quality

Blue Mouse03a183d3230a
diff --git a/src/captcha/captcha.controller.ts b/src/captcha/captcha.controller.ts
index 3bc3cfdfb..2b3810fe2 100644
--- a/src/captcha/captcha.controller.ts
+++ b/src/captcha/captcha.controller.ts
@@ -1,12 +1,13 @@
import {
Controller, Post, Body, UseGuards, ForbiddenException, InternalServerErrorException,
- Headers, Request, Req, Response, Res, HttpCode, BadRequestException,
+ Headers, Request, Req, Response, Res, HttpCode,
} from '@nestjs/common'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
import { AppLoggerService } from '../logger/logger.service'
-import { CaptchaService, CAPTCHA_COOKIE_KEY } from './captcha.service'
+import { CAPTCHA_COOKIE_KEY } from '../common/constants'
+import { CaptchaService } 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'
diff --git a/src/captcha/captcha.service.ts b/src/captcha/captcha.service.ts
index 0da2bb469..b4af2802c 100644
--- a/src/captcha/captcha.service.ts
+++ b/src/captcha/captcha.service.ts
@@ -16,29 +16,22 @@ import {
} from '../common/constants'
import { getElValue } from '../analytics/analytics.controller'
import { ProjectService } from 'src/project/project.service'
+import { GeneratedCaptcha } from './interfaces/generated-captcha'
+import { TokenCaptcha } from './interfaces/token-captcha'
import {
- CAPTCHA_SALT, CAPTCHA_ENCRYPTION_KEY,
+ CAPTCHA_SALT, CAPTCHA_ENCRYPTION_KEY, CAPTCHA_COOKIE_KEY,
+ CAPTCHA_TOKEN_LIFETIME,
} from '../common/constants'
dayjs.extend(utc)
-interface GeneratedCaptcha {
- data: string
- hash: string
-}
-
-interface TokenCaptcha {
- manuallyVerified: number
- automaticallyVerified: number
-}
-
const encryptString = (text: string, key: string): string => {
- return CryptoJS.Rabbit.encrypt(text, key + key).toString()
+ return CryptoJS.Rabbit.encrypt(text, key).toString()
}
const decryptString = (text: string, key: string): string => {
- const bytes = CryptoJS.Rabbit.decrypt(text, key + key)
+ const bytes = CryptoJS.Rabbit.decrypt(text, key)
return bytes.toString(CryptoJS.enc.Utf8)
}
@@ -52,9 +45,6 @@ const COOKIE_MAX_AGE = 300 * 24 * 60 * 60 * 1000
const captchaString = (text: string) => `${_toLower(text)}${CAPTCHA_SALT}`
-export const CAPTCHA_COOKIE_KEY = 'swetrix-captcha-token'
-export const CAPTCHA_TOKEN_LIFETIME = 300 // seconds (5 minutes).
-
const isTokenAlreadyUsed = async (token: string): Promise<boolean> => {
const captchaKey = getRedisCaptchaKey(token)
const key = await redis.get(captchaKey)
diff --git a/src/captcha/dtos/automatic.dto.ts b/src/captcha/dtos/automatic.dto.ts
index fcb6fc175..95bc87820 100644
--- a/src/captcha/dtos/automatic.dto.ts
+++ b/src/captcha/dtos/automatic.dto.ts
@@ -1,5 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsNotEmpty } from 'class-validator'
+import { IsNotEmpty, Matches } from 'class-validator'
+
+import { PID_REGEX } from '../../common/constants'
export class AutomaticDTO {
@ApiProperty({
@@ -8,5 +10,6 @@ export class AutomaticDTO {
description: 'A unique project ID',
})
@IsNotEmpty()
+ @Matches(PID_REGEX)
pid: string
}
diff --git a/src/captcha/dtos/generate.dto.ts b/src/captcha/dtos/generate.dto.ts
index 25f1c9c73..a64ef83bb 100644
--- a/src/captcha/dtos/generate.dto.ts
+++ b/src/captcha/dtos/generate.dto.ts
@@ -1,5 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsOptional, IsNotEmpty } from 'class-validator'
+import { IsOptional, IsNotEmpty, Matches } from 'class-validator'
+
+import { PID_REGEX } from '../../common/constants'
export const DEFAULT_THEME = 'light'
@@ -10,6 +12,7 @@ export class GenerateDTO {
description: 'A unique project ID',
})
@IsNotEmpty()
+ @Matches(PID_REGEX)
pid: string
@ApiProperty({
diff --git a/src/captcha/dtos/manual.dto.ts b/src/captcha/dtos/manual.dto.ts
index 444cf4217..b3be84e84 100644
--- a/src/captcha/dtos/manual.dto.ts
+++ b/src/captcha/dtos/manual.dto.ts
@@ -1,5 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsNotEmpty } from 'class-validator'
+import { IsNotEmpty, IsString, Matches } from 'class-validator'
+
+import { PID_REGEX } from '../../common/constants'
export class ManualDTO {
@ApiProperty({
@@ -8,12 +10,14 @@ export class ManualDTO {
description: 'A unique project ID',
})
@IsNotEmpty()
+ @Matches(PID_REGEX)
pid: string
@ApiProperty({
required: true,
})
@IsNotEmpty()
+ @IsString()
hash: string
@ApiProperty({
@@ -22,5 +26,6 @@ export class ManualDTO {
description: 'Captcha code',
})
@IsNotEmpty()
+ @IsString()
code: string
}
diff --git a/src/captcha/dtos/validate.dto.ts b/src/captcha/dtos/validate.dto.ts
index f8d1bf1c2..cfc8fe619 100644
--- a/src/captcha/dtos/validate.dto.ts
+++ b/src/captcha/dtos/validate.dto.ts
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsNotEmpty } from 'class-validator'
+import { IsNotEmpty, IsString } from 'class-validator'
export class ValidateDTO {
@ApiProperty({
@@ -7,6 +7,7 @@ export class ValidateDTO {
description: 'Catpcha pass token',
})
@IsNotEmpty()
+ @IsString()
token: string
@ApiProperty({
@@ -14,5 +15,6 @@ export class ValidateDTO {
description: 'Secret API key',
})
@IsNotEmpty()
+ @IsString()
secret: string
}
diff --git a/src/captcha/interfaces/generated-captcha.ts b/src/captcha/interfaces/generated-captcha.ts
new file mode 100644
index 000000000..1057935f0
--- /dev/null
+++ b/src/captcha/interfaces/generated-captcha.ts
@@ -0,0 +1,4 @@
+export interface GeneratedCaptcha {
+ data: string
+ hash: string
+}
diff --git a/src/captcha/interfaces/token-captcha.ts b/src/captcha/interfaces/token-captcha.ts
new file mode 100644
index 000000000..29e747368
--- /dev/null
+++ b/src/captcha/interfaces/token-captcha.ts
@@ -0,0 +1,4 @@
+export interface TokenCaptcha {
+ manuallyVerified: number
+ automaticallyVerified: number
+}
diff --git a/src/common/constants.ts b/src/common/constants.ts
index 47bc22e19..301e8427a 100644
--- a/src/common/constants.ts
+++ b/src/common/constants.ts
@@ -201,6 +201,9 @@ const SEND_WARNING_AT_PERC = 85
const PROJECT_INVITE_EXPIRE = 48
+const CAPTCHA_COOKIE_KEY = 'swetrix-captcha-token'
+const CAPTCHA_TOKEN_LIFETIME = 300 // seconds (5 minutes).
+
export {
clickhouse,
JWT_LIFE_TIME,
@@ -239,4 +242,7 @@ export {
CAPTCHA_ENCRYPTION_KEY,
isDevelopment,
getRedisCaptchaKey,
+ CAPTCHA_COOKIE_KEY,
+ CAPTCHA_TOKEN_LIFETIME,
+ PID_REGEX,
}