debian-mirror-gitlab/spec/lib/gitlab/octokit/middleware_spec.rb

96 lines
2.5 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2019-07-31 22:56:46 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Gitlab::Octokit::Middleware, feature_category: :importers do
2019-07-31 22:56:46 +05:30
let(:app) { double(:app) }
let(:middleware) { described_class.new(app) }
2023-06-20 00:43:36 +05:30
shared_examples 'Allowed URL' do
2019-07-31 22:56:46 +05:30
it 'does not raise an error' do
expect(app).to receive(:call).with(env)
expect { middleware.call(env) }.not_to raise_error
end
end
2023-06-20 00:43:36 +05:30
shared_examples 'Blocked URL' do
2019-07-31 22:56:46 +05:30
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' } }
2023-06-20 00:43:36 +05:30
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
2019-07-31 22:56:46 +05:30
end
2022-08-27 11:52:29 +05:30
context 'when the URL is a localhost address' do
2019-07-31 22:56:46 +05:30
let(:env) { { url: 'http://127.0.0.1' } }
context 'when localhost requests are not allowed' do
before do
2019-10-12 21:52:04 +05:30
stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
2019-07-31 22:56:46 +05:30
end
2023-06-20 00:43:36 +05:30
it_behaves_like 'Blocked URL'
2019-07-31 22:56:46 +05:30
end
context 'when localhost requests are allowed' do
before do
2019-10-12 21:52:04 +05:30
stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
2019-07-31 22:56:46 +05:30
end
2023-06-20 00:43:36 +05:30
it_behaves_like 'Allowed URL'
2019-07-31 22:56:46 +05:30
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
2019-10-12 21:52:04 +05:30
stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
2019-07-31 22:56:46 +05:30
end
2023-06-20 00:43:36 +05:30
it_behaves_like 'Blocked URL'
2019-07-31 22:56:46 +05:30
end
context 'when local network requests are allowed' do
before do
2019-10-12 21:52:04 +05:30
stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
2019-07-31 22:56:46 +05:30
end
2023-06-20 00:43:36 +05:30
it_behaves_like 'Allowed URL'
2019-07-31 22:56:46 +05:30
end
end
2023-01-13 00:05:48 +05:30
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
2019-07-31 22:56:46 +05:30
end
end