summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/octokit/middleware_spec.rb
blob: f7063f2c4f29fbb80cfbd9429a90a66f3f753792 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Octokit::Middleware, feature_category: :importers do
  let(:app) { double(:app) }
  let(:middleware) { described_class.new(app) }

  shared_examples 'Allowed URL' do
    it 'does not raise an error' do
      expect(app).to receive(:call).with(env)

      expect { middleware.call(env) }.not_to raise_error
    end
  end

  shared_examples 'Blocked URL' do
    it 'raises an error' do
      expect { middleware.call(env) }.to raise_error(Gitlab::UrlBlocker::BlockedUrlError)
    end
  end

  describe '#call' do
    context 'when the URL is a public URL' do
      let(:env) { { url: 'https://public-url.com' } }

      it_behaves_like 'Allowed URL'

      context 'with failed address check' do
        before do
          stub_env('RSPEC_ALLOW_INVALID_URLS', 'false')
          allow(Addrinfo).to receive(:getaddrinfo).and_raise(SocketError)
        end

        it_behaves_like 'Blocked URL'

        context 'with disabled dns rebinding check' do
          before do
            stub_application_setting(dns_rebinding_protection_enabled: false)
          end

          it_behaves_like 'Allowed URL'
        end
      end
    end

    context 'when the URL is a localhost address' do
      let(:env) { { url: 'http://127.0.0.1' } }

      context 'when localhost requests are not allowed' do
        before do
          stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
        end

        it_behaves_like 'Blocked URL'
      end

      context 'when localhost requests are allowed' do
        before do
          stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
        end

        it_behaves_like 'Allowed URL'
      end
    end

    context 'when the URL is a local network address' do
      let(:env) { { url: 'http://172.16.0.0' } }

      context 'when local network requests are not allowed' do
        before do
          stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
        end

        it_behaves_like 'Blocked URL'
      end

      context 'when local network requests are allowed' do
        before do
          stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
        end

        it_behaves_like 'Allowed URL'
      end
    end

    context 'when a non HTTP/HTTPS URL is provided' do
      let(:env) { { url: 'ssh://172.16.0.0' } }

      it 'raises an error' do
        expect { middleware.call(env) }.to raise_error(Gitlab::UrlBlocker::BlockedUrlError)
      end
    end
  end
end