📈 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
Moved custom event validation to DTOs
Blue Mousee7818311febf
diff --git a/.gitignore b/.gitignoreindex 919c24ad7..31f8f019d 100644--- a/.gitignore+++ b/.gitignore@@ -53,6 +53,10 @@ yarn.lock# Sentry Config File.sentryclirc+# API+backend/.yarn/*+backend/.yarnrc.yml+# Frontendweb/buildweb/public/builddiff --git a/backend/apps/production/src/analytics/analytics.controller.ts b/backend/apps/production/src/analytics/analytics.controller.tsindex 81f7e16b9..4c9f619ba 100644--- a/backend/apps/production/src/analytics/analytics.controller.ts+++ b/backend/apps/production/src/analytics/analytics.controller.ts@@ -1091,7 +1091,7 @@ export class AnalyticsController {await this.analyticsService.throwIfBot(errorDTO.pid, userAgent)- await this.analyticsService.validate(errorDTO, origin, 'error', ip)+ await this.analyticsService.validate(errorDTO, origin, ip)const { city, region, country } = getGeoDetails(ip, errorDTO.tz)@@ -1176,7 +1176,7 @@ export class AnalyticsController {this.analyticsService.validateCustomEVMeta(eventsDTO.meta)await this.analyticsService.throwIfBot(eventsDTO.pid, userAgent)- await this.analyticsService.validate(eventsDTO, origin, 'custom', ip)+ await this.analyticsService.validate(eventsDTO, origin, ip)if (eventsDTO.unique) {const [unique] = await this.analyticsService.isUnique(@@ -1279,7 +1279,7 @@ export class AnalyticsController {await this.analyticsService.throwIfBot(logDTO.pid, userAgent)- await this.analyticsService.validate(logDTO, origin, 'log', ip)+ await this.analyticsService.validate(logDTO, origin, ip)const [unique, psid] = await this.analyticsService.isUnique(logDTO.pid,@@ -1410,9 +1410,10 @@ export class AnalyticsController {pid,}- await this.analyticsService.validate(logDTO, origin)-const ip = getIPFromHeaders(headers) || reqIP || ''++ await this.analyticsService.validate(logDTO, origin, ip)+const [unique, psid] = await this.analyticsService.isUnique(logDTO.pid,userAgent,diff --git a/backend/apps/production/src/analytics/analytics.service.ts b/backend/apps/production/src/analytics/analytics.service.tsindex 8fe4e1d88..e0b81e9e1 100644--- a/backend/apps/production/src/analytics/analytics.service.ts+++ b/backend/apps/production/src/analytics/analytics.service.ts@@ -148,10 +148,6 @@ const timeBucketToDays = [{ lt: 99999, tb: [TimeBucketType.YEAR] },]-// Smaller than 64 characters, must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.)-// eslint-disable-next-line no-useless-escape-const customEVvalidate = /^[a-zA-Z](?:[\w\.]){0,62}$/-const timeBucketConversion = {minute: 'toStartOfMinute',hour: 'toStartOfHour',@@ -441,7 +437,6 @@ export class AnalyticsService {async validate(logDTO: PageviewsDTO | EventsDTO | ErrorDTO,origin: string,- type: 'custom' | 'log' | 'error' = 'log',ip?: string,): Promise<string | null> {if (_isEmpty(logDTO)) {@@ -451,20 +446,6 @@ export class AnalyticsService {const { pid } = logDTOthis.validatePID(pid)- if (type === 'custom') {- const { ev } = <EventsDTO>logDTO-- if (_isEmpty(ev)) {- throw new BadRequestException('Empty custom events are not allowed')- }-- if (!customEVvalidate.test(ev)) {- throw new BadRequestException(- 'An incorrect event name (ev) is provided',- )- }- }-// 'tz' does not need validation as it's based on getCountryForTimezone detectionconst { lc } = logDTOdiff --git a/backend/apps/production/src/analytics/dto/events.dto.ts b/backend/apps/production/src/analytics/dto/events.dto.tsindex 9f394c921..7b5a14a99 100644--- a/backend/apps/production/src/analytics/dto/events.dto.ts+++ b/backend/apps/production/src/analytics/dto/events.dto.ts@@ -9,6 +9,7 @@ import {ValidatorConstraint,ValidatorConstraintInterface,Validate,+ Matches,} from 'class-validator'export const MAX_METADATA_KEYS = 20@@ -62,6 +63,11 @@ export class EventsDTO {maxLength: 64,})@IsNotEmpty()+ // Smaller than 64 characters, must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.)+ @Matches(/^[a-zA-Z][\w.]{0,62}$/, {+ message:+ 'Event name must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.). Maximum length is 63 characters.',+ })ev: string@ApiProperty({diff --git a/backend/apps/selfhosted/src/analytics/analytics.controller.ts b/backend/apps/selfhosted/src/analytics/analytics.controller.tsindex a53783d1f..a31d3afd3 100644--- a/backend/apps/selfhosted/src/analytics/analytics.controller.ts+++ b/backend/apps/selfhosted/src/analytics/analytics.controller.ts@@ -896,7 +896,7 @@ export class AnalyticsController {this.analyticsService.validateCustomEVMeta(eventsDTO.meta)await this.analyticsService.throwIfBot(eventsDTO.pid, userAgent)- await this.analyticsService.validate(eventsDTO, origin, 'custom', ip)+ await this.analyticsService.validate(eventsDTO, origin, ip)if (eventsDTO.unique) {const [unique] = await this.analyticsService.isUnique(@@ -998,7 +998,7 @@ export class AnalyticsController {await this.analyticsService.throwIfBot(logDTO.pid, userAgent)- await this.analyticsService.validate(logDTO, origin, 'log', ip)+ await this.analyticsService.validate(logDTO, origin, ip)const [unique, psid] = await this.analyticsService.isUnique(logDTO.pid,@@ -1125,13 +1125,14 @@ export class AnalyticsController {return res.end(TRANSPARENT_GIF_BUFFER, 'binary')}+ const ip = getIPFromHeaders(headers) || reqIP || ''+const logDTO: PageviewsDTO = {pid,}- await this.analyticsService.validate(logDTO, origin)+ await this.analyticsService.validate(logDTO, origin, ip)- const ip = getIPFromHeaders(headers) || reqIP || ''const [unique, psid] = await this.analyticsService.isUnique(logDTO.pid,userAgent,@@ -1428,7 +1429,7 @@ export class AnalyticsController {await this.analyticsService.throwIfBot(errorDTO.pid, userAgent)- await this.analyticsService.validate(errorDTO, origin, 'error', ip)+ await this.analyticsService.validate(errorDTO, origin, ip)const { city, region, country } = getGeoDetails(ip, errorDTO.tz, headers)diff --git a/backend/apps/selfhosted/src/analytics/analytics.service.ts b/backend/apps/selfhosted/src/analytics/analytics.service.tsindex 030331a75..c88ee096c 100644--- a/backend/apps/selfhosted/src/analytics/analytics.service.ts+++ b/backend/apps/selfhosted/src/analytics/analytics.service.ts@@ -137,10 +137,6 @@ const timeBucketToDays = [{ lt: 99999, tb: [TimeBucketType.YEAR] },]-// Smaller than 64 characters, must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.)-// eslint-disable-next-line no-useless-escape-const customEVvalidate = /^[a-zA-Z](?:[\w\.]){0,62}$/-const timeBucketConversion = {minute: 'toStartOfMinute',hour: 'toStartOfHour',@@ -390,7 +386,6 @@ export class AnalyticsService {async validate(logDTO: PageviewsDTO | EventsDTO | ErrorDTO,origin: string,- type: 'custom' | 'log' | 'error' = 'log',ip?: string,): Promise<string | null> {if (_isEmpty(logDTO)) {@@ -400,20 +395,6 @@ export class AnalyticsService {const { pid } = logDTOthis.validatePID(pid)- if (type === 'custom') {- const { ev } = <EventsDTO>logDTO-- if (_isEmpty(ev)) {- throw new BadRequestException('Empty custom events are not allowed')- }-- if (!customEVvalidate.test(ev)) {- throw new BadRequestException(- 'An incorrect event name (ev) is provided',- )- }- }-// 'tz' does not need validation as it's based on getCountryForTimezone detectionconst { lc } = logDTOdiff --git a/backend/apps/selfhosted/src/analytics/dto/events.dto.ts b/backend/apps/selfhosted/src/analytics/dto/events.dto.tsindex 9f394c921..7b5a14a99 100644--- a/backend/apps/selfhosted/src/analytics/dto/events.dto.ts+++ b/backend/apps/selfhosted/src/analytics/dto/events.dto.ts@@ -9,6 +9,7 @@ import {ValidatorConstraint,ValidatorConstraintInterface,Validate,+ Matches,} from 'class-validator'export const MAX_METADATA_KEYS = 20@@ -62,6 +63,11 @@ export class EventsDTO {maxLength: 64,})@IsNotEmpty()+ // Smaller than 64 characters, must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.)+ @Matches(/^[a-zA-Z][\w.]{0,62}$/, {+ message:+ 'Event name must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.). Maximum length is 63 characters.',+ })ev: string@ApiProperty({