Skip to content

Input validation in Activate, Track and GetVariation methods. #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var notificationCenter = require('../core/notification_center');
var projectConfig = require('../core/project_config');
var projectConfigSchema = require('./project_config_schema');
var sprintf = require('sprintf');
var userIdValidator = require('../utils/user_id_validator');
var userProfileServiceValidator = require('../utils/user_profile_service_validator');
var stringValidator = require('../utils/string_value_validator');

var ERROR_MESSAGES = enums.ERROR_MESSAGES;
var LOG_LEVEL = enums.LOG_LEVEL;
Expand Down Expand Up @@ -129,6 +129,10 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
return null;
}

if (!this.__validateInputs({experiment_key: experimentKey, user_id: userId}, attributes)) {
return this.__notActivatingExperiment(experimentKey, userId);
}

try {
var variationKey = this.getVariation(experimentKey, userId, attributes);
if (variationKey === null) {
Expand Down Expand Up @@ -221,7 +225,7 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
}

try {
if (!this.__validateInputs(userId, attributes, eventTags)) {
if (!this.__validateInputs({user_id: userId, event_key: eventKey}, attributes, eventTags)) {
return;
}

Expand Down Expand Up @@ -297,7 +301,7 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes)
}

try {
if (!this.__validateInputs(userId, attributes)) {
if (!this.__validateInputs({experiment_key: experimentKey, user_id: userId}, attributes)) {
return null;
}

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

/**
* Validates user ID and attributes parameters
* @param {string} userId ID of user
* @param {string} stringInputs Map of string keys and associated values
* @param {Object} userAttributes Optional parameter for user's attributes
* @param {Object} eventTags Optional parameter for event tags
* @return {boolean} True if inputs are valid
*
*/
Optimizely.prototype.__validateInputs = function(userId, userAttributes, eventTags) {
Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, eventTags) {
try {
userIdValidator.validate(userId);
var inputKeys = Object.keys(stringInputs);
for (var index=0; index < inputKeys.length; index++) {
var key = inputKeys[index];
if (!stringValidator.validate(stringInputs[key])) {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, key));
}
}
if (userAttributes) {
attributesValidator.validate(userAttributes);
}
Expand Down Expand Up @@ -473,12 +483,12 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
return false;
}

var feature = projectConfig.getFeatureFromKey(this.configObj, featureKey, this.logger);
if (!feature) {
if (!this.__validateInputs({feature_key: featureKey, user_id: userId}, attributes)) {
return false;
}

if (!this.__validateInputs(userId, attributes)) {
var feature = projectConfig.getFeatureFromKey(this.configObj, featureKey, this.logger);
if (!feature) {
return false;
}

Expand Down Expand Up @@ -546,6 +556,10 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
return null;
}

if (!this.__validateInputs({feature_key: featureKey, variable_key: variableKey, user_id: userId}, attributes)) {
return null;
}

var featureFlag = projectConfig.getFeatureFromKey(this.configObj, featureKey, this.logger);
if (!featureFlag) {
return null;
Expand All @@ -561,10 +575,6 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
return null;
}

if (!this.__validateInputs(userId, attributes)) {
return null;
}

var decision = this.decisionService.getVariationForFeature(featureFlag, userId, attributes);
var variableValue;
if (decision.variation !== null) {
Expand Down
Loading