2019-06-05 12:25:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
RSpec.describe Gitlab::HTTPConnectionAdapter, feature_category: :shared do
|
2019-10-12 21:52:04 +05:30
|
|
|
include StubRequests
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:uri) { URI('https://example.org') }
|
|
|
|
let(:options) { {} }
|
|
|
|
|
|
|
|
subject(:connection) { described_class.new(uri, options).connection }
|
|
|
|
|
2019-06-05 12:25:43 +05:30
|
|
|
describe '#connection' do
|
2019-10-12 21:52:04 +05:30
|
|
|
before do
|
|
|
|
stub_all_dns('https://example.org', ip_address: '93.184.216.34')
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when local requests are allowed' do
|
|
|
|
let(:options) { { allow_local_requests: true } }
|
|
|
|
|
2019-06-05 12:25:43 +05:30
|
|
|
it 'sets up the connection' do
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(connection).to be_a(Gitlab::NetHttpAdapter)
|
2021-04-17 20:07:23 +05:30
|
|
|
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-06-05 12:25:43 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when local requests are not allowed' do
|
|
|
|
let(:options) { { allow_local_requests: false } }
|
2019-06-05 12:25:43 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'sets up the connection' do
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(connection).to be_a(Gitlab::NetHttpAdapter)
|
2019-06-05 12:25:43 +05:30
|
|
|
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-04-17 20:07:23 +05:30
|
|
|
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,
|
2023-03-04 22:38:38 +05:30
|
|
|
"URL is blocked: Requests to the local network are not allowed"
|
2021-04-17 20:07:23 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when local request allowed' do
|
|
|
|
let(:options) { { allow_local_requests: true } }
|
2019-06-05 12:25:43 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'sets up the connection' do
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(connection).to be_a(Gitlab::NetHttpAdapter)
|
2021-04-17 20:07:23 +05:30
|
|
|
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-06-05 12:25:43 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
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,
|
2023-03-04 22:38:38 +05:30
|
|
|
"URL is blocked: Requests to localhost are not allowed"
|
2021-04-17 20:07:23 +05:30
|
|
|
)
|
|
|
|
end
|
2019-06-05 12:25:43 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when local request allowed' do
|
|
|
|
let(:options) { { allow_local_requests: true } }
|
|
|
|
|
|
|
|
it 'sets up the connection' do
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(connection).to be_a(Gitlab::NetHttpAdapter)
|
2021-04-17 20:07:23 +05:30
|
|
|
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-06-05 12:25:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when port different from URL scheme is used' do
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:uri) { URI('https://example.org:8080') }
|
2019-06-05 12:25:43 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'sets up the addr_port accordingly' do
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(connection).to be_a(Gitlab::NetHttpAdapter)
|
2019-06-05 12:25:43 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
context 'when DNS rebinding protection is disabled' do
|
2021-04-17 20:07:23 +05:30
|
|
|
before do
|
2019-06-05 12:25:43 +05:30
|
|
|
stub_application_setting(dns_rebinding_protection_enabled: false)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
2019-06-05 12:25:43 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'sets up the connection' do
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(connection).to be_a(Gitlab::NetHttpAdapter)
|
2019-06-05 12:25:43 +05:30
|
|
|
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
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
context 'when proxy is enabled' do
|
2021-04-17 20:07:23 +05:30
|
|
|
before do
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_env('http_proxy', 'http://proxy.example.com')
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
2019-06-05 12:25:43 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it 'proxy stays configured' do
|
|
|
|
expect(connection.proxy?).to be true
|
|
|
|
expect(connection.proxy_from_env?).to be true
|
|
|
|
expect(connection.proxy_address).to eq('proxy.example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no_proxy matches the request' do
|
|
|
|
before do
|
|
|
|
stub_env('no_proxy', 'example.org')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'proxy is disabled' do
|
|
|
|
expect(connection.proxy?).to be false
|
|
|
|
expect(connection.proxy_from_env?).to be false
|
|
|
|
expect(connection.proxy_address).to be nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no_proxy does not match the request' do
|
|
|
|
before do
|
|
|
|
stub_env('no_proxy', 'example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'proxy stays configured' do
|
|
|
|
expect(connection.proxy?).to be true
|
|
|
|
expect(connection.proxy_from_env?).to be true
|
|
|
|
expect(connection.proxy_address).to eq('proxy.example.com')
|
|
|
|
end
|
2019-06-05 12:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
context 'when URL scheme is not HTTP/HTTPS' do
|
|
|
|
let(:uri) { URI('ssh://example.org') }
|
|
|
|
|
|
|
|
it 'raises error' do
|
|
|
|
expect { subject }.to raise_error(
|
|
|
|
Gitlab::HTTP::BlockedUrlError,
|
|
|
|
"URL is blocked: Only allowed schemes are http, https"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2019-06-05 12:25:43 +05:30
|
|
|
end
|
|
|
|
end
|