Welcome to mirror list, hosted at ThFree Co, Russian Federation.

path_traversal_check_spec.rb « middleware « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91081cc88ea1968c975e1c5f977f90c9b79b4001 (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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ::Gitlab::Middleware::PathTraversalCheck, feature_category: :shared do
  using RSpec::Parameterized::TableSyntax

  let(:fake_response) { [200, { 'Content-Type' => 'text/plain' }, ['OK']] }
  let(:fake_app) { ->(_) { fake_response } }
  let(:middleware) { described_class.new(fake_app) }

  describe '#call' do
    let(:fullpath) { ::Rack::Request.new(env).fullpath }
    let(:decoded_fullpath) { CGI.unescape(fullpath) }

    let(:env) do
      Rack::MockRequest.env_for(
        path,
        method: method,
        params: query_params
      )
    end

    subject { middleware.call(env) }

    shared_examples 'no issue' do
      it 'measures and logs the execution time' do
        expect(::Gitlab::PathTraversal)
          .to receive(:check_path_traversal!)
                .with(decoded_fullpath, skip_decoding: true)
                .and_call_original
        expect(::Gitlab::AppLogger)
          .to receive(:warn)
                .with({ class_name: described_class.name, duration_ms: instance_of(Float) })
                .and_call_original

        expect(subject).to eq(fake_response)
      end

      context 'with log_execution_time_path_traversal_middleware disabled' do
        before do
          stub_feature_flags(log_execution_time_path_traversal_middleware: false)
        end

        it 'does nothing' do
          expect(::Gitlab::PathTraversal)
            .to receive(:check_path_traversal!)
                  .with(decoded_fullpath, skip_decoding: true)
                  .and_call_original
          expect(::Gitlab::AppLogger)
            .not_to receive(:warn)

          expect(subject).to eq(fake_response)
        end
      end
    end

    shared_examples 'excluded path' do
      it 'measures and logs the execution time' do
        expect(::Gitlab::PathTraversal)
          .not_to receive(:check_path_traversal!)
        expect(::Gitlab::AppLogger)
          .to receive(:warn)
                .with({ class_name: described_class.name, duration_ms: instance_of(Float) })
                .and_call_original

        expect(subject).to eq(fake_response)
      end

      context 'with log_execution_time_path_traversal_middleware disabled' do
        before do
          stub_feature_flags(log_execution_time_path_traversal_middleware: false)
        end

        it 'does nothing' do
          expect(::Gitlab::PathTraversal)
            .not_to receive(:check_path_traversal!)
          expect(::Gitlab::AppLogger)
            .not_to receive(:warn)

          expect(subject).to eq(fake_response)
        end
      end
    end

    shared_examples 'path traversal' do
      it 'logs the problem and measures the execution time' do
        expect(::Gitlab::PathTraversal)
          .to receive(:check_path_traversal!)
                .with(decoded_fullpath, skip_decoding: true)
                .and_call_original
        expect(::Gitlab::AppLogger)
          .to receive(:warn)
                .with({ message: described_class::PATH_TRAVERSAL_MESSAGE, path: instance_of(String) })
        expect(::Gitlab::AppLogger)
          .to receive(:warn)
                .with({
                  class_name: described_class.name,
                  duration_ms: instance_of(Float),
                  message: described_class::PATH_TRAVERSAL_MESSAGE,
                  fullpath: fullpath,
                  method: method.upcase
                }).and_call_original

        expect(subject).to eq(fake_response)
      end

      context 'with log_execution_time_path_traversal_middleware disabled' do
        before do
          stub_feature_flags(log_execution_time_path_traversal_middleware: false)
        end

        it 'logs the problem without the execution time' do
          expect(::Gitlab::PathTraversal)
            .to receive(:check_path_traversal!)
                  .with(decoded_fullpath, skip_decoding: true)
                  .and_call_original
          expect(::Gitlab::AppLogger)
            .to receive(:warn)
                  .with({ message: described_class::PATH_TRAVERSAL_MESSAGE, path: instance_of(String) })
          expect(::Gitlab::AppLogger)
            .to receive(:warn)
                  .with({
                    class_name: described_class.name,
                    message: described_class::PATH_TRAVERSAL_MESSAGE,
                    fullpath: fullpath,
                    method: method.upcase
                  }).and_call_original

          expect(subject).to eq(fake_response)
        end
      end
    end

    # we use Rack request.full_path, this will dump the accessed path and
    # the query string. The query string is only for GETs requests.
    # Hence different expectation (when params are set) for GETs and
    # the other methods (see below)
    context 'when using get' do
      let(:method) { 'get' }

      where(:path, :query_params, :shared_example_name) do
        '/foo/bar'            | {}                           | 'no issue'
        '/foo/../bar'         | {}                           | 'path traversal'
        '/foo%2Fbar'          | {}                           | 'no issue'
        '/foo%2F..%2Fbar'     | {}                           | 'path traversal'
        '/foo%252F..%252Fbar' | {}                           | 'no issue'

        '/foo/bar'            | { x: 'foo' }                 | 'no issue'
        '/foo/bar'            | { x: 'foo/../bar' }          | 'path traversal'
        '/foo/bar'            | { x: 'foo%2Fbar' }           | 'no issue'
        '/foo/bar'            | { x: 'foo%2F..%2Fbar' }      | 'no issue'
        '/foo/bar'            | { x: 'foo%252F..%252Fbar' }  | 'no issue'
        '/foo%2F..%2Fbar'     | { x: 'foo%252F..%252Fbar' }  | 'path traversal'
      end

      with_them do
        it_behaves_like params[:shared_example_name]
      end

      context 'for global search excluded paths' do
        excluded_paths = %w[
          /search
          /search/count
          /api/v4/search
          /api/v4/search.json
          /api/v4/projects/4/search
          /api/v4/projects/4/search.json
          /api/v4/projects/4/-/search
          /api/v4/projects/4/-/search.json
          /api/v4/projects/my%2Fproject/search
          /api/v4/projects/my%2Fproject/search.json
          /api/v4/projects/my%2Fproject/-/search
          /api/v4/projects/my%2Fproject/-/search.json
          /api/v4/groups/4/search
          /api/v4/groups/4/search.json
          /api/v4/groups/4/-/search
          /api/v4/groups/4/-/search.json
          /api/v4/groups/my%2Fgroup/search
          /api/v4/groups/my%2Fgroup/search.json
          /api/v4/groups/my%2Fgroup/-/search
          /api/v4/groups/my%2Fgroup/-/search.json
        ]
        query_params_with_no_path_traversal = [
          {},
          { x: 'foo' },
          { x: 'foo%2F..%2Fbar' },
          { x: 'foo%2F..%2Fbar' },
          { x: 'foo%252F..%252Fbar' }
        ]
        query_params_with_path_traversal = [
          { x: 'foo/../bar' }
        ]

        excluded_paths.each do |excluded_path|
          [query_params_with_no_path_traversal + query_params_with_path_traversal].flatten.each do |qp|
            context "for excluded path #{excluded_path} with query params #{qp}" do
              let(:query_params) { qp }
              let(:path) { excluded_path }

              it_behaves_like 'excluded path'
            end
          end

          non_excluded_path = excluded_path.gsub('search', 'searchtest')

          query_params_with_no_path_traversal.each do |qp|
            context "for non excluded path #{non_excluded_path} with query params #{qp}" do
              let(:query_params) { qp }
              let(:path) { non_excluded_path }

              it_behaves_like 'no issue'
            end
          end

          query_params_with_path_traversal.each do |qp|
            context "for non excluded path #{non_excluded_path} with query params #{qp}" do
              let(:query_params) { qp }
              let(:path) { non_excluded_path }

              it_behaves_like 'path traversal'
            end
          end
        end
      end

      context 'with a issues search path' do
        let(:query_params) { {} }
        let(:path) do
          'project/-/issues/?sort=updated_desc&milestone_title=16.0&search=Release%20%252525&first_page_size=20'
        end

        it_behaves_like 'no issue'
      end
    end

    %w[post put post delete patch].each do |http_method|
      context "when using #{http_method}" do
        let(:method) { http_method }

        where(:path, :query_params, :shared_example_name) do
          '/foo/bar'            | {}                          | 'no issue'
          '/foo/../bar'         | {}                          | 'path traversal'
          '/foo%2Fbar'          | {}                          | 'no issue'
          '/foo%2F..%2Fbar'     | {}                          | 'path traversal'
          '/foo%252F..%252Fbar' | {}                          | 'no issue'

          '/foo/bar'            | { x: 'foo' }                | 'no issue'
          '/foo/bar'            | { x: 'foo/../bar' }         | 'no issue'
          '/foo/bar'            | { x: 'foo%2Fbar' }          | 'no issue'
          '/foo/bar'            | { x: 'foo%2F..%2Fbar' }     | 'no issue'
          '/foo/bar'            | { x: 'foo%252F..%252Fbar' } | 'no issue'
          '/foo%2F..%2Fbar'     | { x: 'foo%252F..%252Fbar' } | 'path traversal'
        end

        with_them do
          it_behaves_like params[:shared_example_name]
        end

        context 'for global search excluded paths' do
          excluded_paths = %w[
            /search
            /search/count
            /api/v4/search
            /api/v4/search.json
            /api/v4/projects/4/search
            /api/v4/projects/4/search.json
            /api/v4/projects/4/-/search
            /api/v4/projects/4/-/search.json
            /api/v4/projects/my%2Fproject/search
            /api/v4/projects/my%2Fproject/search.json
            /api/v4/projects/my%2Fproject/-/search
            /api/v4/projects/my%2Fproject/-/search.json
            /api/v4/groups/4/search
            /api/v4/groups/4/search.json
            /api/v4/groups/4/-/search
            /api/v4/groups/4/-/search.json
            /api/v4/groups/my%2Fgroup/search
            /api/v4/groups/my%2Fgroup/search.json
            /api/v4/groups/my%2Fgroup/-/search
            /api/v4/groups/my%2Fgroup/-/search.json
          ]
          all_query_params = [
            {},
            { x: 'foo' },
            { x: 'foo%2F..%2Fbar' },
            { x: 'foo%2F..%2Fbar' },
            { x: 'foo%252F..%252Fbar' },
            { x: 'foo/../bar' }
          ]

          excluded_paths.each do |excluded_path|
            all_query_params.each do |qp|
              context "for excluded path #{excluded_path} with query params #{qp}" do
                let(:query_params) { qp }
                let(:path) { excluded_path }

                it_behaves_like 'excluded path'
              end

              non_excluded_path = excluded_path.gsub('search', 'searchtest')

              context "for non excluded path #{non_excluded_path} with query params #{qp}" do
                let(:query_params) { qp }
                let(:path) { excluded_path.gsub('search', 'searchtest') }

                it_behaves_like 'no issue'
              end
            end
          end
        end
      end
    end

    context 'with check_path_traversal_middleware disabled' do
      before do
        stub_feature_flags(check_path_traversal_middleware: false)
      end

      where(:path, :query_params) do
        '/foo/bar'            | {}
        '/foo/../bar'         | {}
        '/foo%2Fbar'          | {}
        '/foo%2F..%2Fbar'     | {}
        '/foo%252F..%252Fbar' | {}
        '/foo/bar'            | { x: 'foo' }
        '/foo/bar'            | { x: 'foo/../bar' }
        '/foo/bar'            | { x: 'foo%2Fbar' }
        '/foo/bar'            | { x: 'foo%2F..%2Fbar' }
        '/foo/bar'            | { x: 'foo%252F..%252Fbar' }
        '/search'             | { x: 'foo/../bar' }
        '/search'             | { x: 'foo%2F..%2Fbar' }
        '/search'             | { x: 'foo%252F..%252Fbar' }
        '%2Fsearch'           | { x: 'foo/../bar' }
        '%2Fsearch'           | { x: 'foo%2F..%2Fbar' }
        '%2Fsearch'           | { x: 'foo%252F..%252Fbar' }
      end

      with_them do
        %w[get post put post delete patch].each do |http_method|
          context "when using #{http_method}" do
            let(:method) { http_method }

            it 'does not check for path traversals' do
              expect(::Gitlab::PathTraversal).not_to receive(:check_path_traversal!)

              subject
            end
          end
        end
      end
    end
  end
end