Skip to content

Commit f782371

Browse files
authored
Merge pull request #1887 from Shopify/fix_top_level_redirection
Fix full page redirection at top level
2 parents 1a7a16f + be6c6c1 commit f782371

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased
22
----------
33

4+
- Handle edge case where we attempted to redirect to login when already at the top level [#1887](https://github.com/Shopify/shopify_app/pull/1887)
5+
46
22.3.0 (July 24, 2024)
57
----------
68
- Deprecate `ShopifyApp::JWTMiddleware`. And remove internal usage. Any existing app code relying on decoded JWT contents set from `request.env` should instead include the `WithShopifyIdToken` concern and call its respective methods. [#1861](https://github.com/Shopify/shopify_app/pull/1861) [Migration Guide](/docs/Upgrading.md#v2300---removed-shopifyappjwtmiddleware)

app/views/shopify_app/shared/redirect.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
<base target="_top">
77
<title>Redirecting…</title>
88

9+
<%
10+
is_iframe = local_assigns.key?(:is_iframe) ? is_iframe : true
11+
if is_iframe
12+
%>
913
<meta name="shopify-api-key" content="<%= ShopifyApp.configuration.api_key %>">
1014
<%= javascript_include_tag "https://cdn.shopify.com/shopifycloud/app-bridge.js" %>
15+
<% end %>
1116

1217
<%= javascript_include_tag('shopify_app/redirect', crossorigin: 'anonymous', integrity: true) %>
1318
</head>

lib/shopify_app/controller_concerns/login_protection.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def fullpage_redirect_to(url)
204204
render(
205205
"shopify_app/shared/redirect",
206206
layout: false,
207-
locals: { url: url, current_shopify_domain: current_shopify_domain },
207+
locals: { url: url, current_shopify_domain: current_shopify_domain, is_iframe: embedded? },
208208
)
209209
else
210210
redirect_to(url)

lib/shopify_app/controller_concerns/sanitized_params.rb

+4
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ def sanitized_params
3333
end
3434
end
3535
end
36+
37+
def embedded?
38+
params[:embedded] == "1" || request.env["HTTP_SEC_FETCH_DEST"] == "iframe"
39+
end
3640
end
3741
end

lib/shopify_app/controller_concerns/token_exchange.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ def redirect_to_bounce_page
9494
)
9595
end
9696

97-
def embedded?
98-
params[:embedded] == "1" || request.env["HTTP_SEC_FETCH_DEST"] == "iframe"
99-
end
100-
10197
def online_token_configured?
10298
ShopifyApp.configuration.online_token_configured?
10399
end
@@ -108,7 +104,7 @@ def fullpage_redirect_to(url)
108104
render(
109105
"shopify_app/shared/redirect",
110106
layout: false,
111-
locals: { url: url, current_shopify_domain: current_shopify_domain },
107+
locals: { url: url, current_shopify_domain: current_shopify_domain, is_iframe: embedded? },
112108
)
113109
end
114110
end

test/shopify_app/controller_concerns/login_protection_test.rb

+24-3
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ class LoginProtectionControllerTest < ActionController::TestCase
509509
with_application_test_routes do
510510
example_shop = "shop.myshopify.com"
511511
get :redirect, params: { shop: example_shop }
512-
assert_fullpage_redirected(example_shop, response)
512+
assert_fullpage_redirected(example_shop, false)
513513
end
514514
end
515515

@@ -519,7 +519,24 @@ class LoginProtectionControllerTest < ActionController::TestCase
519519
ShopifyApp::SessionRepository.expects(:load_session)
520520
.returns(ShopifyAPI::Auth::Session.new(shop: example_shop))
521521
get :redirect
522-
assert_fullpage_redirected(example_shop, response)
522+
assert_fullpage_redirected(example_shop, false)
523+
end
524+
end
525+
526+
test "#fullpage_redirect_to loads app bridge when embedded param is set" do
527+
with_application_test_routes do
528+
example_shop = "shop.myshopify.com"
529+
get :redirect, params: { shop: example_shop, embedded: "1" }
530+
assert_fullpage_redirected(example_shop, true)
531+
end
532+
end
533+
534+
test "#fullpage_redirect_to loads app bridge when Sec-Fetch-Dest header is present" do
535+
with_application_test_routes do
536+
example_shop = "shop.myshopify.com"
537+
request.headers.merge!({ "Sec-Fetch-Dest" => "iframe" })
538+
get :redirect, params: { shop: example_shop }
539+
assert_fullpage_redirected(example_shop, true)
523540
end
524541
end
525542

@@ -603,14 +620,18 @@ class LoginProtectionControllerTest < ActionController::TestCase
603620

604621
private
605622

606-
def assert_fullpage_redirected(shop_domain, _response)
623+
def assert_fullpage_redirected(shop_domain, expect_embedded)
607624
example_url = "https://example.com"
608625

609626
assert_template("shared/redirect")
610627
assert_select "[id=redirection-target]", 1 do |elements|
611628
assert_equal "{\"myshopifyUrl\":\"https://#{shop_domain}\",\"url\":\"#{example_url}\"}",
612629
elements.first["data-target"]
613630
end
631+
assert_select "script" do |elements|
632+
assert elements.any? { |element| element["src"] =~ %r/\/assets\/shopify_app\/redirect-[^\.]*\.js/ }
633+
assert_equal expect_embedded, elements.any? { |element| element["src"] == "https://cdn.shopify.com/shopifycloud/app-bridge.js" }
634+
end
614635
end
615636

616637
def with_application_test_routes

0 commit comments

Comments
 (0)