From bcf6f7b4d5c872fa0ad013ecbf9df526e3a95d2d Mon Sep 17 00:00:00 2001 From: Sandeep Tamhankar Date: Wed, 30 Mar 2016 12:31:31 -0700 Subject: [PATCH 1/2] RUBY-165 -- Added input validation around protocol_version cluster option. --- lib/cassandra.rb | 10 ++++++++++ spec/cassandra_spec.rb | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/cassandra.rb b/lib/cassandra.rb index 0e31a4387..02d73728e 100644 --- a/lib/cassandra.rb +++ b/lib/cassandra.rb @@ -633,6 +633,16 @@ def self.validate_and_massage_options(options) end end + if options.key?(:protocol_version) + protocol_version = options[:protocol_version] + unless protocol_version.nil? + Util.assert_instance_of(::Integer, protocol_version) + Util.assert_one_of(1..4, protocol_version) do + ":protocol_version must be a positive integer, #{protocol_version.inspect} given" + end + end + end + if options.key?(:futures_factory) futures_factory = options[:futures_factory] methods = [:error, :value, :promise, :all] diff --git a/spec/cassandra_spec.rb b/spec/cassandra_spec.rb index bcd5ad1a2..5b91e28fb 100644 --- a/spec/cassandra_spec.rb +++ b/spec/cassandra_spec.rb @@ -317,6 +317,16 @@ def unavailable expect(C.validate(page_size: nil)).to eq({ page_size: nil }) end + it 'should validate :protocol_version' do + expect { C.validate(protocol_version: 'a') }.to raise_error(ArgumentError) + expect { C.validate(protocol_version: 0) }.to raise_error(ArgumentError) + expect { C.validate(protocol_version: 1.5) }.to raise_error(ArgumentError) + expect { C.validate(protocol_version: 5) }.to raise_error(ArgumentError) + expect(C.validate(protocol_version: 1)).to eq({ protocol_version: 1 }) + expect(C.validate(protocol_version: 4)).to eq({ protocol_version: 4 }) + expect(C.validate(protocol_version: nil)).to eq({ protocol_version: nil }) + end + it 'should validate :futures_factory option' do class GoodFactory def error From cfeca765887b42ae8d779aaf96a62854d76c5674 Mon Sep 17 00:00:00 2001 From: Sandeep Tamhankar Date: Wed, 30 Mar 2016 12:35:17 -0700 Subject: [PATCH 2/2] RUBY-165 -- Added input validation around protocol_version cluster option. * Disable rubocop warnings around validate_and_massage_options method. --- lib/cassandra.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/cassandra.rb b/lib/cassandra.rb index 02d73728e..edf94a68c 100644 --- a/lib/cassandra.rb +++ b/lib/cassandra.rb @@ -323,6 +323,9 @@ def self.cluster_async(options = {}) # @private SSL_CLASSES = [::TrueClass, ::FalseClass, ::OpenSSL::SSL::SSLContext].freeze + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/CyclomaticComplexity + # rubocop:disable Metrics/PerceivedComplexity # @private def self.validate_and_massage_options(options) options = options.select do |key, _|