From 9a7adcd178986a0e6cb30cfc046902fd1a677596 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Wed, 30 Jun 2021 11:04:07 +0800 Subject: [PATCH] FIX: Use absolute URL when redirecting SVG sprite path. This ensures that CDN URLs with relative paths do not end up losing the relative paths. --- app/controllers/svg_sprite_controller.rb | 4 ++-- spec/requests/svg_sprite_controller_spec.rb | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/controllers/svg_sprite_controller.rb b/app/controllers/svg_sprite_controller.rb index 259fb11c326..b5b6db3731e 100644 --- a/app/controllers/svg_sprite_controller.rb +++ b/app/controllers/svg_sprite_controller.rb @@ -12,10 +12,10 @@ class SvgSpriteController < ApplicationController no_cookies RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do - theme_id = params[:theme_id].to_i + theme_id = params[:theme_id].to_i if params[:theme_id].present? if SvgSprite.version(theme_id) != params[:version] - return redirect_to path(SvgSprite.path(theme_id)) + return redirect_to UrlHelper.absolute((SvgSprite.path(theme_id))) end svg_sprite = "window.__svg_sprite = #{SvgSprite.bundle(theme_id).inspect};" diff --git a/spec/requests/svg_sprite_controller_spec.rb b/spec/requests/svg_sprite_controller_spec.rb index 047e0e462fe..13058b7083d 100644 --- a/spec/requests/svg_sprite_controller_spec.rb +++ b/spec/requests/svg_sprite_controller_spec.rb @@ -24,8 +24,17 @@ describe SvgSpriteController do random_hash = Digest::SHA1.hexdigest("somerandomstring") get "/svg-sprite/#{Discourse.current_hostname}/svg--#{random_hash}.js" - expect(response.status).to eq(302) - expect(response.location).to include(SvgSprite.version) + expect(response).to redirect_to( + "/svg-sprite/test.localhost/svg--#{SvgSprite.version}.js" + ) + + set_cdn_url "//some-cdn.com/site" + + get "/svg-sprite/#{Discourse.current_hostname}/svg--#{random_hash}.js" + + expect(response).to redirect_to( + "https://some-cdn.com/site/svg-sprite/test.localhost/svg--#{SvgSprite.version}.js" + ) end end