2019-08-22 10:57:44 +00:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2017-03-15 20:09:08 +00:00
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
|
RSpec.describe Gitlab::UrlBlocker, :stub_invalid_dns_only do
|
2019-09-05 06:07:17 +00:00
|
|
|
|
include StubRequests
|
|
|
|
|
|
2019-04-21 10:03:26 +00:00
|
|
|
|
describe '#validate!' do
|
2019-09-05 06:07:17 +00:00
|
|
|
|
subject { described_class.validate!(import_url) }
|
|
|
|
|
|
|
|
|
|
shared_examples 'validates URI and hostname' do
|
|
|
|
|
it 'runs the url validations' do
|
|
|
|
|
uri, hostname = subject
|
|
|
|
|
|
|
|
|
|
expect(uri).to eq(Addressable::URI.parse(expected_uri))
|
|
|
|
|
expect(hostname).to eq(expected_hostname)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-21 10:03:26 +00:00
|
|
|
|
context 'when URI is nil' do
|
|
|
|
|
let(:import_url) { nil }
|
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
|
|
|
|
let(:expected_uri) { nil }
|
|
|
|
|
let(:expected_hostname) { nil }
|
2019-04-21 10:03:26 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when URI is internal' do
|
|
|
|
|
let(:import_url) { 'http://localhost' }
|
|
|
|
|
|
2019-09-13 13:26:31 +00:00
|
|
|
|
before do
|
|
|
|
|
stub_dns(import_url, ip_address: '127.0.0.1')
|
|
|
|
|
end
|
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
2019-09-13 13:26:31 +00:00
|
|
|
|
let(:expected_uri) { 'http://127.0.0.1' }
|
2019-09-05 06:07:17 +00:00
|
|
|
|
let(:expected_hostname) { 'localhost' }
|
2019-04-21 10:03:26 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the URL hostname is a domain' do
|
2019-09-05 06:07:17 +00:00
|
|
|
|
context 'when domain can be resolved' do
|
|
|
|
|
let(:import_url) { 'https://example.org' }
|
2019-04-21 10:03:26 +00:00
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
before do
|
|
|
|
|
stub_dns(import_url, ip_address: '93.184.216.34')
|
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
|
|
|
|
let(:expected_uri) { 'https://93.184.216.34' }
|
|
|
|
|
let(:expected_hostname) { 'example.org' }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when domain cannot be resolved' do
|
|
|
|
|
let(:import_url) { 'http://foobar.x' }
|
|
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
|
stub_env('RSPEC_ALLOW_INVALID_URLS', 'false')
|
|
|
|
|
|
|
|
|
|
expect { subject }.to raise_error(described_class::BlockedUrlError)
|
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
end
|
2019-10-03 21:07:29 +00:00
|
|
|
|
|
|
|
|
|
context 'when domain is too long' do
|
|
|
|
|
let(:import_url) { 'https://example' + 'a' * 1024 + '.com' }
|
|
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
|
expect { subject }.to raise_error(described_class::BlockedUrlError)
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the URL hostname is an IP address' do
|
|
|
|
|
let(:import_url) { 'https://93.184.216.34' }
|
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
|
|
|
|
let(:expected_uri) { import_url }
|
|
|
|
|
let(:expected_hostname) { nil }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the address is invalid' do
|
|
|
|
|
let(:import_url) { 'http://1.1.1.1.1' }
|
2019-04-21 10:03:26 +00:00
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
it 'raises an error' do
|
|
|
|
|
stub_env('RSPEC_ALLOW_INVALID_URLS', 'false')
|
|
|
|
|
|
|
|
|
|
expect { subject }.to raise_error(described_class::BlockedUrlError)
|
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-05-29 16:43:07 +00:00
|
|
|
|
|
2021-02-02 00:09:14 +00:00
|
|
|
|
context 'DNS rebinding protection with IP allowed' do
|
|
|
|
|
let(:import_url) { 'http://a.192.168.0.120.3times.127.0.0.1.1time.repeat.rebind.network:9121/scrape?target=unix:///var/opt/gitlab/redis/redis.socket&check-keys=*' }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
stub_dns(import_url, ip_address: '192.168.0.120')
|
|
|
|
|
|
|
|
|
|
allow(Gitlab::UrlBlockers::UrlAllowlist).to receive(:ip_allowed?).and_return(true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
|
|
|
|
let(:expected_uri) { 'http://192.168.0.120:9121/scrape?target=unix:///var/opt/gitlab/redis/redis.socket&check-keys=*' }
|
|
|
|
|
let(:expected_hostname) { 'a.192.168.0.120.3times.127.0.0.1.1time.repeat.rebind.network' }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-05-29 16:43:07 +00:00
|
|
|
|
context 'disabled DNS rebinding protection' do
|
2019-09-05 06:07:17 +00:00
|
|
|
|
subject { described_class.validate!(import_url, dns_rebind_protection: false) }
|
|
|
|
|
|
2019-05-29 16:43:07 +00:00
|
|
|
|
context 'when URI is internal' do
|
|
|
|
|
let(:import_url) { 'http://localhost' }
|
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
|
|
|
|
let(:expected_uri) { import_url }
|
|
|
|
|
let(:expected_hostname) { nil }
|
2019-05-29 16:43:07 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the URL hostname is a domain' do
|
|
|
|
|
let(:import_url) { 'https://example.org' }
|
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
before do
|
|
|
|
|
stub_env('RSPEC_ALLOW_INVALID_URLS', 'false')
|
|
|
|
|
end
|
2019-05-29 16:43:07 +00:00
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
context 'when domain can be resolved' do
|
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
|
|
|
|
let(:expected_uri) { import_url }
|
|
|
|
|
let(:expected_hostname) { nil }
|
|
|
|
|
end
|
2019-05-29 16:43:07 +00:00
|
|
|
|
end
|
2019-07-02 16:38:23 +00:00
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
context 'when domain cannot be resolved' do
|
2019-07-02 16:38:23 +00:00
|
|
|
|
let(:import_url) { 'http://foobar.x' }
|
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
|
|
|
|
let(:expected_uri) { import_url }
|
|
|
|
|
let(:expected_hostname) { nil }
|
2019-07-02 16:38:23 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-05-29 16:43:07 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the URL hostname is an IP address' do
|
|
|
|
|
let(:import_url) { 'https://93.184.216.34' }
|
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
|
|
|
|
let(:expected_uri) { import_url }
|
|
|
|
|
let(:expected_hostname) { nil }
|
2019-05-29 16:43:07 +00:00
|
|
|
|
end
|
2019-07-02 16:38:23 +00:00
|
|
|
|
|
|
|
|
|
context 'when it is invalid' do
|
|
|
|
|
let(:import_url) { 'http://1.1.1.1.1' }
|
|
|
|
|
|
2019-09-05 06:07:17 +00:00
|
|
|
|
it_behaves_like 'validates URI and hostname' do
|
|
|
|
|
let(:expected_uri) { import_url }
|
|
|
|
|
let(:expected_hostname) { nil }
|
2019-07-02 16:38:23 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-05-29 16:43:07 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-04-21 10:03:26 +00:00
|
|
|
|
end
|
|
|
|
|
|
2017-03-15 20:09:08 +00:00
|
|
|
|
describe '#blocked_url?' do
|
2018-06-01 11:43:53 +00:00
|
|
|
|
let(:ports) { Project::VALID_IMPORT_PORTS }
|
2018-03-13 22:38:25 +00:00
|
|
|
|
|
2017-03-15 20:09:08 +00:00
|
|
|
|
it 'allows imports from configured web host and port' do
|
2021-03-10 09:09:29 +00:00
|
|
|
|
import_url = "http://#{Gitlab.host_with_port}/t.git"
|
2017-03-15 20:09:08 +00:00
|
|
|
|
expect(described_class.blocked_url?(import_url)).to be false
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-28 18:37:12 +00:00
|
|
|
|
it 'allows mirroring from configured SSH host and port' do
|
|
|
|
|
import_url = "ssh://#{Gitlab.config.gitlab_shell.ssh_host}:#{Gitlab.config.gitlab_shell.ssh_port}/t.git"
|
2017-03-15 20:09:08 +00:00
|
|
|
|
expect(described_class.blocked_url?(import_url)).to be false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true for bad localhost hostname' do
|
|
|
|
|
expect(described_class.blocked_url?('https://localhost:65535/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true for bad port' do
|
2018-06-01 11:43:53 +00:00
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com:25/foo/foo.git', ports: ports)).to be true
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-11 06:29:07 +00:00
|
|
|
|
it 'returns true for bad scheme' do
|
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com/foo/foo.git', schemes: ['https'])).to be false
|
2018-06-01 11:43:53 +00:00
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com/foo/foo.git')).to be false
|
2019-04-11 06:29:07 +00:00
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com/foo/foo.git', schemes: ['http'])).to be true
|
2017-03-15 20:09:08 +00:00
|
|
|
|
end
|
|
|
|
|
|
2018-11-28 18:37:12 +00:00
|
|
|
|
it 'returns true for bad protocol on configured web/SSH host and ports' do
|
2021-03-10 09:09:29 +00:00
|
|
|
|
web_url = "javascript://#{Gitlab.host_with_port}/t.git%0aalert(1)"
|
2018-11-28 18:37:12 +00:00
|
|
|
|
expect(described_class.blocked_url?(web_url)).to be true
|
|
|
|
|
|
|
|
|
|
ssh_url = "javascript://#{Gitlab.config.gitlab_shell.ssh_host}:#{Gitlab.config.gitlab_shell.ssh_port}/t.git%0aalert(1)"
|
|
|
|
|
expect(described_class.blocked_url?(ssh_url)).to be true
|
|
|
|
|
end
|
|
|
|
|
|
2018-09-06 04:41:59 +00:00
|
|
|
|
it 'returns true for localhost IPs' do
|
2018-11-26 08:45:38 +00:00
|
|
|
|
expect(described_class.blocked_url?('https://[0:0:0:0:0:0:0:0]/foo/foo.git')).to be true
|
2018-09-06 04:41:59 +00:00
|
|
|
|
expect(described_class.blocked_url?('https://0.0.0.0/foo/foo.git')).to be true
|
2018-11-26 08:45:38 +00:00
|
|
|
|
expect(described_class.blocked_url?('https://[::]/foo/foo.git')).to be true
|
2018-09-06 04:41:59 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true for loopback IP' do
|
|
|
|
|
expect(described_class.blocked_url?('https://127.0.0.2/foo/foo.git')).to be true
|
2018-11-26 08:45:38 +00:00
|
|
|
|
expect(described_class.blocked_url?('https://127.0.0.1/foo/foo.git')).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://[::1]/foo/foo.git')).to be true
|
2018-09-06 04:41:59 +00:00
|
|
|
|
end
|
|
|
|
|
|
2017-11-07 08:30:38 +00:00
|
|
|
|
it 'returns true for alternative version of 127.0.0.1 (0177.1)' do
|
|
|
|
|
expect(described_class.blocked_url?('https://0177.1:65535/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-26 08:45:38 +00:00
|
|
|
|
it 'returns true for alternative version of 127.0.0.1 (017700000001)' do
|
|
|
|
|
expect(described_class.blocked_url?('https://017700000001:65535/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
2017-11-07 08:30:38 +00:00
|
|
|
|
it 'returns true for alternative version of 127.0.0.1 (0x7f.1)' do
|
|
|
|
|
expect(described_class.blocked_url?('https://0x7f.1:65535/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-26 08:45:38 +00:00
|
|
|
|
it 'returns true for alternative version of 127.0.0.1 (0x7f.0.0.1)' do
|
|
|
|
|
expect(described_class.blocked_url?('https://0x7f.0.0.1:65535/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true for alternative version of 127.0.0.1 (0x7f000001)' do
|
|
|
|
|
expect(described_class.blocked_url?('https://0x7f000001:65535/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
2017-11-07 08:30:38 +00:00
|
|
|
|
it 'returns true for alternative version of 127.0.0.1 (2130706433)' do
|
|
|
|
|
expect(described_class.blocked_url?('https://2130706433:65535/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true for alternative version of 127.0.0.1 (127.000.000.001)' do
|
|
|
|
|
expect(described_class.blocked_url?('https://127.000.000.001:65535/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-26 08:45:38 +00:00
|
|
|
|
it 'returns true for alternative version of 127.0.0.1 (127.0.1)' do
|
|
|
|
|
expect(described_class.blocked_url?('https://127.0.1:65535/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with ipv6 mapped address' do
|
|
|
|
|
it 'returns true for localhost IPs' do
|
|
|
|
|
expect(described_class.blocked_url?('https://[0:0:0:0:0:ffff:0.0.0.0]/foo/foo.git')).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://[::ffff:0.0.0.0]/foo/foo.git')).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://[::ffff:0:0]/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true for loopback IPs' do
|
|
|
|
|
expect(described_class.blocked_url?('https://[0:0:0:0:0:ffff:127.0.0.1]/foo/foo.git')).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://[::ffff:127.0.0.1]/foo/foo.git')).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://[::ffff:7f00:1]/foo/foo.git')).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://[0:0:0:0:0:ffff:127.0.0.2]/foo/foo.git')).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://[::ffff:127.0.0.2]/foo/foo.git')).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://[::ffff:7f00:2]/foo/foo.git')).to be true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-08-08 17:36:24 +00:00
|
|
|
|
it 'returns true for a non-alphanumeric hostname' do
|
|
|
|
|
aggregate_failures do
|
|
|
|
|
expect(described_class).to be_blocked_url('ssh://-oProxyCommand=whoami/a')
|
|
|
|
|
|
|
|
|
|
# The leading character here is a Unicode "soft hyphen"
|
|
|
|
|
expect(described_class).to be_blocked_url('ssh://oProxyCommand=whoami/a')
|
|
|
|
|
|
|
|
|
|
# Unicode alphanumerics are allowed
|
|
|
|
|
expect(described_class).not_to be_blocked_url('ssh://ğitlab.com/a')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-15 20:09:08 +00:00
|
|
|
|
it 'returns true for invalid URL' do
|
|
|
|
|
expect(described_class.blocked_url?('http://:8080')).to be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns false for legitimate URL' do
|
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com/foo/foo.git')).to be false
|
|
|
|
|
end
|
2018-03-13 22:38:25 +00:00
|
|
|
|
|
2018-04-02 15:13:47 +00:00
|
|
|
|
context 'when allow_local_network is' do
|
2018-11-26 08:45:38 +00:00
|
|
|
|
let(:local_ips) do
|
|
|
|
|
[
|
|
|
|
|
'192.168.1.2',
|
|
|
|
|
'[0:0:0:0:0:ffff:192.168.1.2]',
|
|
|
|
|
'[::ffff:c0a8:102]',
|
|
|
|
|
'10.0.0.2',
|
|
|
|
|
'[0:0:0:0:0:ffff:10.0.0.2]',
|
|
|
|
|
'[::ffff:a00:2]',
|
|
|
|
|
'172.16.0.2',
|
|
|
|
|
'[0:0:0:0:0:ffff:172.16.0.2]',
|
|
|
|
|
'[::ffff:ac10:20]',
|
|
|
|
|
'[feef::1]',
|
|
|
|
|
'[fee2::]',
|
|
|
|
|
'[fc00:bf8b:e62c:abcd:abcd:aaaa:aaaa:aaaa]'
|
|
|
|
|
]
|
|
|
|
|
end
|
2020-08-11 03:11:00 +00:00
|
|
|
|
|
2018-03-13 22:38:25 +00:00
|
|
|
|
let(:fake_domain) { 'www.fakedomain.fake' }
|
|
|
|
|
|
2019-07-24 17:59:38 +00:00
|
|
|
|
shared_examples 'allows local requests' do |url_blocker_attributes|
|
2018-03-13 22:38:25 +00:00
|
|
|
|
it 'does not block urls from private networks' do
|
2018-04-02 15:13:47 +00:00
|
|
|
|
local_ips.each do |ip|
|
2019-07-24 17:59:38 +00:00
|
|
|
|
stub_domain_resolv(fake_domain, ip) do
|
2021-02-03 09:09:07 +00:00
|
|
|
|
expect(described_class).not_to be_blocked_url("http://#{fake_domain}", **url_blocker_attributes)
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
2018-03-13 22:38:25 +00:00
|
|
|
|
|
2021-02-03 09:09:07 +00:00
|
|
|
|
expect(described_class).not_to be_blocked_url("http://#{ip}", **url_blocker_attributes)
|
2018-03-13 22:38:25 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2018-08-11 10:38:21 +00:00
|
|
|
|
|
2018-09-06 04:41:59 +00:00
|
|
|
|
it 'allows localhost endpoints' do
|
2021-02-03 09:09:07 +00:00
|
|
|
|
expect(described_class).not_to be_blocked_url('http://0.0.0.0', **url_blocker_attributes)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://localhost', **url_blocker_attributes)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://127.0.0.1', **url_blocker_attributes)
|
2018-09-06 04:41:59 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'allows loopback endpoints' do
|
2021-02-03 09:09:07 +00:00
|
|
|
|
expect(described_class).not_to be_blocked_url('http://127.0.0.2', **url_blocker_attributes)
|
2018-09-06 04:41:59 +00:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-11 10:38:21 +00:00
|
|
|
|
it 'allows IPv4 link-local endpoints' do
|
2021-02-03 09:09:07 +00:00
|
|
|
|
expect(described_class).not_to be_blocked_url('http://169.254.169.254', **url_blocker_attributes)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://169.254.168.100', **url_blocker_attributes)
|
2018-08-11 10:38:21 +00:00
|
|
|
|
end
|
|
|
|
|
|
2018-11-26 08:45:38 +00:00
|
|
|
|
it 'allows IPv6 link-local endpoints' do
|
2021-02-03 09:09:07 +00:00
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[0:0:0:0:0:ffff:169.254.169.254]', **url_blocker_attributes)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[::ffff:169.254.169.254]', **url_blocker_attributes)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[::ffff:a9fe:a9fe]', **url_blocker_attributes)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[0:0:0:0:0:ffff:169.254.168.100]', **url_blocker_attributes)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[::ffff:169.254.168.100]', **url_blocker_attributes)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[::ffff:a9fe:a864]', **url_blocker_attributes)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[fe80::c800:eff:fe74:8]', **url_blocker_attributes)
|
2018-08-11 10:38:21 +00:00
|
|
|
|
end
|
2018-03-13 22:38:25 +00:00
|
|
|
|
end
|
|
|
|
|
|
2019-07-24 17:59:38 +00:00
|
|
|
|
context 'true (default)' do
|
|
|
|
|
it_behaves_like 'allows local requests', { allow_localhost: true, allow_local_network: true }
|
|
|
|
|
end
|
|
|
|
|
|
2018-03-13 22:38:25 +00:00
|
|
|
|
context 'false' do
|
|
|
|
|
it 'blocks urls from private networks' do
|
2018-04-02 15:13:47 +00:00
|
|
|
|
local_ips.each do |ip|
|
2019-07-24 17:59:38 +00:00
|
|
|
|
stub_domain_resolv(fake_domain, ip) do
|
|
|
|
|
expect(described_class).to be_blocked_url("http://#{fake_domain}", allow_local_network: false)
|
|
|
|
|
end
|
2018-03-13 22:38:25 +00:00
|
|
|
|
|
2018-04-02 15:13:47 +00:00
|
|
|
|
expect(described_class).to be_blocked_url("http://#{ip}", allow_local_network: false)
|
2018-03-13 22:38:25 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2018-08-11 10:38:21 +00:00
|
|
|
|
|
|
|
|
|
it 'blocks IPv4 link-local endpoints' do
|
|
|
|
|
expect(described_class).to be_blocked_url('http://169.254.169.254', allow_local_network: false)
|
|
|
|
|
expect(described_class).to be_blocked_url('http://169.254.168.100', allow_local_network: false)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'blocks IPv6 link-local endpoints' do
|
2018-11-26 08:45:38 +00:00
|
|
|
|
expect(described_class).to be_blocked_url('http://[0:0:0:0:0:ffff:169.254.169.254]', allow_local_network: false)
|
2018-08-11 10:38:21 +00:00
|
|
|
|
expect(described_class).to be_blocked_url('http://[::ffff:169.254.169.254]', allow_local_network: false)
|
2018-11-26 08:45:38 +00:00
|
|
|
|
expect(described_class).to be_blocked_url('http://[::ffff:a9fe:a9fe]', allow_local_network: false)
|
|
|
|
|
expect(described_class).to be_blocked_url('http://[0:0:0:0:0:ffff:169.254.168.100]', allow_local_network: false)
|
2018-08-11 10:38:21 +00:00
|
|
|
|
expect(described_class).to be_blocked_url('http://[::ffff:169.254.168.100]', allow_local_network: false)
|
2018-11-26 08:45:38 +00:00
|
|
|
|
expect(described_class).to be_blocked_url('http://[::ffff:a9fe:a864]', allow_local_network: false)
|
|
|
|
|
expect(described_class).to be_blocked_url('http://[fe80::c800:eff:fe74:8]', allow_local_network: false)
|
2018-08-11 10:38:21 +00:00
|
|
|
|
end
|
2019-07-24 17:59:38 +00:00
|
|
|
|
|
2020-11-10 12:08:57 +00:00
|
|
|
|
context 'when local domain/IP is allowed' do
|
2019-07-24 17:59:38 +00:00
|
|
|
|
let(:url_blocker_attributes) do
|
|
|
|
|
{
|
|
|
|
|
allow_localhost: false,
|
|
|
|
|
allow_local_network: false
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
before do
|
2019-09-13 13:26:31 +00:00
|
|
|
|
allow(ApplicationSetting).to receive(:current).and_return(ApplicationSetting.new)
|
2020-11-10 12:08:57 +00:00
|
|
|
|
stub_application_setting(outbound_local_requests_whitelist: allowlist)
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
|
|
|
|
|
2020-11-10 12:08:57 +00:00
|
|
|
|
context 'with IPs in allowlist' do
|
|
|
|
|
let(:allowlist) do
|
2019-07-24 17:59:38 +00:00
|
|
|
|
[
|
|
|
|
|
'0.0.0.0',
|
|
|
|
|
'127.0.0.1',
|
|
|
|
|
'127.0.0.2',
|
|
|
|
|
'192.168.1.1',
|
|
|
|
|
'192.168.1.2',
|
|
|
|
|
'0:0:0:0:0:ffff:192.168.1.2',
|
|
|
|
|
'::ffff:c0a8:102',
|
|
|
|
|
'10.0.0.2',
|
|
|
|
|
'0:0:0:0:0:ffff:10.0.0.2',
|
|
|
|
|
'::ffff:a00:2',
|
|
|
|
|
'172.16.0.2',
|
|
|
|
|
'0:0:0:0:0:ffff:172.16.0.2',
|
|
|
|
|
'::ffff:ac10:20',
|
|
|
|
|
'feef::1',
|
|
|
|
|
'fee2::',
|
|
|
|
|
'fc00:bf8b:e62c:abcd:abcd:aaaa:aaaa:aaaa',
|
|
|
|
|
'0:0:0:0:0:ffff:169.254.169.254',
|
|
|
|
|
'::ffff:a9fe:a9fe',
|
|
|
|
|
'::ffff:169.254.168.100',
|
|
|
|
|
'::ffff:a9fe:a864',
|
|
|
|
|
'fe80::c800:eff:fe74:8',
|
|
|
|
|
|
|
|
|
|
# garbage IPs
|
|
|
|
|
'45645632345',
|
|
|
|
|
'garbage456:more345gar:bage'
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'allows local requests', { allow_localhost: false, allow_local_network: false }
|
|
|
|
|
|
2020-11-10 12:08:57 +00:00
|
|
|
|
it 'allows IP when dns_rebind_protection is disabled' do
|
2019-09-13 13:26:31 +00:00
|
|
|
|
url = "http://example.com"
|
|
|
|
|
attrs = url_blocker_attributes.merge(dns_rebind_protection: false)
|
|
|
|
|
|
|
|
|
|
stub_domain_resolv('example.com', '192.168.1.2') do
|
2021-02-03 09:09:07 +00:00
|
|
|
|
expect(described_class).not_to be_blocked_url(url, **attrs)
|
2019-09-13 13:26:31 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
stub_domain_resolv('example.com', '192.168.1.3') do
|
2021-02-03 09:09:07 +00:00
|
|
|
|
expect(described_class).to be_blocked_url(url, **attrs)
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-10 12:08:57 +00:00
|
|
|
|
context 'with domains in allowlist' do
|
|
|
|
|
let(:allowlist) do
|
2019-07-24 17:59:38 +00:00
|
|
|
|
[
|
|
|
|
|
'www.example.com',
|
|
|
|
|
'example.com',
|
|
|
|
|
'xn--itlab-j1a.com',
|
|
|
|
|
'garbage$^$%#$^&$'
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-10 12:08:57 +00:00
|
|
|
|
it 'allows domains present in allowlist' do
|
2019-07-24 17:59:38 +00:00
|
|
|
|
domain = 'example.com'
|
|
|
|
|
subdomain1 = 'www.example.com'
|
|
|
|
|
subdomain2 = 'subdomain.example.com'
|
|
|
|
|
|
|
|
|
|
stub_domain_resolv(domain, '192.168.1.1') do
|
|
|
|
|
expect(described_class).not_to be_blocked_url("http://#{domain}",
|
2021-02-03 09:09:07 +00:00
|
|
|
|
**url_blocker_attributes)
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
stub_domain_resolv(subdomain1, '192.168.1.1') do
|
|
|
|
|
expect(described_class).not_to be_blocked_url("http://#{subdomain1}",
|
2021-02-03 09:09:07 +00:00
|
|
|
|
**url_blocker_attributes)
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
|
|
|
|
|
2020-11-10 12:08:57 +00:00
|
|
|
|
# subdomain2 is not part of the allowlist so it should be blocked
|
2019-07-24 17:59:38 +00:00
|
|
|
|
stub_domain_resolv(subdomain2, '192.168.1.1') do
|
|
|
|
|
expect(described_class).to be_blocked_url("http://#{subdomain2}",
|
2021-02-03 09:09:07 +00:00
|
|
|
|
**url_blocker_attributes)
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'works with unicode and idna encoded domains' do
|
|
|
|
|
unicode_domain = 'ğitlab.com'
|
|
|
|
|
idna_encoded_domain = 'xn--itlab-j1a.com'
|
|
|
|
|
|
|
|
|
|
stub_domain_resolv(unicode_domain, '192.168.1.1') do
|
|
|
|
|
expect(described_class).not_to be_blocked_url("http://#{unicode_domain}",
|
2021-02-03 09:09:07 +00:00
|
|
|
|
**url_blocker_attributes)
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
stub_domain_resolv(idna_encoded_domain, '192.168.1.1') do
|
|
|
|
|
expect(described_class).not_to be_blocked_url("http://#{idna_encoded_domain}",
|
2021-02-03 09:09:07 +00:00
|
|
|
|
**url_blocker_attributes)
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
|
|
shared_examples 'dns rebinding checks' do
|
2020-11-10 12:08:57 +00:00
|
|
|
|
shared_examples 'allowlists the domain' do
|
|
|
|
|
let(:allowlist) { [domain] }
|
2019-09-13 13:26:31 +00:00
|
|
|
|
let(:url) { "http://#{domain}" }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
stub_env('RSPEC_ALLOW_INVALID_URLS', 'false')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
expect(described_class).not_to be_blocked_url(url, dns_rebind_protection: dns_rebind_value)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when dns_rebinding_setting is' do
|
|
|
|
|
context 'enabled' do
|
|
|
|
|
let(:dns_rebind_value) { true }
|
|
|
|
|
|
2020-11-10 12:08:57 +00:00
|
|
|
|
it_behaves_like 'allowlists the domain'
|
2019-09-13 13:26:31 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'disabled' do
|
|
|
|
|
let(:dns_rebind_value) { false }
|
|
|
|
|
|
2020-11-10 12:08:57 +00:00
|
|
|
|
it_behaves_like 'allowlists the domain'
|
2019-09-13 13:26:31 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the domain cannot be resolved' do
|
|
|
|
|
let(:domain) { 'foobar.x' }
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'dns rebinding checks'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the domain can be resolved' do
|
|
|
|
|
let(:domain) { 'example.com' }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
stub_dns(url, ip_address: '93.184.216.34')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like 'dns rebinding checks'
|
|
|
|
|
end
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
2020-03-16 03:09:14 +00:00
|
|
|
|
|
|
|
|
|
context 'with ports' do
|
2020-11-10 12:08:57 +00:00
|
|
|
|
let(:allowlist) do
|
2020-03-16 03:09:14 +00:00
|
|
|
|
["127.0.0.1:2000"]
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-10 12:08:57 +00:00
|
|
|
|
it 'allows domain with port when resolved ip has port allowed' do
|
2020-03-16 03:09:14 +00:00
|
|
|
|
stub_domain_resolv("www.resolve-domain.com", '127.0.0.1') do
|
2021-02-03 09:09:07 +00:00
|
|
|
|
expect(described_class).not_to be_blocked_url("http://www.resolve-domain.com:2000", **url_blocker_attributes)
|
2020-03-16 03:09:14 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-07-24 17:59:38 +00:00
|
|
|
|
end
|
2018-03-13 22:38:25 +00:00
|
|
|
|
end
|
|
|
|
|
|
2020-03-09 18:07:59 +00:00
|
|
|
|
def stub_domain_resolv(domain, ip, port = 80, &block)
|
|
|
|
|
address = instance_double(Addrinfo,
|
|
|
|
|
ip_address: ip,
|
|
|
|
|
ipv4_private?: true,
|
|
|
|
|
ipv6_linklocal?: false,
|
|
|
|
|
ipv4_loopback?: false,
|
|
|
|
|
ipv6_loopback?: false,
|
|
|
|
|
ipv4?: false,
|
|
|
|
|
ip_port: port
|
|
|
|
|
)
|
|
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).with(domain, port, any_args).and_return([address])
|
2018-11-26 08:45:38 +00:00
|
|
|
|
allow(address).to receive(:ipv6_v4mapped?).and_return(false)
|
2018-03-13 22:38:25 +00:00
|
|
|
|
|
2019-07-24 17:59:38 +00:00
|
|
|
|
yield
|
|
|
|
|
|
2018-03-13 22:38:25 +00:00
|
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).and_call_original
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-06-11 13:29:37 +00:00
|
|
|
|
|
|
|
|
|
context 'when enforce_user is' do
|
|
|
|
|
context 'false (default)' do
|
|
|
|
|
it 'does not block urls with a non-alphanumeric username' do
|
|
|
|
|
expect(described_class).not_to be_blocked_url('ssh://-oProxyCommand=whoami@example.com/a')
|
|
|
|
|
|
|
|
|
|
# The leading character here is a Unicode "soft hyphen"
|
|
|
|
|
expect(described_class).not_to be_blocked_url('ssh://oProxyCommand=whoami@example.com/a')
|
|
|
|
|
|
|
|
|
|
# Unicode alphanumerics are allowed
|
|
|
|
|
expect(described_class).not_to be_blocked_url('ssh://ğitlab@example.com/a')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'true' do
|
|
|
|
|
it 'blocks urls with a non-alphanumeric username' do
|
|
|
|
|
aggregate_failures do
|
|
|
|
|
expect(described_class).to be_blocked_url('ssh://-oProxyCommand=whoami@example.com/a', enforce_user: true)
|
|
|
|
|
|
|
|
|
|
# The leading character here is a Unicode "soft hyphen"
|
|
|
|
|
expect(described_class).to be_blocked_url('ssh://oProxyCommand=whoami@example.com/a', enforce_user: true)
|
|
|
|
|
|
|
|
|
|
# Unicode alphanumerics are allowed
|
|
|
|
|
expect(described_class).not_to be_blocked_url('ssh://ğitlab@example.com/a', enforce_user: true)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-12-05 20:14:09 +00:00
|
|
|
|
|
|
|
|
|
context 'when ascii_only is true' do
|
|
|
|
|
it 'returns true for unicode domain' do
|
|
|
|
|
expect(described_class.blocked_url?('https://𝕘itⅼαƄ.com/foo/foo.bar', ascii_only: true)).to be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true for unicode tld' do
|
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.ᴄοm/foo/foo.bar', ascii_only: true)).to be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true for unicode path' do
|
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com/𝒇οο/𝒇οο.Ƅαꮁ', ascii_only: true)).to be true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns true for IDNA deviations' do
|
|
|
|
|
expect(described_class.blocked_url?('https://mißile.com/foo/foo.bar', ascii_only: true)).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://miςςile.com/foo/foo.bar', ascii_only: true)).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com/foo/foo.bar', ascii_only: true)).to be true
|
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com/foo/foo.bar', ascii_only: true)).to be true
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-07-02 16:38:23 +00:00
|
|
|
|
|
|
|
|
|
it 'blocks urls with invalid ip address' do
|
|
|
|
|
stub_env('RSPEC_ALLOW_INVALID_URLS', 'false')
|
|
|
|
|
|
|
|
|
|
expect(described_class).to be_blocked_url('http://8.8.8.8.8')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'blocks urls whose hostname cannot be resolved' do
|
|
|
|
|
stub_env('RSPEC_ALLOW_INVALID_URLS', 'false')
|
|
|
|
|
|
|
|
|
|
expect(described_class).to be_blocked_url('http://foobar.x')
|
|
|
|
|
end
|
2017-03-15 20:09:08 +00:00
|
|
|
|
end
|
2017-08-08 17:36:24 +00:00
|
|
|
|
|
2019-07-12 07:04:44 +00:00
|
|
|
|
describe '#validate_hostname' do
|
2018-11-26 08:45:38 +00:00
|
|
|
|
let(:ip_addresses) do
|
|
|
|
|
[
|
|
|
|
|
'2001:db8:1f70::999:de8:7648:6e8',
|
|
|
|
|
'FE80::C800:EFF:FE74:8',
|
|
|
|
|
'::ffff:127.0.0.1',
|
|
|
|
|
'::ffff:169.254.168.100',
|
|
|
|
|
'::ffff:7f00:1',
|
|
|
|
|
'0:0:0:0:0:ffff:0.0.0.0',
|
|
|
|
|
'localhost',
|
|
|
|
|
'127.0.0.1',
|
|
|
|
|
'127.000.000.001',
|
|
|
|
|
'0x7f000001',
|
|
|
|
|
'0x7f.0.0.1',
|
|
|
|
|
'0x7f.0.0.1',
|
|
|
|
|
'017700000001',
|
|
|
|
|
'0177.1',
|
|
|
|
|
'2130706433',
|
|
|
|
|
'::',
|
|
|
|
|
'::1'
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'does not raise error for valid Ip addresses' do
|
|
|
|
|
ip_addresses.each do |ip|
|
2019-07-12 07:04:44 +00:00
|
|
|
|
expect { described_class.send(:validate_hostname, ip) }.not_to raise_error
|
2018-11-26 08:45:38 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-03-15 20:09:08 +00:00
|
|
|
|
end
|