Skip to content

Commit 7988063

Browse files
mfahadahmedmikeproeng37
authored andcommitted
Input validation in Activate, Track and GetVariation methods. (#91)
1 parent d9c5ada commit 7988063

File tree

7 files changed

+121
-151
lines changed

7 files changed

+121
-151
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ var notificationCenter = require('../core/notification_center');
2424
var projectConfig = require('../core/project_config');
2525
var projectConfigSchema = require('./project_config_schema');
2626
var sprintf = require('sprintf');
27-
var userIdValidator = require('../utils/user_id_validator');
2827
var userProfileServiceValidator = require('../utils/user_profile_service_validator');
28+
var stringValidator = require('../utils/string_value_validator');
2929

3030
var ERROR_MESSAGES = enums.ERROR_MESSAGES;
3131
var LOG_LEVEL = enums.LOG_LEVEL;
@@ -129,6 +129,10 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
129129
return null;
130130
}
131131

132+
if (!this.__validateInputs({experiment_key: experimentKey, user_id: userId}, attributes)) {
133+
return this.__notActivatingExperiment(experimentKey, userId);
134+
}
135+
132136
try {
133137
var variationKey = this.getVariation(experimentKey, userId, attributes);
134138
if (variationKey === null) {
@@ -221,7 +225,7 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
221225
}
222226

223227
try {
224-
if (!this.__validateInputs(userId, attributes, eventTags)) {
228+
if (!this.__validateInputs({user_id: userId, event_key: eventKey}, attributes, eventTags)) {
225229
return;
226230
}
227231

@@ -297,7 +301,7 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes)
297301
}
298302

299303
try {
300-
if (!this.__validateInputs(userId, attributes)) {
304+
if (!this.__validateInputs({experiment_key: experimentKey, user_id: userId}, attributes)) {
301305
return null;
302306
}
303307

@@ -349,15 +353,21 @@ Optimizely.prototype.getForcedVariation = function(experimentKey, userId) {
349353

350354
/**
351355
* Validates user ID and attributes parameters
352-
* @param {string} userId ID of user
356+
* @param {string} stringInputs Map of string keys and associated values
353357
* @param {Object} userAttributes Optional parameter for user's attributes
354358
* @param {Object} eventTags Optional parameter for event tags
355359
* @return {boolean} True if inputs are valid
356360
*
357361
*/
358-
Optimizely.prototype.__validateInputs = function(userId, userAttributes, eventTags) {
362+
Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, eventTags) {
359363
try {
360-
userIdValidator.validate(userId);
364+
var inputKeys = Object.keys(stringInputs);
365+
for (var index=0; index < inputKeys.length; index++) {
366+
var key = inputKeys[index];
367+
if (!stringValidator.validate(stringInputs[key])) {
368+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, key));
369+
}
370+
}
361371
if (userAttributes) {
362372
attributesValidator.validate(userAttributes);
363373
}
@@ -473,12 +483,12 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
473483
return false;
474484
}
475485

476-
var feature = projectConfig.getFeatureFromKey(this.configObj, featureKey, this.logger);
477-
if (!feature) {
486+
if (!this.__validateInputs({feature_key: featureKey, user_id: userId}, attributes)) {
478487
return false;
479488
}
480489

481-
if (!this.__validateInputs(userId, attributes)) {
490+
var feature = projectConfig.getFeatureFromKey(this.configObj, featureKey, this.logger);
491+
if (!feature) {
482492
return false;
483493
}
484494

@@ -546,6 +556,10 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
546556
return null;
547557
}
548558

559+
if (!this.__validateInputs({feature_key: featureKey, variable_key: variableKey, user_id: userId}, attributes)) {
560+
return null;
561+
}
562+
549563
var featureFlag = projectConfig.getFeatureFromKey(this.configObj, featureKey, this.logger);
550564
if (!featureFlag) {
551565
return null;
@@ -561,10 +575,6 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
561575
return null;
562576
}
563577

564-
if (!this.__validateInputs(userId, attributes)) {
565-
return null;
566-
}
567-
568578
var decision = this.decisionService.getVariationForFeature(featureFlag, userId, attributes);
569579
var variableValue;
570580
if (decision.variation !== null) {

0 commit comments

Comments
 (0)