shopify_app 7.2.0 → 17.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +5 -5
  2. data/.babelrc +5 -0
  3. data/.github/CODEOWNERS +1 -0
  4. data/.github/ISSUE_TEMPLATE/bug-report.md +63 -0
  5. data/.github/ISSUE_TEMPLATE/config.yml +1 -0
  6. data/.github/ISSUE_TEMPLATE/feature-request.md +33 -0
  7. data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  8. data/.github/probots.yml +2 -0
  9. data/.github/workflows/build.yml +38 -0
  10. data/.github/workflows/release.yml +24 -0
  11. data/.github/workflows/rubocop.yml +22 -0
  12. data/.gitignore +4 -1
  13. data/.nvmrc +1 -0
  14. data/.rubocop.yml +18 -0
  15. data/.ruby-version +1 -0
  16. data/CHANGELOG.md +465 -0
  17. data/CONTRIBUTING.md +76 -0
  18. data/Gemfile +7 -0
  19. data/Gemfile.lock +256 -0
  20. data/README.md +73 -288
  21. data/Rakefile +1 -0
  22. data/SECURITY.md +59 -0
  23. data/app/assets/images/storage_access.svg +1 -0
  24. data/app/assets/javascripts/shopify_app/enable_cookies.js +3 -0
  25. data/app/assets/javascripts/shopify_app/itp_helper.js +40 -0
  26. data/app/assets/javascripts/shopify_app/partition_cookies.js +8 -0
  27. data/app/assets/javascripts/shopify_app/redirect.js +33 -0
  28. data/app/assets/javascripts/shopify_app/request_storage_access.js +3 -0
  29. data/app/assets/javascripts/shopify_app/storage_access.js +154 -0
  30. data/app/assets/javascripts/shopify_app/storage_access_redirect.js +17 -0
  31. data/app/assets/javascripts/shopify_app/top_level.js +2 -0
  32. data/app/assets/javascripts/shopify_app/top_level_interaction.js +11 -0
  33. data/app/controllers/concerns/shopify_app/authenticated.rb +16 -0
  34. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +26 -0
  35. data/app/controllers/concerns/shopify_app/require_known_shop.rb +39 -0
  36. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +32 -0
  37. data/app/controllers/shopify_app/authenticated_controller.rb +5 -5
  38. data/app/controllers/shopify_app/callback_controller.rb +196 -0
  39. data/app/controllers/shopify_app/extension_verification_controller.rb +15 -0
  40. data/app/controllers/shopify_app/sessions_controller.rb +190 -2
  41. data/app/controllers/shopify_app/webhooks_controller.rb +16 -7
  42. data/app/views/shopify_app/partials/_button_styles.html.erb +109 -0
  43. data/app/views/shopify_app/partials/_card_styles.html.erb +33 -0
  44. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +98 -0
  45. data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
  46. data/app/views/shopify_app/partials/_layout_styles.html.erb +182 -0
  47. data/app/views/shopify_app/partials/_typography_styles.html.erb +35 -0
  48. data/app/views/shopify_app/sessions/enable_cookies.html.erb +70 -0
  49. data/app/views/shopify_app/sessions/new.html.erb +39 -83
  50. data/app/views/shopify_app/sessions/request_storage_access.html.erb +68 -0
  51. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +63 -0
  52. data/app/views/shopify_app/shared/redirect.html.erb +23 -0
  53. data/config/locales/cs.yml +23 -0
  54. data/config/locales/da.yml +20 -0
  55. data/config/locales/de.yml +22 -0
  56. data/config/locales/en.yml +12 -1
  57. data/config/locales/es.yml +21 -3
  58. data/config/locales/fi.yml +20 -0
  59. data/config/locales/fr.yml +23 -0
  60. data/config/locales/hi.yml +23 -0
  61. data/config/locales/it.yml +21 -0
  62. data/config/locales/ja.yml +17 -0
  63. data/config/locales/ko.yml +19 -0
  64. data/config/locales/ms.yml +22 -0
  65. data/config/locales/nb.yml +21 -0
  66. data/config/locales/nl.yml +21 -0
  67. data/config/locales/pl.yml +21 -0
  68. data/config/locales/pt-BR.yml +21 -0
  69. data/config/locales/pt-PT.yml +22 -0
  70. data/config/locales/sv.yml +21 -0
  71. data/config/locales/th.yml +20 -0
  72. data/config/locales/tr.yml +22 -0
  73. data/config/locales/vi.yml +22 -0
  74. data/config/locales/zh-CN.yml +16 -0
  75. data/config/locales/zh-TW.yml +16 -0
  76. data/config/routes.rb +12 -1
  77. data/docs/Quickstart.md +31 -0
  78. data/docs/Releasing.md +21 -0
  79. data/docs/Troubleshooting.md +16 -0
  80. data/docs/Upgrading.md +110 -0
  81. data/docs/shopify_app/authentication.md +124 -0
  82. data/docs/shopify_app/engine.md +82 -0
  83. data/docs/shopify_app/generators.md +127 -0
  84. data/docs/shopify_app/handling-access-scopes-changes.md +8 -0
  85. data/docs/shopify_app/script-tags.md +28 -0
  86. data/docs/shopify_app/session-repository.md +88 -0
  87. data/docs/shopify_app/testing.md +38 -0
  88. data/docs/shopify_app/webhooks.md +72 -0
  89. data/karma.conf.js +44 -0
  90. data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +47 -0
  91. data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +11 -0
  92. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +40 -0
  93. data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +62 -0
  94. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +5 -4
  95. data/lib/generators/shopify_app/add_webhook/templates/{webhook_job.rb → webhook_job.rb.tt} +5 -0
  96. data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +4 -3
  97. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +3 -3
  98. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +10 -9
  99. data/lib/generators/shopify_app/app_proxy_controller/templates/index.html.erb +2 -2
  100. data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +15 -0
  101. data/lib/generators/shopify_app/authenticated_controller/templates/authenticated_controller.rb +5 -0
  102. data/lib/generators/shopify_app/controllers/controllers_generator.rb +2 -1
  103. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +31 -9
  104. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +6 -1
  105. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +70 -6
  106. data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +11 -0
  107. data/lib/generators/shopify_app/install/install_generator.rb +78 -27
  108. data/lib/generators/shopify_app/install/templates/_flash_messages.html.erb +1 -13
  109. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +12 -11
  110. data/lib/generators/shopify_app/install/templates/flash_messages.js +24 -0
  111. data/lib/generators/shopify_app/install/templates/omniauth.rb +3 -1
  112. data/lib/generators/shopify_app/install/templates/session_store.rb +4 -0
  113. data/lib/generators/shopify_app/install/templates/shopify_app.js +15 -0
  114. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +25 -0
  115. data/lib/generators/shopify_app/install/templates/shopify_app_index.js +2 -0
  116. data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
  117. data/lib/generators/shopify_app/install/templates/user_agent.rb +6 -0
  118. data/lib/generators/shopify_app/products_controller/products_controller_generator.rb +19 -0
  119. data/lib/generators/shopify_app/products_controller/templates/products_controller.rb +8 -0
  120. data/lib/generators/shopify_app/rotate_shopify_token_job/rotate_shopify_token_job_generator.rb +16 -0
  121. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token.rake +17 -0
  122. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token_job.rb +42 -0
  123. data/lib/generators/shopify_app/routes/routes_generator.rb +1 -0
  124. data/lib/generators/shopify_app/routes/templates/routes.rb +10 -9
  125. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +42 -14
  126. data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
  127. data/lib/generators/shopify_app/shop_model/templates/db/migrate/{create_shops.rb → create_shops.erb} +1 -1
  128. data/lib/generators/shopify_app/shop_model/templates/shop.rb +6 -2
  129. data/lib/generators/shopify_app/shopify_app_generator.rb +5 -3
  130. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
  131. data/lib/generators/shopify_app/user_model/templates/db/migrate/create_users.erb +16 -0
  132. data/lib/generators/shopify_app/user_model/templates/user.rb +8 -0
  133. data/lib/generators/shopify_app/user_model/templates/users.yml +4 -0
  134. data/lib/generators/shopify_app/user_model/user_model_generator.rb +70 -0
  135. data/lib/generators/shopify_app/views/views_generator.rb +2 -1
  136. data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
  137. data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
  138. data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
  139. data/lib/shopify_app/configuration.rb +69 -5
  140. data/lib/shopify_app/{app_proxy_verification.rb → controller_concerns/app_proxy_verification.rb} +4 -9
  141. data/lib/shopify_app/controller_concerns/csrf_protection.rb +15 -0
  142. data/lib/shopify_app/controller_concerns/embedded_app.rb +20 -0
  143. data/lib/shopify_app/controller_concerns/itp.rb +45 -0
  144. data/lib/shopify_app/controller_concerns/localization.rb +23 -0
  145. data/lib/shopify_app/controller_concerns/login_protection.rb +244 -0
  146. data/lib/shopify_app/controller_concerns/payload_verification.rb +24 -0
  147. data/lib/shopify_app/controller_concerns/webhook_verification.rb +23 -0
  148. data/lib/shopify_app/engine.rb +40 -0
  149. data/lib/shopify_app/jobs/scripttags_manager_job.rb +16 -0
  150. data/lib/shopify_app/{webhooks_manager_job.rb → jobs/webhooks_manager_job.rb} +3 -2
  151. data/lib/shopify_app/{scripttags_manager.rb → managers/scripttags_manager.rb} +25 -8
  152. data/lib/shopify_app/{webhooks_manager.rb → managers/webhooks_manager.rb} +6 -5
  153. data/lib/shopify_app/middleware/jwt_middleware.rb +42 -0
  154. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +34 -0
  155. data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
  156. data/lib/shopify_app/session/in_memory_session_store.rb +31 -0
  157. data/lib/shopify_app/session/in_memory_shop_session_store.rb +16 -0
  158. data/lib/shopify_app/session/in_memory_user_session_store.rb +16 -0
  159. data/lib/shopify_app/session/jwt.rb +63 -0
  160. data/lib/shopify_app/session/null_user_session_store.rb +22 -0
  161. data/lib/shopify_app/session/session_repository.rb +56 -0
  162. data/lib/shopify_app/session/session_storage.rb +20 -0
  163. data/lib/shopify_app/session/shop_session_storage.rb +42 -0
  164. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +58 -0
  165. data/lib/shopify_app/session/user_session_storage.rb +42 -0
  166. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +58 -0
  167. data/lib/shopify_app/test_helpers/all.rb +2 -0
  168. data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +17 -0
  169. data/lib/shopify_app/utils.rb +24 -4
  170. data/lib/shopify_app/version.rb +2 -1
  171. data/lib/shopify_app.rb +65 -24
  172. data/package.json +27 -0
  173. data/service.yml +7 -0
  174. data/shipit.rubygems.yml +3 -0
  175. data/shopify_app.gemspec +20 -9
  176. data/translation.yml +7 -0
  177. data/webpack.config.js +24 -0
  178. data/yarn.lock +5215 -0
  179. metadata +274 -43
  180. data/.travis.yml +0 -17
  181. data/Gemfile.rails50 +0 -5
  182. data/Gemfile.ruby22 +0 -6
  183. data/Gemfile.ruby22.rails50 +0 -9
  184. data/ISSUE_TEMPLATE.md +0 -14
  185. data/QUICKSTART.md +0 -72
  186. data/RELEASING +0 -13
  187. data/lib/generators/shopify_app/home_controller/templates/shopify_app_ready_script.html.erb +0 -11
  188. data/lib/generators/shopify_app/install/templates/shopify_app.rb +0 -9
  189. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -4
  190. data/lib/generators/shopify_app/install/templates/shopify_session_repository.rb +0 -23
  191. data/lib/generators/shopify_app/shop_model/templates/shopify_session_repository.rb +0 -7
  192. data/lib/shopify_app/in_memory_session_store.rb +0 -25
  193. data/lib/shopify_app/login_protection.rb +0 -103
  194. data/lib/shopify_app/scripttags_manager_job.rb +0 -15
  195. data/lib/shopify_app/session_storage.rb +0 -23
  196. data/lib/shopify_app/sessions_concern.rb +0 -101
  197. data/lib/shopify_app/shop.rb +0 -15
  198. data/lib/shopify_app/shopify_session_repository.rb +0 -34
  199. data/lib/shopify_app/webhook_verification.rb +0 -39
