📈 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 secret CAPTCHA keys, added token lifetime support
Blue Mouse39b1a3921af3
diff --git a/src/analytics/analytics.module.ts b/src/analytics/analytics.module.tsindex 0619c76c0..b9d46551a 100644--- a/src/analytics/analytics.module.ts+++ b/src/analytics/analytics.module.ts@@ -6,7 +6,6 @@ import { UserModule } from '../user/user.module'import { AppLoggerModule } from '../logger/logger.module'import { ProjectModule } from '../project/project.module'import { TaskManagerModule } from '../task-manager/task-manager.module'-import { TypeOrmModule } from '@nestjs/typeorm'@Module({imports: [diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.tsindex 7af37b931..894e812cd 100644--- a/src/analytics/analytics.service.ts+++ b/src/analytics/analytics.service.ts@@ -31,15 +31,12 @@ import { ACCOUNT_PLANS, DEFAULT_TIMEZONE, PlanCode } from '../user/entities/userimport {redis,isValidPID,- getRedisProjectKey,- redisProjectCacheTimeout,UNIQUE_SESSION_LIFE_TIME,clickhouse,isSelfhosted,REDIS_SESSION_SALT_KEY,} from '../common/constants'import {- getProjectsClickhouse,calculateRelativePercentage,millisecondsToSeconds,} from '../common/utils'@@ -167,59 +164,9 @@ export const checkIfTBAllowed = (export class AnalyticsService {constructor(private readonly projectService: ProjectService) { }- async getRedisProject(pid: string): Promise<Project | null> {- const pidKey = getRedisProjectKey(pid)- let project: string | Project = await redis.get(pidKey)-- if (_isEmpty(project)) {- if (isSelfhosted) {- project = await getProjectsClickhouse(pid)- } else {- // todo: optimise the relations - select- // select only required columns- // https://stackoverflow.com/questions/59645009/how-to-return-only-some-columns-of-a-relations-with-typeorm- project = await this.projectService.findOne(pid, {- relations: ['admin'],- select: ['origins', 'active', 'admin', 'public', 'ipBlacklist'],- })- }- if (_isEmpty(project))- throw new BadRequestException(- 'The provided Project ID (pid) is incorrect',- )-- if (!isSelfhosted) {- const share = await this.projectService.findShare({- where: {- project: pid,- },- relations: ['user'],- })- // @ts-ignore- project = { ...project, share }- }-- await redis.set(- pidKey,- JSON.stringify(project),- 'EX',- redisProjectCacheTimeout,- )- } else {- try {- project = JSON.parse(project)- } catch {- throw new InternalServerErrorException('Error while processing project')- }- }-- // @ts-ignore- return project- }-async checkProjectAccess(pid: string, uid: string | null): Promise<void> {if (!isSelfhosted) {- const project = await this.getRedisProject(pid)+ const project = await this.projectService.getRedisProject(pid)this.projectService.allowedToView(project, uid)}}@@ -312,7 +259,7 @@ export class AnalyticsService {}}- const project = await this.getRedisProject(pid)+ const project = await this.projectService.getRedisProject(pid)this.checkIpBlacklist(project, ip)diff --git a/src/captcha/captcha.controller.ts b/src/captcha/captcha.controller.tsindex f5ae04262..682025dac 100644--- a/src/captcha/captcha.controller.ts+++ b/src/captcha/captcha.controller.ts@@ -1,6 +1,6 @@import {Controller, Post, Body, UseGuards, ForbiddenException, InternalServerErrorException,- Headers, Ip, Request, Req, Response, Res, HttpCode, Get,+ Headers, Request, Req, Response, Res, HttpCode, BadRequestException,} from '@nestjs/common'import * as dayjs from 'dayjs'import * as utc from 'dayjs/plugin/utc'@@ -16,8 +16,6 @@ import { GenerateDTO, DEFAULT_THEME } from './dtos/generate.dto'dayjs.extend(utc)-const TEST_PUBLIC_KEY = 'test'-@Controller({version: '1',path: 'captcha',@@ -41,7 +39,7 @@ export class CaptchaController {theme = DEFAULT_THEME, pid,} = generateDTO- console.log(generateDTO)+ this.captchaService.validatePIDForCAPTCHA(pid)return await this.captchaService.generateCaptcha(theme, pid)}@@ -67,6 +65,8 @@ export class CaptchaController {code, hash, pid,} = manualDTO+ this.captchaService.validatePIDForCAPTCHA(pid)+if (!this.captchaService.verifyCaptcha(code, hash, pid)) {throw new ForbiddenException('Incorrect captcha')}@@ -91,7 +91,7 @@ export class CaptchaController {const timestamp = dayjs.utc().unix()- const token = this.captchaService.generateToken(TEST_PUBLIC_KEY, hash, timestamp, false)+ const token = await this.captchaService.generateToken(pid, hash, timestamp, false)await this.captchaService.logCaptchaPass(pid, userAgent, headers, timestamp, true)@@ -121,6 +121,8 @@ export class CaptchaController {const { pid } = automaticDTO+ this.captchaService.validatePIDForCAPTCHA(pid)+// @ts-ignorelet tokenCookie = request?.cookies?.[CAPTCHA_COOKIE_KEY]@@ -167,7 +169,7 @@ export class CaptchaController {const timestamp = dayjs.utc().unix()- const token = this.captchaService.generateToken(TEST_PUBLIC_KEY, null, timestamp, true)+ const token = await this.captchaService.generateToken(pid, null, timestamp, true)await this.captchaService.logCaptchaPass(pid, userAgent, headers, timestamp, false)diff --git a/src/captcha/captcha.module.ts b/src/captcha/captcha.module.tsindex 1275e6448..86e96d136 100644--- a/src/captcha/captcha.module.ts+++ b/src/captcha/captcha.module.ts@@ -3,9 +3,13 @@ import { Module } from '@nestjs/common'import { AppLoggerModule } from '../logger/logger.module'import { CaptchaController } from './captcha.controller'import { CaptchaService } from './captcha.service'+import { ProjectModule } from '../project/project.module'@Module({- imports: [AppLoggerModule],+ imports: [+ AppLoggerModule,+ ProjectModule,+ ],providers: [CaptchaService],exports: [CaptchaService],controllers: [CaptchaController],diff --git a/src/captcha/captcha.service.ts b/src/captcha/captcha.service.tsindex 6255217e7..7b97f1261 100644--- a/src/captcha/captcha.service.ts+++ b/src/captcha/captcha.service.ts@@ -12,9 +12,10 @@ import * as utc from 'dayjs/plugin/utc'import { AppLoggerService } from '../logger/logger.service'import {- isDevelopment, redis, REDIS_LOG_CAPTCHA_CACHE_KEY,+ isDevelopment, redis, REDIS_LOG_CAPTCHA_CACHE_KEY, isValidPID,} from '../common/constants'import { getElValue } from '../analytics/analytics.controller'+import { ProjectService } from 'src/project/project.service'import {CAPTCHA_SALT, CAPTCHA_ENCRYPTION_KEY,@@ -33,11 +34,11 @@ interface TokenCaptcha {}const encryptString = (text: string, key: string): string => {- return CryptoJS.Rabbit.encrypt(text, key).toString()+ return CryptoJS.Rabbit.encrypt(text, key + key).toString()}const decryptString = (text: string, key: string): string => {- const bytes = CryptoJS.Rabbit.decrypt(text, key)+ const bytes = CryptoJS.Rabbit.decrypt(text, key + key)return bytes.toString(CryptoJS.enc.Utf8)}@@ -51,9 +52,8 @@ const COOKIE_MAX_AGE = 300 * 24 * 60 * 60 * 1000const captchaString = (text: string) => `${_toLower(text)}${CAPTCHA_SALT}`-const TEST_SECRET_KEY = 'wfOw1Jw3JAjcrHaQFvIvBS4qdG'-export const CAPTCHA_COOKIE_KEY = 'swetrix-captcha-token'+export const CAPTCHA_TOKEN_LIFETIME = 300 // seconds (5 minutes).const captchaDTO = (pid: string,@@ -79,8 +79,35 @@ const captchaDTO = (export class CaptchaService {constructor(private readonly logger: AppLoggerService,+ private readonly projectService: ProjectService,) { }+ // checks if captcha is enabled for pid+ async _isCaptchaEnabledForPID(pid: string): Promise<boolean> {+ const project = await this.projectService.getRedisProject(pid)++ if (!project) {+ return false+ }++ return project.isCaptchaEnabled+ }++ // validates pid, checks if captcha is enabled and throws an error otherwise+ async validatePIDForCAPTCHA(pid: string): Promise<void> {+ if (!isValidPID(pid)) {+ throw new BadRequestException(+ 'The provided Project ID (pid) is incorrect',+ )+ }++ if (!(await this._isCaptchaEnabledForPID(pid))) {+ throw new BadRequestException(+ 'CAPTCHA is not enabled for this Project',+ )+ }+ }+async logCaptchaPass(pid: string, userAgent: string, headers: any, timestamp: number, isManual: boolean): Promise<void> {const ua = UAParser(userAgent)const dv = ua.device.type || 'desktop'@@ -99,12 +126,22 @@ export class CaptchaService {}}- generateToken(publicKey: string, hash: string, timestamp: number, autoVerified: boolean): string {- // todo: lookup the secret key from the public key in the database- const secretKey = TEST_SECRET_KEY+ async generateToken(pid: string, hash: string, timestamp: number, autoVerified: boolean): Promise<string> {+ const project = await this.projectService.getRedisProject(pid)++ if (!project) {+ throw new BadRequestException('Project not found')+ }++ // @ts-ignore+ const secretKey = project.captchaSecretKey++ if (!secretKey) {+ throw new BadRequestException('No secret key generated for this project')+ }const token = {- hash, timestamp, autoVerified,+ hash, timestamp, autoVerified, pid,}return encryptString(JSON.stringify(token), secretKey)@@ -120,6 +157,10 @@ export class CaptchaService {throw new BadRequestException('Could not decrypt token')}+ if (dayjs.utc().diff(dayjs.utc(parsed.timestamp), 'second') > CAPTCHA_TOKEN_LIFETIME) {+ throw new BadRequestException('Token expired')+ }+return parsed}diff --git a/src/project/entity/project.entity.ts b/src/project/entity/project.entity.tsindex 63183ad8c..92b06b4c3 100644--- a/src/project/entity/project.entity.ts+++ b/src/project/entity/project.entity.ts@@ -48,6 +48,24 @@ export class Project {})public: boolean+ // Swetrix CAPTCHA related stuff++ @ApiProperty()+ @Column({ default: true })+ isAnalyticsProject: boolean++ @ApiProperty()+ @Column({ default: false })+ isCaptchaProject: boolean++ @ApiProperty()+ @Column({ default: false })+ isCaptchaEnabled: boolean++ @ApiProperty()+ @Column('varchar', { default: null, length: 50 })+ captchaSecretKey: string+@ApiProperty({ type: () => User })@ManyToOne(() => User, user => user.projects)admin: User@@ -69,16 +87,4 @@ export class Project {)@JoinTable()extensions: ExtensionToProject[]-- @ApiProperty()- @Column({ default: true })- isAnalyticsProject: boolean-- @ApiProperty()- @Column({ default: false})- isCaptchaProject: boolean-- @ApiProperty()- @Column({ default: false})- isCaptchaEnabled: boolean}diff --git a/src/project/project.service.ts b/src/project/project.service.tsindex 4fb2c157c..7de0f7b80 100644--- a/src/project/project.service.ts+++ b/src/project/project.service.ts@@ -40,7 +40,11 @@ import {IP_REGEX,ORIGINS_REGEX,getRedisProjectKey,+ redisProjectCacheTimeout,} from '../common/constants'+import {+ getProjectsClickhouse,+} from '../common/utils'dayjs.extend(utc)@@ -105,6 +109,56 @@ export class ProjectService {private userService: UserService,) { }+ async getRedisProject(pid: string): Promise<Project | null> {+ const pidKey = getRedisProjectKey(pid)+ let project: string | Project = await redis.get(pidKey)++ if (_isEmpty(project)) {+ if (isSelfhosted) {+ project = await getProjectsClickhouse(pid)+ } else {+ // todo: optimise the relations - select+ // select only required columns+ // https://stackoverflow.com/questions/59645009/how-to-return-only-some-columns-of-a-relations-with-typeorm+ project = await this.findOne(pid, {+ relations: ['admin'],+ select: ['origins', 'active', 'admin', 'public', 'ipBlacklist', 'captchaSecretKey', 'isCaptchaEnabled'],+ })+ }+ if (_isEmpty(project))+ throw new BadRequestException(+ 'The provided Project ID (pid) is incorrect',+ )++ if (!isSelfhosted) {+ const share = await this.findShare({+ where: {+ project: pid,+ },+ relations: ['user'],+ })+ // @ts-ignore+ project = { ...project, share }+ }++ await redis.set(+ pidKey,+ JSON.stringify(project),+ 'EX',+ redisProjectCacheTimeout,+ )+ } else {+ try {+ project = JSON.parse(project)+ } catch {+ throw new InternalServerErrorException('Error while processing project')+ }+ }++ // @ts-ignore+ return project+ }+async paginate(options: PaginationOptionsInterface,where: Record<string, unknown> | undefined,