📈 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
added 'token already used' check
Blue Mouseb4aa28c02323
diff --git a/src/captcha/captcha.controller.ts b/src/captcha/captcha.controller.tsindex 682025dac..3bc3cfdfb 100644--- a/src/captcha/captcha.controller.ts+++ b/src/captcha/captcha.controller.ts@@ -194,7 +194,7 @@ export class CaptchaController {return {success: true,- data: this.captchaService.validateToken(token, secret),+ data: await this.captchaService.validateToken(token, secret),}}}diff --git a/src/captcha/captcha.service.ts b/src/captcha/captcha.service.tsindex 2a41afd20..0da2bb469 100644--- a/src/captcha/captcha.service.ts+++ b/src/captcha/captcha.service.ts@@ -12,7 +12,7 @@ import * as utc from 'dayjs/plugin/utc'import { AppLoggerService } from '../logger/logger.service'import {- isDevelopment, redis, REDIS_LOG_CAPTCHA_CACHE_KEY, isValidPID,+ isDevelopment, redis, REDIS_LOG_CAPTCHA_CACHE_KEY, isValidPID, getRedisCaptchaKey,} from '../common/constants'import { getElValue } from '../analytics/analytics.controller'import { ProjectService } from 'src/project/project.service'@@ -55,6 +55,19 @@ 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)++ if (key) {+ return true+ }++ await redis.set(captchaKey, '1', 'EX', CAPTCHA_TOKEN_LIFETIME)++ return false+}+const captchaDTO = (pid: string,dv: string,@@ -147,7 +160,7 @@ export class CaptchaService {return encryptString(JSON.stringify(token), secretKey)}- validateToken(token: string, secretKey: string): object {+ async validateToken(token: string, secretKey: string): Promise<object> {let parsedtry {@@ -161,6 +174,12 @@ export class CaptchaService {throw new BadRequestException('Token expired')}+ const tokenUsed = await isTokenAlreadyUsed(token)++ if (tokenUsed) {+ throw new BadRequestException('Token already used')+ }+return parsed}diff --git a/src/common/constants.ts b/src/common/constants.tsindex b0ff2c51e..47bc22e19 100644--- a/src/common/constants.ts+++ b/src/common/constants.ts@@ -1,5 +1,6 @@import { ClickHouse } from 'clickhouse'import Redis from 'ioredis'+import { hash } from 'blake3'import { v5 as uuidv5 } from 'uuid'import * as _toNumber from 'lodash/toNumber'@@ -164,6 +165,7 @@ const isValidPID = (pid: string) => PID_REGEX.test(pid)// redis keysconst getRedisProjectKey = (pid: string) => `pid_${pid}`const getRedisUserCountKey = (uid: string) => `user_c_${uid}`+const getRedisCaptchaKey = (token: string) => `captcha_${hash(token)}`const REDIS_LOG_DATA_CACHE_KEY = 'log_cache'const REDIS_LOG_CAPTCHA_CACHE_KEY = 'log:captcha'@@ -236,4 +238,5 @@ export {CAPTCHA_SALT,CAPTCHA_ENCRYPTION_KEY,isDevelopment,+ getRedisCaptchaKey,}