From 47fb451d0cf1df93aa6832b13336eb8f1d396bbb Mon Sep 17 00:00:00 2001 From: Guilherme Carreiro Date: Fri, 28 Jan 2022 14:49:39 +0100 Subject: [PATCH] Revert "Fix CORS (Cross-origin resource sharing) errors (#1952)" (#1985) --- CHANGELOG.md | 2 + lib/shopify_cli/theme/dev_server.rb | 2 - .../theme/dev_server/cdn/cdn_helper.rb | 49 ----- .../theme/dev_server/cdn_assets.rb | 69 ------- lib/shopify_cli/theme/dev_server/cdn_fonts.rb | 36 +++- .../theme/dev_server/local_assets.rb | 4 - .../theme/dev_server/cdn/cdn_helper_test.rb | 64 ------ .../theme/dev_server/cdn_assets_test.rb | 188 ------------------ 8 files changed, 30 insertions(+), 384 deletions(-) delete mode 100644 lib/shopify_cli/theme/dev_server/cdn/cdn_helper.rb delete mode 100644 lib/shopify_cli/theme/dev_server/cdn_assets.rb delete mode 100644 test/shopify-cli/theme/dev_server/cdn/cdn_helper_test.rb delete mode 100644 test/shopify-cli/theme/dev_server/cdn_assets_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e9e1bb378..2eca99c938 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ From version 2.6.0, the sections in this file adhere to the [keep a changelog](https://keepachangelog.com/en/1.0.0/) specification. ## [Unreleased] +### Fixed +* [#1985](https://github.com/Shopify/shopify-cli/pull/1985): Revert "Fix CORS (Cross-origin resource sharing) errors (#1952)" ## Version 2.10.0 ### Fixed diff --git a/lib/shopify_cli/theme/dev_server.rb b/lib/shopify_cli/theme/dev_server.rb index e44be6d08d..9b4c404280 100644 --- a/lib/shopify_cli/theme/dev_server.rb +++ b/lib/shopify_cli/theme/dev_server.rb @@ -3,7 +3,6 @@ require_relative "ignore_filter" require_relative "syncer" -require_relative "dev_server/cdn_assets" require_relative "dev_server/cdn_fonts" require_relative "dev_server/hot_reload" require_relative "dev_server/header_hash" @@ -38,7 +37,6 @@ def start(ctx, root, host: "127.0.0.1", port: 9292, poll: false, mode: ReloadMod @app = Proxy.new(ctx, theme: theme, syncer: @syncer) @app = CdnFonts.new(@app, theme: theme) @app = LocalAssets.new(ctx, @app, theme: theme) - @app = CdnAssets.new(@app, theme: theme) @app = HotReload.new(ctx, @app, theme: theme, watcher: watcher, mode: mode, ignore_filter: ignore_filter) stopped = false address = "http://#{host}:#{port}" diff --git a/lib/shopify_cli/theme/dev_server/cdn/cdn_helper.rb b/lib/shopify_cli/theme/dev_server/cdn/cdn_helper.rb deleted file mode 100644 index a5b7a9d1cc..0000000000 --- a/lib/shopify_cli/theme/dev_server/cdn/cdn_helper.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -module ShopifyCLI - module Theme - module DevServer - module Cdn - module CdnHelper - def proxy_request(env, uri, theme) - response = Net::HTTP.start(uri.host, 443, use_ssl: true) do |http| - req_class = Net::HTTP.const_get(method(env)) - - req = req_class.new(uri) - req.initialize_http_header(req_headers(theme)) - req.body_stream = req_body(env) - - http.request(req) - end - - [ - response.code.to_s, - { - "Content-Type" => response.content_type, - "Content-Length" => response.content_length.to_s, - }, - [response.body], - ] - end - - private - - def method(env) - env["REQUEST_METHOD"].capitalize - end - - def req_body(env) - env["rack.input"] - end - - def req_headers(theme) - { - "Referer" => "https://#{theme.shop}", - "Transfer-Encoding" => "chunked", - } - end - end - end - end - end -end diff --git a/lib/shopify_cli/theme/dev_server/cdn_assets.rb b/lib/shopify_cli/theme/dev_server/cdn_assets.rb deleted file mode 100644 index efea18ad45..0000000000 --- a/lib/shopify_cli/theme/dev_server/cdn_assets.rb +++ /dev/null @@ -1,69 +0,0 @@ -# frozen_string_literal: true - -require_relative "cdn/cdn_helper" -require_relative "local_assets" - -module ShopifyCLI - module Theme - module DevServer - class CdnAssets - include Cdn::CdnHelper - - ASSETS_PROXY_PATH = "/cdn_asset" - ASSETS_CDN = "//cdn.shopify.com" - ASSETS_CDN_REGEX = %r{(https?:)?#{ASSETS_CDN}} - ASSETS_SOURCE_MAP_REGEX = /\/[\/|\*]# sourceMappingURL\=(\/.*)/ - - def initialize(app, theme:) - @app = app - @theme = theme - end - - def call(env) - path = env["PATH_INFO"] - - # Serve assets from CDN - return serve_asset(env, path) if path.start_with?(ASSETS_PROXY_PATH) - - # Proxy the request, and replace the URLs in the response - status, headers, body = @app.call(env) - body = replace_asset_urls(body) - [status, headers, body] - end - - private - - def serve_asset(env, path) - path = path.gsub(%r{^#{ASSETS_PROXY_PATH}}, "") - query = env["QUERY_STRING"] - uri = asset_cdn_uri(path, query) - - status, headers, body = proxy_request(env, uri, @theme) - - [status, headers, replace_source_map_url(body)] - end - - def asset_cdn_uri(path, query) - uri = URI.join("https:#{ASSETS_CDN}", path) - uri.query = query.split("&").last - uri - end - - def replace_asset_urls(body) - [body.join.gsub(ASSETS_CDN_REGEX, ASSETS_PROXY_PATH)] - end - - def replace_source_map_url(body) - body_content = body.join - map_regex_match = body_content.match(ASSETS_SOURCE_MAP_REGEX) - return body if map_regex_match.nil? - - map_url = map_regex_match[1] - return body if map_url.nil? - - [body_content.gsub(map_url, "#{ASSETS_PROXY_PATH}#{map_url}")] - end - end - end - end -end diff --git a/lib/shopify_cli/theme/dev_server/cdn_fonts.rb b/lib/shopify_cli/theme/dev_server/cdn_fonts.rb index 3099d3dc70..811e0b7b87 100644 --- a/lib/shopify_cli/theme/dev_server/cdn_fonts.rb +++ b/lib/shopify_cli/theme/dev_server/cdn_fonts.rb @@ -1,13 +1,9 @@ # frozen_string_literal: true -require_relative "cdn/cdn_helper" - module ShopifyCLI module Theme module DevServer class CdnFonts - include Cdn::CdnHelper - FONTS_PATH = "/fonts" FONTS_CDN = "https://fonts.shopifycdn.com" FONTS_REGEX = %r{#{FONTS_CDN}} @@ -21,7 +17,7 @@ def call(env) path = env["PATH_INFO"] # Serve from fonts CDN - return serve_font(env, path) if path.start_with?(FONTS_PATH) + return serve_font(env) if path.start_with?(FONTS_PATH) # Proxy the request, and replace the URLs in the response status, headers, body = @app.call(env) @@ -31,11 +27,35 @@ def call(env) private - def serve_font(env, path) - query = env["QUERY_STRING"] + def serve_font(env) + parameters = %w(PATH_INFO QUERY_STRING REQUEST_METHOD rack.input) + path, query, method, body_stream = *env.slice(*parameters).values + uri = fonts_cdn_uri(path, query) - proxy_request(env, uri, @theme) + response = Net::HTTP.start(uri.host, 443, use_ssl: true) do |http| + req_class = Net::HTTP.const_get(method.capitalize) + req = req_class.new(uri) + req.initialize_http_header(fonts_cdn_headers) + req.body_stream = body_stream + http.request(req) + end + + [ + response.code.to_s, + { + "Content-Type" => response.content_type, + "Content-Length" => response.content_length.to_s, + }, + [response.body], + ] + end + + def fonts_cdn_headers + { + "Referer" => "https://#{@theme.shop}", + "Transfer-Encoding" => "chunked", + } end def fonts_cdn_uri(path, query) diff --git a/lib/shopify_cli/theme/dev_server/local_assets.rb b/lib/shopify_cli/theme/dev_server/local_assets.rb index 76c59dfa81..d2640eb0eb 100644 --- a/lib/shopify_cli/theme/dev_server/local_assets.rb +++ b/lib/shopify_cli/theme/dev_server/local_assets.rb @@ -11,10 +11,6 @@ def initialize(path) @path = path end - def join - @path.read - end - # Naive implementation. Only used in unit tests. def each yield @path.read diff --git a/test/shopify-cli/theme/dev_server/cdn/cdn_helper_test.rb b/test/shopify-cli/theme/dev_server/cdn/cdn_helper_test.rb deleted file mode 100644 index 6f50923e8c..0000000000 --- a/test/shopify-cli/theme/dev_server/cdn/cdn_helper_test.rb +++ /dev/null @@ -1,64 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" -require "shopify_cli/theme/dev_server/cdn/cdn_helper" - -module ShopifyCLI - module Theme - module DevServer - module Cdn - class CdnHelperTest < Minitest::Test - include Cdn::CdnHelper - - def setup - super - - @env = { - "REQUEST_METHOD" => "get", - "rack.input" => body_stream, - } - @headers = { - "Referer" => "https://my-test-shop.myshopify.com", - "Transfer-Encoding" => "chunked", - } - @uri = URI("http://cdn.shopify/assets/base.css") - end - - def test_proxy_request - expected_code = "200" - expected_content = "" - expected_headers = { - "Content-Type" => "text/css", - "Content-Length" => "42", - } - - stub_request(:get, "https://cdn.shopify/assets/base.css") - .with(body: body_stream.read, headers: @headers) - .to_return(status: 200, - body: expected_content, - headers: expected_headers) - - actual_response = proxy_request(@env, @uri, theme) - expected_response = [expected_code, expected_headers, [expected_content]] - - assert_equal(expected_response, actual_response) - end - - private - - def theme - theme_mock = mock("Theme") - theme_mock.stubs(:shop).returns("my-test-shop.myshopify.com") - theme_mock - end - - def body_stream - body_mock = mock("Body") - body_mock.stubs(:read).returns("") - body_mock - end - end - end - end - end -end diff --git a/test/shopify-cli/theme/dev_server/cdn_assets_test.rb b/test/shopify-cli/theme/dev_server/cdn_assets_test.rb deleted file mode 100644 index af069954a9..0000000000 --- a/test/shopify-cli/theme/dev_server/cdn_assets_test.rb +++ /dev/null @@ -1,188 +0,0 @@ -# frozen_string_literal: true -require "test_helper" -require "shopify_cli/theme/dev_server" -require "rack/mock" - -module ShopifyCLI - module Theme - module DevServer - class CdnAssetsTest < Minitest::Test - def test_replace_cdn_css_in_reponse_body - original_html = <<~HTML - - - - - - HTML - expected_html = <<~HTML - - - - - - HTML - assert_equal(expected_html, serve(original_html).body) - end - - def test_replace_cdn_js_in_reponse_body - original_html = <<~HTML - - - - - - HTML - expected_html = <<~HTML - - - - - - HTML - assert_equal(expected_html, serve(original_html).body) - end - - def test_replace_two_cdn_css_files_on_same_line - original_html = <<~HTML - - - - - - HTML - expected_html = <<~HTML - - - - - - HTML - assert_equal(expected_html, serve(original_html).body) - end - - def test_replace_two_cdn_js_files_on_same_line - original_html = <<~HTML - - - - - - HTML - expected_html = <<~HTML - - - - - - HTML - assert_equal(expected_html, serve(original_html).body) - end - - def test_serve_js_asset_from_cdn - response_body = "" \ - "//# sourceMappingURL=/script.js.map" - expected_body = "" \ - "//# sourceMappingURL=/cdn_asset/script.js.map" - - stub_request(:get, "https://cdn.shopify.com/script.js") - .with(headers: { - "Referer" => "https://my-test-shop.myshopify.com", - "Transfer-Encoding" => "chunked", - }) - .to_return(status: 200, body: response_body, headers: {}) - - response = serve(path: "/cdn_asset/script.js") - actual_body = response.body - - assert_equal expected_body, actual_body - end - - def test_serve_css_asset_from_cdn - response_body = "" \ - "/*# sourceMappingURL=/style.css.map */" - expected_body = "" \ - "/*# sourceMappingURL=/cdn_asset/style.css.map */" - - stub_request(:get, "https://cdn.shopify.com/style.css") - .with(headers: { - "Referer" => "https://my-test-shop.myshopify.com", - "Transfer-Encoding" => "chunked", - }) - .to_return(status: 200, body: response_body, headers: {}) - - response = serve(path: "/cdn_asset/style.css") - actual_body = response.body - - assert_equal expected_body, actual_body - end - - def test_serve_map_from_cdn - expected_body = "" - - stub_request(:get, "https://cdn.shopify.com/s/any/theme.css.min.map") - .with(headers: { - "Referer" => "https://my-test-shop.myshopify.com", - "Transfer-Encoding" => "chunked", - }) - .to_return(status: 200, body: expected_body, headers: {}) - - response = serve(path: "/cdn_asset/s/any/theme.css.min.map") - actual_body = response.body - - assert_equal expected_body, actual_body - end - - def test_404_on_missing_cdn_asset - stub_request(:get, "https://cdn.shopify.com/not_found_resource.js") - .with(headers: { - "Referer" => "https://my-test-shop.myshopify.com", - "Transfer-Encoding" => "chunked", - }) - .to_return(status: 404, body: "Not found", headers: {}) - - response = serve(path: "/cdn_asset/not_found_resource.js") - - assert_equal(404, response.status) - assert_equal("Not found", response.body) - end - - def test_404_on_missing_cdn_map - stub_request(:get, "https://cdn.shopify.com/s/any/not_found_resource.map") - .with(headers: { - "Referer" => "https://my-test-shop.myshopify.com", - "Transfer-Encoding" => "chunked", - }) - .to_return(status: 404, body: "Not found", headers: {}) - - response = serve(path: "/cdn_asset/s/any/not_found_resource.map") - - assert_equal(404, response.status) - assert_equal("Not found", response.body) - end - - private - - def serve(response_body = "", path: "/") - app = lambda do |_env| - [200, {}, [response_body]] - end - stack = CdnAssets.new(app, theme: theme) - request = Rack::MockRequest.new(stack) - request.get(path) - end - - def root - @root ||= ShopifyCLI::ROOT + "/test/fixtures/theme" - end - - def theme - return @theme if @theme - @theme = Theme.new(nil, root: root) - @theme.stubs(shop: "my-test-shop.myshopify.com") - @theme - end - end - end - end -end