📈 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
properly validating columns for a specific data type
Blue Mouse0ae4b9cd9f8c
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.tsindex c2e6e5f5b..f5ee606f1 100644--- a/src/analytics/analytics.controller.ts+++ b/src/analytics/analytics.controller.ts@@ -35,6 +35,7 @@ import {AnalyticsService,getSessionKey,getHeartbeatKey,+ DataType,} from './analytics.service'import { TaskManagerService } from '../task-manager/task-manager.service'import { CurrentUserId } from '../auth/decorators/current-user-id.decorator'@@ -279,7 +280,7 @@ export class AnalyticsController {this.analyticsService.validateTimebucket(timeBucket)const [filtersQuery, filtersParams, parsedFilters] =- this.analyticsService.getFiltersQuery(filters)+ this.analyticsService.getFiltersQuery(filters, isCaptcha ? DataType.CAPTCHA : DataType.ANALYTICS)const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(from,to,@@ -393,7 +394,7 @@ export class AnalyticsController {this.analyticsService.validateTimebucket(timeBucket)const [filtersQuery, filtersParams] =- this.analyticsService.getFiltersQuery(filters)+ this.analyticsService.getFiltersQuery(filters, DataType.PERFORMANCE)const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(from,to,@@ -913,7 +914,7 @@ export class AnalyticsController {this.analyticsService.validateTimebucket(timeBucket)const [filtersQuery, filtersParams] =- this.analyticsService.getFiltersQuery(filters)+ this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS)await this.analyticsService.checkProjectAccess(pid, uid)const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.tsindex e4e89dc18..3b5147197 100644--- a/src/analytics/analytics.service.ts+++ b/src/analytics/analytics.service.ts@@ -90,7 +90,7 @@ export const cols = [export const captchaColumns = ['cc', 'br', 'os', 'dv']-export const perfCols = ['cc', 'pg', 'dv', 'br']+export const perfColumns = ['cc', 'pg', 'dv', 'br']const validPeriods = ['today',@@ -211,6 +211,12 @@ const generateParamsQuery = (return `SELECT ${col}, count(*) ${subQuery} AND ${col} IS NOT NULL AND unique='1' GROUP BY ${col}`}+export enum DataType {+ ANALYTICS = 'analytics',+ PERFORMANCE = 'performance',+ CAPTCHA = 'captcha',+}+@Injectable()export class AnalyticsService {constructor(private readonly projectService: ProjectService) {}@@ -343,6 +349,18 @@ export class AnalyticsService {return null}+ getDataTypeColumns(dataType: DataType): string[] {+ if (dataType === DataType.ANALYTICS) {+ return cols+ }++ if (dataType === DataType.PERFORMANCE) {+ return perfColumns+ }++ return captchaColumns+ }+getGroupFromTo(from: string,to: string,@@ -557,8 +575,7 @@ export class AnalyticsService {}// returns SQL filters query in a format like 'AND col=value AND ...'- getFiltersQuery(filters: string): GetFiltersQuery {- // TODO: Use captchaColumns for validation of CAPTCHA filters+ getFiltersQuery(filters: string, dataType: DataType): GetFiltersQuery {let parsed = []let query = ''let params = {}@@ -578,6 +595,8 @@ export class AnalyticsService {return [query, params, parsed]}+ const columns = this.getDataTypeColumns(dataType)+for (let i = 0; i < _size(parsed); ++i) {const { column, filter, isExclusive } = parsed[i]@@ -591,7 +610,7 @@ export class AnalyticsService {continue}- if (!_includes(cols, column)) {+ if (!_includes(columns, column)) {throw new UnprocessableEntityException(`The provided filter (${column}) is not supported`,)@@ -760,7 +779,7 @@ export class AnalyticsService {}if (isPerformance) {- columns = perfCols+ columns = perfColumns}const paramsPromises = _map(columns, async col => {