2018-08-11 06:38:21 -04:00
|
|
|
|
# coding: utf-8
|
2017-03-15 16:09:08 -04:00
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
|
describe Gitlab::UrlBlocker do
|
2017-03-15 16:09:08 -04:00
|
|
|
|
describe '#blocked_url?' do
|
2018-06-01 07:43:53 -04:00
|
|
|
|
let(:ports) { Project::VALID_IMPORT_PORTS }
|
2018-03-13 18:38:25 -04:00
|
|
|
|
|
2017-03-15 16:09:08 -04:00
|
|
|
|
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
|
|
|
|
|
|
2018-11-28 13:37:12 -05: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 16:09:08 -04: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 07:43:53 -04:00
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com:25/foo/foo.git', ports: ports)).to be true
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-11 02:29:07 -04: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 07:43:53 -04:00
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com/foo/foo.git')).to be false
|
2019-04-11 02:29:07 -04:00
|
|
|
|
expect(described_class.blocked_url?('https://gitlab.com/foo/foo.git', schemes: ['http'])).to be true
|
2017-03-15 16:09:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2018-11-28 13:37:12 -05:00
|
|
|
|
it 'returns true for bad protocol on configured web/SSH host and ports' do
|
|
|
|
|
web_url = "javascript://#{Gitlab.config.gitlab.host}:#{Gitlab.config.gitlab.port}/t.git%0aalert(1)"
|
|
|
|
|
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 00:41:59 -04:00
|
|
|
|
it 'returns true for localhost IPs' do
|
2018-11-26 03:45:38 -05:00
|
|
|
|
expect(described_class.blocked_url?('https://[0:0:0:0:0:0:0:0]/foo/foo.git')).to be true
|
2018-09-06 00:41:59 -04:00
|
|
|
|
expect(described_class.blocked_url?('https://0.0.0.0/foo/foo.git')).to be true
|
2018-11-26 03:45:38 -05:00
|
|
|
|
expect(described_class.blocked_url?('https://[::]/foo/foo.git')).to be true
|
2018-09-06 00:41:59 -04: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 03:45:38 -05: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 00:41:59 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-11-07 03:30:38 -05: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 03:45:38 -05: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 03:30:38 -05: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 03:45:38 -05: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 03:30:38 -05: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 03:45:38 -05: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 13:36:24 -04:00
|
|
|
|
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
|
|
|
|
|
|
2017-03-15 16:09:08 -04: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 18:38:25 -04:00
|
|
|
|
|
2018-04-02 11:13:47 -04:00
|
|
|
|
context 'when allow_local_network is' do
|
2018-11-26 03:45:38 -05: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
|
2018-03-13 18:38:25 -04:00
|
|
|
|
let(:fake_domain) { 'www.fakedomain.fake' }
|
|
|
|
|
|
|
|
|
|
context 'true (default)' do
|
|
|
|
|
it 'does not block urls from private networks' do
|
2018-04-02 11:13:47 -04:00
|
|
|
|
local_ips.each do |ip|
|
2018-03-13 18:38:25 -04:00
|
|
|
|
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
|
2018-08-11 06:38:21 -04:00
|
|
|
|
|
2018-09-06 00:41:59 -04:00
|
|
|
|
it 'allows localhost endpoints' do
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://0.0.0.0', allow_localhost: true)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://localhost', allow_localhost: true)
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://127.0.0.1', allow_localhost: true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'allows loopback endpoints' do
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://127.0.0.2', allow_localhost: true)
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-11 06:38:21 -04:00
|
|
|
|
it 'allows IPv4 link-local endpoints' do
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://169.254.169.254')
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://169.254.168.100')
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-26 03:45:38 -05:00
|
|
|
|
it 'allows IPv6 link-local endpoints' do
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[0:0:0:0:0:ffff:169.254.169.254]')
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[::ffff:169.254.169.254]')
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[::ffff:a9fe:a9fe]')
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[0:0:0:0:0:ffff:169.254.168.100]')
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[::ffff:169.254.168.100]')
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[::ffff:a9fe:a864]')
|
|
|
|
|
expect(described_class).not_to be_blocked_url('http://[fe80::c800:eff:fe74:8]')
|
2018-08-11 06:38:21 -04:00
|
|
|
|
end
|
2018-03-13 18:38:25 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'false' do
|
|
|
|
|
it 'blocks urls from private networks' do
|
2018-04-02 11:13:47 -04:00
|
|
|
|
local_ips.each do |ip|
|
2018-03-13 18:38:25 -04:00
|
|
|
|
stub_domain_resolv(fake_domain, ip)
|
|
|
|
|
|
2018-04-02 11:13:47 -04:00
|
|
|
|
expect(described_class).to be_blocked_url("http://#{fake_domain}", allow_local_network: false)
|
2018-03-13 18:38:25 -04:00
|
|
|
|
|
|
|
|
|
unstub_domain_resolv
|
|
|
|
|
|
2018-04-02 11:13:47 -04:00
|
|
|
|
expect(described_class).to be_blocked_url("http://#{ip}", allow_local_network: false)
|
2018-03-13 18:38:25 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2018-08-11 06:38:21 -04: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 03:45:38 -05: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 06:38:21 -04:00
|
|
|
|
expect(described_class).to be_blocked_url('http://[::ffff:169.254.169.254]', allow_local_network: false)
|
2018-11-26 03:45:38 -05: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 06:38:21 -04:00
|
|
|
|
expect(described_class).to be_blocked_url('http://[::ffff:169.254.168.100]', allow_local_network: false)
|
2018-11-26 03:45:38 -05: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 06:38:21 -04:00
|
|
|
|
end
|
2018-03-13 18:38:25 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def stub_domain_resolv(domain, ip)
|
2018-11-26 03:45:38 -05:00
|
|
|
|
address = double(ip_address: ip, ipv4_private?: true, ipv6_link_local?: false, ipv4_loopback?: false, ipv6_loopback?: false)
|
|
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).with(domain, any_args).and_return([address])
|
|
|
|
|
allow(address).to receive(:ipv6_v4mapped?).and_return(false)
|
2018-03-13 18:38:25 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def unstub_domain_resolv
|
|
|
|
|
allow(Addrinfo).to receive(:getaddrinfo).and_call_original
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-06-11 09:29:37 -04:00
|
|
|
|
|
|
|
|
|
context 'when enforce_user is' do
|
|
|
|
|
before do
|
|
|
|
|
stub_resolv
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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 15:14:09 -05: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
|
2017-03-15 16:09:08 -04:00
|
|
|
|
end
|
2017-08-08 13:36:24 -04:00
|
|
|
|
|
2018-11-26 03:45:38 -05:00
|
|
|
|
describe '#validate_hostname!' do
|
|
|
|
|
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|
|
|
|
|
|
expect { described_class.send(:validate_hostname!, ip) }.not_to raise_error
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-08-08 13:36:24 -04:00
|
|
|
|
# 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
|
2017-03-15 16:09:08 -04:00
|
|
|
|
end
|