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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::RawController, feature_category: :source_code_management do
include RepoHelpers
let_it_be(:project) { create(:project, :public, :repository) }
let(:inline) { nil }
let(:params) { {} }
describe 'GET #show' do
def get_show
get(:show,
params: {
namespace_id: project.namespace,
project_id: project,
id: file_path,
inline: inline
}.merge(params))
end
subject { get_show }
shared_examples 'limited number of Gitaly request' do
it 'makes a limited number of Gitaly request', :request_store, :clean_gitlab_redis_cache do
# Warm up to populate repository cache
get_show
RequestStore.clear!
expect { get_show }.to change { Gitlab::GitalyClient.get_request_count }.by(2)
end
end
context 'regular filename' do
let(:file_path) { 'master/CONTRIBUTING.md' }
it 'delivers ASCII file' do
allow(Gitlab::Workhorse).to receive(:send_git_blob).and_call_original
subject
expect(response).to have_gitlab_http_status(:ok)
expect(response.header['Content-Type']).to eq('text/plain; charset=utf-8')
expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq 'true'
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with('git-blob:')
expect(Gitlab::Workhorse).to have_received(:send_git_blob) do |repository, blob|
expected_blob = project.repository.blob_at('master', 'CONTRIBUTING.md')
expect(repository).to eq(project.repository)
expect(blob.id).to eq(expected_blob.id)
expect(blob).to be_truncated
end
end
it_behaves_like 'project cache control headers'
it_behaves_like 'content disposition headers'
include_examples 'limited number of Gitaly request'
end
context 'image header' do
let(:file_path) { 'master/files/images/6049019_460s.jpg' }
it 'leaves image content disposition' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with('git-blob:')
end
it_behaves_like 'project cache control headers'
it_behaves_like 'content disposition headers'
include_examples 'limited number of Gitaly request'
end
context 'with LFS files' do
let(:filename) { 'lfs_object.iso' }
let(:file_path) { "be93687/files/lfs/#{filename}" }
it_behaves_like 'a controller that can serve LFS files'
it_behaves_like 'project cache control headers'
include_examples 'limited number of Gitaly request'
end
context 'when the endpoint receives requests above the limit' do
let(:file_path) { 'master/README.md' }
let(:path_without_ref) { 'README.md' }
before do
allow(::Gitlab::ApplicationRateLimiter).to(
receive(:throttled?).with(:raw_blob, scope: [project, path_without_ref]).and_return(true)
)
end
it 'prevents from accessing the raw file' do
expect { get_show }.not_to change { Gitlab::GitalyClient.get_request_count }
expect(response.body).to eq(_('You cannot access the raw file. Please wait a minute.'))
expect(response).to have_gitlab_http_status(:too_many_requests)
end
context 'when receiving an external storage request' do
let(:token) { 'letmein' }
before do
stub_application_setting(
static_objects_external_storage_url: 'https://cdn.gitlab.com',
static_objects_external_storage_auth_token: token
)
end
it 'does not prevent from accessing the raw file' do
expect(::Gitlab::ApplicationRateLimiter).not_to receive(:throttled?)
request.headers['X-Gitlab-External-Storage-Token'] = token
get_show
expect(response).to have_gitlab_http_status(:ok)
end
end
end
context 'as a sessionless user' do
let_it_be(:project) { create(:project, :private, :repository) }
let_it_be(:user) { create(:user, static_object_token: 'very-secure-token') }
let_it_be(:file_path) { 'master/README.md' }
let(:token) { user.static_object_token }
before do
project.add_developer(user)
end
context 'when no token is provided' do
it 'redirects to sign in page' do
get_show
expect(response).to have_gitlab_http_status(:found)
expect(response.location).to end_with('/users/sign_in')
end
end
context 'when a token param is present' do
context 'when token is correct' do
let(:params) { { token: token } }
it 'calls the action normally' do
get_show
expect(response).to have_gitlab_http_status(:ok)
end
context 'when user with expired password' do
let_it_be(:user) { create(:user, password_expires_at: 2.minutes.ago) }
it 'redirects to sign in page' do
get_show
expect(response).to have_gitlab_http_status(:found)
expect(response.location).to end_with('/users/sign_in')
end
end
context 'when password expiration is not applicable' do
context 'when ldap user' do
let_it_be(:user) { create(:omniauth_user, provider: 'ldap', password_expires_at: 2.minutes.ago) }
it 'calls the action normally' do
get_show
expect(response).to have_gitlab_http_status(:ok)
end
end
end
end
context 'when token is incorrect' do
let(:params) { { token: 'foobar' } }
it 'redirects to sign in page' do
get_show
expect(response).to have_gitlab_http_status(:found)
expect(response.location).to end_with('/users/sign_in')
end
end
end
context 'when a token header is present' do
before do
request.headers['X-Gitlab-Static-Object-Token'] = token
end
context 'when token is correct' do
it 'calls the action normally' do
get_show
expect(response).to have_gitlab_http_status(:ok)
end
context 'when user with expired password' do
let_it_be(:user) { create(:user, password_expires_at: 2.minutes.ago) }
it 'redirects to sign in page' do
get_show
expect(response).to have_gitlab_http_status(:found)
expect(response.location).to end_with('/users/sign_in')
end
end
context 'when password expiration is not applicable' do
context 'when ldap user' do
let_it_be(:user) { create(:omniauth_user, provider: 'ldap', password_expires_at: 2.minutes.ago) }
it 'calls the action normally' do
get_show
expect(response).to have_gitlab_http_status(:ok)
end
end
end
end
context 'when token is incorrect' do
let(:token) { 'foobar' }
it 'redirects to sign in page' do
get_show
expect(response).to have_gitlab_http_status(:found)
expect(response.location).to end_with('/users/sign_in')
end
end
end
end
describe 'caching' do
let(:ref) { project.default_branch }
def request_file
get(:show, params: { namespace_id: project.namespace, project_id: project, id: "#{ref}/README.md" })
end
it 'sets appropriate caching headers' do
sign_in create(:user)
request_file
expect(response.headers['ETag']).to eq("\"bdd5aa537c1e1f6d1b66de4bac8a6132\"")
expect(response.cache_control[:no_store]).to be_nil
expect(response.header['Cache-Control']).to eq(
'max-age=60, public, must-revalidate, stale-while-revalidate=60, stale-if-error=300, s-maxage=60'
)
end
context 'when a blob access by permalink' do
let(:ref) { project.commit.id }
it 'sets appropriate caching headers with longer max-age' do
sign_in create(:user)
request_file
expect(response.headers['ETag']).to eq("\"bdd5aa537c1e1f6d1b66de4bac8a6132\"")
expect(response.cache_control[:no_store]).to be_nil
expect(response.header['Cache-Control']).to eq(
'max-age=3600, public, must-revalidate, stale-while-revalidate=60, stale-if-error=300, s-maxage=60'
)
end
end
context 'when a public project has private repo' do
let(:project) { create(:project, :public, :repository, :repository_private) }
let(:user) { create(:user, maintainer_projects: [project]) }
it 'does not set public caching header' do
sign_in user
request_file
expect(response.header['Cache-Control']).to eq(
'max-age=60, private, must-revalidate, stale-while-revalidate=60, stale-if-error=300, s-maxage=60'
)
end
end
context 'when If-None-Match header is set' do
it 'returns a 304 status' do
request_file
request.headers['If-None-Match'] = response.headers['ETag']
request_file
expect(response).to have_gitlab_http_status(:not_modified)
end
end
end
end
end
|