A platform for community discussion. Free, open, simple.
FIX: login redirect to root path in subfolder setup (#36609)
Régis Hanol045a9521a3fd
diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rbindex 6c735ccf62a..b8fd7e76250 100644--- a/app/controllers/static_controller.rb+++ b/app/controllers/static_controller.rb@@ -28,24 +28,25 @@ class StaticController < ApplicationControllerCUSTOM_PAGES = {} # Add via `#add_topic_static_page` in plugin APIdef extract_redirect_param- redirect_path = params[:redirect]- if redirect_path.present?- begin- forum_host = URI(Discourse.base_url).host- uri = URI(redirect_path)+ return "/" if params[:redirect].blank?- if uri.path.present? && !uri.path.starts_with?(login_path) &&- (uri.host.blank? || uri.host == forum_host) && uri.path =~ %r{\A\/{1}[^\.\s]*\z}- return "#{uri.path}#{uri.query ? "?#{uri.query}" : ""}"- end- rescue URI::Error, ArgumentError- # If the URI is invalid, return "/" below- end- end+ uri = URI(params[:redirect])+ return "/" unless valid_redirect_uri?(uri)+ uri.query ? "#{uri.path}?#{uri.query}" : uri.path+ rescue URI::Error, ArgumentError"/"end+ def valid_redirect_uri?(uri)+ return false if uri.path.blank?+ return false if uri.path.starts_with?("#{Discourse.base_path}/login")+ return false if uri.host.present? && uri.host != URI(Discourse.base_url).host+ return false if !uri.path.match?(%r{\A/[^\.\s]*\z})++ true+ end+def showif params[:id] == "login"destination = extract_redirect_param@@ -152,14 +153,15 @@ class StaticController < ApplicationControllerdestination = extract_redirect_param- allow_other_hosts = false+ allow_other_host = falseif cookies[:sso_destination_url]destination = cookies.delete(:sso_destination_url)- allow_other_hosts = true+ allow_other_host = trueend- redirect_to(destination, allow_other_host: allow_other_hosts)+ destination = path(destination) if destination == "/"+ redirect_to(destination, allow_other_host:)endFAVICON = -"favicon"diff --git a/spec/requests/static_controller_spec.rb b/spec/requests/static_controller_spec.rbindex 3f302020598..a1ee06bda07 100644--- a/spec/requests/static_controller_spec.rb+++ b/spec/requests/static_controller_spec.rb@@ -396,6 +396,31 @@ RSpec.describe StaticController doexpect(response).to redirect_to("/")endend++ context "with a subfolder" do+ before { set_subfolder "/sub_test" }++ context "without a redirect path" do+ it "redirects to the subfolder root" do+ post "/login.json"+ expect(response).to redirect_to("/sub_test/")+ end+ end++ context "when the redirect path is the login page" do+ it "redirects to the subfolder root" do+ post "/login.json", params: { redirect: "#{Discourse.base_path}/login" }+ expect(response).to redirect_to("/sub_test/")+ end+ end++ context "when the redirect path is invalid" do+ it "redirects to the subfolder root" do+ post "/login.json", params: { redirect: "test" }+ expect(response).to redirect_to("/sub_test/")+ end+ end+ endenddescribe "#service_worker_asset" do