2019-04-21 10:03:26 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::HTTPConnectionAdapter do
|
2019-07-24 03:48:44 +00:00
|
|
|
include StubRequests
|
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
let(:uri) { URI('https://example.org') }
|
|
|
|
let(:options) { {} }
|
|
|
|
|
|
|
|
subject(:connection) { described_class.new(uri, options).connection }
|
|
|
|
|
2019-04-21 10:03:26 +00:00
|
|
|
describe '#connection' do
|
2019-07-24 03:48:44 +00:00
|
|
|
before do
|
|
|
|
stub_all_dns('https://example.org', ip_address: '93.184.216.34')
|
|
|
|
end
|
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when local requests are allowed' do
|
|
|
|
let(:options) { { allow_local_requests: true } }
|
|
|
|
|
2019-04-21 10:03:26 +00:00
|
|
|
it 'sets up the connection' do
|
2021-03-09 18:09:41 +00:00
|
|
|
expect(connection).to be_a(Net::HTTP)
|
|
|
|
expect(connection.address).to eq('93.184.216.34')
|
|
|
|
expect(connection.hostname_override).to eq('example.org')
|
|
|
|
expect(connection.addr_port).to eq('example.org')
|
|
|
|
expect(connection.port).to eq(443)
|
|
|
|
end
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when local requests are not allowed' do
|
|
|
|
let(:options) { { allow_local_requests: false } }
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
it 'sets up the connection' do
|
2019-04-21 10:03:26 +00:00
|
|
|
expect(connection).to be_a(Net::HTTP)
|
|
|
|
expect(connection.address).to eq('93.184.216.34')
|
|
|
|
expect(connection.hostname_override).to eq('example.org')
|
|
|
|
expect(connection.addr_port).to eq('example.org')
|
|
|
|
expect(connection.port).to eq(443)
|
|
|
|
end
|
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when it is a request to local network' do
|
|
|
|
let(:uri) { URI('http://172.16.0.0/12') }
|
|
|
|
|
|
|
|
it 'raises error' do
|
|
|
|
expect { subject }.to raise_error(
|
|
|
|
Gitlab::HTTP::BlockedUrlError,
|
|
|
|
"URL 'http://172.16.0.0/12' is blocked: Requests to the local network are not allowed"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when local request allowed' do
|
|
|
|
let(:options) { { allow_local_requests: true } }
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
it 'sets up the connection' do
|
|
|
|
expect(connection).to be_a(Net::HTTP)
|
|
|
|
expect(connection.address).to eq('172.16.0.0')
|
|
|
|
expect(connection.hostname_override).to be(nil)
|
|
|
|
expect(connection.addr_port).to eq('172.16.0.0')
|
|
|
|
expect(connection.port).to eq(80)
|
|
|
|
end
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
end
|
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when it is a request to local address' do
|
|
|
|
let(:uri) { URI('http://127.0.0.1') }
|
|
|
|
|
|
|
|
it 'raises error' do
|
|
|
|
expect { subject }.to raise_error(
|
|
|
|
Gitlab::HTTP::BlockedUrlError,
|
|
|
|
"URL 'http://127.0.0.1' is blocked: Requests to localhost are not allowed"
|
|
|
|
)
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when local request allowed' do
|
|
|
|
let(:options) { { allow_local_requests: true } }
|
|
|
|
|
|
|
|
it 'sets up the connection' do
|
|
|
|
expect(connection).to be_a(Net::HTTP)
|
|
|
|
expect(connection.address).to eq('127.0.0.1')
|
|
|
|
expect(connection.hostname_override).to be(nil)
|
|
|
|
expect(connection.addr_port).to eq('127.0.0.1')
|
|
|
|
expect(connection.port).to eq(80)
|
|
|
|
end
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when port different from URL scheme is used' do
|
2021-03-09 18:09:41 +00:00
|
|
|
let(:uri) { URI('https://example.org:8080') }
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
it 'sets up the addr_port accordingly' do
|
|
|
|
expect(connection).to be_a(Net::HTTP)
|
2019-04-21 10:03:26 +00:00
|
|
|
expect(connection.address).to eq('93.184.216.34')
|
|
|
|
expect(connection.hostname_override).to eq('example.org')
|
|
|
|
expect(connection.addr_port).to eq('example.org:8080')
|
|
|
|
expect(connection.port).to eq(8080)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-29 16:43:07 +00:00
|
|
|
context 'when DNS rebinding protection is disabled' do
|
2021-03-09 18:09:41 +00:00
|
|
|
before do
|
2019-05-29 16:43:07 +00:00
|
|
|
stub_application_setting(dns_rebinding_protection_enabled: false)
|
2021-03-09 18:09:41 +00:00
|
|
|
end
|
2019-05-29 16:43:07 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
it 'sets up the connection' do
|
2019-05-29 16:43:07 +00:00
|
|
|
expect(connection).to be_a(Net::HTTP)
|
|
|
|
expect(connection.address).to eq('example.org')
|
|
|
|
expect(connection.hostname_override).to eq(nil)
|
|
|
|
expect(connection.addr_port).to eq('example.org')
|
|
|
|
expect(connection.port).to eq(443)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when http(s) environment variable is set' do
|
2021-03-09 18:09:41 +00:00
|
|
|
before do
|
2019-05-29 16:43:07 +00:00
|
|
|
stub_env('https_proxy' => 'https://my.proxy')
|
2021-03-09 18:09:41 +00:00
|
|
|
end
|
2019-05-29 16:43:07 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
it 'sets up the connection' do
|
2019-05-29 16:43:07 +00:00
|
|
|
expect(connection).to be_a(Net::HTTP)
|
|
|
|
expect(connection.address).to eq('example.org')
|
|
|
|
expect(connection.hostname_override).to eq(nil)
|
|
|
|
expect(connection.addr_port).to eq('example.org')
|
|
|
|
expect(connection.port).to eq(443)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when proxy settings are configured' do
|
|
|
|
let(:options) do
|
|
|
|
{
|
|
|
|
http_proxyaddr: 'https://proxy.org',
|
|
|
|
http_proxyport: 1557,
|
|
|
|
http_proxyuser: 'user',
|
|
|
|
http_proxypass: 'pass'
|
|
|
|
}
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
before do
|
|
|
|
stub_all_dns('https://proxy.org', ip_address: '166.84.12.54')
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
it 'sets up the proxy settings' do
|
|
|
|
expect(connection.proxy_address).to eq('https://166.84.12.54')
|
|
|
|
expect(connection.proxy_port).to eq(1557)
|
|
|
|
expect(connection.proxy_user).to eq('user')
|
|
|
|
expect(connection.proxy_pass).to eq('pass')
|
2019-04-21 10:03:26 +00:00
|
|
|
end
|
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when the address has path' do
|
|
|
|
before do
|
|
|
|
options[:http_proxyaddr] = 'https://proxy.org/path'
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
it 'sets up the proxy settings' do
|
|
|
|
expect(connection.proxy_address).to eq('https://166.84.12.54/path')
|
|
|
|
expect(connection.proxy_port).to eq(1557)
|
|
|
|
end
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when the port is in the address and port' do
|
|
|
|
before do
|
|
|
|
options[:http_proxyaddr] = 'https://proxy.org:1422'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets up the proxy settings' do
|
|
|
|
expect(connection.proxy_address).to eq('https://166.84.12.54')
|
|
|
|
expect(connection.proxy_port).to eq(1557)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the port is only in the address' do
|
|
|
|
before do
|
|
|
|
options[:http_proxyport] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets up the proxy settings' do
|
|
|
|
expect(connection.proxy_address).to eq('https://166.84.12.54')
|
|
|
|
expect(connection.proxy_port).to eq(1422)
|
|
|
|
end
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
end
|
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when it is a request to local network' do
|
|
|
|
before do
|
|
|
|
options[:http_proxyaddr] = 'http://172.16.0.0/12'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises error' do
|
|
|
|
expect { subject }.to raise_error(
|
|
|
|
Gitlab::HTTP::BlockedUrlError,
|
|
|
|
"URL 'http://172.16.0.0:1557/12' is blocked: Requests to the local network are not allowed"
|
|
|
|
)
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
context 'when local request allowed' do
|
|
|
|
before do
|
|
|
|
options[:allow_local_requests] = true
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
2021-03-09 18:09:41 +00:00
|
|
|
it 'sets up the connection' do
|
|
|
|
expect(connection.proxy_address).to eq('http://172.16.0.0/12')
|
|
|
|
expect(connection.proxy_port).to eq(1557)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is a request to local address' do
|
|
|
|
before do
|
|
|
|
options[:http_proxyaddr] = 'http://127.0.0.1'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises error' do
|
|
|
|
expect { subject }.to raise_error(
|
|
|
|
Gitlab::HTTP::BlockedUrlError,
|
|
|
|
"URL 'http://127.0.0.1:1557' is blocked: Requests to localhost are not allowed"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when local request allowed' do
|
|
|
|
before do
|
|
|
|
options[:allow_local_requests] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets up the connection' do
|
|
|
|
expect(connection.proxy_address).to eq('http://127.0.0.1')
|
|
|
|
expect(connection.proxy_port).to eq(1557)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when http(s) environment variable is set' do
|
|
|
|
before do
|
|
|
|
stub_env('https_proxy' => 'https://my.proxy')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets up the connection' do
|
|
|
|
expect(connection.proxy_address).to eq('https://proxy.org')
|
|
|
|
expect(connection.proxy_port).to eq(1557)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when DNS rebinding protection is disabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(dns_rebinding_protection_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets up the connection' do
|
|
|
|
expect(connection.proxy_address).to eq('https://proxy.org')
|
|
|
|
expect(connection.proxy_port).to eq(1557)
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|