From 45d538f7c99fa65c70f2ac3e5036fabd7dbfc970 Mon Sep 17 00:00:00 2001 From: Blake Date: Thu, 23 Jul 2026 20:44:03 -0400 Subject: [PATCH 01/10] feat(flags): add rules engine boundary --- .../__tests__/__utils__/rulesTestUtils.ts | 82 +++ .../configuration/__tests__/rules.test.ts | 175 +++++ .../configuration/__tests__/wire.test.ts | 59 ++ .../core/src/flags/configuration/rules.ts | 614 ++++++++++++++++++ packages/core/src/flags/configuration/wire.ts | 94 ++- 5 files changed, 1021 insertions(+), 3 deletions(-) create mode 100644 packages/core/src/flags/configuration/__tests__/__utils__/rulesTestUtils.ts create mode 100644 packages/core/src/flags/configuration/__tests__/rules.test.ts create mode 100644 packages/core/src/flags/configuration/rules.ts diff --git a/packages/core/src/flags/configuration/__tests__/__utils__/rulesTestUtils.ts b/packages/core/src/flags/configuration/__tests__/__utils__/rulesTestUtils.ts new file mode 100644 index 000000000..4eb2aef52 --- /dev/null +++ b/packages/core/src/flags/configuration/__tests__/__utils__/rulesTestUtils.ts @@ -0,0 +1,82 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://github.com/DataDog). + * Copyright 2016-Present Datadog, Inc. + */ + +import { OperatorType } from '@datadog/flagging-core'; +import type { UniversalFlagConfigurationV1 } from '@datadog/flagging-core'; + +import type { + RulesEngine, + RulesEvaluationDetails, + RulesEvaluationRequest, + RulesValueType +} from '../../rules'; + +export const buildRulesConfiguration = (): UniversalFlagConfigurationV1 => ({ + createdAt: '2026-07-23T12:00:00.000Z', + format: 'SERVER', + environment: { name: 'test' }, + flags: { + 'dynamic-flag': { + key: 'dynamic-flag', + enabled: true, + variationType: 'BOOLEAN', + variations: { + enabled: { key: 'enabled', value: true }, + disabled: { key: 'disabled', value: false } + }, + allocations: [ + { + key: 'allocation-1', + rules: [ + { + conditions: [ + { + operator: OperatorType.ONE_OF, + attribute: 'country', + value: ['US'] + } + ] + } + ], + splits: [ + { + variationKey: 'enabled', + serialId: 7, + extraLogging: { experiment: 'checkout' }, + shards: [ + { + salt: 'test-salt', + ranges: [{ start: 0, end: 100 }], + totalShards: 100 + } + ] + } + ], + doLog: false + } + ] + } + } +}); + +type FakeRulesEvaluation = RulesEvaluationDetails; + +export interface FakeRulesEngine extends RulesEngine { + evaluate: jest.Mock< + FakeRulesEvaluation, + [RulesEvaluationRequest] + >; +} + +// TODO(FFL-2837): Remove this fake after the upstream rules wire and engine +// contract are published and the state-matrix tests can use canonical vectors. +export const createFakeRulesEngine = ( + result: FakeRulesEvaluation +): FakeRulesEngine => { + return { + evaluate: jest.fn(() => result) + } as FakeRulesEngine; +}; diff --git a/packages/core/src/flags/configuration/__tests__/rules.test.ts b/packages/core/src/flags/configuration/__tests__/rules.test.ts new file mode 100644 index 000000000..932851cdd --- /dev/null +++ b/packages/core/src/flags/configuration/__tests__/rules.test.ts @@ -0,0 +1,175 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://github.com/DataDog). + * Copyright 2016-Present Datadog, Inc. + */ + +import { + flaggingCoreRulesEngine, + getNoopRulesLogger, + prepareRulesConfiguration, + toRulesEvaluationContext +} from '../rules'; + +import { + buildRulesConfiguration, + createFakeRulesEngine +} from './__utils__/rulesTestUtils'; + +describe('rules configuration', () => { + it('converts an SDK context to a flat rules context and reserves identifiers', () => { + expect( + toRulesEvaluationContext({ + targetingKey: 'user-1', + attributes: { + country: 'US', + id: 'customer-id', + targetingKey: 'attribute-key', + enabled: true + } + }) + ).toEqual({ + targetingKey: 'user-1', + country: 'US', + enabled: true + }); + }); + + it('clones and freezes a valid rules configuration', () => { + const source = buildRulesConfiguration(); + const prepared = prepareRulesConfiguration(source); + + expect(prepared.status).toBe('ready'); + if (prepared.status !== 'ready') { + throw new Error(prepared.errorMessage); + } + + source.flags['dynamic-flag'].enabled = false; + + expect(prepared.configuration.flags['dynamic-flag'].enabled).toBe(true); + expect(Object.isFrozen(prepared.configuration)).toBe(true); + expect( + Object.isFrozen( + prepared.configuration.flags['dynamic-flag'].allocations[0] + ) + ).toBe(true); + }); + + it('rejects an unsupported operator', () => { + const source = buildRulesConfiguration(); + const condition = + source.flags['dynamic-flag'].allocations[0].rules?.[0] + .conditions[0]; + + if (!condition) { + throw new Error('The fixture has no condition.'); + } + (condition as { operator: string }).operator = 'ONE_OF_SHA256'; + + expect(prepareRulesConfiguration(source)).toEqual({ + status: 'error', + errorMessage: + 'The rules configuration uses the unsupported operator "ONE_OF_SHA256".' + }); + }); + + it('rejects an invalid regular expression', () => { + const source = buildRulesConfiguration(); + const conditions = + source.flags['dynamic-flag'].allocations[0].rules?.[0].conditions; + if (!conditions) { + throw new Error('The fixture has no conditions.'); + } + conditions[0] = { + operator: 'MATCHES', + attribute: 'country', + value: '[' + } as typeof conditions[number]; + + expect(prepareRulesConfiguration(source)).toEqual({ + status: 'error', + errorMessage: 'A regular expression condition is not valid.' + }); + }); + + it('rejects a split that points to an absent variation', () => { + const source = buildRulesConfiguration(); + source.flags['dynamic-flag'].allocations[0].splits[0].variationKey = + 'absent'; + + expect(prepareRulesConfiguration(source)).toEqual({ + status: 'error', + errorMessage: 'A split has an invalid variation key.' + }); + }); + + it('normalizes a real flagging-core evaluation', () => { + const configuration = buildRulesConfiguration(); + + const result = flaggingCoreRulesEngine.evaluate({ + configuration, + type: 'boolean', + flagKey: 'dynamic-flag', + defaultValue: false, + context: { + targetingKey: 'user-1', + country: 'US' + }, + logger: getNoopRulesLogger() + }); + + expect(result).toMatchObject({ + value: true, + variant: 'enabled', + reason: 'TARGETING_MATCH', + metadata: { + allocationKey: 'allocation-1', + variationType: 'boolean', + doLog: false, + extraLogging: { experiment: 'checkout' }, + splitSerialId: 7 + } + }); + expect(result.metadata.evaluationTimestampMs).toEqual( + expect.any(Number) + ); + }); + + it('checks own properties before it calls flagging-core', () => { + const result = flaggingCoreRulesEngine.evaluate({ + configuration: buildRulesConfiguration(), + type: 'boolean', + flagKey: 'toString', + defaultValue: false, + context: { targetingKey: 'user-1' }, + logger: getNoopRulesLogger() + }); + + expect(result).toEqual({ + value: false, + reason: 'ERROR', + errorCode: 'FLAG_NOT_FOUND', + metadata: {} + }); + }); + + it('provides a deterministic fake engine for client tests', () => { + const fake = createFakeRulesEngine({ + value: true, + variant: 'fake', + reason: 'TARGETING_MATCH', + metadata: {} + }); + + expect( + fake.evaluate({ + configuration: buildRulesConfiguration(), + type: 'boolean', + flagKey: 'dynamic-flag', + defaultValue: false, + context: { targetingKey: 'user-1' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ value: true, variant: 'fake' }); + }); +}); diff --git a/packages/core/src/flags/configuration/__tests__/wire.test.ts b/packages/core/src/flags/configuration/__tests__/wire.test.ts index 23f1d359b..e464a701b 100644 --- a/packages/core/src/flags/configuration/__tests__/wire.test.ts +++ b/packages/core/src/flags/configuration/__tests__/wire.test.ts @@ -7,6 +7,8 @@ import type { ParsedFlagsConfiguration } from '../types'; import { configurationFromString, configurationToString } from '../wire'; +import { buildRulesConfiguration } from './__utils__/rulesTestUtils'; + const buildResponse = () => ({ data: { id: '2', @@ -125,3 +127,60 @@ describe('configurationToString round-trip', () => { ); }); }); + +describe('rules configuration wire compatibility', () => { + it('parses and serializes a rules configuration', () => { + const rulesBased = { + response: buildRulesConfiguration(), + fetchedAt: 123, + etag: 'rules-etag' + }; + const wire = JSON.stringify({ + version: 1, + rulesBased: { + ...rulesBased, + response: JSON.stringify(rulesBased.response) + } + }); + + const parsed = configurationFromString(wire) as { + rulesBased?: typeof rulesBased; + }; + + expect(parsed.rulesBased).toEqual(rulesBased); + expect( + configurationFromString( + configurationToString( + (parsed as unknown) as ParsedFlagsConfiguration + ) + ) + ).toEqual(parsed); + }); + + it('keeps both branches in a mixed configuration', () => { + const mixedWire = buildWire({ + rulesBased: { + response: JSON.stringify(buildRulesConfiguration()) + } + }); + + const parsed = configurationFromString(mixedWire) as { + precomputed?: unknown; + rulesBased?: unknown; + }; + + expect(parsed.precomputed).toBeDefined(); + expect(parsed.rulesBased).toBeDefined(); + }); + + it('returns an empty configuration for malformed rules JSON', () => { + expect( + configurationFromString( + JSON.stringify({ + version: 1, + rulesBased: { response: '{' } + }) + ) + ).toEqual({}); + }); +}); diff --git a/packages/core/src/flags/configuration/rules.ts b/packages/core/src/flags/configuration/rules.ts new file mode 100644 index 000000000..592f4216d --- /dev/null +++ b/packages/core/src/flags/configuration/rules.ts @@ -0,0 +1,614 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2016-Present Datadog, Inc. + */ + +import { + evaluateRulesBasedConfiguration, + OperatorType +} from '@datadog/flagging-core'; +import type { UniversalFlagConfigurationV1 } from '@datadog/flagging-core'; + +import type { EvaluationContext, JsonValue, PrimitiveValue } from '../types'; + +export type RulesValueType = 'boolean' | 'string' | 'number' | 'object'; + +type RulesValueByType = { + boolean: boolean; + string: string; + number: number; + object: JsonValue; +}; + +export interface RulesLogger { + debug: (message: string, ...args: unknown[]) => void; + info: (message: string, ...args: unknown[]) => void; + warn: (message: string, ...args: unknown[]) => void; + error: (message: string, ...args: unknown[]) => void; +} + +export interface RulesEvaluationContext { + targetingKey?: string; + [key: string]: PrimitiveValue | undefined; +} + +export interface RulesEvaluationMetadata { + allocationKey?: string; + variationType?: RulesValueType; + doLog?: boolean; + extraLogging?: Record; + splitSerialId?: number; + evaluationTimestampMs?: number; +} + +export interface RulesEvaluationDetails { + value: T; + reason?: string; + variant?: string; + errorCode?: string; + errorMessage?: string; + metadata: RulesEvaluationMetadata; +} + +export interface RulesEvaluationRequest { + configuration: UniversalFlagConfigurationV1; + type: T; + flagKey: string; + defaultValue: RulesValueByType[T]; + context: RulesEvaluationContext; + logger: RulesLogger; +} + +export interface RulesEngine { + evaluate( + request: RulesEvaluationRequest + ): RulesEvaluationDetails; +} + +type RawEvaluationDetails = { + value: T; + reason?: string; + variant?: string; + errorCode?: string; + errorMessage?: string; + flagMetadata?: Record; +}; + +type EvaluateRules = ( + configuration: UniversalFlagConfigurationV1, + type: T, + flagKey: string, + defaultValue: RulesValueByType[T], + context: RulesEvaluationContext, + logger: RulesLogger +) => RawEvaluationDetails; + +const evaluateRules = evaluateRulesBasedConfiguration as EvaluateRules; + +const NOOP_LOGGER: RulesLogger = { + debug: () => {}, + info: () => {}, + warn: () => {}, + error: () => {} +}; + +export const getNoopRulesLogger = (): RulesLogger => NOOP_LOGGER; + +/** + * Convert the SDK context to the flat context that flagging-core uses. + * + * `id` and `targetingKey` are reserved. The adapter always derives them from + * `EvaluationContext.targetingKey`. + */ +export const toRulesEvaluationContext = ( + context: EvaluationContext +): RulesEvaluationContext => { + const attributes = new Map(); + + for (const [key, value] of Object.entries(context.attributes ?? {})) { + if (key === 'id' || key === 'targetingKey' || value === undefined) { + continue; + } + attributes.set(key, value); + } + + return { + ...Object.fromEntries(attributes), + targetingKey: context.targetingKey + }; +}; + +const hasOwn = (value: object, key: PropertyKey): boolean => + Object.prototype.hasOwnProperty.call(value, key); + +const isRecord = (value: unknown): value is Record => + typeof value === 'object' && value !== null && !Array.isArray(value); + +const isStringRecord = (value: unknown): value is Record => { + if (!isRecord(value)) { + return false; + } + + return Object.values(value).every(item => typeof item === 'string'); +}; + +const isJsonValue = (value: unknown): value is JsonValue => { + if ( + value === null || + typeof value === 'string' || + typeof value === 'boolean' + ) { + return true; + } + if (typeof value === 'number') { + return Number.isFinite(value); + } + if (Array.isArray(value)) { + return value.every(isJsonValue); + } + if (isRecord(value)) { + return Object.values(value).every(isJsonValue); + } + return false; +}; + +const variationValueIsValid = ( + variationType: string, + value: unknown +): boolean => { + switch (variationType) { + case 'BOOLEAN': + return typeof value === 'boolean'; + case 'STRING': + return typeof value === 'string'; + case 'INTEGER': + case 'NUMERIC': + return typeof value === 'number' && Number.isFinite(value); + case 'JSON': + return isJsonValue(value); + default: + return false; + } +}; + +const SUPPORTED_OPERATORS: ReadonlySet = new Set( + Object.values(OperatorType) +); + +const validateCondition = (value: unknown): string | undefined => { + if ( + !isRecord(value) || + typeof value.attribute !== 'string' || + typeof value.operator !== 'string' + ) { + return 'A rule condition has an invalid shape.'; + } + + if (!SUPPORTED_OPERATORS.has(value.operator)) { + return `The rules configuration uses the unsupported operator "${value.operator}".`; + } + + switch (value.operator) { + case OperatorType.MATCHES: + case OperatorType.NOT_MATCHES: + if (typeof value.value !== 'string') { + return 'A regular expression condition must contain a string.'; + } + try { + // TODO(FFL-2837): Replace this compile-only check with the upstream + // safe-regex policy before dynamic offline rules leave draft state. + RegExp(value.value); // dd-iac-scan ignore-line + } catch { + return 'A regular expression condition is not valid.'; + } + return undefined; + case OperatorType.ONE_OF: + case OperatorType.NOT_ONE_OF: + return Array.isArray(value.value) && + value.value.every(item => typeof item === 'string') + ? undefined + : 'A membership condition must contain a string array.'; + case OperatorType.GTE: + case OperatorType.GT: + case OperatorType.LTE: + case OperatorType.LT: + return typeof value.value === 'number' && + Number.isFinite(value.value) + ? undefined + : 'A numeric condition must contain a finite number.'; + case OperatorType.IS_NULL: + return typeof value.value === 'boolean' + ? undefined + : 'A null condition must contain a boolean.'; + default: + return 'The rules configuration uses an unsupported operator.'; + } +}; + +const validateRules = (value: unknown): string | undefined => { + if (value === undefined) { + return undefined; + } + if (!Array.isArray(value)) { + return 'An allocation rules field must be an array.'; + } + + for (const rule of value) { + if (!isRecord(rule) || !Array.isArray(rule.conditions)) { + return 'A rule has an invalid shape.'; + } + for (const condition of rule.conditions) { + const error = validateCondition(condition); + if (error) { + return error; + } + } + } + + return undefined; +}; + +const validateShards = (value: unknown): string | undefined => { + if (!Array.isArray(value)) { + return 'A split shards field must be an array.'; + } + + for (const shard of value) { + if ( + !isRecord(shard) || + typeof shard.salt !== 'string' || + !Number.isInteger(shard.totalShards) || + (shard.totalShards as number) <= 0 || + !Array.isArray(shard.ranges) + ) { + return 'A shard has an invalid shape.'; + } + + for (const range of shard.ranges) { + if ( + !isRecord(range) || + !Number.isInteger(range.start) || + !Number.isInteger(range.end) || + (range.start as number) < 0 || + (range.end as number) <= (range.start as number) || + (range.end as number) > (shard.totalShards as number) + ) { + return 'A shard range is not valid.'; + } + } + } + + return undefined; +}; + +const isValidDate = (value: unknown): boolean => + value instanceof Date + ? !Number.isNaN(value.getTime()) + : typeof value === 'string' && !Number.isNaN(Date.parse(value)); + +const validateAllocation = ( + value: unknown, + variations: Record +): string | undefined => { + if ( + !isRecord(value) || + typeof value.key !== 'string' || + !Array.isArray(value.splits) + ) { + return 'An allocation has an invalid shape.'; + } + + if (value.startAt !== undefined && !isValidDate(value.startAt)) { + return 'An allocation start time is not valid.'; + } + if (value.endAt !== undefined && !isValidDate(value.endAt)) { + return 'An allocation end time is not valid.'; + } + + const rulesError = validateRules(value.rules); + if (rulesError) { + return rulesError; + } + + for (const split of value.splits) { + if ( + !isRecord(split) || + typeof split.variationKey !== 'string' || + !hasOwn(variations, split.variationKey) + ) { + return 'A split has an invalid variation key.'; + } + if (split.serialId !== undefined && !Number.isInteger(split.serialId)) { + return 'A split serial ID is not valid.'; + } + if ( + split.extraLogging !== undefined && + !isStringRecord(split.extraLogging) + ) { + return 'A split extraLogging field is not valid.'; + } + + const shardsError = validateShards(split.shards); + if (shardsError) { + return shardsError; + } + } + + return undefined; +}; + +const validateFlag = (value: unknown): string | undefined => { + if ( + !isRecord(value) || + typeof value.key !== 'string' || + typeof value.enabled !== 'boolean' || + typeof value.variationType !== 'string' || + !isRecord(value.variations) || + !Array.isArray(value.allocations) + ) { + return 'A flag has an invalid shape.'; + } + + if ( + !['BOOLEAN', 'INTEGER', 'NUMERIC', 'STRING', 'JSON'].includes( + value.variationType + ) + ) { + return `A flag uses the unsupported variation type "${value.variationType}".`; + } + + for (const variation of Object.values(value.variations)) { + if ( + !isRecord(variation) || + typeof variation.key !== 'string' || + !variationValueIsValid(value.variationType, variation.value) + ) { + return 'A variation has an invalid shape or value.'; + } + } + + for (const allocation of value.allocations) { + const error = validateAllocation(allocation, value.variations); + if (error) { + return error; + } + } + + return undefined; +}; + +const validateRulesConfiguration = (value: unknown): string | undefined => { + if ( + !isRecord(value) || + typeof value.createdAt !== 'string' || + typeof value.format !== 'string' || + !isRecord(value.environment) || + typeof value.environment.name !== 'string' || + !isRecord(value.flags) + ) { + return 'The rules configuration has an invalid envelope.'; + } + + for (const flag of Object.values(value.flags)) { + const error = validateFlag(flag); + if (error) { + return error; + } + } + + return undefined; +}; + +const cloneValue = (value: unknown): unknown => { + if (value instanceof Date) { + return new Date(value.getTime()); + } + if (Array.isArray(value)) { + return value.map(cloneValue); + } + if (isRecord(value)) { + return Object.fromEntries( + Object.entries(value).map(([key, item]) => [key, cloneValue(item)]) + ); + } + return value; +}; + +const freezeValue = (value: unknown): void => { + if (typeof value !== 'object' || value === null || Object.isFrozen(value)) { + return; + } + + Object.freeze(value); + for (const item of Object.values(value)) { + freezeValue(item); + } +}; + +export type PreparedRulesConfiguration = + | { + status: 'ready'; + configuration: UniversalFlagConfigurationV1; + } + | { + status: 'error'; + errorMessage: string; + }; + +/** + * Clone and validate untrusted rules before `FlagsClient` stores them. + */ +export const prepareRulesConfiguration = ( + value: unknown +): PreparedRulesConfiguration => { + const clone = cloneValue(value); + + // TODO(FFL-2837): Replace the temporary SDK validator with the published + // flagging-core validation API. Keep the branch-level result contract. + const errorMessage = validateRulesConfiguration(clone); + if (errorMessage) { + return { status: 'error', errorMessage }; + } + + freezeValue(clone); + return { + status: 'ready', + configuration: clone as UniversalFlagConfigurationV1 + }; +}; + +const normalizeVariationType = ( + variationType: unknown +): RulesValueType | undefined => { + switch (variationType) { + case 'boolean': + case 'string': + case 'number': + case 'object': + return variationType; + case 'BOOLEAN': + return 'boolean'; + case 'STRING': + return 'string'; + case 'INTEGER': + case 'NUMERIC': + return 'number'; + case 'JSON': + return 'object'; + default: + return undefined; + } +}; + +const recoverSplitMetadata = ( + configuration: UniversalFlagConfigurationV1, + flagKey: string, + variant: string | undefined, + allocationKey: string | undefined, + splitSerialId: number | undefined +): Pick => { + const flags = configuration.flags as Record; + if (!hasOwn(flags, flagKey)) { + return {}; + } + + const flag = flags[flagKey]; + if (!isRecord(flag)) { + return {}; + } + + const variationType = normalizeVariationType(flag.variationType); + if (!Array.isArray(flag.allocations)) { + return { variationType }; + } + + const variations = isRecord(flag.variations) ? flag.variations : {}; + const variationEntry = Object.entries(variations).find(([, value]) => { + return isRecord(value) && value.key === variant; + }); + const variationKey = variationEntry?.[0]; + + for (const allocation of flag.allocations) { + if ( + !isRecord(allocation) || + allocation.key !== allocationKey || + !Array.isArray(allocation.splits) + ) { + continue; + } + const split = allocation.splits.find(candidate => { + if (!isRecord(candidate)) { + return false; + } + if ( + splitSerialId !== undefined && + candidate.serialId === splitSerialId + ) { + return true; + } + return ( + splitSerialId === undefined && + variationKey !== undefined && + candidate.variationKey === variationKey + ); + }); + if (isRecord(split) && isStringRecord(split.extraLogging)) { + return { extraLogging: split.extraLogging, variationType }; + } + } + + return { variationType }; +}; + +export const flaggingCoreRulesEngine: RulesEngine = { + evaluate( + request: RulesEvaluationRequest + ): RulesEvaluationDetails { + const flags = request.configuration.flags as Record; + if (!hasOwn(flags, request.flagKey)) { + return { + value: request.defaultValue, + reason: 'ERROR', + errorCode: 'FLAG_NOT_FOUND', + metadata: {} + }; + } + + const result = evaluateRules( + request.configuration, + request.type, + request.flagKey, + request.defaultValue, + request.context, + request.logger + ); + const rawMetadata = result.flagMetadata ?? {}; + const allocationKey = + typeof rawMetadata.allocationKey === 'string' + ? rawMetadata.allocationKey + : typeof rawMetadata.__dd_allocation_key === 'string' + ? rawMetadata.__dd_allocation_key + : undefined; + const splitSerialId = + typeof rawMetadata.__dd_split_serial_id === 'number' + ? rawMetadata.__dd_split_serial_id + : undefined; + + // TODO(FFL-2837): Remove this metadata lookup when flagging-core + // returns extraLogging and the original UFC variation type. + const recoveredMetadata = recoverSplitMetadata( + request.configuration, + request.flagKey, + result.variant, + allocationKey, + splitSerialId + ); + + return { + value: result.value, + reason: result.reason, + variant: result.variant, + errorCode: result.errorCode, + errorMessage: result.errorMessage, + metadata: { + allocationKey, + variationType: + normalizeVariationType(rawMetadata.variationType) ?? + recoveredMetadata.variationType, + doLog: + typeof rawMetadata.doLog === 'boolean' + ? rawMetadata.doLog + : typeof rawMetadata.__dd_do_log === 'boolean' + ? rawMetadata.__dd_do_log + : undefined, + extraLogging: recoveredMetadata.extraLogging, + splitSerialId, + evaluationTimestampMs: + typeof rawMetadata.__dd_eval_timestamp_ms === 'number' + ? rawMetadata.__dd_eval_timestamp_ms + : undefined + } + }; + } +}; diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index 952636fd0..f47a6fb84 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -9,7 +9,95 @@ // it returns an empty configuration (`{}`) for malformed input or an unsupported wire // version rather than throwing. `configurationToString` is the inverse (its fix from // https://github.com/DataDog/openfeature-js-client/pull/331 shipped in flagging-core 2.0.0). -export { - configurationFromString, - configurationToString +import { + configurationFromString as coreConfigurationFromString, + configurationToString as coreConfigurationToString } from '@datadog/flagging-core'; +import type { + FlagsConfiguration, + UniversalFlagConfigurationV1 +} from '@datadog/flagging-core'; + +type PendingRulesConfiguration = FlagsConfiguration & { + rulesBased?: { + response: UniversalFlagConfigurationV1; + fetchedAt?: number; + etag?: string; + }; +}; + +type PendingRulesWire = { + version: 1; + rulesBased?: { + response: string; + fetchedAt?: number; + etag?: string; + }; +}; + +const readPendingRulesWire = ( + source: string +): PendingRulesWire['rulesBased'] | undefined => { + try { + const wire = JSON.parse(source) as PendingRulesWire; + if ( + wire.version !== 1 || + !wire.rulesBased || + typeof wire.rulesBased.response !== 'string' + ) { + return undefined; + } + + return wire.rulesBased; + } catch { + return undefined; + } +}; + +/** + * Use flagging-core to parse a configuration wire. + */ +export const configurationFromString = (source: string): FlagsConfiguration => { + const configuration = coreConfigurationFromString( + source + ) as PendingRulesConfiguration; + + // TODO(FFL-2837): Delete this JSON compatibility shim after + // DataDog/openfeature-js-client#336 is published by flagging-core. + const pendingRules = readPendingRulesWire(source); + if (pendingRules) { + try { + configuration.rulesBased = { + ...pendingRules, + response: JSON.parse(pendingRules.response) + }; + } catch { + return {}; + } + } + + return configuration; +}; + +/** + * Use flagging-core to serialize a parsed configuration. + */ +export const configurationToString = ( + configuration: FlagsConfiguration +): string => { + const serialized = coreConfigurationToString(configuration); + const pendingConfiguration = configuration as PendingRulesConfiguration; + if (!pendingConfiguration.rulesBased) { + return serialized; + } + + // TODO(FFL-2837): Delete this JSON compatibility shim after + // DataDog/openfeature-js-client#336 is published by flagging-core. + const wire = JSON.parse(serialized) as PendingRulesWire; + wire.rulesBased = { + fetchedAt: pendingConfiguration.rulesBased.fetchedAt, + etag: pendingConfiguration.rulesBased.etag, + response: JSON.stringify(pendingConfiguration.rulesBased.response) + }; + return JSON.stringify(wire); +}; From d9d1bc0f9e33f12c5f950c8b164ec603cb60046d Mon Sep 17 00:00:00 2001 From: Blake Date: Mon, 27 Jul 2026 16:10:42 -0400 Subject: [PATCH 02/10] fix(flags): align rules boundary with upstream --- .../__tests__/__utils__/rulesTestUtils.ts | 5 +- .../configuration/__tests__/rules.test.ts | 122 +++++++++++------ .../configuration/__tests__/wire.test.ts | 46 ++++--- .../core/src/flags/configuration/rules.ts | 128 ++++-------------- packages/core/src/flags/configuration/wire.ts | 23 ++-- 5 files changed, 148 insertions(+), 176 deletions(-) diff --git a/packages/core/src/flags/configuration/__tests__/__utils__/rulesTestUtils.ts b/packages/core/src/flags/configuration/__tests__/__utils__/rulesTestUtils.ts index 4eb2aef52..81190c964 100644 --- a/packages/core/src/flags/configuration/__tests__/__utils__/rulesTestUtils.ts +++ b/packages/core/src/flags/configuration/__tests__/__utils__/rulesTestUtils.ts @@ -45,7 +45,6 @@ export const buildRulesConfiguration = (): UniversalFlagConfigurationV1 => ({ { variationKey: 'enabled', serialId: 7, - extraLogging: { experiment: 'checkout' }, shards: [ { salt: 'test-salt', @@ -71,8 +70,8 @@ export interface FakeRulesEngine extends RulesEngine { >; } -// TODO(FFL-2837): Remove this fake after the upstream rules wire and engine -// contract are published and the state-matrix tests can use canonical vectors. +// Client tests use this fake to control evaluation independently of the +// flagging-core implementation and its canonical integration vectors. export const createFakeRulesEngine = ( result: FakeRulesEvaluation ): FakeRulesEngine => { diff --git a/packages/core/src/flags/configuration/__tests__/rules.test.ts b/packages/core/src/flags/configuration/__tests__/rules.test.ts index 932851cdd..aeb3abdbf 100644 --- a/packages/core/src/flags/configuration/__tests__/rules.test.ts +++ b/packages/core/src/flags/configuration/__tests__/rules.test.ts @@ -35,6 +35,17 @@ describe('rules configuration', () => { }); }); + it('preserves the difference between a missing and empty targeting key', () => { + expect(toRulesEvaluationContext({})).toHaveProperty( + 'targetingKey', + undefined + ); + expect(toRulesEvaluationContext({ targetingKey: '' })).toHaveProperty( + 'targetingKey', + '' + ); + }); + it('clones and freezes a valid rules configuration', () => { const source = buildRulesConfiguration(); const prepared = prepareRulesConfiguration(source); @@ -55,7 +66,7 @@ describe('rules configuration', () => { ).toBe(true); }); - it('rejects an unsupported operator', () => { + it('omits a flag that uses an unsupported operator', () => { const source = buildRulesConfiguration(); const condition = source.flags['dynamic-flag'].allocations[0].rules?.[0] @@ -64,16 +75,18 @@ describe('rules configuration', () => { if (!condition) { throw new Error('The fixture has no condition.'); } - (condition as { operator: string }).operator = 'ONE_OF_SHA256'; + (condition as { operator: string }).operator = 'FUTURE_OPERATOR'; - expect(prepareRulesConfiguration(source)).toEqual({ - status: 'error', - errorMessage: - 'The rules configuration uses the unsupported operator "ONE_OF_SHA256".' - }); + const prepared = prepareRulesConfiguration(source); + + expect(prepared.status).toBe('ready'); + if (prepared.status !== 'ready') { + throw new Error(prepared.errorMessage); + } + expect(prepared.configuration.flags).toEqual({}); }); - it('rejects an invalid regular expression', () => { + it('omits a flag that contains an invalid regular expression', () => { const source = buildRulesConfiguration(); const conditions = source.flags['dynamic-flag'].allocations[0].rules?.[0].conditions; @@ -86,21 +99,52 @@ describe('rules configuration', () => { value: '[' } as typeof conditions[number]; - expect(prepareRulesConfiguration(source)).toEqual({ - status: 'error', - errorMessage: 'A regular expression condition is not valid.' - }); + const prepared = prepareRulesConfiguration(source); + + expect(prepared.status).toBe('ready'); + if (prepared.status !== 'ready') { + throw new Error(prepared.errorMessage); + } + expect(prepared.configuration.flags).toEqual({}); }); - it('rejects a split that points to an absent variation', () => { + it('omits a flag whose split points to an absent variation', () => { const source = buildRulesConfiguration(); source.flags['dynamic-flag'].allocations[0].splits[0].variationKey = 'absent'; - expect(prepareRulesConfiguration(source)).toEqual({ - status: 'error', - errorMessage: 'A split has an invalid variation key.' - }); + const prepared = prepareRulesConfiguration(source); + + expect(prepared.status).toBe('ready'); + if (prepared.status !== 'ready') { + throw new Error(prepared.errorMessage); + } + expect(prepared.configuration.flags).toEqual({}); + }); + + it('keeps valid flags when it omits an invalid flag', () => { + const source = buildRulesConfiguration(); + const validFlag = buildRulesConfiguration().flags['dynamic-flag']; + validFlag.key = 'valid-flag'; + source.flags['valid-flag'] = validFlag; + + const condition = + source.flags['dynamic-flag'].allocations[0].rules?.[0] + .conditions[0]; + if (!condition) { + throw new Error('The fixture has no condition.'); + } + (condition as { operator: string }).operator = 'FUTURE_OPERATOR'; + + const prepared = prepareRulesConfiguration(source); + + expect(prepared.status).toBe('ready'); + if (prepared.status !== 'ready') { + throw new Error(prepared.errorMessage); + } + expect(Object.keys(prepared.configuration.flags)).toEqual([ + 'valid-flag' + ]); }); it('normalizes a real flagging-core evaluation', () => { @@ -125,33 +169,31 @@ describe('rules configuration', () => { metadata: { allocationKey: 'allocation-1', variationType: 'boolean', - doLog: false, - extraLogging: { experiment: 'checkout' }, - splitSerialId: 7 + doLog: false } }); - expect(result.metadata.evaluationTimestampMs).toEqual( - expect.any(Number) - ); }); - it('checks own properties before it calls flagging-core', () => { - const result = flaggingCoreRulesEngine.evaluate({ - configuration: buildRulesConfiguration(), - type: 'boolean', - flagKey: 'toString', - defaultValue: false, - context: { targetingKey: 'user-1' }, - logger: getNoopRulesLogger() - }); - - expect(result).toEqual({ - value: false, - reason: 'ERROR', - errorCode: 'FLAG_NOT_FOUND', - metadata: {} - }); - }); + it.each(['toString', 'constructor', '__proto__'])( + 'checks own properties before it evaluates %s', + flagKey => { + const result = flaggingCoreRulesEngine.evaluate({ + configuration: buildRulesConfiguration(), + type: 'boolean', + flagKey, + defaultValue: false, + context: { targetingKey: 'user-1' }, + logger: getNoopRulesLogger() + }); + + expect(result).toEqual({ + value: false, + reason: 'ERROR', + errorCode: 'FLAG_NOT_FOUND', + metadata: {} + }); + } + ); it('provides a deterministic fake engine for client tests', () => { const fake = createFakeRulesEngine({ diff --git a/packages/core/src/flags/configuration/__tests__/wire.test.ts b/packages/core/src/flags/configuration/__tests__/wire.test.ts index e464a701b..9be51f2b6 100644 --- a/packages/core/src/flags/configuration/__tests__/wire.test.ts +++ b/packages/core/src/flags/configuration/__tests__/wire.test.ts @@ -128,8 +128,8 @@ describe('configurationToString round-trip', () => { }); }); -describe('rules configuration wire compatibility', () => { - it('parses and serializes a rules configuration', () => { +describe('temporary rules configuration wire compatibility', () => { + it('parses a legacy rules configuration', () => { const rulesBased = { response: buildRulesConfiguration(), fetchedAt: 123, @@ -148,13 +148,22 @@ describe('rules configuration wire compatibility', () => { }; expect(parsed.rulesBased).toEqual(rulesBased); - expect( - configurationFromString( - configurationToString( - (parsed as unknown) as ParsedFlagsConfiguration - ) + }); + + it('does not serialize a rules configuration', () => { + const configuration = { + rulesBased: { + response: buildRulesConfiguration() + } + }; + + expect(() => + configurationToString( + (configuration as unknown) as ParsedFlagsConfiguration ) - ).toEqual(parsed); + ).toThrow( + 'Rules configurations cannot be serialized to the wire format' + ); }); it('keeps both branches in a mixed configuration', () => { @@ -173,14 +182,17 @@ describe('rules configuration wire compatibility', () => { expect(parsed.rulesBased).toBeDefined(); }); - it('returns an empty configuration for malformed rules JSON', () => { - expect( - configurationFromString( - JSON.stringify({ - version: 1, - rulesBased: { response: '{' } - }) - ) - ).toEqual({}); + it('keeps a valid precomputed branch when rules JSON is malformed', () => { + const parsed = configurationFromString( + buildWire({ + rulesBased: { response: '{' } + }) + ) as { + precomputed?: unknown; + rulesBased?: unknown; + }; + + expect(parsed.precomputed).toBeDefined(); + expect(parsed.rulesBased).toBeUndefined(); }); }); diff --git a/packages/core/src/flags/configuration/rules.ts b/packages/core/src/flags/configuration/rules.ts index 592f4216d..0ee103dd5 100644 --- a/packages/core/src/flags/configuration/rules.ts +++ b/packages/core/src/flags/configuration/rules.ts @@ -37,9 +37,6 @@ export interface RulesEvaluationMetadata { allocationKey?: string; variationType?: RulesValueType; doLog?: boolean; - extraLogging?: Record; - splitSerialId?: number; - evaluationTimestampMs?: number; } export interface RulesEvaluationDetails { @@ -125,14 +122,6 @@ const hasOwn = (value: object, key: PropertyKey): boolean => const isRecord = (value: unknown): value is Record => typeof value === 'object' && value !== null && !Array.isArray(value); -const isStringRecord = (value: unknown): value is Record => { - if (!isRecord(value)) { - return false; - } - - return Object.values(value).every(item => typeof item === 'string'); -}; - const isJsonValue = (value: unknown): value is JsonValue => { if ( value === null || @@ -196,8 +185,9 @@ const validateCondition = (value: unknown): string | undefined => { return 'A regular expression condition must contain a string.'; } try { - // TODO(FFL-2837): Replace this compile-only check with the upstream - // safe-regex policy before dynamic offline rules leave draft state. + // TODO(FFL-2837): Define a bounded regular expression policy before + // dynamic offline rules leave draft state. Upstream PR #344 validates + // regular expression syntax, but it does not limit expensive patterns. RegExp(value.value); // dd-iac-scan ignore-line } catch { return 'A regular expression condition is not valid.'; @@ -322,12 +312,6 @@ const validateAllocation = ( if (split.serialId !== undefined && !Number.isInteger(split.serialId)) { return 'A split serial ID is not valid.'; } - if ( - split.extraLogging !== undefined && - !isStringRecord(split.extraLogging) - ) { - return 'A split extraLogging field is not valid.'; - } const shardsError = validateShards(split.shards); if (shardsError) { @@ -378,7 +362,9 @@ const validateFlag = (value: unknown): string | undefined => { return undefined; }; -const validateRulesConfiguration = (value: unknown): string | undefined => { +const validateRulesConfigurationEnvelope = ( + value: unknown +): string | undefined => { if ( !isRecord(value) || typeof value.createdAt !== 'string' || @@ -390,13 +376,6 @@ const validateRulesConfiguration = (value: unknown): string | undefined => { return 'The rules configuration has an invalid envelope.'; } - for (const flag of Object.values(value.flags)) { - const error = validateFlag(flag); - if (error) { - return error; - } - } - return undefined; }; @@ -444,13 +423,21 @@ export const prepareRulesConfiguration = ( ): PreparedRulesConfiguration => { const clone = cloneValue(value); - // TODO(FFL-2837): Replace the temporary SDK validator with the published - // flagging-core validation API. Keep the branch-level result contract. - const errorMessage = validateRulesConfiguration(clone); + // TODO(FFL-2837): Delete this legacy JSON clone and validator after a + // flagging-core release contains upstream PR #344. That implementation + // decodes the protobuf response and omits unsupported or invalid flags. + const errorMessage = validateRulesConfigurationEnvelope(clone); if (errorMessage) { return { status: 'error', errorMessage }; } + const flags = (clone as UniversalFlagConfigurationV1).flags; + for (const [flagKey, flag] of Object.entries(flags)) { + if (validateFlag(flag)) { + delete flags[flagKey]; + } + } + freezeValue(clone); return { status: 'ready', @@ -481,64 +468,21 @@ const normalizeVariationType = ( } }; -const recoverSplitMetadata = ( +const recoverVariationType = ( configuration: UniversalFlagConfigurationV1, - flagKey: string, - variant: string | undefined, - allocationKey: string | undefined, - splitSerialId: number | undefined -): Pick => { + flagKey: string +): RulesValueType | undefined => { const flags = configuration.flags as Record; if (!hasOwn(flags, flagKey)) { - return {}; + return undefined; } const flag = flags[flagKey]; if (!isRecord(flag)) { - return {}; - } - - const variationType = normalizeVariationType(flag.variationType); - if (!Array.isArray(flag.allocations)) { - return { variationType }; - } - - const variations = isRecord(flag.variations) ? flag.variations : {}; - const variationEntry = Object.entries(variations).find(([, value]) => { - return isRecord(value) && value.key === variant; - }); - const variationKey = variationEntry?.[0]; - - for (const allocation of flag.allocations) { - if ( - !isRecord(allocation) || - allocation.key !== allocationKey || - !Array.isArray(allocation.splits) - ) { - continue; - } - const split = allocation.splits.find(candidate => { - if (!isRecord(candidate)) { - return false; - } - if ( - splitSerialId !== undefined && - candidate.serialId === splitSerialId - ) { - return true; - } - return ( - splitSerialId === undefined && - variationKey !== undefined && - candidate.variationKey === variationKey - ); - }); - if (isRecord(split) && isStringRecord(split.extraLogging)) { - return { extraLogging: split.extraLogging, variationType }; - } + return undefined; } - return { variationType }; + return normalizeVariationType(flag.variationType); }; export const flaggingCoreRulesEngine: RulesEngine = { @@ -570,21 +514,6 @@ export const flaggingCoreRulesEngine: RulesEngine = { : typeof rawMetadata.__dd_allocation_key === 'string' ? rawMetadata.__dd_allocation_key : undefined; - const splitSerialId = - typeof rawMetadata.__dd_split_serial_id === 'number' - ? rawMetadata.__dd_split_serial_id - : undefined; - - // TODO(FFL-2837): Remove this metadata lookup when flagging-core - // returns extraLogging and the original UFC variation type. - const recoveredMetadata = recoverSplitMetadata( - request.configuration, - request.flagKey, - result.variant, - allocationKey, - splitSerialId - ); - return { value: result.value, reason: result.reason, @@ -595,18 +524,15 @@ export const flaggingCoreRulesEngine: RulesEngine = { allocationKey, variationType: normalizeVariationType(rawMetadata.variationType) ?? - recoveredMetadata.variationType, + recoverVariationType( + request.configuration, + request.flagKey + ), doLog: typeof rawMetadata.doLog === 'boolean' ? rawMetadata.doLog : typeof rawMetadata.__dd_do_log === 'boolean' ? rawMetadata.__dd_do_log - : undefined, - extraLogging: recoveredMetadata.extraLogging, - splitSerialId, - evaluationTimestampMs: - typeof rawMetadata.__dd_eval_timestamp_ms === 'number' - ? rawMetadata.__dd_eval_timestamp_ms : undefined } }; diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index f47a6fb84..681f68b94 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -62,8 +62,8 @@ export const configurationFromString = (source: string): FlagsConfiguration => { source ) as PendingRulesConfiguration; - // TODO(FFL-2837): Delete this JSON compatibility shim after - // DataDog/openfeature-js-client#336 is published by flagging-core. + // TODO(FFL-2837): Delete this legacy JSON compatibility shim after a + // flagging-core release contains DataDog/openfeature-js-client#344. const pendingRules = readPendingRulesWire(source); if (pendingRules) { try { @@ -72,7 +72,7 @@ export const configurationFromString = (source: string): FlagsConfiguration => { response: JSON.parse(pendingRules.response) }; } catch { - return {}; + return configuration; } } @@ -85,19 +85,12 @@ export const configurationFromString = (source: string): FlagsConfiguration => { export const configurationToString = ( configuration: FlagsConfiguration ): string => { - const serialized = coreConfigurationToString(configuration); const pendingConfiguration = configuration as PendingRulesConfiguration; - if (!pendingConfiguration.rulesBased) { - return serialized; + if (pendingConfiguration.rulesBased) { + throw new Error( + 'Rules configurations cannot be serialized to the wire format' + ); } - // TODO(FFL-2837): Delete this JSON compatibility shim after - // DataDog/openfeature-js-client#336 is published by flagging-core. - const wire = JSON.parse(serialized) as PendingRulesWire; - wire.rulesBased = { - fetchedAt: pendingConfiguration.rulesBased.fetchedAt, - etag: pendingConfiguration.rulesBased.etag, - response: JSON.stringify(pendingConfiguration.rulesBased.response) - }; - return JSON.stringify(wire); + return coreConfigurationToString(configuration); }; From 9ae131221ddff776d395762ad586a59564d2f455 Mon Sep 17 00:00:00 2001 From: Blake Date: Tue, 28 Jul 2026 14:55:17 -0400 Subject: [PATCH 03/10] fix(flags): refresh upstream compatibility TODOs --- .../configuration/__tests__/rules.test.ts | 33 +++++++++++++++++++ .../core/src/flags/configuration/rules.ts | 30 ++++++++++++----- packages/core/src/flags/configuration/wire.ts | 11 +++++-- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/packages/core/src/flags/configuration/__tests__/rules.test.ts b/packages/core/src/flags/configuration/__tests__/rules.test.ts index aeb3abdbf..58488478f 100644 --- a/packages/core/src/flags/configuration/__tests__/rules.test.ts +++ b/packages/core/src/flags/configuration/__tests__/rules.test.ts @@ -174,6 +174,39 @@ describe('rules configuration', () => { }); }); + it.each([ + ['INTEGER', 42], + ['NUMERIC', 1.5] + ] as const)( + 'normalizes %s variation metadata to number', + (variationType, variationValue) => { + const configuration = buildRulesConfiguration(); + const flag = configuration.flags['dynamic-flag']; + flag.variationType = variationType; + flag.variations.enabled.value = variationValue; + flag.variations.disabled.value = 0; + + const result = flaggingCoreRulesEngine.evaluate({ + configuration, + type: 'number', + flagKey: 'dynamic-flag', + defaultValue: 0, + context: { + targetingKey: 'user-1', + country: 'US' + }, + logger: getNoopRulesLogger() + }); + + expect(result).toMatchObject({ + value: variationValue, + metadata: { + variationType: 'number' + } + }); + } + ); + it.each(['toString', 'constructor', '__proto__'])( 'checks own properties before it evaluates %s', flagKey => { diff --git a/packages/core/src/flags/configuration/rules.ts b/packages/core/src/flags/configuration/rules.ts index 0ee103dd5..024f2060b 100644 --- a/packages/core/src/flags/configuration/rules.ts +++ b/packages/core/src/flags/configuration/rules.ts @@ -12,6 +12,11 @@ import type { UniversalFlagConfigurationV1 } from '@datadog/flagging-core'; import type { EvaluationContext, JsonValue, PrimitiveValue } from '../types'; +// TODO(FFL-2837): Replace this legacy UFC v1 alias with +// `NonNullable['response']` after a flagging-core +// release contains DataDog/openfeature-js-client#344. +type RulesConfigurationResponse = UniversalFlagConfigurationV1; + export type RulesValueType = 'boolean' | 'string' | 'number' | 'object'; type RulesValueByType = { @@ -49,7 +54,7 @@ export interface RulesEvaluationDetails { } export interface RulesEvaluationRequest { - configuration: UniversalFlagConfigurationV1; + configuration: RulesConfigurationResponse; type: T; flagKey: string; defaultValue: RulesValueByType[T]; @@ -73,7 +78,7 @@ type RawEvaluationDetails = { }; type EvaluateRules = ( - configuration: UniversalFlagConfigurationV1, + configuration: RulesConfigurationResponse, type: T, flagKey: string, defaultValue: RulesValueByType[T], @@ -187,7 +192,7 @@ const validateCondition = (value: unknown): string | undefined => { try { // TODO(FFL-2837): Define a bounded regular expression policy before // dynamic offline rules leave draft state. Upstream PR #344 validates - // regular expression syntax, but it does not limit expensive patterns. + // the protobuf indexes, but it does not limit expensive patterns. RegExp(value.value); // dd-iac-scan ignore-line } catch { return 'A regular expression condition is not valid.'; @@ -408,7 +413,7 @@ const freezeValue = (value: unknown): void => { export type PreparedRulesConfiguration = | { status: 'ready'; - configuration: UniversalFlagConfigurationV1; + configuration: RulesConfigurationResponse; } | { status: 'error'; @@ -425,13 +430,14 @@ export const prepareRulesConfiguration = ( // TODO(FFL-2837): Delete this legacy JSON clone and validator after a // flagging-core release contains upstream PR #344. That implementation - // decodes the protobuf response and omits unsupported or invalid flags. + // decodes a generated Protobuf-ES response and omits unsupported or invalid + // flags. Do not adapt this validator to the generated response type. const errorMessage = validateRulesConfigurationEnvelope(clone); if (errorMessage) { return { status: 'error', errorMessage }; } - const flags = (clone as UniversalFlagConfigurationV1).flags; + const flags = (clone as RulesConfigurationResponse).flags; for (const [flagKey, flag] of Object.entries(flags)) { if (validateFlag(flag)) { delete flags[flagKey]; @@ -441,7 +447,7 @@ export const prepareRulesConfiguration = ( freezeValue(clone); return { status: 'ready', - configuration: clone as UniversalFlagConfigurationV1 + configuration: clone as RulesConfigurationResponse }; }; @@ -468,8 +474,12 @@ const normalizeVariationType = ( } }; +// TODO(FFL-2837): Delete this legacy UFC v1 metadata fallback after the +// flagging-core dependency contains DataDog/openfeature-js-client#344. +// The protobuf evaluator supplies `variationType` and maps integer and numeric +// variations to the OpenFeature type `number`. const recoverVariationType = ( - configuration: UniversalFlagConfigurationV1, + configuration: RulesConfigurationResponse, flagKey: string ): RulesValueType | undefined => { const flags = configuration.flags as Record; @@ -490,6 +500,10 @@ export const flaggingCoreRulesEngine: RulesEngine = { request: RulesEvaluationRequest ): RulesEvaluationDetails { const flags = request.configuration.flags as Record; + + // TODO(FFL-2837): Delete this local compatibility guard after the + // flagging-core dependency contains DataDog/openfeature-js-client#344. + // Keep the reserved-name contract tests for the upstream implementation. if (!hasOwn(flags, request.flagKey)) { return { value: request.defaultValue, diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index 681f68b94..7a3fe7cd1 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -18,6 +18,9 @@ import type { UniversalFlagConfigurationV1 } from '@datadog/flagging-core'; +// TODO(FFL-2837): Delete the pending `rulesBased` types, reader, and wrappers +// after a flagging-core release contains DataDog/openfeature-js-client#344. +// Re-export the upstream functions and use `FlagsConfiguration.rules`. type PendingRulesConfiguration = FlagsConfiguration & { rulesBased?: { response: UniversalFlagConfigurationV1; @@ -62,8 +65,9 @@ export const configurationFromString = (source: string): FlagsConfiguration => { source ) as PendingRulesConfiguration; - // TODO(FFL-2837): Delete this legacy JSON compatibility shim after a - // flagging-core release contains DataDog/openfeature-js-client#344. + // TODO(FFL-2837): Delete this legacy JSON compatibility shim with the + // pending types above. The upstream parser decodes `rules.response` as a + // generated Protobuf-ES message. const pendingRules = readPendingRulesWire(source); if (pendingRules) { try { @@ -86,6 +90,9 @@ export const configurationToString = ( configuration: FlagsConfiguration ): string => { const pendingConfiguration = configuration as PendingRulesConfiguration; + + // TODO(FFL-2837): Delete this local serialization guard with the pending + // types above. PR #344 makes the upstream serializer reject `rules`. if (pendingConfiguration.rulesBased) { throw new Error( 'Rules configurations cannot be serialized to the wire format' From d6abc8c1530c520154c1f0a5b74eff020039b1a7 Mon Sep 17 00:00:00 2001 From: Blake Date: Tue, 28 Jul 2026 18:37:17 -0400 Subject: [PATCH 04/10] test(flags): enforce portable wire boundary --- .../configuration/__tests__/wire.test.ts | 14 ++++++++++++++ packages/core/src/flags/configuration/wire.ts | 19 +++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/core/src/flags/configuration/__tests__/wire.test.ts b/packages/core/src/flags/configuration/__tests__/wire.test.ts index 9be51f2b6..dc81e8a4b 100644 --- a/packages/core/src/flags/configuration/__tests__/wire.test.ts +++ b/packages/core/src/flags/configuration/__tests__/wire.test.ts @@ -92,6 +92,20 @@ describe('configurationFromString', () => { expect(configurationFromString('not json')).toEqual({}); }); + it('does not treat a raw protobuf response as a portable wire', () => { + // A service or distribution layer must put one base64 encoding of + // these bytes in a version 1 `rules.response` JSON envelope. + const rawProtobufAsBase64 = 'CgR0ZXN0'; + + expect(configurationFromString(rawProtobufAsBase64)).toEqual({}); + }); + + it('does not treat the legacy UFC JSON response as a portable wire', () => { + const legacyServiceResponse = JSON.stringify(buildRulesConfiguration()); + + expect(configurationFromString(legacyServiceResponse)).toEqual({}); + }); + it('returns an empty config when the inner response is invalid JSON', () => { const wire = JSON.stringify({ version: 1, diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index 7a3fe7cd1..9ebc5d79b 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -5,9 +5,11 @@ */ // Wire (de)serialization is reused from `@datadog/flagging-core` (the canonical -// implementation) rather than reimplemented here. `configurationFromString` is lenient: -// it returns an empty configuration (`{}`) for malformed input or an unsupported wire -// version rather than throwing. `configurationToString` is the inverse (its fix from +// implementation) rather than reimplemented here. The input is the complete portable JSON +// envelope. It is not the raw protobuf or legacy JSON response from the UFC service. +// `configurationFromString` is lenient: it returns an empty configuration (`{}`) for +// malformed input or an unsupported wire version rather than throwing. +// `configurationToString` is the inverse (its fix from // https://github.com/DataDog/openfeature-js-client/pull/331 shipped in flagging-core 2.0.0). import { configurationFromString as coreConfigurationFromString, @@ -20,7 +22,10 @@ import type { // TODO(FFL-2837): Delete the pending `rulesBased` types, reader, and wrappers // after a flagging-core release contains DataDog/openfeature-js-client#344. -// Re-export the upstream functions and use `FlagsConfiguration.rules`. +// Re-export the upstream functions and use `FlagsConfiguration.rules`. The +// distribution layer must put one base64 encoding of the raw dd-source#34959 +// protobuf response in the version 1 `rules.response` field. Do not add that +// service transport or envelope construction here. type PendingRulesConfiguration = FlagsConfiguration & { rulesBased?: { response: UniversalFlagConfigurationV1; @@ -67,7 +72,8 @@ export const configurationFromString = (source: string): FlagsConfiguration => { // TODO(FFL-2837): Delete this legacy JSON compatibility shim with the // pending types above. The upstream parser decodes `rules.response` as a - // generated Protobuf-ES message. + // generated Protobuf-ES message. Do not adapt this shim to decode a raw + // service response or to add a base64 layer. const pendingRules = readPendingRulesWire(source); if (pendingRules) { try { @@ -92,7 +98,8 @@ export const configurationToString = ( const pendingConfiguration = configuration as PendingRulesConfiguration; // TODO(FFL-2837): Delete this local serialization guard with the pending - // types above. PR #344 makes the upstream serializer reject `rules`. + // types above. PR #344 makes the upstream serializer reject `rules`. The + // parsed protobuf does not contain the original portable-wire bytes. if (pendingConfiguration.rulesBased) { throw new Error( 'Rules configurations cannot be serialized to the wire format' From ef71e3de36405cb2a712824de8f1f3301bbdc664 Mon Sep 17 00:00:00 2001 From: Blake Date: Tue, 28 Jul 2026 20:24:45 -0400 Subject: [PATCH 05/10] docs(flags): align parser migration TODOs --- packages/core/src/flags/configuration/rules.ts | 3 ++- packages/core/src/flags/configuration/wire.ts | 18 +++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/core/src/flags/configuration/rules.ts b/packages/core/src/flags/configuration/rules.ts index 024f2060b..76ebf3770 100644 --- a/packages/core/src/flags/configuration/rules.ts +++ b/packages/core/src/flags/configuration/rules.ts @@ -14,7 +14,8 @@ import type { EvaluationContext, JsonValue, PrimitiveValue } from '../types'; // TODO(FFL-2837): Replace this legacy UFC v1 alias with // `NonNullable['response']` after a flagging-core -// release contains DataDog/openfeature-js-client#344. +// release contains DataDog/openfeature-js-client#344. Keep the +// `FlagsConfiguration` type import on the flagging-core package root. type RulesConfigurationResponse = UniversalFlagConfigurationV1; export type RulesValueType = 'boolean' | 'string' | 'number' | 'object'; diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index 9ebc5d79b..0d31d43a8 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -4,8 +4,9 @@ * Copyright 2016-Present Datadog, Inc. */ -// Wire (de)serialization is reused from `@datadog/flagging-core` (the canonical -// implementation) rather than reimplemented here. The input is the complete portable JSON +// Published flagging-core 2.0.2 exports wire conversion from its package root. PR #344 moves +// that conversion to the opt-in `@datadog/flagging-core/configuration` entry point so the default +// entry point does not load Protobuf-ES. In both versions, the input is the complete portable JSON // envelope. It is not the raw protobuf or legacy JSON response from the UFC service. // `configurationFromString` is lenient: it returns an empty configuration (`{}`) for // malformed input or an unsupported wire version rather than throwing. @@ -22,10 +23,12 @@ import type { // TODO(FFL-2837): Delete the pending `rulesBased` types, reader, and wrappers // after a flagging-core release contains DataDog/openfeature-js-client#344. -// Re-export the upstream functions and use `FlagsConfiguration.rules`. The -// distribution layer must put one base64 encoding of the raw dd-source#34959 -// protobuf response in the version 1 `rules.response` field. Do not add that -// service transport or envelope construction here. +// Import and re-export the wire functions and `FlagsConfigurationWire` type from +// `@datadog/flagging-core/configuration`. Keep `FlagsConfiguration` and the rules +// evaluator on the package root. Use `FlagsConfiguration.rules`. The distribution +// layer must put one base64 encoding of the raw dd-source#34959 protobuf response +// in the version 1 `rules.response` field. Do not add that service transport or +// envelope construction here. type PendingRulesConfiguration = FlagsConfiguration & { rulesBased?: { response: UniversalFlagConfigurationV1; @@ -73,7 +76,8 @@ export const configurationFromString = (source: string): FlagsConfiguration => { // TODO(FFL-2837): Delete this legacy JSON compatibility shim with the // pending types above. The upstream parser decodes `rules.response` as a // generated Protobuf-ES message. Do not adapt this shim to decode a raw - // service response or to add a base64 layer. + // service response or to add a base64 layer. Do not copy the strict base64 + // validator that PR #344 removed in favor of the Protobuf-ES decoder. const pendingRules = readPendingRulesWire(source); if (pendingRules) { try { From 775d1c4e48bf8d323a43fcb6751644425cba4f12 Mon Sep 17 00:00:00 2001 From: Blake Date: Wed, 29 Jul 2026 10:20:21 -0400 Subject: [PATCH 06/10] fix(flags): preserve invalid rules errors --- .../configuration/__tests__/rules.test.ts | 91 +++++++++++++++++-- .../core/src/flags/configuration/rules.ts | 46 ++++++++-- packages/core/src/flags/configuration/wire.ts | 6 +- 3 files changed, 127 insertions(+), 16 deletions(-) diff --git a/packages/core/src/flags/configuration/__tests__/rules.test.ts b/packages/core/src/flags/configuration/__tests__/rules.test.ts index 58488478f..496d7c42a 100644 --- a/packages/core/src/flags/configuration/__tests__/rules.test.ts +++ b/packages/core/src/flags/configuration/__tests__/rules.test.ts @@ -66,7 +66,7 @@ describe('rules configuration', () => { ).toBe(true); }); - it('omits a flag that uses an unsupported operator', () => { + it('preserves a flag with an unsupported operator and reports PARSE_ERROR', () => { const source = buildRulesConfiguration(); const condition = source.flags['dynamic-flag'].allocations[0].rules?.[0] @@ -83,10 +83,25 @@ describe('rules configuration', () => { if (prepared.status !== 'ready') { throw new Error(prepared.errorMessage); } - expect(prepared.configuration.flags).toEqual({}); + expect(prepared.configuration.flags).toHaveProperty('dynamic-flag'); + expect( + flaggingCoreRulesEngine.evaluate({ + configuration: prepared.configuration, + type: 'boolean', + flagKey: 'dynamic-flag', + defaultValue: false, + context: { targetingKey: 'user-1' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ + value: false, + reason: 'ERROR', + errorCode: 'PARSE_ERROR', + errorMessage: expect.stringContaining('FUTURE_OPERATOR') + }); }); - it('omits a flag that contains an invalid regular expression', () => { + it('reports PARSE_ERROR for a flag with an invalid regular expression', () => { const source = buildRulesConfiguration(); const conditions = source.flags['dynamic-flag'].allocations[0].rules?.[0].conditions; @@ -105,10 +120,22 @@ describe('rules configuration', () => { if (prepared.status !== 'ready') { throw new Error(prepared.errorMessage); } - expect(prepared.configuration.flags).toEqual({}); + expect( + flaggingCoreRulesEngine.evaluate({ + configuration: prepared.configuration, + type: 'boolean', + flagKey: 'dynamic-flag', + defaultValue: false, + context: { targetingKey: 'user-1' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ + errorCode: 'PARSE_ERROR', + errorMessage: 'A regular expression condition is not valid.' + }); }); - it('omits a flag whose split points to an absent variation', () => { + it('reports PARSE_ERROR when a split points to an absent variation', () => { const source = buildRulesConfiguration(); source.flags['dynamic-flag'].allocations[0].splits[0].variationKey = 'absent'; @@ -119,10 +146,22 @@ describe('rules configuration', () => { if (prepared.status !== 'ready') { throw new Error(prepared.errorMessage); } - expect(prepared.configuration.flags).toEqual({}); + expect( + flaggingCoreRulesEngine.evaluate({ + configuration: prepared.configuration, + type: 'boolean', + flagKey: 'dynamic-flag', + defaultValue: false, + context: { targetingKey: 'user-1' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ + errorCode: 'PARSE_ERROR', + errorMessage: 'A split has an invalid variation key.' + }); }); - it('keeps valid flags when it omits an invalid flag', () => { + it('keeps valid flags usable when another flag has a parse error', () => { const source = buildRulesConfiguration(); const validFlag = buildRulesConfiguration().flags['dynamic-flag']; validFlag.key = 'valid-flag'; @@ -143,8 +182,46 @@ describe('rules configuration', () => { throw new Error(prepared.errorMessage); } expect(Object.keys(prepared.configuration.flags)).toEqual([ + 'dynamic-flag', 'valid-flag' ]); + expect( + flaggingCoreRulesEngine.evaluate({ + configuration: prepared.configuration, + type: 'boolean', + flagKey: 'valid-flag', + defaultValue: false, + context: { targetingKey: 'user-1', country: 'US' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ value: true, errorCode: undefined }); + }); + + // TODO(FFL-2837): Replace this legacy JSON compatibility test with a + // generated protobuf fixture after a flagging-core release contains + // DataDog/openfeature-js-client#344 at or after `be0d886`. + it('keeps supported known data when an unknown field is present', () => { + const source = buildRulesConfiguration(); + (source.flags['dynamic-flag'] as typeof source.flags['dynamic-flag'] & { + futureField: string; + }).futureField = 'ignored'; + + const prepared = prepareRulesConfiguration(source); + + expect(prepared.status).toBe('ready'); + if (prepared.status !== 'ready') { + throw new Error(prepared.errorMessage); + } + expect( + flaggingCoreRulesEngine.evaluate({ + configuration: prepared.configuration, + type: 'boolean', + flagKey: 'dynamic-flag', + defaultValue: false, + context: { targetingKey: 'user-1', country: 'US' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ value: true, errorCode: undefined }); }); it('normalizes a real flagging-core evaluation', () => { diff --git a/packages/core/src/flags/configuration/rules.ts b/packages/core/src/flags/configuration/rules.ts index 76ebf3770..8e5d7a528 100644 --- a/packages/core/src/flags/configuration/rules.ts +++ b/packages/core/src/flags/configuration/rules.ts @@ -15,7 +15,8 @@ import type { EvaluationContext, JsonValue, PrimitiveValue } from '../types'; // TODO(FFL-2837): Replace this legacy UFC v1 alias with // `NonNullable['response']` after a flagging-core // release contains DataDog/openfeature-js-client#344. Keep the -// `FlagsConfiguration` type import on the flagging-core package root. +// `FlagsConfiguration` type import on the flagging-core package root. PR #344 +// now preserves invalid flags and reports their stored errors during evaluation. type RulesConfigurationResponse = UniversalFlagConfigurationV1; export type RulesValueType = 'boolean' | 'string' | 'number' | 'object'; @@ -125,6 +126,15 @@ export const toRulesEvaluationContext = ( const hasOwn = (value: object, key: PropertyKey): boolean => Object.prototype.hasOwnProperty.call(value, key); +// TODO(FFL-2837): Delete this compatibility error store after a flagging-core +// release contains DataDog/openfeature-js-client#344 at or after `ba1dbaf`. +// The generated protobuf parser uses the same per-configuration error model, +// and its evaluator returns `PARSE_ERROR` with the stored validation message. +const errorsByConfiguration = new WeakMap< + RulesConfigurationResponse, + ReadonlyMap +>(); + const isRecord = (value: unknown): value is Record => typeof value === 'object' && value !== null && !Array.isArray(value); @@ -431,24 +441,31 @@ export const prepareRulesConfiguration = ( // TODO(FFL-2837): Delete this legacy JSON clone and validator after a // flagging-core release contains upstream PR #344. That implementation - // decodes a generated Protobuf-ES response and omits unsupported or invalid - // flags. Do not adapt this validator to the generated response type. + // decodes a generated Protobuf-ES response, preserves invalid flags, and + // records per-flag errors for evaluation. Do not adapt this validator to + // the generated response type. const errorMessage = validateRulesConfigurationEnvelope(clone); if (errorMessage) { return { status: 'error', errorMessage }; } - const flags = (clone as RulesConfigurationResponse).flags; + const configuration = clone as RulesConfigurationResponse; + const flags = configuration.flags; + const errors = new Map(); for (const [flagKey, flag] of Object.entries(flags)) { - if (validateFlag(flag)) { - delete flags[flagKey]; + const flagError = validateFlag(flag); + if (flagError) { + errors.set(flagKey, flagError); } } freezeValue(clone); + if (errors.size > 0) { + errorsByConfiguration.set(configuration, errors); + } return { status: 'ready', - configuration: clone as RulesConfigurationResponse + configuration }; }; @@ -514,6 +531,21 @@ export const flaggingCoreRulesEngine: RulesEngine = { }; } + // TODO(FFL-2837): Delete this compatibility check with the local error + // store after the published PR #344 evaluator reports parser errors. + const configurationError = errorsByConfiguration + .get(request.configuration) + ?.get(request.flagKey); + if (configurationError) { + return { + value: request.defaultValue, + reason: 'ERROR', + errorCode: 'PARSE_ERROR', + errorMessage: configurationError, + metadata: {} + }; + } + const result = evaluateRules( request.configuration, request.type, diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index 0d31d43a8..cfbef5abc 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -28,7 +28,8 @@ import type { // evaluator on the package root. Use `FlagsConfiguration.rules`. The distribution // layer must put one base64 encoding of the raw dd-source#34959 protobuf response // in the version 1 `rules.response` field. Do not add that service transport or -// envelope construction here. +// envelope construction here. PR #344 preserves invalid protobuf flags and +// reports their validation errors when the flag is evaluated. type PendingRulesConfiguration = FlagsConfiguration & { rulesBased?: { response: UniversalFlagConfigurationV1; @@ -77,7 +78,8 @@ export const configurationFromString = (source: string): FlagsConfiguration => { // pending types above. The upstream parser decodes `rules.response` as a // generated Protobuf-ES message. Do not adapt this shim to decode a raw // service response or to add a base64 layer. Do not copy the strict base64 - // validator that PR #344 removed in favor of the Protobuf-ES decoder. + // validator that PR #344 removed in favor of the Protobuf-ES decoder. The + // published parser must also include PR #344's unknown-field tolerance. const pendingRules = readPendingRulesWire(source); if (pendingRules) { try { From 8ed8c4b9f465bde74542edb5ea817020488a068f Mon Sep 17 00:00:00 2001 From: Blake Date: Wed, 29 Jul 2026 12:22:48 -0400 Subject: [PATCH 07/10] fix(flags): reject unsafe rules integers --- .../configuration/__tests__/rules.test.ts | 71 ++++++++++++++++++- .../core/src/flags/configuration/rules.ts | 52 +++++++++----- packages/core/src/flags/configuration/wire.ts | 11 +-- 3 files changed, 113 insertions(+), 21 deletions(-) diff --git a/packages/core/src/flags/configuration/__tests__/rules.test.ts b/packages/core/src/flags/configuration/__tests__/rules.test.ts index 496d7c42a..634e3133c 100644 --- a/packages/core/src/flags/configuration/__tests__/rules.test.ts +++ b/packages/core/src/flags/configuration/__tests__/rules.test.ts @@ -199,7 +199,7 @@ describe('rules configuration', () => { // TODO(FFL-2837): Replace this legacy JSON compatibility test with a // generated protobuf fixture after a flagging-core release contains - // DataDog/openfeature-js-client#344 at or after `be0d886`. + // DataDog/openfeature-js-client#344 at or after `4f6f40c`. it('keeps supported known data when an unknown field is present', () => { const source = buildRulesConfiguration(); (source.flags['dynamic-flag'] as typeof source.flags['dynamic-flag'] & { @@ -224,6 +224,75 @@ describe('rules configuration', () => { ).toMatchObject({ value: true, errorCode: undefined }); }); + // TODO(FFL-2837): Replace this unsafe JSON number with an out-of-range + // protobuf `int64` fixture after flagging-core contains PR #344 at or after + // `4f6f40c`. The generated parser must preserve the source value as `bigint`. + it('returns PARSE_ERROR instead of serving an unsafe integer', () => { + const source = buildRulesConfiguration(); + const flag = source.flags['dynamic-flag']; + flag.variationType = 'INTEGER'; + flag.variations.enabled.value = Number.MAX_SAFE_INTEGER + 1; + flag.variations.disabled.value = 0; + + const prepared = prepareRulesConfiguration(source); + + expect(prepared.status).toBe('ready'); + if (prepared.status !== 'ready') { + throw new Error(prepared.errorMessage); + } + expect( + prepared.configuration.flags['dynamic-flag'].variations.enabled + .value + ).toBe(Number.MAX_SAFE_INTEGER + 1); + expect( + flaggingCoreRulesEngine.evaluate({ + configuration: prepared.configuration, + type: 'number', + flagKey: 'dynamic-flag', + defaultValue: 0, + context: { targetingKey: 'user-1', country: 'US' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ + value: 0, + reason: 'ERROR', + errorCode: 'PARSE_ERROR', + errorMessage: + 'Integer variation value cannot be represented safely as a JavaScript number' + }); + }); + + it('returns PARSE_ERROR for an unsafe shard integer', () => { + const source = buildRulesConfiguration(); + source.flags[ + 'dynamic-flag' + ].allocations[0].splits[0].shards[0].totalShards = + Number.MAX_SAFE_INTEGER + 1; + + const prepared = prepareRulesConfiguration(source); + + expect(prepared.status).toBe('ready'); + if (prepared.status !== 'ready') { + throw new Error(prepared.errorMessage); + } + expect( + flaggingCoreRulesEngine.evaluate({ + configuration: prepared.configuration, + type: 'boolean', + flagKey: 'dynamic-flag', + defaultValue: false, + context: { targetingKey: 'user-1', country: 'US' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ + value: false, + reason: 'ERROR', + errorCode: 'PARSE_ERROR', + errorMessage: + 'Protobuf uint64 cannot be represented safely as a JavaScript number' + }); + }); + it('normalizes a real flagging-core evaluation', () => { const configuration = buildRulesConfiguration(); diff --git a/packages/core/src/flags/configuration/rules.ts b/packages/core/src/flags/configuration/rules.ts index 8e5d7a528..f0bc143f6 100644 --- a/packages/core/src/flags/configuration/rules.ts +++ b/packages/core/src/flags/configuration/rules.ts @@ -14,9 +14,10 @@ import type { EvaluationContext, JsonValue, PrimitiveValue } from '../types'; // TODO(FFL-2837): Replace this legacy UFC v1 alias with // `NonNullable['response']` after a flagging-core -// release contains DataDog/openfeature-js-client#344. Keep the +// release contains DataDog/openfeature-js-client#344 through `4f6f40c`. Keep the // `FlagsConfiguration` type import on the flagging-core package root. PR #344 -// now preserves invalid flags and reports their stored errors during evaluation. +// preserves protobuf integers as `bigint`, and it reports unsafe conversions as +// stored per-flag errors during evaluation. type RulesConfigurationResponse = UniversalFlagConfigurationV1; export type RulesValueType = 'boolean' | 'string' | 'number' | 'object'; @@ -127,9 +128,9 @@ const hasOwn = (value: object, key: PropertyKey): boolean => Object.prototype.hasOwnProperty.call(value, key); // TODO(FFL-2837): Delete this compatibility error store after a flagging-core -// release contains DataDog/openfeature-js-client#344 at or after `ba1dbaf`. +// release contains DataDog/openfeature-js-client#344 at or after `4f6f40c`. // The generated protobuf parser uses the same per-configuration error model, -// and its evaluator returns `PARSE_ERROR` with the stored validation message. +// including `PARSE_ERROR` for an integer that is not a safe JavaScript number. const errorsByConfiguration = new WeakMap< RulesConfigurationResponse, ReadonlyMap @@ -168,6 +169,7 @@ const variationValueIsValid = ( case 'STRING': return typeof value === 'string'; case 'INTEGER': + return typeof value === 'number' && Number.isSafeInteger(value); case 'NUMERIC': return typeof value === 'number' && Number.isFinite(value); case 'JSON': @@ -202,8 +204,8 @@ const validateCondition = (value: unknown): string | undefined => { } try { // TODO(FFL-2837): Define a bounded regular expression policy before - // dynamic offline rules leave draft state. Upstream PR #344 validates - // the protobuf indexes, but it does not limit expensive patterns. + // dynamic offline rules leave draft state. Upstream PR #344 through + // `4f6f40c` validates protobuf data, but it does not limit patterns. RegExp(value.value); // dd-iac-scan ignore-line } catch { return 'A regular expression condition is not valid.'; @@ -270,6 +272,9 @@ const validateShards = (value: unknown): string | undefined => { ) { return 'A shard has an invalid shape.'; } + if (!Number.isSafeInteger(shard.totalShards)) { + return 'Protobuf uint64 cannot be represented safely as a JavaScript number'; + } for (const range of shard.ranges) { if ( @@ -282,6 +287,12 @@ const validateShards = (value: unknown): string | undefined => { ) { return 'A shard range is not valid.'; } + if ( + !Number.isSafeInteger(range.start) || + !Number.isSafeInteger(range.end) + ) { + return 'Protobuf uint64 cannot be represented safely as a JavaScript number'; + } } } @@ -359,6 +370,14 @@ const validateFlag = (value: unknown): string | undefined => { } for (const variation of Object.values(value.variations)) { + if ( + value.variationType === 'INTEGER' && + isRecord(variation) && + typeof variation.value === 'number' && + !Number.isSafeInteger(variation.value) + ) { + return 'Integer variation value cannot be represented safely as a JavaScript number'; + } if ( !isRecord(variation) || typeof variation.key !== 'string' || @@ -440,10 +459,10 @@ export const prepareRulesConfiguration = ( const clone = cloneValue(value); // TODO(FFL-2837): Delete this legacy JSON clone and validator after a - // flagging-core release contains upstream PR #344. That implementation - // decodes a generated Protobuf-ES response, preserves invalid flags, and - // records per-flag errors for evaluation. Do not adapt this validator to - // the generated response type. + // flagging-core release contains upstream PR #344 through `4f6f40c`. That + // implementation preserves protobuf integers as `bigint` and records a + // per-flag error when evaluation cannot produce a safe JavaScript number. + // Do not adapt this validator to the generated response type. const errorMessage = validateRulesConfigurationEnvelope(clone); if (errorMessage) { return { status: 'error', errorMessage }; @@ -493,9 +512,9 @@ const normalizeVariationType = ( }; // TODO(FFL-2837): Delete this legacy UFC v1 metadata fallback after the -// flagging-core dependency contains DataDog/openfeature-js-client#344. -// The protobuf evaluator supplies `variationType` and maps integer and numeric -// variations to the OpenFeature type `number`. +// flagging-core dependency contains DataDog/openfeature-js-client#344 through +// `4f6f40c`. The protobuf evaluator maps only safely represented integer +// variations, and all numeric variations, to the OpenFeature type `number`. const recoverVariationType = ( configuration: RulesConfigurationResponse, flagKey: string @@ -520,8 +539,8 @@ export const flaggingCoreRulesEngine: RulesEngine = { const flags = request.configuration.flags as Record; // TODO(FFL-2837): Delete this local compatibility guard after the - // flagging-core dependency contains DataDog/openfeature-js-client#344. - // Keep the reserved-name contract tests for the upstream implementation. + // flagging-core dependency contains DataDog/openfeature-js-client#344 + // through `4f6f40c`. Keep the reserved-name contract tests. if (!hasOwn(flags, request.flagKey)) { return { value: request.defaultValue, @@ -532,7 +551,8 @@ export const flaggingCoreRulesEngine: RulesEngine = { } // TODO(FFL-2837): Delete this compatibility check with the local error - // store after the published PR #344 evaluator reports parser errors. + // store after the published PR #344 evaluator at or after `4f6f40c` + // reports parser errors, including unsafe integer conversions. const configurationError = errorsByConfiguration .get(request.configuration) ?.get(request.flagKey); diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index cfbef5abc..33f4e1586 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -22,14 +22,15 @@ import type { } from '@datadog/flagging-core'; // TODO(FFL-2837): Delete the pending `rulesBased` types, reader, and wrappers -// after a flagging-core release contains DataDog/openfeature-js-client#344. +// after a flagging-core release contains DataDog/openfeature-js-client#344 +// through `4f6f40c`. // Import and re-export the wire functions and `FlagsConfigurationWire` type from // `@datadog/flagging-core/configuration`. Keep `FlagsConfiguration` and the rules // evaluator on the package root. Use `FlagsConfiguration.rules`. The distribution // layer must put one base64 encoding of the raw dd-source#34959 protobuf response // in the version 1 `rules.response` field. Do not add that service transport or // envelope construction here. PR #344 preserves invalid protobuf flags and -// reports their validation errors when the flag is evaluated. +// protobuf integers, and reports unsafe integer conversion when evaluated. type PendingRulesConfiguration = FlagsConfiguration & { rulesBased?: { response: UniversalFlagConfigurationV1; @@ -79,7 +80,8 @@ export const configurationFromString = (source: string): FlagsConfiguration => { // generated Protobuf-ES message. Do not adapt this shim to decode a raw // service response or to add a base64 layer. Do not copy the strict base64 // validator that PR #344 removed in favor of the Protobuf-ES decoder. The - // published parser must also include PR #344's unknown-field tolerance. + // published parser must also include PR #344's unknown-field tolerance and + // lossless integer parsing through `4f6f40c`. const pendingRules = readPendingRulesWire(source); if (pendingRules) { try { @@ -105,7 +107,8 @@ export const configurationToString = ( // TODO(FFL-2837): Delete this local serialization guard with the pending // types above. PR #344 makes the upstream serializer reject `rules`. The - // parsed protobuf does not contain the original portable-wire bytes. + // parsed protobuf does not contain the original portable-wire bytes and can + // contain `bigint` values after `4f6f40c`. if (pendingConfiguration.rulesBased) { throw new Error( 'Rules configurations cannot be serialized to the wire format' From 5c4fef8065af6be06f88d45be1ec5bc6f4edfef1 Mon Sep 17 00:00:00 2001 From: Blake Date: Thu, 30 Jul 2026 08:35:16 -0400 Subject: [PATCH 08/10] fix(flags): align compatibility with upstream rules --- .../configuration/__tests__/rules.test.ts | 7 ++-- .../configuration/__tests__/wire.test.ts | 20 ++++++----- .../core/src/flags/configuration/rules.ts | 33 +++++++++++-------- packages/core/src/flags/configuration/wire.ts | 28 +++++++++------- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/packages/core/src/flags/configuration/__tests__/rules.test.ts b/packages/core/src/flags/configuration/__tests__/rules.test.ts index 634e3133c..f026b9619 100644 --- a/packages/core/src/flags/configuration/__tests__/rules.test.ts +++ b/packages/core/src/flags/configuration/__tests__/rules.test.ts @@ -97,7 +97,8 @@ describe('rules configuration', () => { value: false, reason: 'ERROR', errorCode: 'PARSE_ERROR', - errorMessage: expect.stringContaining('FUTURE_OPERATOR') + errorMessage: + 'The rules configuration uses an unsupported operator.' }); }); @@ -199,7 +200,7 @@ describe('rules configuration', () => { // TODO(FFL-2837): Replace this legacy JSON compatibility test with a // generated protobuf fixture after a flagging-core release contains - // DataDog/openfeature-js-client#344 at or after `4f6f40c`. + // DataDog/openfeature-js-client#344 through `41dff20`. it('keeps supported known data when an unknown field is present', () => { const source = buildRulesConfiguration(); (source.flags['dynamic-flag'] as typeof source.flags['dynamic-flag'] & { @@ -226,7 +227,7 @@ describe('rules configuration', () => { // TODO(FFL-2837): Replace this unsafe JSON number with an out-of-range // protobuf `int64` fixture after flagging-core contains PR #344 at or after - // `4f6f40c`. The generated parser must preserve the source value as `bigint`. + // `41dff20`. The generated parser must preserve the source value as `bigint`. it('returns PARSE_ERROR instead of serving an unsafe integer', () => { const source = buildRulesConfiguration(); const flag = source.flags['dynamic-flag']; diff --git a/packages/core/src/flags/configuration/__tests__/wire.test.ts b/packages/core/src/flags/configuration/__tests__/wire.test.ts index dc81e8a4b..984bea29c 100644 --- a/packages/core/src/flags/configuration/__tests__/wire.test.ts +++ b/packages/core/src/flags/configuration/__tests__/wire.test.ts @@ -164,20 +164,24 @@ describe('temporary rules configuration wire compatibility', () => { expect(parsed.rulesBased).toEqual(rulesBased); }); - it('does not serialize a rules configuration', () => { - const configuration = { + it('round-trips a legacy rules configuration', () => { + const original = { rulesBased: { - response: buildRulesConfiguration() + response: buildRulesConfiguration(), + fetchedAt: 123, + etag: 'rules-etag' } }; - expect(() => + const restored = configurationFromString( configurationToString( - (configuration as unknown) as ParsedFlagsConfiguration + (original as unknown) as ParsedFlagsConfiguration ) - ).toThrow( - 'Rules configurations cannot be serialized to the wire format' - ); + ) as { + rulesBased?: typeof original.rulesBased; + }; + + expect(restored.rulesBased).toEqual(original.rulesBased); }); it('keeps both branches in a mixed configuration', () => { diff --git a/packages/core/src/flags/configuration/rules.ts b/packages/core/src/flags/configuration/rules.ts index f0bc143f6..40f642921 100644 --- a/packages/core/src/flags/configuration/rules.ts +++ b/packages/core/src/flags/configuration/rules.ts @@ -14,10 +14,10 @@ import type { EvaluationContext, JsonValue, PrimitiveValue } from '../types'; // TODO(FFL-2837): Replace this legacy UFC v1 alias with // `NonNullable['response']` after a flagging-core -// release contains DataDog/openfeature-js-client#344 through `4f6f40c`. Keep the +// release contains DataDog/openfeature-js-client#344 through `41dff20`. Keep the // `FlagsConfiguration` type import on the flagging-core package root. PR #344 -// preserves protobuf integers as `bigint`, and it reports unsafe conversions as -// stored per-flag errors during evaluation. +// preserves protobuf integers as `bigint`, and its evaluator reports unsafe +// conversions as deterministic per-flag `PARSE_ERROR` results. type RulesConfigurationResponse = UniversalFlagConfigurationV1; export type RulesValueType = 'boolean' | 'string' | 'number' | 'object'; @@ -128,9 +128,11 @@ const hasOwn = (value: object, key: PropertyKey): boolean => Object.prototype.hasOwnProperty.call(value, key); // TODO(FFL-2837): Delete this compatibility error store after a flagging-core -// release contains DataDog/openfeature-js-client#344 at or after `4f6f40c`. -// The generated protobuf parser uses the same per-configuration error model, -// including `PARSE_ERROR` for an integer that is not a safe JavaScript number. +// release contains DataDog/openfeature-js-client#344 through `41dff20`. +// The generated protobuf evaluator validates the requested flag and the data +// that evaluation reaches. It does not build this error map during parsing. +// It returns deterministic `PARSE_ERROR` results, including for an integer that +// is not a safe JavaScript number. const errorsByConfiguration = new WeakMap< RulesConfigurationResponse, ReadonlyMap @@ -193,7 +195,7 @@ const validateCondition = (value: unknown): string | undefined => { } if (!SUPPORTED_OPERATORS.has(value.operator)) { - return `The rules configuration uses the unsupported operator "${value.operator}".`; + return 'The rules configuration uses an unsupported operator.'; } switch (value.operator) { @@ -205,7 +207,8 @@ const validateCondition = (value: unknown): string | undefined => { try { // TODO(FFL-2837): Define a bounded regular expression policy before // dynamic offline rules leave draft state. Upstream PR #344 through - // `4f6f40c` validates protobuf data, but it does not limit patterns. + // `41dff20` compiles protobuf regular expressions lazily and caches + // them by configuration and index, but it does not limit patterns. RegExp(value.value); // dd-iac-scan ignore-line } catch { return 'A regular expression condition is not valid.'; @@ -459,8 +462,9 @@ export const prepareRulesConfiguration = ( const clone = cloneValue(value); // TODO(FFL-2837): Delete this legacy JSON clone and validator after a - // flagging-core release contains upstream PR #344 through `4f6f40c`. That - // implementation preserves protobuf integers as `bigint` and records a + // flagging-core release contains upstream PR #344 through `41dff20`. That + // implementation preserves protobuf integers as `bigint` and validates only + // the requested flag data that evaluation reaches. It returns a deterministic // per-flag error when evaluation cannot produce a safe JavaScript number. // Do not adapt this validator to the generated response type. const errorMessage = validateRulesConfigurationEnvelope(clone); @@ -513,7 +517,7 @@ const normalizeVariationType = ( // TODO(FFL-2837): Delete this legacy UFC v1 metadata fallback after the // flagging-core dependency contains DataDog/openfeature-js-client#344 through -// `4f6f40c`. The protobuf evaluator maps only safely represented integer +// `41dff20`. The protobuf evaluator maps only safely represented integer // variations, and all numeric variations, to the OpenFeature type `number`. const recoverVariationType = ( configuration: RulesConfigurationResponse, @@ -540,7 +544,7 @@ export const flaggingCoreRulesEngine: RulesEngine = { // TODO(FFL-2837): Delete this local compatibility guard after the // flagging-core dependency contains DataDog/openfeature-js-client#344 - // through `4f6f40c`. Keep the reserved-name contract tests. + // through `41dff20`. Keep the reserved-name contract tests. if (!hasOwn(flags, request.flagKey)) { return { value: request.defaultValue, @@ -551,8 +555,9 @@ export const flaggingCoreRulesEngine: RulesEngine = { } // TODO(FFL-2837): Delete this compatibility check with the local error - // store after the published PR #344 evaluator at or after `4f6f40c` - // reports parser errors, including unsafe integer conversions. + // store after the published PR #344 evaluator through `41dff20` validates + // reached flag data and reports deterministic errors, including unsafe + // integer conversions. const configurationError = errorsByConfiguration .get(request.configuration) ?.get(request.flagKey); diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index 33f4e1586..01426a543 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -23,14 +23,15 @@ import type { // TODO(FFL-2837): Delete the pending `rulesBased` types, reader, and wrappers // after a flagging-core release contains DataDog/openfeature-js-client#344 -// through `4f6f40c`. +// through `41dff20`. // Import and re-export the wire functions and `FlagsConfigurationWire` type from // `@datadog/flagging-core/configuration`. Keep `FlagsConfiguration` and the rules // evaluator on the package root. Use `FlagsConfiguration.rules`. The distribution // layer must put one base64 encoding of the raw dd-source#34959 protobuf response // in the version 1 `rules.response` field. Do not add that service transport or -// envelope construction here. PR #344 preserves invalid protobuf flags and -// protobuf integers, and reports unsafe integer conversion when evaluated. +// envelope construction here. PR #344 preserves decoded protobuf flags and +// protobuf integers. Its evaluator reports invalid reached data and unsafe +// integer conversion as deterministic `PARSE_ERROR` results. type PendingRulesConfiguration = FlagsConfiguration & { rulesBased?: { response: UniversalFlagConfigurationV1; @@ -81,7 +82,7 @@ export const configurationFromString = (source: string): FlagsConfiguration => { // service response or to add a base64 layer. Do not copy the strict base64 // validator that PR #344 removed in favor of the Protobuf-ES decoder. The // published parser must also include PR #344's unknown-field tolerance and - // lossless integer parsing through `4f6f40c`. + // lossless integer parsing through `41dff20`. const pendingRules = readPendingRulesWire(source); if (pendingRules) { try { @@ -105,14 +106,19 @@ export const configurationToString = ( ): string => { const pendingConfiguration = configuration as PendingRulesConfiguration; - // TODO(FFL-2837): Delete this local serialization guard with the pending - // types above. PR #344 makes the upstream serializer reject `rules`. The - // parsed protobuf does not contain the original portable-wire bytes and can - // contain `bigint` values after `4f6f40c`. + // TODO(FFL-2837): Delete this legacy serialization wrapper with the pending + // types above after the dependency contains PR #344 through `41dff20`. + // The upstream serializer encodes generated protobuf rules back to base64. + // This temporary UFC v1 shim serializes its legacy JSON response instead. if (pendingConfiguration.rulesBased) { - throw new Error( - 'Rules configurations cannot be serialized to the wire format' - ); + const serialized = JSON.parse( + coreConfigurationToString(configuration) + ) as PendingRulesWire; + serialized.rulesBased = { + ...pendingConfiguration.rulesBased, + response: JSON.stringify(pendingConfiguration.rulesBased.response) + }; + return JSON.stringify(serialized); } return coreConfigurationToString(configuration); From 54519d217e8bcb88bbf8a5431fc24aec73c16078 Mon Sep 17 00:00:00 2001 From: Blake Date: Fri, 31 Jul 2026 09:51:47 -0400 Subject: [PATCH 09/10] test(flags): preserve unknown rules fields --- .../configuration/__tests__/rules.test.ts | 7 ++++-- .../configuration/__tests__/wire.test.ts | 8 ++++++- .../core/src/flags/configuration/rules.ts | 22 +++++++++++-------- packages/core/src/flags/configuration/wire.ts | 12 +++++----- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/packages/core/src/flags/configuration/__tests__/rules.test.ts b/packages/core/src/flags/configuration/__tests__/rules.test.ts index f026b9619..8eeba39f5 100644 --- a/packages/core/src/flags/configuration/__tests__/rules.test.ts +++ b/packages/core/src/flags/configuration/__tests__/rules.test.ts @@ -200,7 +200,8 @@ describe('rules configuration', () => { // TODO(FFL-2837): Replace this legacy JSON compatibility test with a // generated protobuf fixture after a flagging-core release contains - // DataDog/openfeature-js-client#344 through `41dff20`. + // DataDog/openfeature-js-client#344 through `41dff20`. Round-trip the + // generated fixture and confirm that serialization preserves the unknown field. it('keeps supported known data when an unknown field is present', () => { const source = buildRulesConfiguration(); (source.flags['dynamic-flag'] as typeof source.flags['dynamic-flag'] & { @@ -227,7 +228,9 @@ describe('rules configuration', () => { // TODO(FFL-2837): Replace this unsafe JSON number with an out-of-range // protobuf `int64` fixture after flagging-core contains PR #344 at or after - // `41dff20`. The generated parser must preserve the source value as `bigint`. + // `41dff20` plus the no-`BigInt` follow-up. The generated parser must preserve + // the source value as `bigint` where supported. Run the same evaluation with + // global `BigInt` unavailable and require `PARSE_ERROR`, not `GENERAL`. it('returns PARSE_ERROR instead of serving an unsafe integer', () => { const source = buildRulesConfiguration(); const flag = source.flags['dynamic-flag']; diff --git a/packages/core/src/flags/configuration/__tests__/wire.test.ts b/packages/core/src/flags/configuration/__tests__/wire.test.ts index 984bea29c..0e1cdc0b7 100644 --- a/packages/core/src/flags/configuration/__tests__/wire.test.ts +++ b/packages/core/src/flags/configuration/__tests__/wire.test.ts @@ -165,9 +165,15 @@ describe('temporary rules configuration wire compatibility', () => { }); it('round-trips a legacy rules configuration', () => { + const response = buildRulesConfiguration() as ReturnType< + typeof buildRulesConfiguration + > & { + futureField?: { value: number }; + }; + response.futureField = { value: 7 }; const original = { rulesBased: { - response: buildRulesConfiguration(), + response, fetchedAt: 123, etag: 'rules-etag' } diff --git a/packages/core/src/flags/configuration/rules.ts b/packages/core/src/flags/configuration/rules.ts index 40f642921..f0f663b9f 100644 --- a/packages/core/src/flags/configuration/rules.ts +++ b/packages/core/src/flags/configuration/rules.ts @@ -14,10 +14,12 @@ import type { EvaluationContext, JsonValue, PrimitiveValue } from '../types'; // TODO(FFL-2837): Replace this legacy UFC v1 alias with // `NonNullable['response']` after a flagging-core -// release contains DataDog/openfeature-js-client#344 through `41dff20`. Keep the -// `FlagsConfiguration` type import on the flagging-core package root. PR #344 -// preserves protobuf integers as `bigint`, and its evaluator reports unsafe -// conversions as deterministic per-flag `PARSE_ERROR` results. +// release contains DataDog/openfeature-js-client#344 through `41dff20`, restores +// 32-byte SHA digest validation, and defines or fixes integer evaluation without +// global `BigInt`. Keep the `FlagsConfiguration` type import on the flagging-core +// package root. PR #344 preserves protobuf integers as `bigint`, and its evaluator +// reports unsafe conversions as deterministic per-flag `PARSE_ERROR` results when +// `BigInt` is available. type RulesConfigurationResponse = UniversalFlagConfigurationV1; export type RulesValueType = 'boolean' | 'string' | 'number' | 'object'; @@ -128,7 +130,8 @@ const hasOwn = (value: object, key: PropertyKey): boolean => Object.prototype.hasOwnProperty.call(value, key); // TODO(FFL-2837): Delete this compatibility error store after a flagging-core -// release contains DataDog/openfeature-js-client#344 through `41dff20`. +// release contains DataDog/openfeature-js-client#344 through `41dff20` and fixes +// or explicitly excludes integer and shard evaluation without global `BigInt`. // The generated protobuf evaluator validates the requested flag and the data // that evaluation reaches. It does not build this error map during parsing. // It returns deterministic `PARSE_ERROR` results, including for an integer that @@ -462,10 +465,11 @@ export const prepareRulesConfiguration = ( const clone = cloneValue(value); // TODO(FFL-2837): Delete this legacy JSON clone and validator after a - // flagging-core release contains upstream PR #344 through `41dff20`. That + // flagging-core release contains upstream PR #344 through `41dff20` and the + // no-`BigInt` integer contract is fixed or declared unsupported. That // implementation preserves protobuf integers as `bigint` and validates only - // the requested flag data that evaluation reaches. It returns a deterministic - // per-flag error when evaluation cannot produce a safe JavaScript number. + // the requested flag data that evaluation reaches. With `BigInt`, it returns a + // deterministic per-flag error when evaluation cannot produce a safe number. // Do not adapt this validator to the generated response type. const errorMessage = validateRulesConfigurationEnvelope(clone); if (errorMessage) { @@ -557,7 +561,7 @@ export const flaggingCoreRulesEngine: RulesEngine = { // TODO(FFL-2837): Delete this compatibility check with the local error // store after the published PR #344 evaluator through `41dff20` validates // reached flag data and reports deterministic errors, including unsafe - // integer conversions. + // integer conversions with and without global `BigInt` when supported. const configurationError = errorsByConfiguration .get(request.configuration) ?.get(request.flagKey); diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index 01426a543..984a52021 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -23,7 +23,7 @@ import type { // TODO(FFL-2837): Delete the pending `rulesBased` types, reader, and wrappers // after a flagging-core release contains DataDog/openfeature-js-client#344 -// through `41dff20`. +// through `41dff20` plus the required SHA digest and no-`BigInt` follow-ups. // Import and re-export the wire functions and `FlagsConfigurationWire` type from // `@datadog/flagging-core/configuration`. Keep `FlagsConfiguration` and the rules // evaluator on the package root. Use `FlagsConfiguration.rules`. The distribution @@ -81,8 +81,9 @@ export const configurationFromString = (source: string): FlagsConfiguration => { // generated Protobuf-ES message. Do not adapt this shim to decode a raw // service response or to add a base64 layer. Do not copy the strict base64 // validator that PR #344 removed in favor of the Protobuf-ES decoder. The - // published parser must also include PR #344's unknown-field tolerance and - // lossless integer parsing through `41dff20`. + // published parser must also include PR #344's unknown-field tolerance, + // unknown-field serialization, and lossless integer parsing through + // `41dff20`, plus the final no-`BigInt` runtime decision. const pendingRules = readPendingRulesWire(source); if (pendingRules) { try { @@ -107,8 +108,9 @@ export const configurationToString = ( const pendingConfiguration = configuration as PendingRulesConfiguration; // TODO(FFL-2837): Delete this legacy serialization wrapper with the pending - // types above after the dependency contains PR #344 through `41dff20`. - // The upstream serializer encodes generated protobuf rules back to base64. + // types above after the dependency contains PR #344 through `41dff20` and + // its required follow-ups. The upstream serializer encodes generated protobuf + // rules back to base64 and preserves unknown protobuf fields. // This temporary UFC v1 shim serializes its legacy JSON response instead. if (pendingConfiguration.rulesBased) { const serialized = JSON.parse( From 4902bc11b355071f87699765b8bc1c41394bc1f9 Mon Sep 17 00:00:00 2001 From: Blake Date: Mon, 3 Aug 2026 09:37:18 -0400 Subject: [PATCH 10/10] docs(flags): refresh capability migration TODOs --- .../configuration/__tests__/rules.test.ts | 6 +++-- .../core/src/flags/configuration/rules.ts | 19 ++++++++-------- packages/core/src/flags/configuration/wire.ts | 22 +++++++++++-------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/packages/core/src/flags/configuration/__tests__/rules.test.ts b/packages/core/src/flags/configuration/__tests__/rules.test.ts index 8eeba39f5..2d8b6a431 100644 --- a/packages/core/src/flags/configuration/__tests__/rules.test.ts +++ b/packages/core/src/flags/configuration/__tests__/rules.test.ts @@ -200,8 +200,10 @@ describe('rules configuration', () => { // TODO(FFL-2837): Replace this legacy JSON compatibility test with a // generated protobuf fixture after a flagging-core release contains - // DataDog/openfeature-js-client#344 through `41dff20`. Round-trip the + // DataDog/openfeature-js-client#344 through `9f794c7`. Round-trip the // generated fixture and confirm that serialization preserves the unknown field. + // Add a fixture with an unsupported minimum feature level and require a + // flag-scoped `PARSE_ERROR`, not `FLAG_NOT_FOUND`. it('keeps supported known data when an unknown field is present', () => { const source = buildRulesConfiguration(); (source.flags['dynamic-flag'] as typeof source.flags['dynamic-flag'] & { @@ -228,7 +230,7 @@ describe('rules configuration', () => { // TODO(FFL-2837): Replace this unsafe JSON number with an out-of-range // protobuf `int64` fixture after flagging-core contains PR #344 at or after - // `41dff20` plus the no-`BigInt` follow-up. The generated parser must preserve + // `9f794c7` plus the no-`BigInt` follow-up. The generated parser must preserve // the source value as `bigint` where supported. Run the same evaluation with // global `BigInt` unavailable and require `PARSE_ERROR`, not `GENERAL`. it('returns PARSE_ERROR instead of serving an unsafe integer', () => { diff --git a/packages/core/src/flags/configuration/rules.ts b/packages/core/src/flags/configuration/rules.ts index f0f663b9f..6949a9e96 100644 --- a/packages/core/src/flags/configuration/rules.ts +++ b/packages/core/src/flags/configuration/rules.ts @@ -14,7 +14,7 @@ import type { EvaluationContext, JsonValue, PrimitiveValue } from '../types'; // TODO(FFL-2837): Replace this legacy UFC v1 alias with // `NonNullable['response']` after a flagging-core -// release contains DataDog/openfeature-js-client#344 through `41dff20`, restores +// release contains DataDog/openfeature-js-client#344 through `9f794c7`, restores // 32-byte SHA digest validation, and defines or fixes integer evaluation without // global `BigInt`. Keep the `FlagsConfiguration` type import on the flagging-core // package root. PR #344 preserves protobuf integers as `bigint`, and its evaluator @@ -130,7 +130,7 @@ const hasOwn = (value: object, key: PropertyKey): boolean => Object.prototype.hasOwnProperty.call(value, key); // TODO(FFL-2837): Delete this compatibility error store after a flagging-core -// release contains DataDog/openfeature-js-client#344 through `41dff20` and fixes +// release contains DataDog/openfeature-js-client#344 through `9f794c7` and fixes // or explicitly excludes integer and shard evaluation without global `BigInt`. // The generated protobuf evaluator validates the requested flag and the data // that evaluation reaches. It does not build this error map during parsing. @@ -210,7 +210,7 @@ const validateCondition = (value: unknown): string | undefined => { try { // TODO(FFL-2837): Define a bounded regular expression policy before // dynamic offline rules leave draft state. Upstream PR #344 through - // `41dff20` compiles protobuf regular expressions lazily and caches + // `9f794c7` compiles protobuf regular expressions lazily and caches // them by configuration and index, but it does not limit patterns. RegExp(value.value); // dd-iac-scan ignore-line } catch { @@ -465,7 +465,7 @@ export const prepareRulesConfiguration = ( const clone = cloneValue(value); // TODO(FFL-2837): Delete this legacy JSON clone and validator after a - // flagging-core release contains upstream PR #344 through `41dff20` and the + // flagging-core release contains upstream PR #344 through `9f794c7` and the // no-`BigInt` integer contract is fixed or declared unsupported. That // implementation preserves protobuf integers as `bigint` and validates only // the requested flag data that evaluation reaches. With `BigInt`, it returns a @@ -521,7 +521,7 @@ const normalizeVariationType = ( // TODO(FFL-2837): Delete this legacy UFC v1 metadata fallback after the // flagging-core dependency contains DataDog/openfeature-js-client#344 through -// `41dff20`. The protobuf evaluator maps only safely represented integer +// `9f794c7`. The protobuf evaluator maps only safely represented integer // variations, and all numeric variations, to the OpenFeature type `number`. const recoverVariationType = ( configuration: RulesConfigurationResponse, @@ -548,7 +548,7 @@ export const flaggingCoreRulesEngine: RulesEngine = { // TODO(FFL-2837): Delete this local compatibility guard after the // flagging-core dependency contains DataDog/openfeature-js-client#344 - // through `41dff20`. Keep the reserved-name contract tests. + // through `9f794c7`. Keep the reserved-name contract tests. if (!hasOwn(flags, request.flagKey)) { return { value: request.defaultValue, @@ -559,9 +559,10 @@ export const flaggingCoreRulesEngine: RulesEngine = { } // TODO(FFL-2837): Delete this compatibility check with the local error - // store after the published PR #344 evaluator through `41dff20` validates - // reached flag data and reports deterministic errors, including unsafe - // integer conversions with and without global `BigInt` when supported. + // store after the published PR #344 evaluator through `9f794c7` validates + // reached flag data and reports deterministic flag-scoped errors, including + // unsupported feature levels and unsafe integer conversions with and + // without global `BigInt` when supported. const configurationError = errorsByConfiguration .get(request.configuration) ?.get(request.flagKey); diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index 984a52021..4a4d4e4df 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -23,15 +23,19 @@ import type { // TODO(FFL-2837): Delete the pending `rulesBased` types, reader, and wrappers // after a flagging-core release contains DataDog/openfeature-js-client#344 -// through `41dff20` plus the required SHA digest and no-`BigInt` follow-ups. +// through `9f794c7` plus the required SHA digest and no-`BigInt` follow-ups. // Import and re-export the wire functions and `FlagsConfigurationWire` type from // `@datadog/flagging-core/configuration`. Keep `FlagsConfiguration` and the rules -// evaluator on the package root. Use `FlagsConfiguration.rules`. The distribution -// layer must put one base64 encoding of the raw dd-source#34959 protobuf response -// in the version 1 `rules.response` field. Do not add that service transport or -// envelope construction here. PR #344 preserves decoded protobuf flags and -// protobuf integers. Its evaluator reports invalid reached data and unsafe -// integer conversion as deterministic `PARSE_ERROR` results. +// evaluator on the package root. The new `@datadog/flagging-core/precomputed` +// subpath is protobuf-free, ignores rules, and is not the parser for this module. +// Use `FlagsConfiguration.rules`. The distribution layer must put one base64 +// encoding of the raw dd-source#34959 protobuf response in the version 1 +// `rules.response` field. Record dd-source#40304 commit `071c4ad` as the schema +// revision and dd-source#34959 as the service producer path. Do not add that +// service transport or envelope construction here. PR #344 preserves decoded +// protobuf flags and integers. Its evaluator reports invalid reached data, +// unsupported feature levels, and unsafe integer conversion as deterministic +// flag-scoped `PARSE_ERROR` results. type PendingRulesConfiguration = FlagsConfiguration & { rulesBased?: { response: UniversalFlagConfigurationV1; @@ -83,7 +87,7 @@ export const configurationFromString = (source: string): FlagsConfiguration => { // validator that PR #344 removed in favor of the Protobuf-ES decoder. The // published parser must also include PR #344's unknown-field tolerance, // unknown-field serialization, and lossless integer parsing through - // `41dff20`, plus the final no-`BigInt` runtime decision. + // `9f794c7`, plus the final no-`BigInt` runtime decision. const pendingRules = readPendingRulesWire(source); if (pendingRules) { try { @@ -108,7 +112,7 @@ export const configurationToString = ( const pendingConfiguration = configuration as PendingRulesConfiguration; // TODO(FFL-2837): Delete this legacy serialization wrapper with the pending - // types above after the dependency contains PR #344 through `41dff20` and + // types above after the dependency contains PR #344 through `9f794c7` and // its required follow-ups. The upstream serializer encodes generated protobuf // rules back to base64 and preserves unknown protobuf fields. // This temporary UFC v1 shim serializes its legacy JSON response instead.