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 captcha validation

Blue Mouse3fe4c482eba2
diff --git a/src/captcha/captcha.controller.ts b/src/captcha/captcha.controller.ts
index 14c2b0fe1..b62559a9b 100644
--- a/src/captcha/captcha.controller.ts
+++ b/src/captcha/captcha.controller.ts
@@ -5,17 +5,14 @@ import {
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
-import { CaptchaService } from './captcha.service'
+import { CaptchaService, CAPTCHA_COOKIE_KEY } from './captcha.service'
import { BotDetectionGuard } from '../common/guards/bot-detection.guard'
import { BotDetection } from '../common/decorators/bot-detection.decorator'
-import { isDevelopment } from '../common/constants'
import { ManualDTO } from './dtos/manual.dto'
import { ValidateDTO } from './dtos/validate.dto'
dayjs.extend(utc)
-const CAPTCHA_COOKIE_KEY = 'swetrix-captcha-token'
-
const TEST_PUBLIC_KEY = 'test'
@Controller({
diff --git a/src/captcha/captcha.service.ts b/src/captcha/captcha.service.ts
index b04bf9e9b..cc4501c21 100644
--- a/src/captcha/captcha.service.ts
+++ b/src/captcha/captcha.service.ts
@@ -5,7 +5,6 @@ import * as svgCaptcha from 'svg-captcha'
import * as CryptoJS from 'crypto-js'
import { hash } from 'blake3'
import * as _toLower from 'lodash/toLower'
-import * as _toUpper from 'lodash/toUpper'
import { isDevelopment } from '../common/constants'
@@ -40,8 +39,12 @@ const THRESHOLD = 1.5
// 300 days
const COOKIE_MAX_AGE = 300 * 24 * 60 * 60 * 1000
+const captchaString = (text: string) => `${_toLower(text)}${CAPTCHA_SALT}`
+
const TEST_SECRET_KEY = 'wfOw1Jw3JAjcrHaQFvIvBS4qdG'
+export const CAPTCHA_COOKIE_KEY = 'swetrix-captcha-token'
+
@Injectable()
export class CaptchaService {
constructor(
@@ -80,7 +83,7 @@ export class CaptchaService {
}
hashCaptcha(text: string): string {
- return encryptString(`${text}${CAPTCHA_SALT}`, CAPTCHA_ENCRYPTION_KEY)
+ return hash(captchaString(text)).toString('hex')
}
async generateCaptcha(): Promise<GeneratedCaptcha> {
@@ -93,8 +96,6 @@ export class CaptchaService {
_toLower(captcha.text)
)
- console.log(captcha.text, hash)
-
return {
data: captcha.data,
hash,
@@ -102,9 +103,7 @@ export class CaptchaService {
}
verifyCaptcha(text: string, hash: string): boolean {
- // Checking for both lower, upper case and the original text
- // return hash === this.hashCaptcha(text) || hash === this.hashCaptcha(_toLower(text)) || hash === this.hashCaptcha(_toUpper(text))
- return text === _toLower(decryptString(hash, CAPTCHA_ENCRYPTION_KEY))
+ return hash === this.hashCaptcha(text)
}
incrementManuallyVerified(tokenCaptcha: TokenCaptcha): TokenCaptcha {