: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix: stop bigint filter values from crashing the feature flags page (#66505)
Apoorva Verma4d10602e0347
diff --git a/frontend/src/lib/utils/json.test.ts b/frontend/src/lib/utils/json.test.tsnew file mode 100644index 000000000000..376c04972741--- /dev/null+++ b/frontend/src/lib/utils/json.test.ts@@ -0,0 +1,50 @@+import { stringifyWithBigInts, tryJsonParse, validateJson } from 'lib/utils/json'++describe('lib/utils/json', () => {+ describe('validateJson', () => {+ it.each([+ ['valid object', '{"a":1}', true],+ ['valid array', '[1,2,3]', true],+ ['invalid json', '{not json}', false],+ ['empty string', '', false],+ ])('returns %s as %s', (_label, input, expected) => {+ expect(validateJson(input)).toEqual(expected)+ })+ })++ describe('tryJsonParse', () => {+ it('parses valid json', () => {+ expect(tryJsonParse('{"a":1}')).toEqual({ a: 1 })+ })++ it('returns the fallback on invalid json', () => {+ expect(tryJsonParse('{not json}', 'fallback')).toEqual('fallback')+ })+ })++ describe('stringifyWithBigInts', () => {+ it.each([+ ['plain string', 'hello', '"hello"'],+ ['plain number', 42, '42'],+ ['null', null, 'null'],+ ['plain object', { a: 1, b: 'two' }, '{"a":1,"b":"two"}'],+ ['array of primitives', [1, 2, 3], '[1,2,3]'],+ ['top-level bigint', BigInt('9007199254740993'), '"9007199254740993"'],+ ['object with bigint', { id: BigInt('9007199254740993') }, '{"id":"9007199254740993"}'],+ ['array of bigints', [BigInt(1), BigInt(2)], '["1","2"]'],+ ['nested bigint', { a: { b: BigInt(7) } }, '{"a":{"b":"7"}}'],+ ['mixed types', { n: 1, b: BigInt(2), s: 'x', a: [BigInt(3)] }, '{"n":1,"b":"2","s":"x","a":["3"]}'],+ ])('serialises %s', (_label, input, expected) => {+ expect(stringifyWithBigInts(input)).toEqual(expected)+ })++ it('does not throw on a bigint where JSON.stringify would throw', () => {+ expect(() => stringifyWithBigInts(BigInt(1))).not.toThrow()+ })++ it('matches JSON.stringify for undefined', () => {+ expect(stringifyWithBigInts(undefined)).toBeUndefined()+ expect(stringifyWithBigInts({ a: undefined, b: 1 })).toEqual('{"b":1}')+ })+ })+})diff --git a/frontend/src/lib/utils/json.ts b/frontend/src/lib/utils/json.tsindex e58ed733fa62..d45e819f83db 100644--- a/frontend/src/lib/utils/json.ts+++ b/frontend/src/lib/utils/json.ts@@ -14,3 +14,13 @@ export function tryJsonParse(value: string, fallback?: any): any {return fallback}}++// Like JSON.stringify, but converts bigints to strings instead of throwing.+// Filter property values can be bigint (PropertyFilterBaseValue), which otherwise+// crashes serialization. Only bigints are handled — circular references and other+// unserializable values still throw, same as JSON.stringify. The bigint->string+// conversion is lossy, which is fine here: the result is only used for+// URL encoding and change-detection keys, not to reconstruct the original value.+export function stringifyWithBigInts(value: unknown): string {+ return JSON.stringify(value, (_key, val) => (typeof val === 'bigint' ? val.toString() : val))+}diff --git a/frontend/src/lib/utils/url.test.ts b/frontend/src/lib/utils/url.test.tsindex 1503c838c7d4..57b923c302df 100644--- a/frontend/src/lib/utils/url.test.ts+++ b/frontend/src/lib/utils/url.test.ts@@ -30,6 +30,14 @@ describe('url utils', () => {const actual = toParams({ include: ['a', 'b'] }, true)expect(actual).toEqual('include=a&include=b')})++ it('does not throw when a nested object value is a bigint', () => {+ // Property filter values may be bigints (see PropertyFilterBaseValue), which crashed+ // toParams with "Do not know how to serialize a BigInt" via JSON.stringify+ const filters = { properties: [{ key: 'user_id', value: BigInt('9007199254740993') }] }+ expect(() => toParams({ filters })).not.toThrow()+ expect(toParams({ filters })).toContain('9007199254740993')+ })})describe('isURL()', () => {diff --git a/frontend/src/lib/utils/url.ts b/frontend/src/lib/utils/url.tsindex c80eb7ef4c85..6737d9fe902f 100644--- a/frontend/src/lib/utils/url.ts+++ b/frontend/src/lib/utils/url.ts@@ -1,4 +1,5 @@import { dayjs } from 'lib/dayjs'+import { stringifyWithBigInts } from 'lib/utils/json'export function toParams(obj: Record<string, any>, explodeArrays: boolean = false): string {if (!obj) {@@ -9,7 +10,7 @@ export function toParams(obj: Record<string, any>, explodeArrays: boolean = falsif (dayjs.isDayjs(val)) {return encodeURIComponent(val.format('YYYY-MM-DD'))}- val = typeof val === 'object' ? JSON.stringify(val) : val+ val = typeof val === 'object' ? stringifyWithBigInts(val) : valreturn encodeURIComponent(val)}diff --git a/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.test.ts b/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.test.tsnew file mode 100644index 000000000000..d8089aa753d2--- /dev/null+++ b/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.test.ts@@ -0,0 +1,50 @@+import { expectLogic } from 'kea-test-utils'++import { initKeaTests } from '~/test/init'+import { FeatureFlagGroupType, PropertyFilterType, PropertyOperator } from '~/types'++import { defaultReleaseConditionsLogic } from './defaultReleaseConditionsLogic'++describe('defaultReleaseConditionsLogic', () => {+ let logic: ReturnType<typeof defaultReleaseConditionsLogic.build>++ const groupWithBigIntId = (value: bigint): FeatureFlagGroupType => ({+ properties: [{ key: 'id', value, type: PropertyFilterType.Person, operator: PropertyOperator.Exact }],+ rollout_percentage: 100,+ variant: null,+ })++ beforeEach(async () => {+ initKeaTests()+ logic = defaultReleaseConditionsLogic()+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadDefaultReleaseConditionsSuccess'])+ })++ afterEach(() => {+ logic.unmount()+ })++ describe('hasChanges', () => {+ // hasChanges used to compare groups via raw JSON.stringify, which throws on bigint+ // property values (PropertyFilterBaseValue allows bigint).+ it.each([+ { candidateValue: BigInt('9007199254740993'), expected: false },+ { candidateValue: BigInt('9007199254740994'), expected: true },+ ])(+ 'does not throw and reports hasChanges=$expected for a bigint property value',+ ({ candidateValue, expected }) => {+ logic.actions.loadDefaultReleaseConditionsSuccess({+ enabled: true,+ default_groups: [groupWithBigIntId(BigInt('9007199254740993'))],+ })++ expect(() => {+ logic.actions.setLocalGroups([groupWithBigIntId(candidateValue)])+ }).not.toThrow()++ expectLogic(logic).toMatchValues({ hasChanges: expected })+ }+ )+ })+})diff --git a/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts b/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.tsindex dbdb034c5a23..e2cb81169d2f 100644--- a/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts+++ b/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts@@ -3,6 +3,7 @@ import { loaders } from 'kea-loaders'import api from 'lib/api'import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'+import { objectsEqual } from 'lib/utils/objects'import { teamLogic } from 'scenes/teamLogic'import { FeatureFlagFilters, FeatureFlagGroupType } from '~/types'@@ -133,10 +134,7 @@ export const defaultReleaseConditionsLogic = kea<defaultReleaseConditionsLogicTyif (!saved) {return false}- return (- localEnabled !== saved.enabled ||- JSON.stringify(localGroups) !== JSON.stringify(saved.default_groups)- )+ return localEnabled !== saved.enabled || !objectsEqual(localGroups, saved.default_groups)},],}),diff --git a/frontend/src/scenes/feature-flags/featureFlagConditionWarningLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagConditionWarningLogic.test.tsindex da4a39f2ef80..6fb776943bba 100644--- a/frontend/src/scenes/feature-flags/featureFlagConditionWarningLogic.test.ts+++ b/frontend/src/scenes/feature-flags/featureFlagConditionWarningLogic.test.ts@@ -56,6 +56,24 @@ describe('featureFlagConditionWarningLogic', () => {})})+ it('mounts without throwing for a bigint property value', () => {+ const properties: AnyPropertyFilter[] = [+ {+ key: 'id',+ type: PropertyFilterType.Person,+ operator: PropertyOperator.Exact,+ value: BigInt('9007199254740993'),+ },+ ]++ const logic = featureFlagConditionWarningLogic({+ properties,+ evaluationRuntime: FeatureFlagEvaluationRuntime.SERVER,+ })++ expect(() => logic.mount()).not.toThrow()+ })+it('returns no warning for regex without unsupported features', () => {const properties: AnyPropertyFilter[] = [{diff --git a/frontend/src/scenes/feature-flags/featureFlagConditionWarningLogic.ts b/frontend/src/scenes/feature-flags/featureFlagConditionWarningLogic.tsindex 4686c3bde7b6..b7696f656d36 100644--- a/frontend/src/scenes/feature-flags/featureFlagConditionWarningLogic.ts+++ b/frontend/src/scenes/feature-flags/featureFlagConditionWarningLogic.ts@@ -1,6 +1,7 @@import { connect, kea, key, path, props, selectors } from 'kea'import { isPropertyFilterWithOperator } from 'lib/components/PropertyFilters/utils'+import { stringifyWithBigInts } from 'lib/utils/json'import { cohortsModel } from '~/models/cohortsModel'import { AnyPropertyFilter, CohortType, FeatureFlagEvaluationRuntime, PropertyFilterType } from '~/types'@@ -19,7 +20,7 @@ const REGEX_BACKREFERENCE = /(?<!\\)\\[1-9]/ // \1 through \9export const featureFlagConditionWarningLogic = kea<featureFlagConditionWarningLogicType>([path(['scenes', 'feature-flags', 'featureFlagConditionWarningLogic']),props({} as FeatureFlagConditionWarningLogicProps),- key((props) => JSON.stringify(props.properties)),+ key((props) => stringifyWithBigInts(props.properties)),connect(() => ({values: [cohortsModel, ['cohortsById']],})),diff --git a/frontend/src/scenes/feature-flags/featureFlagConfirmationLogic.ts b/frontend/src/scenes/feature-flags/featureFlagConfirmationLogic.tsindex aa7b175c47fa..8cda6e8d4f91 100644--- a/frontend/src/scenes/feature-flags/featureFlagConfirmationLogic.ts+++ b/frontend/src/scenes/feature-flags/featureFlagConfirmationLogic.ts@@ -1,5 +1,7 @@import { actions, kea, key, listeners, path, props, reducers, selectors } from 'kea'+import { objectsEqual } from 'lib/utils/objects'+import { FeatureFlagType } from '~/types'import { openConfirmationModal } from './ConfirmationModal'@@ -42,11 +44,10 @@ function detectFeatureFlagChanges(statusChanged = true}- // Check for any filter changes (comprehensive detection)- const originalFilters = JSON.stringify(originalFlag.filters || {})- const updatedFilters = JSON.stringify(updatedFlag.filters || {})-- if (originalFilters !== updatedFilters) {+ // Check for any filter changes (comprehensive detection).+ // objectsEqual (fast-deep-equal) compares bigint filter values directly instead of+ // serialising them, which JSON.stringify can't do (it throws on bigint).+ if (!objectsEqual(originalFlag.filters || {}, updatedFlag.filters || {})) {// Try to detect specific types of changes for better messagingconst originalGroups = originalFlag.filters?.groups || []const updatedGroups = updatedFlag.filters?.groups || []@@ -60,20 +61,18 @@ function detectFeatureFlagChanges(// Check for variant changesconst originalVariants = originalFlag.filters?.multivariate?.variants || []const updatedVariants = updatedFlag.filters?.multivariate?.variants || []- const variantsChanged = JSON.stringify(originalVariants) !== JSON.stringify(updatedVariants)+ const variantsChanged = !objectsEqual(originalVariants, updatedVariants)// Check for release condition changes (properties, etc.)const conditionsChanged = originalGroups.some((group, index) => {const updatedGroup = updatedGroups[index]- return (- updatedGroup && JSON.stringify(group.properties || []) !== JSON.stringify(updatedGroup.properties || [])- )+ return updatedGroup && !objectsEqual(group.properties || [], updatedGroup.properties || [])})// Check for payload changesconst originalPayloads = originalFlag.filters?.payloads || {}const updatedPayloads = updatedFlag.filters?.payloads || {}- const payloadsChanged = JSON.stringify(originalPayloads) !== JSON.stringify(updatedPayloads)+ const payloadsChanged = !objectsEqual(originalPayloads, updatedPayloads)// Add specific change messagesif (rolloutChanged) {diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.test.tsindex 1f9b4f142321..2ae750ae5106 100644--- a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts@@ -543,6 +543,29 @@ describe('featureFlagLogic', () => {expect(changes).toContain('Release condition rollout percentage changed')})+ it('does not throw and detects changes when a filter value is a bigint', () => {+ // Property values can be bigint (PropertyFilterBaseValue); raw JSON.stringify throws on them.+ const filtersWithBigIntId = (value: bigint): FeatureFlagFilters => ({+ groups: [+ {+ properties: [+ { key: 'id', value, type: PropertyFilterType.Person, operator: PropertyOperator.Exact },+ ],+ rollout_percentage: 100,+ variant: null,+ },+ ],+ })+ const originalFlag = { ...MOCK_FEATURE_FLAG, filters: filtersWithBigIntId(BigInt('9007199254740993')) }+ const changedFlag = { ...MOCK_FEATURE_FLAG, filters: filtersWithBigIntId(BigInt('9007199254740994')) }++ let changes: string[] = []+ expect(() => {+ changes = detectFeatureFlagChanges(originalFlag, changedFlag)+ }).not.toThrow()+ expect(changes).toContain('Release conditions changed')+ })+it('returns no changes for new flags', () => {const newFlag = { ...NEW_FLAG, key: 'new-flag', name: 'New Flag' }const changes = detectFeatureFlagChanges(null, newFlag)diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.tsindex 1c7e60b35764..db089391a190 100644--- a/frontend/src/scenes/feature-flags/featureFlagLogic.ts+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.ts@@ -1397,10 +1397,7 @@ export const featureFlagLogic = kea<featureFlagLogicType>([?.values.featureFlags.results.find((flag) => flag.id === props.id)// If we've got a cached flag and the filters have changed, we've updated the release conditions- if (- cachedFlag &&- JSON.stringify(cachedFlag?.filters) !== JSON.stringify(values.featureFlag.filters)- ) {+ if (cachedFlag && !objectsEqual(cachedFlag?.filters, values.featureFlag.filters)) {globalSetupLogic.findMounted()?.actions.markTaskAsCompleted(SetupTaskId.UpdateFeatureFlagReleaseConditions)