diff --git a/lib/ldclient-rb/file_data_source.rb b/lib/ldclient-rb/file_data_source.rb index 7606c1d3..cfea75f7 100644 --- a/lib/ldclient-rb/file_data_source.rb +++ b/lib/ldclient-rb/file_data_source.rb @@ -21,9 +21,11 @@ def self.have_listen? end # - # Provides a way to use local files as a source of feature flag state. This would typically be - # used in a test environment, to operate using a predetermined feature flag state without an - # actual LaunchDarkly connection. + # Provides a way to use local files as a source of feature flag state. This allows using a + # predetermined feature flag state without an actual LaunchDarkly connection. + # + # Reading flags from a file is only intended for pre-production environments. Production + # environments should always be configured to receive flag updates from LaunchDarkly. # # To use this component, call {FileDataSource#factory}, and store its return value in the # {Config#data_source} property of your LaunchDarkly client configuration. In the options @@ -206,7 +208,7 @@ def parse_content(content) # We can use the Ruby YAML parser for both YAML and JSON (JSON is a subset of YAML and while # not all YAML parsers handle it correctly, we have verified that the Ruby one does, at least # for all the samples of actual flag data that we've tested). - symbolize_all_keys(YAML.load(content)) + symbolize_all_keys(YAML.safe_load(content)) end def symbolize_all_keys(value) diff --git a/spec/file_data_source_spec.rb b/spec/file_data_source_spec.rb index c827222d..837b775d 100644 --- a/spec/file_data_source_spec.rb +++ b/spec/file_data_source_spec.rb @@ -1,6 +1,14 @@ require "spec_helper" require "tempfile" +# see does not allow Ruby objects in YAML" for the purpose of the following two things +$created_bad_class = false +class BadClassWeShouldNotInstantiate < Hash + def []=(key, value) + $created_bad_class = true + end +end + describe LaunchDarkly::FileDataSource do let(:full_flag_1_key) { "flag1" } let(:full_flag_1_value) { "on" } @@ -78,6 +86,12 @@ EOF } + let(:unsafe_yaml) { <<-EOF +--- !ruby/hash:BadClassWeShouldNotInstantiate +foo: bar +EOF + } + let(:bad_file_path) { "no-such-file" } before do @@ -138,6 +152,20 @@ def with_data_source(options) end end + it "does not allow Ruby objects in YAML" do + # This tests for the vulnerability described here: https://trailofbits.github.io/rubysec/yaml/index.html + # The file we're loading contains a hash with a custom Ruby class, BadClassWeShouldNotInstantiate (see top + # of file). If we're not loading in safe mode, it will create an instance of that class and call its []= + # method, which we've defined to set $created_bad_class to true. In safe mode, it refuses to parse this file. + file = make_temp_file(unsafe_yaml) + with_data_source({ paths: [file.path ] }) do |ds| + event = ds.start + expect(event.set?).to eq(true) + expect(ds.initialized?).to eq(false) + expect($created_bad_class).to eq(false) + end + end + it "sets start event and initialized on successful load" do file = make_temp_file(all_properties_json) with_data_source({ paths: [ file.path ] }) do |ds|