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 potential security issues

Blue Moused6a1a3e7c122
diff --git a/backend/apps/cloud/src/feature-flag/dto/feature-flag.dto.ts b/backend/apps/cloud/src/feature-flag/dto/feature-flag.dto.ts
index 15152a641..1ba50d2c3 100644
--- a/backend/apps/cloud/src/feature-flag/dto/feature-flag.dto.ts
+++ b/backend/apps/cloud/src/feature-flag/dto/feature-flag.dto.ts
@@ -11,19 +11,36 @@ import {
Max,
MaxLength,
Matches,
+ ArrayMaxSize,
+ IsIn,
} from 'class-validator'
import { Type } from 'class-transformer'
import { FeatureFlagType, TargetingRule } from '../entity/feature-flag.entity'
+// Valid targeting rule columns (attribute keys)
+export const VALID_TARGETING_COLUMNS = [
+ 'cc', // country code
+ 'ct', // city
+ 'rg', // region
+ 'dv', // device type
+ 'br', // browser
+ 'os', // operating system
+] as const
+
export class TargetingRuleDto implements TargetingRule {
@ApiProperty({
- description: 'Column/attribute to filter on (cc, dv, br, os, pg, etc.)',
+ description: 'Column/attribute to filter on (cc, ct, rg, dv, br, os)',
+ enum: VALID_TARGETING_COLUMNS,
})
@IsString()
+ @IsIn(VALID_TARGETING_COLUMNS, {
+ message: 'Column must be one of: cc, ct, rg, dv, br, os',
+ })
column: string
@ApiProperty({ description: 'Value to match against' })
@IsString()
+ @MaxLength(256)
filter: string
@ApiProperty({
@@ -80,10 +97,11 @@ export class CreateFeatureFlagDto {
@ApiPropertyOptional({
type: [TargetingRuleDto],
description:
- 'Array of targeting rules to filter which visitors see this flag',
+ 'Array of targeting rules to filter which visitors see this flag (max 50)',
})
@IsOptional()
@IsArray()
+ @ArrayMaxSize(50, { message: 'Cannot have more than 50 targeting rules' })
@ValidateNested({ each: true })
@Type(() => TargetingRuleDto)
targetingRules?: TargetingRuleDto[]
@@ -135,10 +153,11 @@ export class UpdateFeatureFlagDto {
@ApiPropertyOptional({
type: [TargetingRuleDto],
- description: 'Array of targeting rules',
+ description: 'Array of targeting rules (max 50)',
})
@IsOptional()
@IsArray()
+ @ArrayMaxSize(50, { message: 'Cannot have more than 50 targeting rules' })
@ValidateNested({ each: true })
@Type(() => TargetingRuleDto)
targetingRules?: TargetingRuleDto[] | null
@@ -152,6 +171,7 @@ export class UpdateFeatureFlagDto {
export class EvaluateFeatureFlagsDto {
@ApiProperty({ description: 'Project ID to evaluate flags for' })
@IsString()
+ @MaxLength(12)
pid: string
@ApiPropertyOptional({
@@ -160,6 +180,7 @@ export class EvaluateFeatureFlagsDto {
})
@IsOptional()
@IsString()
+ @MaxLength(256)
profileId?: string
}
diff --git a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
index dc181a541..39cf141eb 100644
--- a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
+++ b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
@@ -258,12 +258,10 @@ export class FeatureFlagController {
where: { id: evaluateDto.pid },
})
- if (_isEmpty(project)) {
- throw new NotFoundException('Project not found')
- }
-
- if (!project.active) {
- throw new BadRequestException('Project is not active')
+ // Return empty flags instead of revealing whether a project exists
+ // This prevents project ID enumeration attacks
+ if (_isEmpty(project) || !project.active) {
+ return { flags: {} }
}
const flags = await this.featureFlagService.findEnabledByProject(
diff --git a/backend/apps/community/src/feature-flag/dto/feature-flag.dto.ts b/backend/apps/community/src/feature-flag/dto/feature-flag.dto.ts
index 15152a641..1ba50d2c3 100644
--- a/backend/apps/community/src/feature-flag/dto/feature-flag.dto.ts
+++ b/backend/apps/community/src/feature-flag/dto/feature-flag.dto.ts
@@ -11,19 +11,36 @@ import {
Max,
MaxLength,
Matches,
+ ArrayMaxSize,
+ IsIn,
} from 'class-validator'
import { Type } from 'class-transformer'
import { FeatureFlagType, TargetingRule } from '../entity/feature-flag.entity'
+// Valid targeting rule columns (attribute keys)
+export const VALID_TARGETING_COLUMNS = [
+ 'cc', // country code
+ 'ct', // city
+ 'rg', // region
+ 'dv', // device type
+ 'br', // browser
+ 'os', // operating system
+] as const
+
export class TargetingRuleDto implements TargetingRule {
@ApiProperty({
- description: 'Column/attribute to filter on (cc, dv, br, os, pg, etc.)',
+ description: 'Column/attribute to filter on (cc, ct, rg, dv, br, os)',
+ enum: VALID_TARGETING_COLUMNS,
})
@IsString()
+ @IsIn(VALID_TARGETING_COLUMNS, {
+ message: 'Column must be one of: cc, ct, rg, dv, br, os',
+ })
column: string
@ApiProperty({ description: 'Value to match against' })
@IsString()
+ @MaxLength(256)
filter: string
@ApiProperty({
@@ -80,10 +97,11 @@ export class CreateFeatureFlagDto {
@ApiPropertyOptional({
type: [TargetingRuleDto],
description:
- 'Array of targeting rules to filter which visitors see this flag',
+ 'Array of targeting rules to filter which visitors see this flag (max 50)',
})
@IsOptional()
@IsArray()
+ @ArrayMaxSize(50, { message: 'Cannot have more than 50 targeting rules' })
@ValidateNested({ each: true })
@Type(() => TargetingRuleDto)
targetingRules?: TargetingRuleDto[]
@@ -135,10 +153,11 @@ export class UpdateFeatureFlagDto {
@ApiPropertyOptional({
type: [TargetingRuleDto],
- description: 'Array of targeting rules',
+ description: 'Array of targeting rules (max 50)',
})
@IsOptional()
@IsArray()
+ @ArrayMaxSize(50, { message: 'Cannot have more than 50 targeting rules' })
@ValidateNested({ each: true })
@Type(() => TargetingRuleDto)
targetingRules?: TargetingRuleDto[] | null
@@ -152,6 +171,7 @@ export class UpdateFeatureFlagDto {
export class EvaluateFeatureFlagsDto {
@ApiProperty({ description: 'Project ID to evaluate flags for' })
@IsString()
+ @MaxLength(12)
pid: string
@ApiPropertyOptional({
@@ -160,6 +180,7 @@ export class EvaluateFeatureFlagsDto {
})
@IsOptional()
@IsString()
+ @MaxLength(256)
profileId?: string
}
diff --git a/backend/apps/community/src/feature-flag/feature-flag.controller.ts b/backend/apps/community/src/feature-flag/feature-flag.controller.ts
index 61d392d51..954765c14 100644
--- a/backend/apps/community/src/feature-flag/feature-flag.controller.ts
+++ b/backend/apps/community/src/feature-flag/feature-flag.controller.ts
@@ -214,12 +214,10 @@ export class FeatureFlagController {
const project = await this.projectService.getRedisProject(evaluateDto.pid)
- if (_isEmpty(project)) {
- throw new NotFoundException('Project not found')
- }
-
- if (!project.active) {
- throw new BadRequestException('Project is not active')
+ // Return empty flags instead of revealing whether a project exists
+ // This prevents project ID enumeration attacks
+ if (_isEmpty(project) || !project.active) {
+ return { flags: {} }
}
const flags = await this.featureFlagService.findEnabledByProject(
diff --git a/backend/libs/shared/src/feature-flag/evaluation.ts b/backend/libs/shared/src/feature-flag/evaluation.ts
index 2f00601ba..4af27dc9d 100644
--- a/backend/libs/shared/src/feature-flag/evaluation.ts
+++ b/backend/libs/shared/src/feature-flag/evaluation.ts
@@ -155,8 +155,9 @@ export function isInRolloutPercentage(
}
// Create a consistent hash based on flag key and profile ID
+ // Using SHA-256 for better cryptographic properties
const hash = crypto
- .createHash('md5')
+ .createHash('sha256')
.update(`${flagKey}:${profileId}`)
.digest('hex')