Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep original path and params when redirecting deep links to embed #1869

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Unreleased
- Handle scenario when invalid URI is passed to `sanitize_shop_domain` [#1852](https://github.com/Shopify/shopify_app/pull/1852)
- Remove references to old JS files during asset precompile [#1865](https://github.com/Shopify/shopify_app/pull/1865)
- Remove old translation keys for `enable_cookies_*`, `top_level_interaction_*` and `request_storage_access_*` [#1865](https://github.com/Shopify/shopify_app/pull/1865)
- Keep original path and params when redirecting deep links to embed [#1869](https://github.com/Shopify/shopify_app/pull/1869)

22.2.1 (May 6,2024)
----------
Expand Down
6 changes: 5 additions & 1 deletion lib/shopify_app/controller_concerns/embedded_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def redirect_to_embed_app_in_admin
return redirect_to(ShopifyApp.configuration.login_url)
end

redirect_path = ShopifyAPI::Auth.embedded_app_url(host)
original_path = request.path
original_params = request.query_parameters.except(:host, :shop, :id_token)
original_path += "?#{original_params.to_query}" if original_params.present?

redirect_path = ShopifyAPI::Auth.embedded_app_url(host) + original_path.to_s
redirect_path = ShopifyApp.configuration.root_url if deduced_phishing_attack?(redirect_path)
redirect_to(redirect_path, allow_other_host: true)
end
Expand Down
13 changes: 11 additions & 2 deletions test/controllers/concerns/embedded_app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,24 @@ def redirect_to_embed
shop = "my-shop.myshopify.com"
host = Base64.encode64("#{shop}/admin")
get :redirect_to_embed, params: { host: host }
assert_redirected_to "https://#{shop}/admin/apps/#{ShopifyApp.configuration.api_key}"
assert_redirected_to "https://#{shop}/admin/apps/#{ShopifyApp.configuration.api_key}/redirect_to_embed"
end

test "#redirect_to_embed_app_in_admin redirects to the embed app in the admin when the shop param is present" do
ShopifyApp.configuration.embedded_app = true

shop = "my-shop.myshopify.com"
get :redirect_to_embed, params: { shop: shop }
assert_redirected_to "https://#{shop}/admin/apps/#{ShopifyApp.configuration.api_key}"
assert_redirected_to "https://#{shop}/admin/apps/#{ShopifyApp.configuration.api_key}/redirect_to_embed"
end

test "#redirect_to_embed_app_in_admin keeps original path and params when redirecting to the embed app" do
ShopifyApp.configuration.embedded_app = true

shop = "my-shop.myshopify.com"
host = Base64.encode64("#{shop}/admin")
get :redirect_to_embed, params: { shop: shop, foo: "bar", host: host, id_token: "id_token" }
assert_redirected_to "https://#{shop}/admin/apps/#{ShopifyApp.configuration.api_key}/redirect_to_embed?foo=bar"
end

test "Redirect to login URL when host nor shop param is present" do
Expand Down
4 changes: 2 additions & 2 deletions test/shopify_app/controller_concerns/token_exchange_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class TokenExchangeControllerTest < ActionController::TestCase
host = Base64.encode64("#{@shop}/admin")
params = { shop: @shop, host: host }

expected_redirect_url = "https://my-shop.myshopify.com/admin/apps/key"
expected_redirect_url = "https://my-shop.myshopify.com/admin/apps/key/"

with_application_test_routes do
get :index, params: params
Expand All @@ -241,7 +241,7 @@ class TokenExchangeControllerTest < ActionController::TestCase

params = { shop: @shop }

expected_redirect_url = "https://my-shop.myshopify.com/admin/apps/key"
expected_redirect_url = "https://my-shop.myshopify.com/admin/apps/key/"

with_application_test_routes do
get :index, params: params
Expand Down
Loading