@@ -1,19 +1,14 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  module AppProxyVerification
3
4
  extend ActiveSupport::Concern
4
-
5
5
  included do
6
- if Rails.version >= '5.0'
7
- skip_before_action :verify_authenticity_token, raise: false
8
- else
9
- skip_before_action :verify_authenticity_token
10
- end
11
-
6
+ skip_before_action :verify_authenticity_token, raise: false
12
7
  before_action :verify_proxy_request
13
8
  end
14
9
 
15
10
  def verify_proxy_request
16
- return head :unauthorized unless query_string_valid?(request.query_string)
11
+ return head(:forbidden) unless query_string_valid?(request.query_string)
17
12
  end
18
13
 
19
14
  private
@@ -31,7 +26,7 @@ module ShopifyApp
31
26
  end
32
27
 
33
28
  def calculated_signature(query_hash_without_signature)
34
- sorted_params = query_hash_without_signature.collect{|k,v| "#{k}=#{Array(v).join(',')}"}.sort.join
29
+ sorted_params = query_hash_without_signature.collect { |k, v| "#{k}=#{Array(v).join(',')}" }.sort.join
35
30
 
36
31
  OpenSSL::HMAC.hexdigest(
37
32
  OpenSSL::Digest.new('sha256'),
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module CsrfProtection
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ protect_from_forgery with: :exception, unless: :valid_session_token?
7
+ end
8
+
9
+ private
10
+
11
+ def valid_session_token?
12
+ request.env['jwt.shopify_domain']
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module EmbeddedApp
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ if ShopifyApp.configuration.embedded_app?
8
+ after_action(:set_esdk_headers)
9
+ layout('embedded_app')
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def set_esdk_headers
16
+ response.set_header('P3P', 'CP="Not used"')
17
+ response.headers.except!('X-Frame-Options')
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApp
4
+ # Cookie management helpers required for ITP implementation
5
+ module Itp
6
+ private
7
+
8
+ def set_test_cookie
9
+ return unless ShopifyApp.configuration.embedded_app?
10
+ return unless user_agent_can_partition_cookies
11
+
12
+ session['shopify.cookies_persist'] = true
13
+ end
14
+
15
+ def set_top_level_oauth_cookie
16
+ session['shopify.top_level_oauth'] = true
17
+ end
18
+
19
+ def clear_top_level_oauth_cookie
20
+ session.delete('shopify.top_level_oauth')
21
+ end
22
+
23
+ def user_agent_is_mobile
24
+ user_agent = BrowserSniffer.new(request.user_agent).browser_info
25
+
26
+ user_agent[:name].to_s.match(/Shopify\sMobile/)
27
+ end
28
+
29
+ def user_agent_is_pos
30
+ user_agent = BrowserSniffer.new(request.user_agent).browser_info
31
+
32
+ user_agent[:name].to_s.match(/Shopify\sPOS/)
33
+ end
34
+
35
+ def user_agent_can_partition_cookies
36
+ user_agent = BrowserSniffer.new(request.user_agent).browser_info
37
+
38
+ is_safari = user_agent[:name].to_s.match(/Safari/)
39
+
40
+ return false unless is_safari
41
+
42
+ user_agent[:version].to_s.match(/12\.0/)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module Localization
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before_action :set_locale
8
+ end
9
+
10
+ private
11
+
12
+ def set_locale
13
+ if params[:locale]
14
+ session[:locale] = params[:locale]
15
+ else
16
+ session[:locale] ||= I18n.default_locale
17
+ end
18
+ I18n.locale = session[:locale]
19
+ rescue I18n::InvalidLocale
20
+ I18n.locale = I18n.default_locale
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,244 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'browser_sniffer'
4
+
5
+ module ShopifyApp
6
+ module LoginProtection
7
+ extend ActiveSupport::Concern
8
+ include ShopifyApp::Itp
9
+
10
+ class ShopifyDomainNotFound < StandardError; end
11
+
12
+ included do
13
+ after_action :set_test_cookie
14
+ rescue_from ActiveResource::UnauthorizedAccess, with: :close_session
15
+ end
16
+
17
+ ACCESS_TOKEN_REQUIRED_HEADER = 'X-Shopify-API-Request-Failure-Unauthorized'
18
+
19
+ def activate_shopify_session
20
+ if user_session_expected? && user_session.blank?
21
+ signal_access_token_required
22
+ return redirect_to_login
23
+ end
24
+
25
+ return redirect_to_login if current_shopify_session.blank?
26
+
27
+ clear_top_level_oauth_cookie
28
+
29
+ begin
30
+ ShopifyAPI::Base.activate_session(current_shopify_session)
31
+ yield
32
+ ensure
33
+ ShopifyAPI::Base.clear_session
34
+ end
35
+ end
36
+
37
+ def current_shopify_session
38
+ @current_shopify_session ||= begin
39
+ user_session || shop_session
40
+ end
41
+ end
42
+
43
+ def user_session
44
+ user_session_by_jwt || user_session_by_cookie
45
+ end
46
+
47
+ def user_session_by_jwt
48
+ return unless ShopifyApp.configuration.allow_jwt_authentication
49
+ return unless jwt_shopify_user_id
50
+ ShopifyApp::SessionRepository.retrieve_user_session_by_shopify_user_id(jwt_shopify_user_id)
51
+ end
52
+
53
+ def user_session_by_cookie
54
+ return unless ShopifyApp.configuration.allow_cookie_authentication
55
+ return unless session[:user_id].present?
56
+ ShopifyApp::SessionRepository.retrieve_user_session(session[:user_id])
57
+ end
58
+
59
+ def shop_session
60
+ shop_session_by_jwt || shop_session_by_cookie
61
+ end
62
+
63
+ def shop_session_by_jwt
64
+ return unless ShopifyApp.configuration.allow_jwt_authentication
65
+ return unless jwt_shopify_domain
66
+ ShopifyApp::SessionRepository.retrieve_shop_session_by_shopify_domain(jwt_shopify_domain)
67
+ end
68
+
69
+ def shop_session_by_cookie
70
+ return unless ShopifyApp.configuration.allow_cookie_authentication
71
+ return unless session[:shop_id].present?
72
+ ShopifyApp::SessionRepository.retrieve_shop_session(session[:shop_id])
73
+ end
74
+
75
+ def login_again_if_different_user_or_shop
76
+ if session[:user_session].present? && params[:session].present? # session data was sent/stored correctly
77
+ clear_session = session[:user_session] != params[:session] # current user is different from stored user
78
+ end
79
+
80
+ if current_shopify_session &&
81
+ params[:shop] && params[:shop].is_a?(String) &&
82
+ (current_shopify_session.domain != params[:shop])
83
+ clear_session = true
84
+ end
85
+
86
+ if clear_session
87
+ clear_shopify_session
88
+ redirect_to_login
89
+ end
90
+ end
91
+
92
+ def signal_access_token_required
93
+ response.set_header(ACCESS_TOKEN_REQUIRED_HEADER, "true")
94
+ end
95
+
96
+ protected
97
+
98
+ def jwt_shopify_domain
99
+ request.env['jwt.shopify_domain']
100
+ end
101
+
102
+ def jwt_shopify_user_id
103
+ request.env['jwt.shopify_user_id']
104
+ end
105
+
106
+ def redirect_to_login
107
+ if request.xhr?
108
+ head(:unauthorized)
109
+ else
110
+ if request.get?
111
+ path = request.path
112
+ query = sanitized_params.to_query
113
+ else
114
+ referer = URI(request.referer || "/")
115
+ path = referer.path
116
+ query = "#{referer.query}&#{sanitized_params.to_query}"
117
+ end
118
+ session[:return_to] = query.blank? ? path.to_s : "#{path}?#{query}"
119
+ redirect_to(login_url_with_optional_shop)
120
+ end
121
+ end
122
+
123
+ def close_session
124
+ clear_shopify_session
125
+ redirect_to(login_url_with_optional_shop)
126
+ end
127
+
128
+ def clear_shopify_session
129
+ session[:shop_id] = nil
130
+ session[:user_id] = nil
131
+ session[:shopify_domain] = nil
132
+ session[:shopify_user] = nil
133
+ session[:user_session] = nil
134
+ end
135
+
136
+ def login_url_with_optional_shop(top_level: false)
137
+ url = ShopifyApp.configuration.login_url
138
+
139
+ query_params = login_url_params(top_level: top_level)
140
+
141
+ url = "#{url}?#{query_params.to_query}" if query_params.present?
142
+ url
143
+ end
144
+
145
+ def login_url_params(top_level:)
146
+ query_params = {}
147
+ query_params[:shop] = sanitized_params[:shop] if params[:shop].present?
148
+
149
+ return_to = RedirectSafely.make_safe(session[:return_to] || params[:return_to], nil)
150
+
151
+ if return_to.present? && return_to_param_required?
152
+ query_params[:return_to] = return_to
153
+ end
154
+
155
+ has_referer_shop_name = referer_sanitized_shop_name.present?
156
+
157
+ if has_referer_shop_name
158
+ query_params[:shop] ||= referer_sanitized_shop_name
159
+ end
160
+
161
+ query_params[:top_level] = true if top_level
162
+ query_params
163
+ end
164
+
165
+ def return_to_param_required?
166
+ native_params = %i[shop hmac timestamp locale protocol return_to]
167
+ request.path != '/' || sanitized_params.except(*native_params).any?
168
+ end
169
+
170
+ def fullpage_redirect_to(url)
171
+ if ShopifyApp.configuration.embedded_app?
172
+ render('shopify_app/shared/redirect', layout: false,
173
+ locals: { url: url, current_shopify_domain: current_shopify_domain })
174
+ else
175
+ redirect_to(url)
176
+ end
177
+ end
178
+
179
+ def current_shopify_domain
180
+ shopify_domain = sanitized_shop_name ||
181
+ jwt_shopify_domain ||
182
+ session[:shopify_domain]
183
+
184
+ return shopify_domain if shopify_domain.present?
185
+
186
+ raise ShopifyDomainNotFound
187
+ end
188
+
189
+ def sanitized_shop_name
190
+ @sanitized_shop_name ||= sanitize_shop_param(params)
191
+ end
192
+
193
+ def referer_sanitized_shop_name
194
+ return unless request.referer.present?
195
+
196
+ @referer_sanitized_shop_name ||= begin
197
+ referer_uri = URI(request.referer)
198
+ query_params = Rack::Utils.parse_query(referer_uri.query)
199
+
200
+ sanitize_shop_param(query_params.with_indifferent_access)
201
+ end
202
+ end
203
+
204
+ def sanitize_shop_param(params)
205
+ return unless params[:shop].present?
206
+ ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
207
+ end
208
+
209
+ def sanitized_params
210
+ request.query_parameters.clone.tap do |query_params|
211
+ if params[:shop].is_a?(String)
212
+ query_params[:shop] = sanitize_shop_param(params)
213
+ end
214
+ end
215
+ end
216
+
217
+ def return_address
218
+ return base_return_address unless ShopifyApp.configuration.allow_jwt_authentication
219
+ return_address_with_params(shop: current_shopify_domain)
220
+ rescue ShopifyDomainNotFound
221
+ base_return_address
222
+ end
223
+
224
+ def base_return_address
225
+ session.delete(:return_to) || ShopifyApp.configuration.root_url
226
+ end
227
+
228
+ def return_address_with_params(params)
229
+ uri = URI(base_return_address)
230
+ uri.query = CGI.parse(uri.query.to_s)
231
+ .symbolize_keys
232
+ .transform_values { |v| v.one? ? v.first : v }
233
+ .merge(params)
234
+ .to_query
235
+ uri.to_s
236
+ end
237
+
238
+ private
239
+
240
+ def user_session_expected?
241
+ !ShopifyApp.configuration.user_session_repository.blank? && ShopifyApp::SessionRepository.user_storage.present?
242
+ end
243
+ end
244
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module PayloadVerification
4
+ extend ActiveSupport::Concern
5
+
6
+ private
7
+
8
+ def shopify_hmac
9
+ request.headers['HTTP_X_SHOPIFY_HMAC_SHA256']
10
+ end
11
+
12
+ def hmac_valid?(data)
13
+ secrets = [ShopifyApp.configuration.secret, ShopifyApp.configuration.old_secret].reject(&:blank?)
14
+
15
+ secrets.any? do |secret|
16
+ digest = OpenSSL::Digest.new('sha256')
17
+ ActiveSupport::SecurityUtils.secure_compare(
18
+ shopify_hmac,
19
+ Base64.strict_encode64(OpenSSL::HMAC.digest(digest, secret, data))
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ module WebhookVerification
4
+ extend ActiveSupport::Concern
5
+ include ShopifyApp::PayloadVerification
6
+
7
+ included do
8
+ skip_before_action :verify_authenticity_token, raise: false
9
+ before_action :verify_request
10
+ end
11
+
12
+ private
13
+
14
+ def verify_request
15
+ data = request.raw_post
16
+ return head(:unauthorized) unless hmac_valid?(data)
17
+ end
18
+
19
+ def shop_domain
20
+ request.headers['HTTP_X_SHOPIFY_SHOP_DOMAIN']
21
+ end
22
+ end
23
+ end
@@ -1,6 +1,46 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
3
+ module RedactJobParams
4
+ private
5
+
6
+ def args_info(job)
7
+ log_disabled_classes = %w(ShopifyApp::ScripttagsManagerJob ShopifyApp::WebhooksManagerJob)
8
+ return "" if log_disabled_classes.include?(job.class.name)
9
+ super
10
+ end
11
+ end
12
+
2
13
  class Engine < Rails::Engine
3
14
  engine_name 'shopify_app'
4
15
  isolate_namespace ShopifyApp
16
+
17
+ initializer "shopify_app.assets.precompile" do |app|
18
+ app.config.assets.precompile += %w[
19
+ shopify_app/redirect.js
20
+ shopify_app/top_level.js
21
+ shopify_app/enable_cookies.js
22
+ shopify_app/request_storage_access.js
23
+ storage_access.svg
24
+ ]
25
+ end
26
+
27
+ initializer "shopify_app.middleware" do |app|
28
+ app.config.middleware.insert_after(::Rack::Runtime, ShopifyApp::SameSiteCookieMiddleware)
29
+
30
+ if ShopifyApp.configuration.allow_jwt_authentication
31
+ app.config.middleware.insert_after(ShopifyApp::SameSiteCookieMiddleware, ShopifyApp::JWTMiddleware)
32
+ end
33
+ end
34
+
35
+ initializer "shopify_app.redact_job_params" do
36
+ ActiveSupport.on_load(:active_job) do
37
+ if ActiveJob::Base.respond_to?(:log_arguments?)
38
+ WebhooksManagerJob.log_arguments = false
39
+ ScripttagsManagerJob.log_arguments = false
40
+ elsif ActiveJob::Logging::LogSubscriber.private_method_defined?(:args_info)
41
+ ActiveJob::Logging::LogSubscriber.prepend(RedactJobParams)
42
+ end
43
+ end
44
+ end
5
45
  end
6
46
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ class ScripttagsManagerJob < ActiveJob::Base
4
+ queue_as do
5
+ ShopifyApp.configuration.scripttags_manager_queue_name
6
+ end
7
+
8
+ def perform(shop_domain:, shop_token:, scripttags:)
9
+ api_version = ShopifyApp.configuration.api_version
10
+ ShopifyAPI::Session.temp(domain: shop_domain, token: shop_token, api_version: api_version) do
11
+ manager = ScripttagsManager.new(scripttags, shop_domain)
12
+ manager.create_scripttags
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  class WebhooksManagerJob < ActiveJob::Base
3
-
4
4
  queue_as do
5
5
  ShopifyApp.configuration.webhooks_manager_queue_name
6
6
  end
7
7
 
8
8
  def perform(shop_domain:, shop_token:, webhooks:)
9
- ShopifyAPI::Session.temp(shop_domain, shop_token) do
9
+ api_version = ShopifyApp.configuration.api_version
10
+ ShopifyAPI::Session.temp(domain: shop_domain, token: shop_token, api_version: api_version) do
10
11
  manager = WebhooksManager.new(webhooks)
11
12
  manager.create_webhooks
12
13
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  class ScripttagsManager
3
4
  class CreationFailed < StandardError; end
@@ -6,14 +7,25 @@ module ShopifyApp
6
7
  ShopifyApp::ScripttagsManagerJob.perform_later(
7
8
  shop_domain: shop_domain,
8
9
  shop_token: shop_token,
9
- scripttags: scripttags
10
+ # Procs cannot be serialized so we interpolate now, if necessary
11
+ scripttags: build_src(scripttags, shop_domain)
10
12
  )
11
13
  end
12
14
 
13
- attr_reader :required_scripttags
15
+ def self.build_src(scripttags, domain)
16
+ scripttags.map do |tag|
17
+ next tag unless tag[:src].respond_to?(:call)
18
+ tag = tag.dup
19
+ tag[:src] = tag[:src].call(domain)
20
+ tag
21
+ end
22
+ end
23
+
24
+ attr_reader :required_scripttags, :shop_domain
14
25
 
15
- def initialize(scripttags)
26
+ def initialize(scripttags, shop_domain)
16
27
  @required_scripttags = scripttags
28
+ @shop_domain = shop_domain
17
29
  end
18
30
 
19
31
  def recreate_scripttags!
@@ -24,14 +36,15 @@ module ShopifyApp
24
36
  def create_scripttags
25
37
  return unless required_scripttags.present?
26
38
 
27
- required_scripttags.each do |scripttag|
39
+ expanded_scripttags.each do |scripttag|
28
40
  create_scripttag(scripttag) unless scripttag_exists?(scripttag[:src])
29
41
  end
30
42
  end
31
43
 
32
44
  def destroy_scripttags
33
- ShopifyAPI::ScriptTag.all.each do |scripttag|
34
- ShopifyAPI::ScriptTag.delete(scripttag.id) if is_required_scripttag?(scripttag)
45
+ scripttags = expanded_scripttags
46
+ ShopifyAPI::ScriptTag.all.each do |tag|
47
+ ShopifyAPI::ScriptTag.delete(tag.id) if required_scripttag?(scripttags, tag)
35
48
  end
36
49
 
37
50
  @current_scripttags = nil
@@ -39,8 +52,12 @@ module ShopifyApp
39
52
 
40
53
  private
41
54
 
42
- def is_required_scripttag?(scripttag)
43
- required_scripttags.map{ |w| w[:src] }.include? scripttag.src
55
+ def expanded_scripttags
56
+ self.class.build_src(required_scripttags, shop_domain)
57
+ end
58
+
59
+ def required_scripttag?(scripttags, tag)
60
+ scripttags.map { |w| w[:src] }.include?(tag.src)
44
61
  end
45
62
 
46
63
  def create_scripttag(attributes)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyApp
2
3
  class WebhooksManager
3
4
  class CreationFailed < StandardError; end
@@ -30,8 +31,8 @@ module ShopifyApp
30
31
  end
31
32
 
32
33
  def destroy_webhooks
33
- ShopifyAPI::Webhook.all.each do |webhook|
34
- ShopifyAPI::Webhook.delete(webhook.id) if is_required_webhook?(webhook)
34
+ ShopifyAPI::Webhook.all.to_a.each do |webhook|
35
+ ShopifyAPI::Webhook.delete(webhook.id) if required_webhook?(webhook)
35
36
  end
36
37
 
37
38
  @current_webhooks = nil
@@ -39,8 +40,8 @@ module ShopifyApp
39
40
 
40
41
  private
41
42
 
42
- def is_required_webhook?(webhook)
43
- required_webhooks.map{ |w| w[:address] }.include? webhook.address
43
+ def required_webhook?(webhook)
44
+ required_webhooks.map { |w| w[:address] }.include?(webhook.address)
44
45
  end
45
46
 
46
47
  def create_webhook(attributes)
@@ -55,7 +56,7 @@ module ShopifyApp
55
56
  end
56
57
 
57
58
  def current_webhooks
58
- @current_webhooks ||= ShopifyAPI::Webhook.all.index_by(&:topic)
59
+ @current_webhooks ||= ShopifyAPI::Webhook.all.to_a.index_by(&:topic)
59
60
  end
60
61
  end
61
62
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ class JWTMiddleware
4
+ TOKEN_REGEX = /^Bearer\s+(.*?)$/
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ return call_next(env) unless authorization_header(env)
12
+
13
+ token = extract_token(env)
14
+ return call_next(env) unless token
15
+
16
+ set_env_variables(token, env)
17
+ call_next(env)
18
+ end
19
+
20
+ private
21
+
22
+ def call_next(env)
23
+ @app.call(env)
24
+ end
25
+
26
+ def authorization_header(env)
27
+ env['HTTP_AUTHORIZATION']
28
+ end
29
+
30
+ def extract_token(env)
31
+ match = authorization_header(env).match(TOKEN_REGEX)
32
+ match && match[1]
33
+ end
34
+
35
+ def set_env_variables(token, env)
36
+ jwt = ShopifyApp::JWT.new(token)
37
+
38
+ env['jwt.shopify_domain'] = jwt.shopify_domain
39
+ env['jwt.shopify_user_id'] = jwt.shopify_user_id
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyApp
3
+ class SameSiteCookieMiddleware
4
+ COOKIE_SEPARATOR = "\n"
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ status, headers, body = @app.call(env)
12
+ user_agent = env['HTTP_USER_AGENT']
13
+
14
+ if headers && headers['Set-Cookie'] &&
15
+ BrowserSniffer.new(user_agent).same_site_none_compatible? &&
16
+ ShopifyApp.configuration.enable_same_site_none &&
17
+ Rack::Request.new(env).ssl?
18
+
19
+ set_cookies = headers['Set-Cookie']
20
+ .split(COOKIE_SEPARATOR)
21
+ .compact
22
+ .map do |cookie|
23
+ cookie << '; Secure' unless cookie =~ /;\s*secure/i
24
+ cookie << '; SameSite=None' unless cookie =~ /;\s*samesite=/i
25
+ cookie
26
+ end
27
+
28
+ headers['Set-Cookie'] = set_cookies.join(COOKIE_SEPARATOR)
29
+ end
30
+
31
+ [status, headers, body]
32
+ end
33
+ end
34
+ end