Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 39ce59b

Browse files
authored
fix (typed audience evaluation): When evaluating exact match condition, properly check type of condition value (optimizely#178)
Fixes a bug in evaluation of exact match conditions. Before this change, evaluation of a condition with a wrongly-typed value property could return a boolean, when it should abort and return null. With this change, when condition.value of an exact match condition is not a string, boolean, or finite number, the condition will evaluate to null.
1 parent cb9d500 commit 39ce59b

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function exactEvaluator(condition, userAttributes) {
8989
var userValueType = typeof userValue;
9090

9191
if (!isValueValidForExactConditions(userValue) ||
92-
!isValueValidForExactConditions(conditionValueType) ||
92+
!isValueValidForExactConditions(conditionValue) ||
9393
conditionValueType !== userValueType) {
9494
return null;
9595
}

packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.tests.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ describe('lib/core/custom_attribute_condition_evaluator', function() {
190190
var result = customAttributeEvaluator.evaluate(exactNumberCondition, {});
191191
assert.isNull(result);
192192
});
193+
194+
it('should return null if the condition value is not finite', function() {
195+
var invalidValueCondition = {
196+
match: 'exact',
197+
name: 'lasers_count',
198+
type: 'custom_attribute',
199+
value: Infinity,
200+
};
201+
var result = customAttributeEvaluator.evaluate(invalidValueCondition, { lasers_count: 9000 });
202+
assert.isNull(result);
203+
});
193204
});
194205

195206
describe('with a boolean condition value', function() {
@@ -279,22 +290,43 @@ describe('lib/core/custom_attribute_condition_evaluator', function() {
279290
assert.isFalse(result);
280291
});
281292

282-
it('should return null if the user-provided value is not a number', function() {
293+
it('should return null if the user-provided value is not a finite number', function() {
283294
var result = customAttributeEvaluator.evaluate(gtCondition, {
284295
meters_travelled: 'a long way',
285296
});
286297
assert.isNull(result);
287298

288-
var result = customAttributeEvaluator.evaluate(gtCondition, {
299+
result = customAttributeEvaluator.evaluate(gtCondition, {
289300
meters_travelled: '1000',
290301
});
291302
assert.isNull(result);
303+
304+
result = customAttributeEvaluator.evaluate(gtCondition, {
305+
meters_travelled: Infinity,
306+
});
307+
assert.isNull(result);
292308
});
293309

294310
it('should return null if there is no user-provided value', function() {
295311
var result = customAttributeEvaluator.evaluate(gtCondition, {});
296312
assert.isNull(result);
297313
});
314+
315+
it('should return null if the condition value is not a finite number', function() {
316+
var userAttributes = { meters_travelled: 58.4 };
317+
var invalidValueCondition = {
318+
match: 'gt',
319+
name: 'meters_travelled',
320+
type: 'custom_attribute',
321+
value: Infinity,
322+
};
323+
var result = customAttributeEvaluator.evaluate(invalidValueCondition, userAttributes);
324+
assert.isNull(result);
325+
326+
invalidValueCondition.value = null;
327+
result = customAttributeEvaluator.evaluate(invalidValueCondition, userAttributes);
328+
assert.isNull(result);
329+
});
298330
});
299331

300332
describe('less than match type', function() {
@@ -319,21 +351,42 @@ describe('lib/core/custom_attribute_condition_evaluator', function() {
319351
assert.isFalse(result);
320352
});
321353

322-
it('should return null if the user-provided value is not a number', function() {
354+
it('should return null if the user-provided value is not a finite number', function() {
323355
var result = customAttributeEvaluator.evaluate(ltCondition, {
324356
meters_travelled: true,
325357
});
326358
assert.isNull(result);
327359

328-
var result = customAttributeEvaluator.evaluate(ltCondition, {
360+
result = customAttributeEvaluator.evaluate(ltCondition, {
329361
meters_travelled: '48.2',
330362
});
331363
assert.isNull(result);
364+
365+
result = customAttributeEvaluator.evaluate(ltCondition, {
366+
meters_travelled: Infinity,
367+
});
368+
assert.isNull(result);
332369
});
333370

334371
it('should return null if there is no user-provided value', function() {
335372
var result = customAttributeEvaluator.evaluate(ltCondition, {});
336373
assert.isNull(result);
337374
});
375+
376+
it('should return null if the condition value is not a finite number', function() {
377+
var userAttributes = { meters_travelled: 10 };
378+
var invalidValueCondition = {
379+
match: 'lt',
380+
name: 'meters_travelled',
381+
type: 'custom_attribute',
382+
value: Infinity,
383+
};
384+
var result = customAttributeEvaluator.evaluate(invalidValueCondition, userAttributes);
385+
assert.isNull(result);
386+
387+
invalidValueCondition.value = {};
388+
result = customAttributeEvaluator.evaluate(invalidValueCondition, userAttributes);
389+
assert.isNull(result);
390+
});
338391
});
339392
});

0 commit comments

Comments
 (0)