A platform for community discussion. Free, open, simple.
FIX: `StaticController#enter` should not redirect to invalid paths (#27913)
Alan Guo Xiang Tana3d319ac2f73
diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rbindex 2d16f24bf29..77a53d808a6 100644--- a/app/controllers/static_controller.rb+++ b/app/controllers/static_controller.rb@@ -133,7 +133,8 @@ class StaticController < ApplicationControllerforum_uri = URI(Discourse.base_url)uri = URI(redirect_location)- if uri.path.present? && (uri.host.blank? || uri.host == forum_uri.host) && uri.path !~ /\./+ if uri.path.present? && (uri.host.blank? || uri.host == forum_uri.host) &&+ uri.path =~ %r{\A\/{1}[^\.\s]*\z}destination = "#{uri.path}#{uri.query ? "?#{uri.query}" : ""}"endrescue URI::Error@@ -141,7 +142,7 @@ class StaticController < ApplicationControllerendend- redirect_to destination+ redirect_to(destination, allow_other_host: false)endFAVICON ||= -"favicon"diff --git a/spec/requests/static_controller_spec.rb b/spec/requests/static_controller_spec.rbindex 65060a8393b..3790fa3c2e7 100644--- a/spec/requests/static_controller_spec.rb+++ b/spec/requests/static_controller_spec.rb@@ -290,7 +290,7 @@ RSpec.describe StaticController doendend- context "with a full url to someone else" do+ context "with a full url to an external host" doit "redirects to the root path" dopost "/login.json", params: { redirect: "http://eviltrout.com/foo" }expect(response).to redirect_to("/")@@ -320,6 +320,13 @@ RSpec.describe StaticController doexpect(response).to redirect_to("/")endend++ context "when the redirect path is invalid" do+ it "redirects to the root URL" do+ post "/login.json", params: { redirect: "test" }+ expect(response).to redirect_to("/")+ end+ endenddescribe "#service_worker_asset" do