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
301
302
303
304
|
# frozen_string_literal: true
RSpec.shared_examples 'a redacted search results' do
let_it_be(:user) { create(:user) }
let_it_be(:accessible_group) { create(:group, :private) }
let_it_be(:accessible_project) { create(:project, :repository, :private, name: 'accessible_project') }
let_it_be(:group_member) { create(:group_member, group: accessible_group, user: user) }
let_it_be(:inaccessible_group) { create(:group, :private) }
let_it_be(:inaccessible_project) { create(:project, :repository, :private, name: 'inaccessible_project') }
let(:search) { 'anything' }
subject(:result) { search_service.search_objects }
def found_blob(project)
Gitlab::Search::FoundBlob.new(project: project)
end
def found_wiki_page(project)
Gitlab::Search::FoundWikiPage.new(found_blob(project))
end
def ar_relation(klass, *objects)
klass.id_in(objects.map(&:id))
end
def kaminari_array(*objects)
Kaminari.paginate_array(objects).page(1).per(20)
end
before do
accessible_project.add_maintainer(user)
allow(search_service)
.to receive_message_chain(:search_results, :objects)
.and_return(unredacted_results)
end
context 'for issues' do
let(:readable) { create(:issue, project: accessible_project) }
let(:unreadable) { create(:issue, project: inaccessible_project) }
let(:unredacted_results) { ar_relation(Issue, readable, unreadable) }
let(:scope) { 'issues' }
it 'redacts the inaccessible issue' do
expect(search_service.send(:logger))
.to receive(:error)
.with(hash_including(
message: "redacted_search_results",
current_user_id: user.id,
query: search,
filtered: array_including(
[
{ class_name: 'Issue', id: unreadable.id, ability: :read_issue }
])))
expect(result).to contain_exactly(readable)
end
end
context 'for notes' do
let(:readable_merge_request) do
create(:merge_request_with_diffs, target_project: accessible_project, source_project: accessible_project)
end
let(:readable_note_on_commit) { create(:note_on_commit, project: accessible_project) }
let(:readable_diff_note) { create(:diff_note_on_commit, project: accessible_project) }
let(:readable_note_on_mr) do
create(:discussion_note_on_merge_request, noteable: readable_merge_request, project: accessible_project)
end
let(:readable_diff_note_on_mr) do
create(:diff_note_on_merge_request, noteable: readable_merge_request, project: accessible_project)
end
let(:readable_note_on_project_snippet) do
create(:note_on_project_snippet, noteable: readable_merge_request, project: accessible_project)
end
let(:unreadable_merge_request) do
create(:merge_request_with_diffs, target_project: inaccessible_project, source_project: inaccessible_project)
end
let(:unreadable_note_on_commit) { create(:note_on_commit, project: inaccessible_project) }
let(:unreadable_diff_note) { create(:diff_note_on_commit, project: inaccessible_project) }
let(:unreadable_note_on_mr) do
create(:discussion_note_on_merge_request, noteable: unreadable_merge_request, project: inaccessible_project)
end
let(:unreadable_note_on_project_snippet) do
create(:note_on_project_snippet, noteable: unreadable_merge_request, project: inaccessible_project)
end
let(:unredacted_results) do
ar_relation(Note,
readable_note_on_commit,
readable_diff_note,
readable_note_on_mr,
readable_diff_note_on_mr,
readable_note_on_project_snippet,
unreadable_note_on_commit,
unreadable_diff_note,
unreadable_note_on_mr,
unreadable_note_on_project_snippet)
end
let(:scope) { 'notes' }
it 'redacts the inaccessible notes' do
expect(search_service.send(:logger))
.to receive(:error)
.with(hash_including(
message: "redacted_search_results",
current_user_id: user.id,
query: search,
filtered: array_including(
[
{ class_name: 'Note', id: unreadable_note_on_commit.id, ability: :read_note },
{ class_name: 'DiffNote', id: unreadable_diff_note.id, ability: :read_note },
{ class_name: 'DiscussionNote', id: unreadable_note_on_mr.id, ability: :read_note },
{ class_name: 'Note', id: unreadable_note_on_project_snippet.id, ability: :read_note }
])))
expect(result).to contain_exactly(readable_note_on_commit,
readable_diff_note,
readable_note_on_mr,
readable_diff_note_on_mr,
readable_note_on_project_snippet)
end
end
context 'for merge_requests' do
let(:readable) { create(:merge_request, source_project: accessible_project) }
let(:unreadable) { create(:merge_request, source_project: inaccessible_project) }
let(:unredacted_results) { ar_relation(MergeRequest, readable, unreadable) }
let(:scope) { 'merge_requests' }
it 'redacts the inaccessible merge request' do
expect(search_service.send(:logger))
.to receive(:error)
.with(hash_including(
message: "redacted_search_results",
current_user_id: user.id,
query: search,
filtered: array_including(
[
{ class_name: 'MergeRequest', id: unreadable.id, ability: :read_merge_request }
])))
expect(result).to contain_exactly(readable)
end
context 'with :with_api_entity_associations' do
let(:unredacted_results) { ar_relation(MergeRequest.with_api_entity_associations, readable, unreadable) }
it_behaves_like "redaction limits N+1 queries", limit: 8
end
end
context 'for blobs' do
let(:readable) { found_blob(accessible_project) }
let(:unreadable) { found_blob(inaccessible_project) }
let(:unredacted_results) { kaminari_array(readable, unreadable) }
let(:scope) { 'blobs' }
it 'redacts the inaccessible blob' do
expect(search_service.send(:logger))
.to receive(:error)
.with(hash_including(
message: "redacted_search_results",
current_user_id: user.id,
query: search,
filtered: array_including(
[
{ class_name: 'Gitlab::Search::FoundBlob', id: unreadable.id, ability: :read_blob }
])))
expect(result).to contain_exactly(readable)
end
end
context 'for wiki blobs' do
let(:readable) { found_wiki_page(accessible_project) }
let(:unreadable) { found_wiki_page(inaccessible_project) }
let(:unredacted_results) { kaminari_array(readable, unreadable) }
let(:scope) { 'wiki_blobs' }
it 'redacts the inaccessible blob' do
expect(search_service.send(:logger))
.to receive(:error)
.with(hash_including(
message: "redacted_search_results",
current_user_id: user.id,
query: search,
filtered: array_including(
[
{ class_name: 'Gitlab::Search::FoundWikiPage', id: unreadable.id, ability: :read_wiki_page }
])))
expect(result).to contain_exactly(readable)
end
end
context 'for project snippets' do
let(:readable) { create(:project_snippet, project: accessible_project) }
let(:unreadable) { create(:project_snippet, project: inaccessible_project) }
let(:unredacted_results) { ar_relation(ProjectSnippet, readable, unreadable) }
let(:scope) { 'snippet_titles' }
it 'redacts the inaccessible snippet' do
expect(search_service.send(:logger))
.to receive(:error)
.with(hash_including(
message: "redacted_search_results",
current_user_id: user.id,
query: search,
filtered: array_including(
[
{ class_name: 'ProjectSnippet', id: unreadable.id, ability: :read_snippet }
])))
expect(result).to contain_exactly(readable)
end
context 'with :with_api_entity_associations' do
it_behaves_like "redaction limits N+1 queries", limit: 14
end
end
context 'for personal snippets' do
let(:readable) { create(:personal_snippet, :private, author: user) }
let(:unreadable) { create(:personal_snippet, :private) }
let(:unredacted_results) { ar_relation(PersonalSnippet, readable, unreadable) }
let(:scope) { 'snippet_titles' }
it 'redacts the inaccessible snippet' do
expect(search_service.send(:logger))
.to receive(:error)
.with(hash_including(
message: "redacted_search_results",
current_user_id: user.id,
query: search,
filtered: array_including(
[
{ class_name: 'PersonalSnippet', id: unreadable.id, ability: :read_snippet }
])))
expect(result).to contain_exactly(readable)
end
context 'with :with_api_entity_associations' do
it_behaves_like "redaction limits N+1 queries", limit: 4
end
end
context 'for commits' do
let(:readable) { accessible_project.commit }
let(:unreadable) { inaccessible_project.commit }
let(:unredacted_results) { kaminari_array(readable, unreadable) }
let(:scope) { 'commits' }
it 'redacts the inaccessible commit' do
expect(search_service.send(:logger))
.to receive(:error)
.with(hash_including(
message: "redacted_search_results",
current_user_id: user.id,
query: search,
filtered: array_including(
[
{ class_name: 'Commit', id: unreadable.id, ability: :read_commit }
])))
expect(result).to contain_exactly(readable)
end
end
context 'for users' do
let(:other_user) { create(:user) }
let(:unredacted_results) { ar_relation(User, user, other_user) }
let(:scope) { 'users' }
it 'passes the users through' do
# Users are always visible to everyone
expect(result).to contain_exactly(user, other_user)
end
end
end
RSpec.shared_examples "redaction limits N+1 queries" do |limit:|
it 'does not exceed the query limit' do
# issuing the query to remove the data loading call
unredacted_results.to_a
# only the calls from the redaction are left
query = ActiveRecord::QueryRecorder.new { result }
# these are the project authorization calls, which are not preloaded
expect(query.count).to be <= limit
end
end
|