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..81190c964 --- /dev/null +++ b/packages/core/src/flags/configuration/__tests__/__utils__/rulesTestUtils.ts @@ -0,0 +1,81 @@ +/* + * 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, + 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] + >; +} + +// 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 => { + 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..496d7c42a --- /dev/null +++ b/packages/core/src/flags/configuration/__tests__/rules.test.ts @@ -0,0 +1,327 @@ +/* + * 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('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); + + 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('preserves a flag with an unsupported operator and reports PARSE_ERROR', () => { + 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 = 'FUTURE_OPERATOR'; + + const prepared = prepareRulesConfiguration(source); + + expect(prepared.status).toBe('ready'); + if (prepared.status !== 'ready') { + throw new Error(prepared.errorMessage); + } + 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('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; + if (!conditions) { + throw new Error('The fixture has no conditions.'); + } + conditions[0] = { + operator: 'MATCHES', + attribute: 'country', + value: '[' + } as typeof conditions[number]; + + 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' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ + errorCode: 'PARSE_ERROR', + errorMessage: 'A regular expression condition is not valid.' + }); + }); + + 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'; + + 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' }, + logger: getNoopRulesLogger() + }) + ).toMatchObject({ + errorCode: 'PARSE_ERROR', + errorMessage: 'A split has an invalid variation key.' + }); + }); + + 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'; + 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([ + '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', () => { + 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 + } + }); + }); + + 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 => { + 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({ + 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..dc81e8a4b 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', @@ -90,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, @@ -125,3 +141,72 @@ describe('configurationToString round-trip', () => { ); }); }); + +describe('temporary rules configuration wire compatibility', () => { + it('parses a legacy 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); + }); + + it('does not serialize a rules configuration', () => { + const configuration = { + rulesBased: { + response: buildRulesConfiguration() + } + }; + + expect(() => + configurationToString( + (configuration as unknown) as ParsedFlagsConfiguration + ) + ).toThrow( + 'Rules configurations cannot be serialized to the wire format' + ); + }); + + 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('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 new file mode 100644 index 000000000..8e5d7a528 --- /dev/null +++ b/packages/core/src/flags/configuration/rules.ts @@ -0,0 +1,587 @@ +/* + * 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'; + +// 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. PR #344 +// now preserves invalid flags and reports their stored errors during evaluation. +type RulesConfigurationResponse = UniversalFlagConfigurationV1; + +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; +} + +export interface RulesEvaluationDetails { + value: T; + reason?: string; + variant?: string; + errorCode?: string; + errorMessage?: string; + metadata: RulesEvaluationMetadata; +} + +export interface RulesEvaluationRequest { + configuration: RulesConfigurationResponse; + 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: RulesConfigurationResponse, + 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); + +// 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); + +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): 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. + 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.'; + } + + 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 validateRulesConfigurationEnvelope = ( + 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.'; + } + + 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: RulesConfigurationResponse; + } + | { + 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): 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. + const errorMessage = validateRulesConfigurationEnvelope(clone); + if (errorMessage) { + return { status: 'error', errorMessage }; + } + + const configuration = clone as RulesConfigurationResponse; + const flags = configuration.flags; + const errors = new Map(); + for (const [flagKey, flag] of Object.entries(flags)) { + const flagError = validateFlag(flag); + if (flagError) { + errors.set(flagKey, flagError); + } + } + + freezeValue(clone); + if (errors.size > 0) { + errorsByConfiguration.set(configuration, errors); + } + return { + status: 'ready', + configuration + }; +}; + +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; + } +}; + +// 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: RulesConfigurationResponse, + flagKey: string +): RulesValueType | undefined => { + const flags = configuration.flags as Record; + if (!hasOwn(flags, flagKey)) { + return undefined; + } + + const flag = flags[flagKey]; + if (!isRecord(flag)) { + return undefined; + } + + return normalizeVariationType(flag.variationType); +}; + +export const flaggingCoreRulesEngine: RulesEngine = { + evaluate( + 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, + reason: 'ERROR', + errorCode: 'FLAG_NOT_FOUND', + metadata: {} + }; + } + + // 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, + 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; + return { + value: result.value, + reason: result.reason, + variant: result.variant, + errorCode: result.errorCode, + errorMessage: result.errorMessage, + metadata: { + allocationKey, + variationType: + normalizeVariationType(rawMetadata.variationType) ?? + recoverVariationType( + request.configuration, + request.flagKey + ), + doLog: + typeof rawMetadata.doLog === 'boolean' + ? rawMetadata.doLog + : typeof rawMetadata.__dd_do_log === 'boolean' + ? rawMetadata.__dd_do_log + : undefined + } + }; + } +}; diff --git a/packages/core/src/flags/configuration/wire.ts b/packages/core/src/flags/configuration/wire.ts index 952636fd0..cfbef5abc 100644 --- a/packages/core/src/flags/configuration/wire.ts +++ b/packages/core/src/flags/configuration/wire.ts @@ -4,12 +4,113 @@ * Copyright 2016-Present Datadog, Inc. */ -// 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 +// 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. +// `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'; + +// TODO(FFL-2837): Delete the pending `rulesBased` types, reader, and wrappers +// after a flagging-core release contains DataDog/openfeature-js-client#344. +// 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. +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 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. 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. + const pendingRules = readPendingRulesWire(source); + if (pendingRules) { + try { + configuration.rulesBased = { + ...pendingRules, + response: JSON.parse(pendingRules.response) + }; + } catch { + return configuration; + } + } + + return configuration; +}; + +/** + * Use flagging-core to serialize a parsed configuration. + */ +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`. 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' + ); + } + + return coreConfigurationToString(configuration); +};