Skip to content

Commit 5c7bfb5

Browse files
committed
Fix API endpoint domain clamping
1 parent 0738ef3 commit 5c7bfb5

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/rubygems/remote_fetcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def api_endpoint(uri)
9696
else
9797
target = res.target.to_s.strip
9898

99-
if /#{host}\z/ =~ target
99+
if /\.#{Regexp.quote(host)}\z/ =~ target
100100
return URI.parse "#{uri.scheme}://#{target}#{uri.path}"
101101
end
102102

test/rubygems/test_gem_remote_fetcher.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,36 @@ def test_api_endpoint_ignores_trans_domain_values
196196
dns.verify
197197
end
198198

199+
def test_api_endpoint_ignores_trans_domain_values_that_starts_with_original
200+
uri = URI.parse "http://example.com/foo"
201+
target = MiniTest::Mock.new
202+
target.expect :target, "example.combadguy.com"
203+
204+
dns = MiniTest::Mock.new
205+
dns.expect :getresource, target, [String, Object]
206+
207+
fetch = Gem::RemoteFetcher.new nil, dns
208+
assert_equal URI.parse("http://example.com/foo"), fetch.api_endpoint(uri)
209+
210+
target.verify
211+
dns.verify
212+
end
213+
214+
def test_api_endpoint_ignores_trans_domain_values_that_end_with_original
215+
uri = URI.parse "http://example.com/foo"
216+
target = MiniTest::Mock.new
217+
target.expect :target, "badexample.com"
218+
219+
dns = MiniTest::Mock.new
220+
dns.expect :getresource, target, [String, Object]
221+
222+
fetch = Gem::RemoteFetcher.new nil, dns
223+
assert_equal URI.parse("http://example.com/foo"), fetch.api_endpoint(uri)
224+
225+
target.verify
226+
dns.verify
227+
end
228+
199229
def test_cache_update_path
200230
uri = URI 'http://example/file'
201231
path = File.join @tempdir, 'file'

0 commit comments

Comments
 (0)