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!

added some dummy pids/tokens logic

Blue Mouse3142ec069bd5
diff --git a/src/captcha/captcha.controller.ts b/src/captcha/captcha.controller.ts
index 91f0daf8d..8fb2f2d4d 100644
--- a/src/captcha/captcha.controller.ts
+++ b/src/captcha/captcha.controller.ts
@@ -7,7 +7,7 @@ import * as utc from 'dayjs/plugin/utc'
import { AppLoggerService } from '../logger/logger.service'
import { CAPTCHA_COOKIE_KEY } from '../common/constants'
-import { CaptchaService } from './captcha.service'
+import { CaptchaService, DUMMY_PIDS, isDummyPID } 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'
@@ -68,7 +68,7 @@ export class CaptchaController {
await this.captchaService.validatePIDForCAPTCHA(pid)
- if (!this.captchaService.verifyCaptcha(code, hash, pid)) {
+ if (pid === DUMMY_PIDS.ALWAYS_FAIL || !this.captchaService.verifyCaptcha(code, hash, pid)) {
throw new ForbiddenException('Incorrect captcha')
}
@@ -83,6 +83,20 @@ export class CaptchaController {
}
}
+ const timestamp = dayjs.utc().unix()
+ const token = await this.captchaService.generateToken(pid, hash, timestamp, false)
+
+ // For dummy (test) PIDs
+ if (pid === DUMMY_PIDS.MANUAL_PASS) {
+ return {
+ success: true,
+ token,
+ timestamp,
+ hash,
+ pid,
+ }
+ }
+
const {
manuallyVerified, automaticallyVerified,
} = this.captchaService.incrementManuallyVerified(decrypted)
@@ -90,10 +104,6 @@ export class CaptchaController {
const newTokenCookie = this.captchaService.getTokenCaptcha(manuallyVerified, automaticallyVerified)
this.captchaService.setTokenCookie(response, newTokenCookie)
- const timestamp = dayjs.utc().unix()
-
- const token = await this.captchaService.generateToken(pid, hash, timestamp, false)
-
await this.captchaService.logCaptchaPass(pid, userAgent, headers, timestamp, true)
return {
@@ -130,11 +140,15 @@ export class CaptchaController {
let verifiable
try {
- verifiable = await this.captchaService.autoVerifiable(tokenCookie)
+ verifiable = await this.captchaService.autoVerifiable(pid, tokenCookie)
} catch (e) {
// Either there was no cookie or the cookie was invalid
let newTokenCookie
+ if (isDummyPID(pid) && (pid !== DUMMY_PIDS.AUTO_PASS || pid === DUMMY_PIDS.ALWAYS_FAIL)) {
+ throw new ForbiddenException('Captcha required')
+ }
+
// Set a new cookie
try {
newTokenCookie = this.captchaService.getTokenCaptcha()
@@ -160,6 +174,18 @@ export class CaptchaController {
throw new InternalServerErrorException('Could not decrypt captcha cookie')
}
+ const timestamp = dayjs.utc().unix()
+ const token = await this.captchaService.generateToken(pid, null, timestamp, true)
+
+ if (pid === DUMMY_PIDS.AUTO_PASS) {
+ return {
+ success: true,
+ token,
+ timestamp,
+ hash: null,
+ }
+ }
+
const {
manuallyVerified, automaticallyVerified,
} = this.captchaService.incrementAutomaticallyVerified(decrypted)
@@ -168,10 +194,6 @@ export class CaptchaController {
this.captchaService.setTokenCookie(response, newTokenCookie)
- const timestamp = dayjs.utc().unix()
-
- const token = await this.captchaService.generateToken(pid, null, timestamp, true)
-
await this.captchaService.logCaptchaPass(pid, userAgent, headers, timestamp, false)
return {
diff --git a/src/captcha/captcha.service.ts b/src/captcha/captcha.service.ts
index de7c42548..6cfedf2d2 100644
--- a/src/captcha/captcha.service.ts
+++ b/src/captcha/captcha.service.ts
@@ -7,6 +7,8 @@ import * as CryptoJS from 'crypto-js'
import * as _toLower from 'lodash/toLower'
import * as UAParser from 'ua-parser-js'
import * as _map from 'lodash/map'
+import * as _values from 'lodash/values'
+import * as _includes from 'lodash/includes'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
@@ -26,6 +28,22 @@ import {
dayjs.extend(utc)
+export const DUMMY_PIDS = {
+ AUTO_PASS: 'AP00000000000',
+ MANUAL_PASS: 'MP00000000000',
+ ALWAYS_FAIL: 'FAIL000000000',
+}
+
+const DUMMY_SECRETS = {
+ ALWAYS_PASS: 'PASS000000000000000000',
+ ALWAYS_FAIL: 'FAIL000000000000000000',
+ TOKEN_USED_FAIL: 'USED000000000000000000',
+}
+
+export const isDummyPID = (pid: string): boolean => {
+ return _includes(_values(DUMMY_PIDS), pid)
+}
+
const encryptString = (text: string, key: string): string => {
return CryptoJS.Rabbit.encrypt(text, key).toString()
}
@@ -98,6 +116,10 @@ export class CaptchaService {
// validates pid, checks if captcha is enabled and throws an error otherwise
async validatePIDForCAPTCHA(pid: string): Promise<void> {
+ if (isDummyPID(pid)) {
+ return
+ }
+
if (!isValidPID(pid)) {
throw new BadRequestException(
'The provided Project ID (pid) is incorrect',
@@ -130,6 +152,10 @@ export class CaptchaService {
}
async generateToken(pid: string, hash: string, timestamp: number, autoVerified: boolean): Promise<string> {
+ if (isDummyPID(pid)) {
+ return encryptString('DUMMY_TOKEN00000111112222233333444445555566666777778888899999', DUMMY_SECRETS.ALWAYS_PASS)
+ }
+
const project = await this.projectService.getRedisProject(pid)
if (!project) {
@@ -153,6 +179,23 @@ export class CaptchaService {
async validateToken(token: string, secretKey: string): Promise<object> {
let parsed
+ if (secretKey === DUMMY_SECRETS.ALWAYS_FAIL) {
+ throw new BadRequestException('Could not decrypt token')
+ }
+
+ if (secretKey === DUMMY_SECRETS.TOKEN_USED_FAIL) {
+ throw new BadRequestException('Token already used')
+ }
+
+ if (secretKey === DUMMY_SECRETS.ALWAYS_PASS) {
+ return {
+ hash: 'DUMMY_HASH00000111112222233333444445555566666777778888899999',
+ timestamp: dayjs().unix(),
+ autoVerified: false,
+ pid: DUMMY_PIDS.AUTO_PASS,
+ }
+ }
+
try {
const decrypted = decryptString(token, secretKey)
parsed = JSON.parse(decrypted)
@@ -173,7 +216,7 @@ export class CaptchaService {
return parsed
}
- hashCaptcha(text: string, pid: string): string {
+ hashCaptcha(text: string): string {
return hash(captchaString(text)).toString('hex')
}
@@ -190,7 +233,7 @@ export class CaptchaService {
...themeParams,
})
const hash = this.hashCaptcha(
- _toLower(captcha.text), pid,
+ _toLower(captcha.text),
)
return {
@@ -200,7 +243,7 @@ export class CaptchaService {
}
verifyCaptcha(text: string, hash: string, pid: string): boolean {
- return hash === this.hashCaptcha(text, pid)
+ return hash === this.hashCaptcha(text)
}
incrementManuallyVerified(tokenCaptcha: TokenCaptcha): TokenCaptcha {
@@ -251,7 +294,11 @@ export class CaptchaService {
return encryptedTokenCaptcha
}
- async autoVerifiable(tokenCookie: string | undefined): Promise<boolean> {
+ async autoVerifiable(pid: string, tokenCookie: string | undefined): Promise<boolean> {
+ if (pid === DUMMY_PIDS.AUTO_PASS) {
+ return true
+ }
+
if (!tokenCookie) {
throw new Error('No JWT captcha cookie')
}