summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/url_blockers/url_whitelist_spec.rb
blob: 7a65516be3c6fd4057585b253e46975d699cccea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::UrlBlockers::UrlWhitelist do
  include StubRequests

  let(:whitelist) { [] }

  before do
    allow(ApplicationSetting).to receive(:current).and_return(ApplicationSetting.new)
    stub_application_setting(outbound_local_requests_whitelist: whitelist)
  end

  describe '#domain_whitelisted?' do
    let(:whitelist) { ['www.example.com', 'example.com'] }

    it 'returns true if domains present in whitelist' do
      not_whitelisted = ['subdomain.example.com', 'example.org']

      aggregate_failures do
        whitelist.each do |domain|
          expect(described_class).to be_domain_whitelisted(domain)
        end

        not_whitelisted.each do |domain|
          expect(described_class).not_to be_domain_whitelisted(domain)
        end
      end
    end

    it 'returns false when domain is blank' do
      expect(described_class).not_to be_domain_whitelisted(nil)
    end

    context 'with ports' do
      let(:whitelist) { ['example.io:3000'] }

      it 'returns true if domain and ports present in whitelist' do
        parsed_whitelist = [['example.io', { port: 3000 }]]
        not_whitelisted = [
          'example.io',
          ['example.io', { port: 3001 }]
        ]

        aggregate_failures do
          parsed_whitelist.each do |domain_and_port|
            expect(described_class).to be_domain_whitelisted(*domain_and_port)
          end

          not_whitelisted.each do |domain_and_port|
            expect(described_class).not_to be_domain_whitelisted(*domain_and_port)
          end
        end
      end
    end
  end

  describe '#ip_whitelisted?' do
    let(:whitelist) do
      [
        '0.0.0.0',
        '127.0.0.1',
        '192.168.1.1',
        '0:0:0:0:0:ffff:192.168.1.2',
        '::ffff:c0a8:102',
        'fc00:bf8b:e62c:abcd:abcd:aaaa:aaaa:aaaa',
        '0:0:0:0:0:ffff:169.254.169.254',
        '::ffff:a9fe:a9fe',
        '::ffff:a9fe:a864',
        'fe80::c800:eff:fe74:8'
      ]
    end

    it 'returns true if ips present in whitelist' do
      aggregate_failures do
        whitelist.each do |ip_address|
          expect(described_class).to be_ip_whitelisted(ip_address)
        end

        ['172.16.2.2', '127.0.0.2', 'fe80::c800:eff:fe74:9'].each do |ip_address|
          expect(described_class).not_to be_ip_whitelisted(ip_address)
        end
      end
    end

    it 'returns false when ip is blank' do
      expect(described_class).not_to be_ip_whitelisted(nil)
    end

    context 'with ip ranges in whitelist' do
      let(:ipv4_range) { '127.0.0.0/28' }
      let(:ipv6_range) { 'fd84:6d02:f6d8:c89e::/124' }

      let(:whitelist) do
        [
          ipv4_range,
          ipv6_range
        ]
      end

      it 'does not whitelist ipv4 range when not in whitelist' do
        stub_application_setting(outbound_local_requests_whitelist: [])

        IPAddr.new(ipv4_range).to_range.to_a.each do |ip|
          expect(described_class).not_to be_ip_whitelisted(ip.to_s)
        end
      end

      it 'whitelists all ipv4s in the range when in whitelist' do
        IPAddr.new(ipv4_range).to_range.to_a.each do |ip|
          expect(described_class).to be_ip_whitelisted(ip.to_s)
        end
      end

      it 'does not whitelist ipv6 range when not in whitelist' do
        stub_application_setting(outbound_local_requests_whitelist: [])

        IPAddr.new(ipv6_range).to_range.to_a.each do |ip|
          expect(described_class).not_to be_ip_whitelisted(ip.to_s)
        end
      end

      it 'whitelists all ipv6s in the range when in whitelist' do
        IPAddr.new(ipv6_range).to_range.to_a.each do |ip|
          expect(described_class).to be_ip_whitelisted(ip.to_s)
        end
      end

      it 'does not whitelist IPs outside the range' do
        expect(described_class).not_to be_ip_whitelisted("fd84:6d02:f6d8:c89e:0:0:1:f")

        expect(described_class).not_to be_ip_whitelisted("127.0.1.15")
      end
    end

    context 'with ports' do
      let(:whitelist) { ['127.0.0.9:3000', '[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443'] }

      it 'returns true if ip and ports present in whitelist' do
        parsed_whitelist = [
          ['127.0.0.9', { port: 3000 }],
          ['[2001:db8:85a3:8d3:1319:8a2e:370:7348]', { port: 443 }]
        ]
        not_whitelisted = [
          '127.0.0.9',
          ['127.0.0.9', { port: 3001 }],
          '[2001:db8:85a3:8d3:1319:8a2e:370:7348]',
          ['[2001:db8:85a3:8d3:1319:8a2e:370:7348]', { port: 3001 }]
        ]

        aggregate_failures do
          parsed_whitelist.each do |ip_and_port|
            expect(described_class).to be_ip_whitelisted(*ip_and_port)
          end

          not_whitelisted.each do |ip_and_port|
            expect(described_class).not_to be_ip_whitelisted(*ip_and_port)
          end
        end
      end
    end
  end
end