From 323ae0990fd10c30a3e120d82c69a682d43a46e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serge=20H=C3=A4nni?= Date: Thu, 16 Jun 2022 15:12:01 +0200 Subject: [PATCH 1/3] Fix webhook request for headers with symbol key E.g. the clearance session is called `:clearance` and it then fails with the error: ``` ShopifyAPITest::Webhooks::WebhookRequestTest#test_with_symbol_headers: NoMethodError: undefined method `sub' for :clearance:Symbol Did you mean? stub ``` --- lib/shopify_api/webhooks/request.rb | 2 +- test/webhooks/request_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/shopify_api/webhooks/request.rb b/lib/shopify_api/webhooks/request.rb index 9539a983a..c283def95 100644 --- a/lib/shopify_api/webhooks/request.rb +++ b/lib/shopify_api/webhooks/request.rb @@ -35,7 +35,7 @@ def parsed_body sig { params(raw_body: String, headers: T::Hash[String, T.untyped]).void } def initialize(raw_body:, headers:) # normalize the headers by forcing lowercase, removing any prepended "http"s, and changing underscores to dashes - headers = headers.to_h { |k, v| [k.downcase.sub("http_", "").gsub("_", "-"), v] } + headers = headers.to_h { |k, v| [k.to_s.downcase.sub("http_", "").gsub("_", "-"), v] } missing_headers = [] [ diff --git a/test/webhooks/request_test.rb b/test/webhooks/request_test.rb index 6daede19c..4f418c70e 100644 --- a/test/webhooks/request_test.rb +++ b/test/webhooks/request_test.rb @@ -21,6 +21,17 @@ def test_error_when_headers_missing ShopifyAPI::Webhooks::Request.new(raw_body: "{}", headers: {}) end end + + def test_with_symbol_headers + headers = { + "HTTP_X_SHOPIFY_TOPIC" => "some/topic", + "HTTP_X_SHOPIFY_HMAC_SHA256" => "some_hmac", + "HTTP_X_SHOPIFY_SHOP_DOMAIN" => "shop.myshopify.com", + :clearance => "session" + } + + assert(ShopifyAPI::Webhooks::Request.new(raw_body: "{}", headers: headers)) + end end end end From 44b75eb4d3419bda0e7f4cae4d7a92e172f37911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serge=20H=C3=A4nni?= Date: Thu, 16 Jun 2022 15:24:09 +0200 Subject: [PATCH 2/3] Add changelog item --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e99c7cea8..9bfd1b8c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api ## Unreleased - [#979](https://github.com/Shopify/shopify_api/pull/979) Update `ShopifyAPI::Context.setup` to take `old_api_secret_key` to support API credentials rotation +- [#977](https://github.com/Shopify/shopify_api/pull/977) Fix webhook requests when a header is present having a symbol key (e.g. `:clearance`) ## Version 10.1.0 From 12abba964c2a72d20ef941ee0b143729c1d35f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serge=20H=C3=A4nni?= Date: Tue, 21 Jun 2022 15:58:06 +0200 Subject: [PATCH 3/3] Fix Rubocop issue --- test/webhooks/request_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/webhooks/request_test.rb b/test/webhooks/request_test.rb index 4f418c70e..c242e520d 100644 --- a/test/webhooks/request_test.rb +++ b/test/webhooks/request_test.rb @@ -27,7 +27,7 @@ def test_with_symbol_headers "HTTP_X_SHOPIFY_TOPIC" => "some/topic", "HTTP_X_SHOPIFY_HMAC_SHA256" => "some_hmac", "HTTP_X_SHOPIFY_SHOP_DOMAIN" => "shop.myshopify.com", - :clearance => "session" + :clearance => "session", } assert(ShopifyAPI::Webhooks::Request.new(raw_body: "{}", headers: headers))