Skip to content

Commit 975bf84

Browse files
committed
fix(datafile-parsing): Prevent newer versions datafile
1 parent ffb910c commit 975bf84

File tree

7 files changed

+28
-40
lines changed

7 files changed

+28
-40
lines changed

lib/optimizely.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,15 @@ def initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = n
6464

6565
begin
6666
@config = ProjectConfig.new(datafile, @logger, @error_handler)
67-
rescue
67+
rescue InvalidDatafileVersionError => e
6868
@is_valid = false
6969
@logger = SimpleLogger.new
70-
@logger.log(Logger::ERROR, InvalidInputError.new('datafile').message)
70+
@logger.log(Logger::ERROR, e.message)
7171
return
72-
end
73-
74-
unless @config.parsing_succeeded?
72+
rescue
7573
@is_valid = false
7674
@logger = SimpleLogger.new
77-
@logger.log(Logger::ERROR, InvalidDatafileVersionError.new.message)
75+
@logger.log(Logger::ERROR, InvalidInputError.new('datafile').message)
7876
return
7977
end
8078

lib/optimizely/exceptions.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2016-2017, Optimizely and contributors
4+
# Copyright 2016-2018, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -85,9 +85,8 @@ def initialize(aborted_method)
8585
class InvalidDatafileVersionError < Error
8686
# Raised when a datafile with an unsupported version is provided
8787

88-
def initialize(msg = 'Provided datafile is an unsupported version. Please use SDK version 1.1.2 or earlier '\
89-
'for datafile version 1.')
90-
super
88+
def initialize(version)
89+
super("This version of the Ruby SDK does not support the given datafile version: #{version}.")
9190
end
9291
end
9392

lib/optimizely/helpers/constants.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ module Constants
321321
'BUCKETING_ID' => '$opt_bucketing_id',
322322
'USER_AGENT' => '$opt_user_agent'
323323
}.freeze
324+
325+
SUPPORTED_VERSIONS = {
326+
'v2' => '2',
327+
'v3' => '3',
328+
'v4' => '4'
329+
}.freeze
324330
end
325331
end
326332
end

lib/optimizely/project_config.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
require_relative 'helpers/validator'
2020

2121
module Optimizely
22-
V1_CONFIG_VERSION = '1'
23-
24-
UNSUPPORTED_VERSIONS = [V1_CONFIG_VERSION].freeze
25-
2622
class ProjectConfig
2723
# Representation of the Optimizely project config.
2824
RUNNING_EXPERIMENT_STATUS = ['Running'].freeze
@@ -39,7 +35,6 @@ class ProjectConfig
3935
attr_reader :experiments
4036
attr_reader :feature_flags
4137
attr_reader :groups
42-
attr_reader :parsing_succeeded
4338
attr_reader :project_id
4439
# Boolean - denotes if Optimizely should remove the last block of visitors' IP address before storing event data
4540
attr_reader :anonymize_ip
@@ -75,12 +70,11 @@ def initialize(datafile, logger, error_handler)
7570

7671
config = JSON.parse(datafile)
7772

78-
@parsing_succeeded = false
7973
@error_handler = error_handler
8074
@logger = logger
8175
@version = config['version']
8276

83-
return if UNSUPPORTED_VERSIONS.include?(@version)
77+
raise InvalidDatafileVersionError, @version unless Helpers::Constants::SUPPORTED_VERSIONS.value?(@version)
8478

8579
@account_id = config['accountId']
8680
@attributes = config.fetch('attributes', [])
@@ -147,7 +141,6 @@ def initialize(datafile, logger, error_handler)
147141
@feature_flag_key_map.each do |key, feature_flag|
148142
@feature_variable_key_map[key] = generate_key_map(feature_flag['variables'], 'key')
149143
end
150-
@parsing_succeeded = true
151144
end
152145

153146
def experiment_running?(experiment)
@@ -389,14 +382,6 @@ def get_attribute_id(attribute_key)
389382
nil
390383
end
391384

392-
def parsing_succeeded?
393-
# Helper method to determine if parsing the datafile was successful.
394-
#
395-
# Returns Boolean depending on whether parsing the datafile succeeded or not.
396-
397-
@parsing_succeeded
398-
end
399-
400385
def variation_id_exists?(experiment_id, variation_id)
401386
# Determines if a given experiment ID / variation ID pair exists in the datafile
402387
#

spec/project_config_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
expect(project_config.groups).to eq(config_body['groups'])
4141
expect(project_config.project_id).to eq(config_body['projectId'])
4242
expect(project_config.revision).to eq(config_body['revision'])
43-
expect(project_config.parsing_succeeded).to be(true)
4443

4544
expected_attribute_key_map = {
4645
'browser_type' => config_body['attributes'][0]
@@ -659,16 +658,6 @@
659658
end
660659
end
661660

662-
describe 'parsing_succeeded?' do
663-
let(:config_body_v2) { OptimizelySpec::VALID_CONFIG_BODY }
664-
let(:config_body_v2_JSON) { OptimizelySpec::VALID_CONFIG_BODY_JSON }
665-
666-
it 'should be true for version 2' do
667-
project_config_v2 = Optimizely::ProjectConfig.new(config_body_v2_JSON, logger, error_handler)
668-
expect(project_config_v2.parsing_succeeded?).to be(true)
669-
end
670-
end
671-
672661
describe '@logger' do
673662
let(:spy_logger) { spy('logger') }
674663
let(:config) { Optimizely::ProjectConfig.new(config_body_JSON, spy_logger, error_handler) }

spec/project_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ def handle_error(error)
6868
expect(instance_with_error_handler.error_handler.handle_error('test_message')). to eq('test_message')
6969
end
7070

71+
it 'should log an error when datafile is null' do
72+
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.')
73+
Optimizely::Project.new(nil)
74+
end
75+
76+
it 'should log an error when datafile is empty' do
77+
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.')
78+
Optimizely::Project.new('')
79+
end
80+
7181
it 'should log an error when given a datafile that does not conform to the schema' do
7282
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.')
7383
Optimizely::Project.new('{"foo": "bar"}')
@@ -109,11 +119,12 @@ class InvalidErrorHandler; end
109119
it 'should log an error when provided an invalid JSON datafile and skip_json_validation is true' do
110120
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.')
111121

112-
Optimizely::Project.new('{"foo": "bar"}', nil, nil, nil, true)
122+
Optimizely::Project.new('{"version": "2", "foo": "bar"}', nil, nil, nil, true)
113123
end
114124

115125
it 'should log an error when provided a datafile of unsupported version' do
116-
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is an unsupported version. Please use SDK version 1.1.2 or earlier for datafile version 1.')
126+
config_body_invalid_json = JSON.parse(config_body_invalid_JSON)
127+
expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, "This version of the Ruby SDK does not support the given datafile version: #{config_body_invalid_json['version']}.")
117128

118129
Optimizely::Project.new(config_body_invalid_JSON, nil, nil, nil, true)
119130
end

spec/spec_params.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,6 @@ module OptimizelySpec
657657
VALID_CONFIG_BODY_JSON = JSON.dump(VALID_CONFIG_BODY)
658658

659659
INVALID_CONFIG_BODY = VALID_CONFIG_BODY.dup
660-
INVALID_CONFIG_BODY['version'] = '1'
660+
INVALID_CONFIG_BODY['version'] = '5'
661661
INVALID_CONFIG_BODY_JSON = JSON.dump(INVALID_CONFIG_BODY)
662662
end

0 commit comments

Comments
 (0)