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 Mousedeeed9de2578
diff --git a/backend/apps/cloud/src/goal/dto/goal.dto.ts b/backend/apps/cloud/src/goal/dto/goal.dto.ts
index 11b22efab..852511446 100644
--- a/backend/apps/cloud/src/goal/dto/goal.dto.ts
+++ b/backend/apps/cloud/src/goal/dto/goal.dto.ts
@@ -8,10 +8,66 @@ import {
MaxLength,
IsBoolean,
IsNotEmpty,
+ ValidatorConstraint,
+ ValidatorConstraintInterface,
+ ValidationArguments,
+ Validate,
} from 'class-validator'
import { Type } from 'class-transformer'
import { GoalType, GoalMatchType, MetadataFilter } from '../entity/goal.entity'
+// Maximum regex length to prevent ReDoS attacks
+const MAX_REGEX_LENGTH = 200
+
+// Patterns that could cause catastrophic backtracking (ReDoS)
+const DANGEROUS_REGEX_PATTERNS = [
+ /(\+|\*|\?)\1{2,}/, // Nested quantifiers like +++, ***, ???
+ /\(\?[^)]*\)\+/, // Possessive quantifiers (not supported, but flag anyway)
+ /\([^)]*\)\{\d+,\}.*\1/, // Backreferences with quantifiers
+ /(\.\*){3,}/, // Multiple consecutive .* patterns
+ /\([^)]*\|[^)]*\)\+/, // Alternation with + quantifier
+]
+
+@ValidatorConstraint({ name: 'safeRegex', async: false })
+export class SafeRegexConstraint implements ValidatorConstraintInterface {
+ validate(value: string, args: ValidationArguments): boolean {
+ const obj = args.object as any
+
+ // Only validate if matchType is regex
+ if (obj.matchType !== GoalMatchType.REGEX) {
+ return true
+ }
+
+ if (!value) {
+ return true
+ }
+
+ // Check length
+ if (value.length > MAX_REGEX_LENGTH) {
+ return false
+ }
+
+ // Check for dangerous patterns
+ for (const pattern of DANGEROUS_REGEX_PATTERNS) {
+ if (pattern.test(value)) {
+ return false
+ }
+ }
+
+ // Try to compile the regex to ensure it's valid
+ try {
+ new RegExp(value)
+ return true
+ } catch {
+ return false
+ }
+ }
+
+ defaultMessage(_args: ValidationArguments): string {
+ return `Invalid regex pattern. Ensure the pattern is valid, not longer than ${MAX_REGEX_LENGTH} characters, and doesn't contain potentially dangerous constructs.`
+ }
+}
+
export class MetadataFilterDto implements MetadataFilter {
@ApiProperty()
@IsString()
@@ -48,6 +104,7 @@ export class CreateGoalDto {
@IsString()
@MaxLength(500)
@IsOptional()
+ @Validate(SafeRegexConstraint)
value?: string
@ApiPropertyOptional({
@@ -85,6 +142,7 @@ export class UpdateGoalDto {
@IsString()
@MaxLength(500)
@IsOptional()
+ @Validate(SafeRegexConstraint)
value?: string
@ApiPropertyOptional({
diff --git a/backend/apps/community/src/goal/dto/goal.dto.ts b/backend/apps/community/src/goal/dto/goal.dto.ts
index 61d5ad5a5..b77641c5a 100644
--- a/backend/apps/community/src/goal/dto/goal.dto.ts
+++ b/backend/apps/community/src/goal/dto/goal.dto.ts
@@ -7,10 +7,66 @@ import {
MaxLength,
IsBoolean,
IsNotEmpty,
+ ValidatorConstraint,
+ ValidatorConstraintInterface,
+ ValidationArguments,
+ Validate,
} from 'class-validator'
import { Type } from 'class-transformer'
import { GoalType, GoalMatchType, MetadataFilter } from '../entity/goal.entity'
+// Maximum regex length to prevent ReDoS attacks
+const MAX_REGEX_LENGTH = 200
+
+// Patterns that could cause catastrophic backtracking (ReDoS)
+const DANGEROUS_REGEX_PATTERNS = [
+ /(\+|\*|\?)\1{2,}/, // Nested quantifiers like +++, ***, ???
+ /\(\?[^)]*\)\+/, // Possessive quantifiers (not supported, but flag anyway)
+ /\([^)]*\)\{\d+,\}.*\1/, // Backreferences with quantifiers
+ /(\.\*){3,}/, // Multiple consecutive .* patterns
+ /\([^)]*\|[^)]*\)\+/, // Alternation with + quantifier
+]
+
+@ValidatorConstraint({ name: 'safeRegex', async: false })
+export class SafeRegexConstraint implements ValidatorConstraintInterface {
+ validate(value: string, args: ValidationArguments): boolean {
+ const obj = args.object as any
+
+ // Only validate if matchType is regex
+ if (obj.matchType !== GoalMatchType.REGEX) {
+ return true
+ }
+
+ if (!value) {
+ return true
+ }
+
+ // Check length
+ if (value.length > MAX_REGEX_LENGTH) {
+ return false
+ }
+
+ // Check for dangerous patterns
+ for (const pattern of DANGEROUS_REGEX_PATTERNS) {
+ if (pattern.test(value)) {
+ return false
+ }
+ }
+
+ // Try to compile the regex to ensure it's valid
+ try {
+ new RegExp(value)
+ return true
+ } catch {
+ return false
+ }
+ }
+
+ defaultMessage(_args: ValidationArguments): string {
+ return `Invalid regex pattern. Ensure the pattern is valid, not longer than ${MAX_REGEX_LENGTH} characters, and doesn't contain potentially dangerous constructs.`
+ }
+}
+
export class MetadataFilterDto implements MetadataFilter {
@IsString()
@IsNotEmpty()
@@ -40,6 +96,7 @@ export class CreateGoalDto {
@IsString()
@MaxLength(500)
@IsOptional()
+ @Validate(SafeRegexConstraint)
value?: string
@IsArray()
@@ -66,6 +123,7 @@ export class UpdateGoalDto {
@IsString()
@MaxLength(500)
@IsOptional()
+ @Validate(SafeRegexConstraint)
value?: string
@IsArray()
diff --git a/backend/apps/community/src/goal/goal.service.ts b/backend/apps/community/src/goal/goal.service.ts
index 7c8e42d03..c127e2744 100644
--- a/backend/apps/community/src/goal/goal.service.ts
+++ b/backend/apps/community/src/goal/goal.service.ts
@@ -239,6 +239,12 @@ export class GoalService {
if (col === 'active') {
return `${col}={${col}:Int8}`
}
+
+ // Handle nullable string columns
+ if (value === null && (col === 'value' || col === 'metadataFilters')) {
+ return `${col}=NULL`
+ }
+
return `${col}={${col}:String}`
}).join(', ')
diff --git a/web/app/pages/Project/Goals/View/GoalsView.tsx b/web/app/pages/Project/Goals/View/GoalsView.tsx
index 3f04d6d6a..1d8c6b971 100644
--- a/web/app/pages/Project/Goals/View/GoalsView.tsx
+++ b/web/app/pages/Project/Goals/View/GoalsView.tsx
@@ -433,6 +433,7 @@ const GoalsView = ({ period, from = '', to = '', timezone }: GoalsViewProps) =>
const [isLoading, setIsLoading] = useState<boolean | null>(null)
const isLoadingRef = useRef(false) // Use ref to avoid stale closure issues in loadGoals
+ const isMountedRef = useRef(true) // Track if component is mounted to prevent memory leaks
const [total, setTotal] = useState(0)
const [goals, setGoals] = useState<Goal[]>([])
const [goalStats, setGoalStats] = useState<Record<string, GoalStats | null>>({})
@@ -450,6 +451,14 @@ const GoalsView = ({ period, from = '', to = '', timezone }: GoalsViewProps) =>
const [isModalOpen, setIsModalOpen] = useState(false)
const [editingGoalId, setEditingGoalId] = useState<string | null>(null)
+ // Cleanup on unmount
+ useEffect(() => {
+ isMountedRef.current = true
+ return () => {
+ isMountedRef.current = false
+ }
+ }, [])
+
const pageAmount = Math.ceil(total / DEFAULT_GOALS_TAKE)
const filteredGoals = useMemo(() => {
@@ -469,13 +478,19 @@ const GoalsView = ({ period, from = '', to = '', timezone }: GoalsViewProps) =>
try {
const result = await getProjectGoals(id, take, skip)
- setGoals(result.results)
- setTotal(result.total)
+ if (isMountedRef.current) {
+ setGoals(result.results)
+ setTotal(result.total)
+ }
} catch (reason: any) {
- setError(reason?.message || reason?.toString() || 'Unknown error')
+ if (isMountedRef.current) {
+ setError(reason?.message || reason?.toString() || 'Unknown error')
+ }
} finally {
isLoadingRef.current = false
- setIsLoading(false)
+ if (isMountedRef.current) {
+ setIsLoading(false)
+ }
}
}
@@ -483,27 +498,36 @@ const GoalsView = ({ period, from = '', to = '', timezone }: GoalsViewProps) =>
setStatsLoading((prev) => ({ ...prev, [goalId]: true }))
try {
const stats = await getGoalStats(goalId, period, from, to, timezone)
- setGoalStats((prev) => ({ ...prev, [goalId]: stats }))
+ if (isMountedRef.current) {
+ setGoalStats((prev) => ({ ...prev, [goalId]: stats }))
+ }
} catch (err) {
console.error('Failed to load goal stats:', err)
- setGoalStats((prev) => ({ ...prev, [goalId]: null }))
+ if (isMountedRef.current) {
+ setGoalStats((prev) => ({ ...prev, [goalId]: null }))
+ }
} finally {
- setStatsLoading((prev) => ({ ...prev, [goalId]: false }))
+ if (isMountedRef.current) {
+ setStatsLoading((prev) => ({ ...prev, [goalId]: false }))
+ }
}
}
const loadGoalChartData = async (goalId: string, showLoading = true) => {
- if (showLoading) {
- setChartLoading((prev) => ({ ...prev, [goalId]: true }))
- }
+ setChartLoading((prev) => ({ ...prev, [goalId]: showLoading ? true : prev[goalId] }))
try {
const result = await getGoalChart(goalId, period, from, to, timeBucket, timezone)
- setGoalChartData((prev) => ({ ...prev, [goalId]: result.chart }))
+ if (isMountedRef.current) {
+ setGoalChartData((prev) => ({ ...prev, [goalId]: result.chart }))
+ }
} catch (err) {
console.error('Failed to load goal chart:', err)
- setGoalChartData((prev) => ({ ...prev, [goalId]: null }))
+ if (isMountedRef.current) {
+ setGoalChartData((prev) => ({ ...prev, [goalId]: null }))
+ }
} finally {
- if (showLoading) {
+ // Always clear loading state
+ if (isMountedRef.current) {
setChartLoading((prev) => ({ ...prev, [goalId]: false }))
}
}