Skip to content

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

SECURITY: Reject disallowed ports in onebox fetches

Nat99cca912ac6f
diff --git a/lib/onebox/helpers.rb b/lib/onebox/helpers.rb
index d84bc42fe73..0b7e66eebbc 100644
--- a/lib/onebox/helpers.rb
+++ b/lib/onebox/helpers.rb
@@ -78,6 +78,7 @@ module Onebox
uri = Addressable::URI.parse(location)
uri = Addressable::URI.join(domain, uri) if !uri.host
+ raise FinalDestination::SSRFError unless FinalDestination.new(uri.to_s).validate_uri
result = StringIO.new
FinalDestination::HTTP.start(
diff --git a/spec/lib/onebox/helpers_spec.rb b/spec/lib/onebox/helpers_spec.rb
index 885a7adc1b0..85a917503b4 100644
--- a/spec/lib/onebox/helpers_spec.rb
+++ b/spec/lib/onebox/helpers_spec.rb
@@ -50,6 +50,39 @@ RSpec.describe Onebox::Helpers do
)
end
end
+
+ it "rejects an unapproved port on an allowlisted internal host" do
+ SiteSetting.s3_endpoint = "http://minio.local:9000"
+ SiteSetting.allowed_internal_hosts = "minio.local"
+ internal_url = "http://minio.local:9200/oembed.json"
+ stub_request(:get, internal_url).to_return(status: 200, body: "internal")
+
+ expect { described_class.fetch_response(internal_url) }.to raise_error(
+ FinalDestination::SSRFError,
+ )
+ expect(WebMock).not_to have_requested(:get, internal_url)
+ end
+
+ it "fetches an allowlisted internal host on its configured S3 port" do
+ SiteSetting.s3_endpoint = "http://minio.local:9000"
+ SiteSetting.allowed_internal_hosts = "minio.local"
+ url = "http://minio.local:9000/oembed.json"
+ stub_request(:get, url).to_return(status: 200, body: "oembed")
+
+ expect(described_class.fetch_response(url)).to eq("oembed")
+ end
+
+ it "rejects redirects to an unapproved port on an allowlisted internal host" do
+ SiteSetting.s3_endpoint = "http://minio.local:9000"
+ SiteSetting.allowed_internal_hosts = "minio.local"
+ url = "https://example.com/oembed"
+ internal_url = "http://minio.local:9200/oembed.json"
+ stub_request(:get, url).to_return(status: 302, headers: { location: internal_url })
+ stub_request(:get, internal_url).to_return(status: 200, body: "internal")
+
+ expect { described_class.fetch_response(url) }.to raise_error(FinalDestination::SSRFError)
+ expect(WebMock).not_to have_requested(:get, internal_url)
+ end
end
describe "fetch_html_doc" do