From 6d35ea182c053518724ffb573977e4965b6fbec6 Mon Sep 17 00:00:00 2001 From: Jesse Shawl Date: Tue, 10 Apr 2018 16:11:39 -0500 Subject: [PATCH] Reject nil keys When listing charges, if a user specifies a `customer_id`, but `customer_id` is nil, raise a TypeError instead of silently omitting the key and returning charges for any user. --- lib/stripe/util.rb | 8 ++++---- test/stripe/util_test.rb | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index ae9d3b176..463e3f1c4 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -253,7 +253,7 @@ def self.normalize_opts(opts) when String { api_key: opts } when Hash - check_api_key!(opts.fetch(:api_key)) if opts.key?(:api_key) + opts.keys.map { |key| check_key!(opts.fetch(key)) } opts.clone else raise TypeError, "normalize_opts expects a string or a hash" @@ -265,9 +265,9 @@ def self.check_string_argument!(key) key end - def self.check_api_key!(key) - raise TypeError, "api_key must be a string" unless key.is_a?(String) - key + def self.check_key!(value) + raise TypeError, "#{value} must be present" if value.nil? + value end # Normalizes header keys so that they're all lower case and each diff --git a/test/stripe/util_test.rb b/test/stripe/util_test.rb index 90b115ab6..f9e3a1621 100644 --- a/test/stripe/util_test.rb +++ b/test/stripe/util_test.rb @@ -130,6 +130,7 @@ class UtilTest < Test::Unit::TestCase should "#normalize_opts should reject nil keys" do assert_raise { Stripe::Util.normalize_opts(nil) } assert_raise { Stripe::Util.normalize_opts(api_key: nil) } + assert_raise { Stripe::Util.normalize_opts(any_specified_key: nil) } end should "#convert_to_stripe_object should pass through unknown types" do