Skip to content

A platform for community discussion. Free, open, simple.

FIX: FinalDestination ignoring all URLs on domain for subfolder installs (#36785)

Rafael dos Santos Silva3d664b6fa147
diff --git a/lib/final_destination.rb b/lib/final_destination.rb
index 78c10f39a02..734f270bd53 100644
--- a/lib/final_destination.rb
+++ b/lib/final_destination.rb
@@ -59,13 +59,15 @@ class FinalDestination
@ignored = []
if @limit > 0
- ignore_redirects = [Discourse.base_url_no_prefix]
+ ignore_redirects = [Discourse.base_url]
ignore_redirects.concat(@opts[:ignore_redirects]) if @opts[:ignore_redirects]
ignore_redirects.each do |ignore_redirect|
ignore_redirect = uri(ignore_redirect)
- @ignored << ignore_redirect.hostname if ignore_redirect.present? && ignore_redirect.hostname
+ if ignore_redirect.present? && ignore_redirect.hostname
+ @ignored << { hostname: ignore_redirect.hostname, path: ignore_redirect.path }
+ end
end
end
@@ -229,8 +231,9 @@ class FinalDestination
return
end
- @ignored.each do |host|
- if @uri&.hostname&.match?(host)
+ @ignored.each do |ignored|
+ if @uri&.hostname&.match?(ignored[:hostname]) &&
+ @uri.path.start_with?(ignored[:path].presence || "/")
@status = :resolved
return @uri
end
diff --git a/spec/lib/final_destination_spec.rb b/spec/lib/final_destination_spec.rb
index 7dd1c0f5755..a6ae8e937c7 100644
--- a/spec/lib/final_destination_spec.rb
+++ b/spec/lib/final_destination_spec.rb
@@ -57,7 +57,55 @@ RSpec.describe FinalDestination do
ignore_redirects: %w[http://google.com youtube.com https://meta.discourse.org ://bing.com],
)
- expect(fd.ignored).to eq(%w[test.localhost google.com meta.discourse.org])
+ expect(fd.ignored).to eq(
+ [
+ { hostname: "test.localhost", path: "" },
+ { hostname: "google.com", path: "" },
+ { hostname: "meta.discourse.org", path: "" },
+ ],
+ )
+ end
+
+ it "does not ignore URLs on the same domain but outside subfolder path" do
+ set_subfolder "/forum"
+
+ fd =
+ FinalDestination.new(
+ "https://test.localhost/blog/article",
+ ignore_redirects: ["https://test.localhost/forum"],
+ )
+
+ expect(fd.ignored).to include({ hostname: "test.localhost", path: "/forum" })
+
+ fd_stub_request(:head, "https://test.localhost/blog/article").to_return(
+ status: 200,
+ headers: {
+ "Content-Type" => "text/html",
+ },
+ )
+
+ result = fd.resolve
+ expect(result.to_s).to eq("https://test.localhost/blog/article")
+ expect(fd.status).to eq(:resolved)
+ end
+
+ it "still performs SSRF checks for URLs on same domain but outside subfolder" do
+ set_subfolder "/forum"
+
+ FinalDestination::SSRFDetector
+ .stubs(:lookup_and_filter_ips)
+ .with("test.localhost")
+ .raises(FinalDestination::SSRFDetector::DisallowedIpError)
+
+ fd =
+ FinalDestination.new(
+ "https://test.localhost/blog/article",
+ ignore_redirects: ["https://test.localhost/forum"],
+ )
+
+ result = fd.resolve
+ expect(result).to be_nil
+ expect(fd.status).to eq(:invalid_address)
end
it "raises an error when URL is too long to encode" do