Repository: Shopify/shopify-cli (Ruby)

Issue:
[BugHunt]: `extension serve` doesn't redirect to store password page for TAE
### Please confirm that you:

- [X] Are working with Shopify CLI 2.0, not 3.0. (You can [open a 3.0 issue here](https://github.com/Shopify/cli/issues/new/choose).)
- [X] Searched [existing issues](https://github.com/Shopify/shopify-cli/issues) to see if your issue is a duplicate. (If you’ve found a duplicate issue, feel free to add additional information in a comment on it.)
- [X] Reproduced the issue in [the latest CLI version](https://github.com/Shopify/shopify-cli/releases).

### What type of project are you working on?

extension

### Expected behavior

When I create a new dev store and run `extension serve`(where the extension is a theme app extension), I should be prompted to give my store password. This will allow me to access the store locally and to test with hot reloading. When the password is needed, the redirect url should include the port of the `extension serve` dev server 



### Actual behavior

When I create a new dev store and run `extension serve`, I navigate to my store locally in the browser and get redirected to `http://127.0.0.1:9292/password?_fd=0` (even though extension serve runs on port 8282)

https://user-images.githubusercontent.com/18431672/189156274-717c2d21-c30e-4237-98b0-4911bb611c62.mp4

http://127.0.0.1:8282 redirects to http://127.0.0.1:9292/password?_fd=0 BUT it should redirect to http://127.0.0.1:8282/password?_fd=0

### Stack trace

_No response_

### Reproduction steps

1. Create a new dev store
2. Navigate to your theme app extension directory and run `extension serve`
3. Navigate to `http://127.0.0.1:8282`
4. Confirm you get redirected `http://127.0.0.1:9292/password?_fd=0`

### Operating System

Mac OS Monterey 12.5.1

### CLI version (run `shopify version` if you're not sure)

2.24.0

### Shell

_No response_

### Ruby version (run `ruby -v` if you're not sure)

ruby 2.7.6p219

### What language and version are you using in your application?

_No response_


Discussion:

Patch:
diff --git a/lib/shopify_cli/theme/dev_server/proxy.rb b/lib/shopify_cli/theme/dev_server/proxy.rb
index 1c660f9046..a87d0d3341 100644
--- a/lib/shopify_cli/theme/dev_server/proxy.rb
+++ b/lib/shopify_cli/theme/dev_server/proxy.rb
@@ -65,7 +65,7 @@ def call(env)
             )
           end
 
-          headers = get_response_headers(response)
+          headers = get_response_headers(response, env)
 
           unless headers["x-storefront-renderer-rendered"]
             @core_endpoints << env["PATH_INFO"]
@@ -170,7 +170,7 @@ def secure_session_id
           @secure_session_id
         end
 
-        def get_response_headers(response)
+        def get_response_headers(response, env)
           response_headers = normalize_headers(
             response.respond_to?(:headers) ? response.headers : response.to_hash
           )
@@ -180,7 +180,7 @@ def get_response_headers(response)
           response_headers.reject! { |k| HOP_BY_HOP_HEADERS.include?(k.downcase) }
 
           if response_headers["location"]&.include?("myshopify.com")
-            response_headers["location"].gsub!(%r{(https://#{shop})}, "http://127.0.0.1:9292")
+            response_headers["location"].gsub!(%r{(https://#{shop})}, "http://#{host(env)}")
           end
 
           new_session_id = extract_secure_session_id_from_response_headers(response_headers)


Test patch:
diff --git a/test/shopify-cli/theme/dev_server/proxy_test.rb b/test/shopify-cli/theme/dev_server/proxy_test.rb
index ab36d5d756..d43d0d4bef 100644
--- a/test/shopify-cli/theme/dev_server/proxy_test.rb
+++ b/test/shopify-cli/theme/dev_server/proxy_test.rb
@@ -171,11 +171,12 @@ def test_storefront_redirect_headers_are_rewritten
             .to_return(status: 302, headers: {
               "Location" => "https://dev-theme-server-store.myshopify.com/password",
             })
+          @proxy.stubs(:host).returns("127.0.0.1:8282")
 
           stub_session_id_request
           response = request.get("/")
 
-          assert_equal("http://127.0.0.1:9292/password", response.headers["Location"])
+          assert_equal("http://127.0.0.1:8282/password", response.headers["Location"])
         end
 
         def test_non_storefront_redirect_headers_are_not_rewritten