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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::ImportExport::RemoteStreamUpload do
include StubRequests
subject do
described_class.new(
download_url: download_url,
upload_url: upload_url,
options: {
upload_method: upload_method,
upload_content_type: upload_content_type
}
)
end
let(:download_url) { 'http://object-storage/file.txt' }
let(:upload_url) { 'http://example.com/file.txt' }
let(:upload_method) { :post }
let(:upload_content_type) { 'text/plain' }
describe '#execute' do
context 'when download request and upload request return 200' do
it 'uploads the downloaded content' do
stub_request(:get, download_url).to_return(status: 200, body: 'ABC', headers: { 'Content-Length' => 3 })
stub_request(:post, upload_url)
subject.execute
expect(
a_request(:post, upload_url).with(
body: 'ABC', headers: { 'Content-Length' => 3, 'Content-Type' => 'text/plain' }
)
).to have_been_made
end
end
context 'when upload method is put' do
let(:upload_method) { :put }
it 'uploads using the put method' do
stub_request(:get, download_url).to_return(status: 200, body: 'ABC', headers: { 'Content-Length' => 3 })
stub_request(:put, upload_url)
subject.execute
expect(
a_request(:put, upload_url).with(
body: 'ABC', headers: { 'Content-Length' => 3, 'Content-Type' => 'text/plain' }
)
).to have_been_made
end
end
context 'when download request does not return 200' do
it do
stub_request(:get, download_url).to_return(status: 404)
expect { subject.execute }.to raise_error(
Gitlab::ImportExport::RemoteStreamUpload::StreamError,
"Invalid response code while downloading file. Code: 404"
)
end
end
context 'when upload request does not returns 200' do
it do
stub_request(:get, download_url).to_return(status: 200, body: 'ABC', headers: { 'Content-Length' => 3 })
stub_request(:post, upload_url).to_return(status: 403)
expect { subject.execute }.to raise_error(
Gitlab::ImportExport::RemoteStreamUpload::StreamError,
"Invalid response code while uploading file. Code: 403"
)
end
end
context 'when download URL is a local address' do
let(:download_url) { 'http://127.0.0.1/file.txt' }
before do
stub_request(:get, download_url)
stub_request(:post, upload_url)
end
it 'raises error' do
expect { subject.execute }.to raise_error(
Gitlab::HTTP::BlockedUrlError,
"URL is blocked: Requests to localhost are not allowed"
)
end
context 'when local requests are allowed' do
before do
stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
end
it 'raises does not error' do
expect { subject.execute }.not_to raise_error
end
end
end
context 'when download URL is a local network' do
let(:download_url) { 'http://172.16.0.0/file.txt' }
before do
stub_request(:get, download_url)
stub_request(:post, upload_url)
end
it 'raises error' do
expect { subject.execute }.to raise_error(
Gitlab::HTTP::BlockedUrlError,
"URL is blocked: Requests to the local network are not allowed"
)
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 'raises does not error' do
expect { subject.execute }.not_to raise_error
end
end
end
context 'when upload URL is a local address' do
let(:upload_url) { 'http://127.0.0.1/file.txt' }
before do
stub_request(:get, download_url)
stub_request(:post, upload_url)
end
it 'raises error' do
stub_request(:get, download_url)
expect { subject.execute }.to raise_error(
Gitlab::HTTP::BlockedUrlError,
"URL is blocked: Requests to localhost are not allowed"
)
end
context 'when local requests are allowed' do
before do
stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
end
it 'raises does not error' do
expect { subject.execute }.not_to raise_error
end
end
end
context 'when upload URL it is a request to local network' do
let(:upload_url) { 'http://172.16.0.0/file.txt' }
before do
stub_request(:get, download_url)
stub_request(:post, upload_url)
end
it 'raises error' do
expect { subject.execute }.to raise_error(
Gitlab::HTTP::BlockedUrlError,
"URL is blocked: Requests to the local network are not allowed"
)
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 'raises does not error' do
expect { subject.execute }.not_to raise_error
end
end
end
context 'when upload URL resolves to a local address' do
let(:upload_url) { 'http://example.com/file.txt' }
it 'raises error' do
stub_request(:get, download_url)
stub_full_request(upload_url, ip_address: '127.0.0.1', method: upload_method)
expect { subject.execute }.to raise_error(
Gitlab::HTTP::BlockedUrlError,
"URL is blocked: Requests to localhost are not allowed"
)
end
end
end
describe Gitlab::ImportExport::RemoteStreamUpload::ChunkStream do
describe 'StringIO#copy_stream compatibility' do
it 'copies all chunks' do
chunks = %w[ABC EFD].to_enum
chunk_stream = described_class.new(chunks)
new_stream = StringIO.new
IO.copy_stream(chunk_stream, new_stream)
new_stream.rewind
expect(new_stream.read).to eq('ABCEFD')
end
context 'with chunks smaller and bigger than buffer size' do
before do
stub_const('Gitlab::ImportExport::RemoteStreamUpload::ChunkStream::DEFAULT_BUFFER_SIZE', 4)
end
it 'copies all chunks' do
chunks = %w[A BC DEF GHIJ KLMNOPQ RSTUVWXYZ].to_enum
chunk_stream = described_class.new(chunks)
new_stream = StringIO.new
IO.copy_stream(chunk_stream, new_stream)
new_stream.rewind
expect(new_stream.read).to eq('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
end
end
end
end
end
|