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 encryption

Blue Mousee18bb9ee7ad8
diff --git a/.env.example b/.env.example
index 99d258930..fde3ed04a 100644
--- a/.env.example
+++ b/.env.example
@@ -53,4 +53,5 @@ CDN_ACCESS_TOKEN=SOME_SECRET_TOKEN
# Captcha API
CAPTCHA_SALT=
+## We use AES-256-GCM, so the key length MUST BE 32 bytes
CAPTCHA_ENCRYPTION_KEY=
diff --git a/package.json b/package.json
index cce820b5c..874a57d98 100644
--- a/package.json
+++ b/package.json
@@ -45,6 +45,7 @@
"cookie-parser": "^1.4.6",
"countries-and-timezones": "^3.4.0",
"cross-env": "^7.0.3",
+ "crypto-js": "^4.1.1",
"dayjs": "^1.11.7",
"dotenv": "^16.0.3",
"express": "^4.18.2",
diff --git a/src/app.module.ts b/src/app.module.ts
index be29adf4c..a1b4064ac 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -19,6 +19,7 @@ import { AlertModule } from './alert/alert.module'
import { I18nModule } from 'nestjs-i18n'
import { getI18nConfig } from './configs'
import { AuthModule } from './auth/auth.module'
+import { CaptchaModule } from './captcha/captcha.module'
const modules = [
ConfigModule.forRoot({ envFilePath: '.env', isGlobal: true }),
@@ -47,6 +48,7 @@ const modules = [
MarketplaceModule,
AlertModule,
AuthModule,
+ CaptchaModule,
]
if (process.env.TG_BOT_TOKEN) {
diff --git a/src/captcha/captcha.controller.ts b/src/captcha/captcha.controller.ts
index c6a70801b..6e233897e 100644
--- a/src/captcha/captcha.controller.ts
+++ b/src/captcha/captcha.controller.ts
@@ -37,13 +37,17 @@ export class CaptchaController {
verifiable = await this.captchaService.autoVerifiable(tokenCookie)
} catch (e) {
// Either there was no cookie or the cookie was invalid
+ let newTokenCookie
// Set a new cookie
- const newTokenCookie = await this.captchaService.setTokenCaptcha()
+ try {
+ newTokenCookie = await this.captchaService.getTokenCaptcha()
+ } catch (e) {
+ console.error(e)
+ throw new InternalServerErrorException('Could not generate a captcha cookie')
+ }
if (isDevelopment) {
- console.log('New captcha cookie:', newTokenCookie)
-
// @ts-ignore
response.cookie(CAPTCHA_COOKIE_KEY, newTokenCookie, {
httpOnly: true,
diff --git a/src/captcha/captcha.module.ts b/src/captcha/captcha.module.ts
index 810c93c5a..ddd49ec0d 100644
--- a/src/captcha/captcha.module.ts
+++ b/src/captcha/captcha.module.ts
@@ -9,4 +9,4 @@ import { CaptchaService } from './captcha.service'
exports: [CaptchaService],
controllers: [CaptchaController],
})
-export class AnalyticsModule {}
+export class CaptchaModule {}
diff --git a/src/captcha/captcha.service.ts b/src/captcha/captcha.service.ts
index 2a1cdb896..184c46876 100644
--- a/src/captcha/captcha.service.ts
+++ b/src/captcha/captcha.service.ts
@@ -2,9 +2,8 @@ import {
Injectable,
} from '@nestjs/common'
import * as svgCaptcha from 'svg-captcha'
+import * as CryptoJS from 'crypto-js'
import { hash } from 'blake3'
-import * as crypto from 'crypto'
-import { promisify } from 'util'
import {
CAPTCHA_SALT, CAPTCHA_ENCRYPTION_KEY,
@@ -20,28 +19,15 @@ interface TokenCaptcha {
automaticallyVerified: number
}
-// Encrypts a string using ChaCha20 algorithm with a given key
-function encryptString(str: string, key: string | undefined = CAPTCHA_ENCRYPTION_KEY): string {
- const iv = crypto.randomBytes(12)
- const cipher = crypto.createCipheriv('chacha20', key, iv)
- const encrypted = Buffer.concat([cipher.update(str, 'utf8'), cipher.final()])
- return Buffer.concat([iv, encrypted]).toString('hex')
+const encryptString = (text: string, key: string): string => {
+ return CryptoJS.Rabbit.encrypt(text, key).toString()
}
-// Decrypts an encrypted string using ChaCha20 algorithm with a given key
-function decryptString(encryptedStr: string, key: string | undefined = CAPTCHA_ENCRYPTION_KEY): string {
- const input = Buffer.from(encryptedStr, 'hex')
- const iv = input.slice(0, 12)
- const encrypted = input.slice(12)
- const decipher = crypto.createDecipheriv('chacha20', key, iv)
- const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()])
- return decrypted.toString('utf8')
+const decryptString = (text: string, key: string): string => {
+ const bytes = CryptoJS.Rabbit.decrypt(text, key)
+ return bytes.toString(CryptoJS.enc.Utf8)
}
-// Promisify the encryption and decryption functions
-const encryptStringAsync = promisify(encryptString)
-const decryptStringAsync = promisify(decryptString)
-
// Set the weights for the manual and automatic verifications
const MANUAL_WEIGHT = 2
const AUTO_WEIGHT = 1
@@ -97,9 +83,9 @@ export class CaptchaService {
}
async decryptTokenCaptcha(jwtCookie: string | undefined): Promise<TokenCaptcha> {
- const decryptedtokenCaptcha = await decryptStringAsync(jwtCookie, CAPTCHA_ENCRYPTION_KEY)
-
try {
+ const decryptedtokenCaptcha = decryptString(jwtCookie, CAPTCHA_ENCRYPTION_KEY)
+
// @ts-ignore
return JSON.parse(decryptedtokenCaptcha)
} catch (e) {
@@ -107,16 +93,16 @@ export class CaptchaService {
}
}
- async setTokenCaptcha(manuallyVerified: number = 0, automaticallyVerified: number = 0): Promise<string> {
+ async getTokenCaptcha(manuallyVerified: number = 0, automaticallyVerified: number = 0): Promise<string> {
const tokenCaptcha: TokenCaptcha = {
manuallyVerified,
automaticallyVerified,
}
// @ts-ignore
- const encryptedtokenCaptcha: string = await encryptStringAsync(JSON.stringify(tokenCaptcha), CAPTCHA_ENCRYPTION_KEY)
+ const encryptedTokenCaptcha: string = encryptString(JSON.stringify(tokenCaptcha), CAPTCHA_ENCRYPTION_KEY)
- return encryptedtokenCaptcha
+ return encryptedTokenCaptcha
}
async autoVerifiable(tokenCookie: string | undefined): Promise<boolean> {