2017-08-17 22:00:37 +05:30
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
|
describe Gitlab::UrlBlocker do
|
2017-08-17 22:00:37 +05:30
|
|
|
|
describe '#blocked_url?' do
|
2018-03-26 14:24:53 +05:30
|
|
|
|
let(:valid_ports) { Project::VALID_IMPORT_PORTS }
|
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
|
it 'allows imports from configured web host and port' do
|
|
|
|
|
import_url = "http://#{Gitlab.config.gitlab.host}:#{Gitlab.config.gitlab.port}/t.git"
|
|
|
|
|
expect(described_class.blocked_url?(import_url)).to be false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'allows imports from configured SSH host and port' do
|
|
|
|
|
import_url = "http://#{Gitlab.config.gitlab_shell.ssh_host}:#{Gitlab.config.gitlab_shell.ssh_port}/t.git"
|
|
|
|
|
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-03-26 14:24:53 +05:30
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com:25/foo/foo.git', valid_ports: valid_ports)).to be true
|
2017-08-17 22:00:37 +05:30
|
|
|
|
end
|
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
|
it 'returns true for a non-alphanumeric hostname' do
|
|
|
|
|
stub_resolv
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
it 'returns true for a non-alphanumeric username' do
|
|
|
|
|
stub_resolv
|
|
|
|
|
|
|
|
|
|
aggregate_failures do
|
|
|
|
|
expect(described_class).to be_blocked_url('ssh://-oProxyCommand=whoami@example.com/a')
|
|
|
|
|
|
|
|
|
|
# The leading character here is a Unicode "soft hyphen"
|
|
|
|
|
expect(described_class).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
|
|
|
|
|
|
|
|
|
|
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-26 14:24:53 +05:30
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
|
context 'when allow_local_network is' do
|
|
|
|
|
let(:local_ips) { ['192.168.1.2', '10.0.0.2', '172.16.0.2'] }
|
2018-03-26 14:24:53 +05:30
|
|
|
|
let(:fake_domain) { 'www.fakedomain.fake' }
|
|
|
|
|
|
|
|
|
|
context 'true (default)' do
|
|
|
|
|
it 'does not block urls from private networks' do
|
2018-05-09 12:01:36 +05:30
|
|
|
|
local_ips.each do |ip|
|
2018-03-26 14:24:53 +05:30
|
|
|
|
stub_domain_resolv(fake_domain, ip)
|
|
|
|
|
|
|
|
|
|
expect(described_class).not_to be_blocked_url("http://#{fake_domain}")
|
|
|
|
|
|
|
|
|
|
unstub_domain_resolv
|
|
|
|
|
|
|
|
|
|
expect(described_class).not_to be_blocked_url("http://#{ip}")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'false' do
|
|
|
|
|
it 'blocks urls from private networks' do
|
2018-05-09 12:01:36 +05:30
|
|
|
|
local_ips.each do |ip|
|
2018-03-26 14:24:53 +05:30
|
|
|
|
stub_domain_resolv(fake_domain, ip)
|
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
|
expect(described_class).to be_blocked_url("http://#{fake_domain}", allow_local_network: false)
|
2018-03-26 14:24:53 +05:30
|
|
|
|
|
|
|
|
|
unstub_domain_resolv
|
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
|
expect(described_class).to be_blocked_url("http://#{ip}", allow_local_network: false)
|
2018-03-26 14:24:53 +05:30
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def stub_domain_resolv(domain, ip)
|
|
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).with(domain, any_args).and_return([double(ip_address: ip, ipv4_private?: true)])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def unstub_domain_resolv
|
|
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).and_call_original
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Resolv does not support resolving UTF-8 domain names
|
|
|
|
|
# See https://bugs.ruby-lang.org/issues/4270
|
|
|
|
|
def stub_resolv
|
|
|
|
|
allow(Resolv).to receive(:getaddresses).and_return([])
|
|
|
|
|
end
|
|
|
|
|
end
|