Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(flags): validate semver values before saving release conditions (#68450)

posthog[bot]634136f34b75
diff --git a/frontend/src/lib/components/PropertyFilters/components/PropertyValue.tsx b/frontend/src/lib/components/PropertyFilters/components/PropertyValue.tsx
index e4b9a4bd9650..631777c6ed32 100644
--- a/frontend/src/lib/components/PropertyFilters/components/PropertyValue.tsx
+++ b/frontend/src/lib/components/PropertyFilters/components/PropertyValue.tsx
@@ -123,9 +123,9 @@ export function PropertyValue({
/>
)
- // TODO: Add semver input validation when a semver operator is selected.
- // This will require detecting isOperatorSemver(operator) and validating the input
- // matches semver format (e.g., "1.2.3", "1.2.3-alpha", etc.)
+ // Semver input validation lives in featureFlagReleaseConditionsLogic's getPropertyValueError,
+ // which gates saving feature flag release conditions. This generic component has no
+ // equivalent check for its other callers (cohorts, insights, etc.).
// we first load a set of suggested values when there is no user input yet to avoid
// options jumping around as the user types, we keep the initially loaded options
diff --git a/frontend/src/lib/utils/semver.test.ts b/frontend/src/lib/utils/semver.test.ts
index 36ce05429bff..caafd595f585 100644
--- a/frontend/src/lib/utils/semver.test.ts
+++ b/frontend/src/lib/utils/semver.test.ts
@@ -1,4 +1,4 @@
-import { createVersionChecker, parseVersion } from './semver'
+import { createVersionChecker, isValidSemverValue, parseVersion } from './semver'
describe('semver', () => {
describe('parseVersion', () => {
@@ -27,4 +27,22 @@ describe('semver', () => {
expect(isSupportedVersion('4.5.6-alpha')).toEqual(false)
})
})
+
+ describe('isValidSemverValue', () => {
+ // Mirrors the backend `parse_semver` gate: drift here re-introduces the feature flag save 400
+ // for non-semver values (or wrongly blocks a real version).
+ it.each(['1.2.3', '1.2', '1', '1.2.3-alpha.1', '1.2.3.4'])('accepts %s', (value) => {
+ expect(isValidSemverValue(value)).toBe(true)
+ })
+
+ it.each(['[email protected]', 'deadbeef', '1.', 'v1.2.3', ''])('rejects %s', (value) => {
+ expect(isValidSemverValue(value)).toBe(false)
+ })
+
+ it('accepts wildcard patterns only when allowWildcard is set', () => {
+ expect(isValidSemverValue('1.2.*', { allowWildcard: true })).toBe(true)
+ expect(isValidSemverValue('1.*', { allowWildcard: true })).toBe(true)
+ expect(isValidSemverValue('1.2.*')).toBe(false)
+ })
+ })
})
diff --git a/frontend/src/lib/utils/semver.ts b/frontend/src/lib/utils/semver.ts
index 30a4da16fb03..bdc43b178c6f 100644
--- a/frontend/src/lib/utils/semver.ts
+++ b/frontend/src/lib/utils/semver.ts
@@ -27,6 +27,30 @@ export function parseVersion(version: string): SemanticVersion {
return { major: majorInt, minor: minorInt, patch: patchInt, extra }
}
+// Validates a version string against the same shape the backend's `parse_semver` enforces
+// (feature flag `validate_filters`), so a non-semver value (an email, a hex string) is caught
+// inline before a save 400s. Intentionally stricter than the backend on component format: the
+// backend coerces each component with Python's `int()`, which tolerates things like a leading
+// `+` or `_` digit separators, while this only accepts plain digits. Kept separate from
+// `parseVersion` because that one accepts a leading `v`, which the backend's `int()` rejects.
+// `allowWildcard` handles patterns like `1.2.*` by dropping the trailing `.`/`*`, matching the
+// backend's `rstrip('.*')` for the wildcard operator.
+export function isValidSemverValue(value: string, options?: { allowWildcard?: boolean }): boolean {
+ let candidate = value.trim()
+ if (options?.allowWildcard) {
+ candidate = candidate.replace(/[.*]+$/, '')
+ }
+ // Strip any pre-release / build suffix (everything after the first hyphen), matching the backend.
+ const base = candidate.split('-')[0]
+ const parts = base.split('.')
+ if (parts[0] === '') {
+ return false
+ }
+ // Missing minor/patch default to "0" (e.g. "1" -> 1.0.0); extra parts are ignored, as on the backend.
+ const [major, minor = '0', patch = '0'] = parts
+ return [major, minor, patch].every((part) => /^\d+$/.test(part))
+}
+
export interface SemanticVersionDiff {
kind: 'major' | 'minor' | 'patch' | 'extra'
diff: number
diff --git a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditions.tsx b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditions.tsx
index a222833d9044..673d56b6b38a 100644
--- a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditions.tsx
+++ b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditions.tsx
@@ -2,7 +2,6 @@ import './FeatureFlag.scss'
import clsx from 'clsx'
import { useActions, useValues } from 'kea'
-import { Fragment } from 'react'
import { IconCopy, IconFlag, IconInfo, IconPlus, IconTrash } from '@posthog/icons'
import { LemonLabel, LemonSelect, LemonSnack, Link, Tooltip } from '@posthog/lemon-ui'
@@ -16,7 +15,7 @@ import { INSTANTLY_AVAILABLE_PROPERTIES } from 'lib/constants'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { GroupsAccessStatus, groupsAccessLogic } from 'lib/introductions/groupsAccessLogic'
import { GroupsIntroductionOption } from 'lib/introductions/GroupsIntroductionOption'
-import { IconArrowDown, IconArrowUp, IconErrorOutline, IconOpenInNew, IconSubArrowRight } from 'lib/lemon-ui/icons'
+import { IconArrowDown, IconArrowUp, IconOpenInNew, IconSubArrowRight } from 'lib/lemon-ui/icons'
import { LemonBanner } from 'lib/lemon-ui/LemonBanner'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonDialog } from 'lib/lemon-ui/LemonDialog'
@@ -52,6 +51,7 @@ import {
featureFlagReleaseConditionsLogic,
isDistinctIdFilter,
} from './featureFlagReleaseConditionsLogic'
+import { getPropertySelectErrorMessages } from './propertySelectErrorMessages'
function PropertyValueComponent({
property,
@@ -381,22 +381,7 @@ export function FeatureFlagReleaseConditions({
}
: undefined
}
- errorMessages={
- propertySelectErrors?.[index]?.properties?.some((message) => !!message.value)
- ? propertySelectErrors[index].properties?.map((message, index) => {
- return message.value ? (
- <div
- key={index}
- className="text-danger flex items-center gap-1 text-sm Field--error"
- >
- <IconErrorOutline className="text-xl" /> {message.value}
- </div>
- ) : (
- <Fragment key={index} />
- )
- })
- : null
- }
+ errorMessages={getPropertySelectErrorMessages(propertySelectErrors, index)}
hideBehavioralCohorts={!realtimeCohortFlagTargeting}
/>
</div>
diff --git a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsCollapsible.tsx b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsCollapsible.tsx
index 48515b620e8d..f6dcb91a2a7b 100644
--- a/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsCollapsible.tsx
+++ b/frontend/src/scenes/feature-flags/FeatureFlagReleaseConditionsCollapsible.tsx
@@ -85,6 +85,7 @@ import {
featureFlagReleaseConditionsLogic,
isDistinctIdFilter,
} from './featureFlagReleaseConditionsLogic'
+import { getPropertySelectErrorMessages, PropertySelectError } from './propertySelectErrorMessages'
interface FeatureFlagReleaseConditionsCollapsibleProps extends FeatureFlagReleaseConditionsLogicProps {
flagId?: FeatureFlagLogicProps['id']
@@ -349,6 +350,7 @@ interface ConditionProps {
filtersTaxonomicOptions: TaxonomicFilterProps['optionsFromProp']
releaseFilters: FeatureFlagFilters
variants?: MultivariateFlagVariant[]
+ propertySelectErrors: PropertySelectError[] | null | undefined
openConditions: string[]
handleOpenConditionsChange: (newKeys: string[]) => void
flagId?: FeatureFlagLogicProps['id']
@@ -412,6 +414,7 @@ const ConditionContent = ({
filtersTaxonomicOptions,
releaseFilters,
variants,
+ propertySelectErrors,
openConditions,
handleOpenConditionsChange,
flagId,
@@ -615,6 +618,7 @@ const ConditionContent = ({
)}
taxonomicFilterOptionsFromProp={filtersTaxonomicOptions}
hasRowOperator={false}
+ errorMessages={getPropertySelectErrorMessages(propertySelectErrors, index)}
hideBehavioralCohorts={!realtimeCohortFlagTargeting}
/>
</div>
@@ -836,6 +840,7 @@ export function FeatureFlagReleaseConditionsCollapsible({
groupTypes,
openConditions,
properties,
+ propertySelectErrors,
isAnyItemDragging,
draggedGroup,
} = useValues(releaseConditionsLogic)
@@ -1239,6 +1244,7 @@ export function FeatureFlagReleaseConditionsCollapsible({
filtersTaxonomicOptions={filtersTaxonomicOptions}
releaseFilters={releaseFilters}
variants={variants}
+ propertySelectErrors={propertySelectErrors}
openConditions={openConditions}
handleOpenConditionsChange={handleOpenConditionsChange}
flagId={flagId}
@@ -1314,6 +1320,7 @@ export function FeatureFlagReleaseConditionsCollapsible({
filtersTaxonomicOptions={filtersTaxonomicOptions}
releaseFilters={releaseFilters}
variants={variants}
+ propertySelectErrors={propertySelectErrors}
openConditions={openConditions}
handleOpenConditionsChange={handleOpenConditionsChange}
flagId={flagId}
@@ -1364,6 +1371,7 @@ export function FeatureFlagReleaseConditionsCollapsible({
filtersTaxonomicOptions={filtersTaxonomicOptions}
releaseFilters={releaseFilters}
variants={variants}
+ propertySelectErrors={propertySelectErrors}
openConditions={openConditions}
handleOpenConditionsChange={handleOpenConditionsChange}
flagId={flagId}
@@ -1396,6 +1404,7 @@ export function FeatureFlagReleaseConditionsCollapsible({
filtersTaxonomicOptions={filtersTaxonomicOptions}
releaseFilters={releaseFilters}
variants={variants}
+ propertySelectErrors={propertySelectErrors}
openConditions={openConditions}
handleOpenConditionsChange={handleOpenConditionsChange}
flagId={flagId}
diff --git a/frontend/src/scenes/feature-flags/featureFlagReleaseConditionsLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagReleaseConditionsLogic.test.ts
index 07327cca6fce..22e4ba776225 100644
--- a/frontend/src/scenes/feature-flags/featureFlagReleaseConditionsLogic.test.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagReleaseConditionsLogic.test.ts
@@ -764,6 +764,39 @@ describe('the feature flag release conditions logic', () => {
})
})
+ describe('semver value validation', () => {
+ // Wiring guard: the form gates submit on `propertySelectErrors`, so a bad semver value must
+ // surface here rather than reaching the backend and failing with an opaque 400.
+ const semverProperty = (value: string): AnyPropertyFilter => ({
+ key: 'app_version',
+ value,
+ operator: PropertyOperator.SemverGt,
+ type: PropertyFilterType.Person,
+ })
+
+ it('flags a non-semver value paired with a semver operator', () => {
+ logic.actions.setFilters(
+ generateFeatureFlagFilters([
+ { properties: [semverProperty('not-a-version')], rollout_percentage: 50, variant: null },
+ ])
+ )
+
+ expect(logic.values.propertySelectErrors[0].properties?.[0]?.value).toBe(
+ 'Enter a valid semver value (e.g. 1.2.3)'
+ )
+ })
+
+ it('accepts a valid semver value', () => {
+ logic.actions.setFilters(
+ generateFeatureFlagFilters([
+ { properties: [semverProperty('1.2.3')], rollout_percentage: 50, variant: null },
+ ])
+ )
+
+ expect(logic.values.propertySelectErrors[0].properties?.[0]).toEqual({ value: undefined })
+ })
+ })
+
describe('condition set descriptions', () => {
it('updates description for a condition set', () => {
const filters = generateFeatureFlagFilters([
diff --git a/frontend/src/scenes/feature-flags/featureFlagReleaseConditionsLogic.ts b/frontend/src/scenes/feature-flags/featureFlagReleaseConditionsLogic.ts
index 006866c980c0..5d4ee80891cb 100644
--- a/frontend/src/scenes/feature-flags/featureFlagReleaseConditionsLogic.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagReleaseConditionsLogic.ts
@@ -16,9 +16,11 @@ import { subscriptions } from 'kea-subscriptions'
import { v4 as uuidv4 } from 'uuid'
import api from 'lib/api'
-import { isEmptyProperty } from 'lib/components/PropertyFilters/utils'
+import { isEmptyProperty, isPropertyFilterWithOperator } from 'lib/components/PropertyFilters/utils'
import { TaxonomicFilterGroupType, TaxonomicFilterProps } from 'lib/components/TaxonomicFilter/types'
import { objectsEqual } from 'lib/utils/objects'
+import { isOperatorSemver } from 'lib/utils/operators'
+import { isValidSemverValue } from 'lib/utils/semver'
import { projectLogic } from 'scenes/projectLogic'
import { groupsModel } from '~/models/groupsModel'
@@ -30,6 +32,7 @@ import {
GroupTypeIndex,
MultivariateFlagVariant,
PropertyFilterType,
+ PropertyOperator,
UserBlastRadiusType,
} from '~/types'
@@ -41,6 +44,21 @@ export function isDistinctIdFilter(property: AnyPropertyFilter): boolean {
return property.type === PropertyFilterType.Person && property.key === 'distinct_id'
}
+// Gates the release-condition save on the same rules the backend enforces, so a bad value is
+// surfaced inline instead of failing with an opaque 400 on submit.
+function getPropertyValueError(property: AnyPropertyFilter): string | undefined {
+ if (isEmptyProperty(property)) {
+ return "Property filters can't be empty"
+ }
+ if (isPropertyFilterWithOperator(property) && isOperatorSemver(property.operator)) {
+ const allowWildcard = property.operator === PropertyOperator.SemverWildcard
+ if (typeof property.value !== 'string' || !isValidSemverValue(property.value, { allowWildcard })) {
+ return 'Enter a valid semver value (e.g. 1.2.3)'
+ }
+ }
+ return undefined
+}
+
// Server caps batch_by_distinct_ids per request; chunk client-side so every id resolves.
const DISTINCT_ID_BATCH_SIZE = 200
@@ -768,7 +786,7 @@ export const featureFlagReleaseConditionsLogic = kea<featureFlagReleaseCondition
(filters) => {
return filters?.groups?.map(({ properties, rollout_percentage }: FeatureFlagGroupType) => ({
properties: properties?.map((property: AnyPropertyFilter) => ({
- value: isEmptyProperty(property) ? "Property filters can't be empty" : undefined,
+ value: getPropertyValueError(property),
})),
rollout_percentage:
rollout_percentage === undefined || rollout_percentage === null
diff --git a/frontend/src/scenes/feature-flags/propertySelectErrorMessages.tsx b/frontend/src/scenes/feature-flags/propertySelectErrorMessages.tsx
new file mode 100644
index 000000000000..9a166f719f31
--- /dev/null
+++ b/frontend/src/scenes/feature-flags/propertySelectErrorMessages.tsx
@@ -0,0 +1,37 @@
+import { Fragment } from 'react'
+
+import { IconErrorOutline } from 'lib/lemon-ui/icons'
+
+// Matches the shape produced by featureFlagReleaseConditionsLogic's `propertySelectErrors`
+// selector, one entry per condition set.
+export type PropertySelectError = {
+ properties?: { value?: string }[]
+ rollout_percentage?: string
+ variant: null
+}
+
+// Renders a condition set's property errors (from featureFlagReleaseConditionsLogic's
+// `propertySelectErrors` selector) as the JSX.Element[] the PropertyFilters `errorMessages`
+// prop expects, one entry per property (empty entries render nothing).
+export function getPropertySelectErrorMessages(
+ propertySelectErrors: PropertySelectError[] | null | undefined,
+ index: number
+): JSX.Element[] | null {
+ const properties = propertySelectErrors?.[index]?.properties
+ if (!properties) {
+ return null
+ }
+ let hasError = false
+ const messages = properties.map((message, messageIndex) => {
+ if (typeof message?.value !== 'string') {
+ return <Fragment key={messageIndex} />
+ }
+ hasError = true
+ return (
+ <div key={messageIndex} className="text-danger flex items-center gap-1 text-sm Field--error">
+ <IconErrorOutline className="text-xl" /> {message.value}
+ </div>
+ )
+ })
+ return hasError ? messages : null
+}