File: base_action_controller_shared_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (89 lines) | stat: -rw-r--r-- 3,371 bytes parent folder | download
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
# frozen_string_literal: true

# Requires `request` subject to be defined
#
# subject(:request) { get root_path }
RSpec.shared_examples 'Base action controller' do
  describe 'security headers' do
    describe 'Cross-Security-Policy' do
      context 'when configuring snowplow' do
        let(:snowplow_host) { 'snowplow.example.com' }

        shared_examples 'snowplow is not in the CSP' do
          it 'does not add the snowplow collector hostname to the CSP' do
            request

            expect(response.headers['Content-Security-Policy']).not_to include(snowplow_host)
          end
        end

        context 'when snowplow is enabled' do
          before do
            stub_application_setting(snowplow_enabled: true, snowplow_collector_hostname: snowplow_host)
          end

          it 'adds snowplow to the csp' do
            request

            expect(response.headers['Content-Security-Policy']).to include(snowplow_host)
          end
        end

        context 'when snowplow is enabled but host is not configured' do
          before do
            stub_application_setting(snowplow_enabled: true, snowplow_collector_hostname: nil)
          end

          it_behaves_like 'snowplow is not in the CSP'
        end

        context 'when snowplow is disabled' do
          before do
            stub_application_setting(snowplow_enabled: false, snowplow_collector_hostname: snowplow_host)
          end

          it_behaves_like 'snowplow is not in the CSP'
        end
      end

      context 'when configuring vite' do
        let(:vite_hmr_websocket_url) { "ws://gitlab.example.com:3808" }
        let(:vite_hmr_http_url) { "http://gitlab.example.com:3808" }
        let(:vite_gitlab_url) { Gitlab::Utils.append_path(Gitlab.config.gitlab.url, 'vite-dev/') }

        context 'when vite enabled during development',
          skip: 'https://gitlab.com/gitlab-org/gitlab/-/issues/424334' do
          before do
            stub_rails_env('development')
            allow(ViteHelper).to receive(:vite_enabled?).and_return(true)
            allow(BaseActionController.helpers).to receive(:vite_enabled?).and_return(true)
            allow(BaseActionController.helpers).to receive(:vite_hmr_websocket_url).and_return(vite_hmr_websocket_url)
            allow(BaseActionController.helpers).to receive(:vite_hmr_http_url).and_return(vite_hmr_http_url)
          end

          it 'adds vite csp' do
            request

            expect(response.headers['Content-Security-Policy']).to include("#{vite_hmr_websocket_url}/vite-dev/")
            expect(response.headers['Content-Security-Policy']).to include("#{vite_hmr_http_url}/vite-dev/")
            expect(response.headers['Content-Security-Policy']).to include(vite_gitlab_url)
          end
        end

        context 'when vite disabled' do
          before do
            allow(BaseActionController.helpers).to receive(:vite_enabled?).and_return(false)
          end

          it "doesn't add vite csp" do
            request

            expect(response.headers['Content-Security-Policy']).not_to include(vite_hmr_websocket_url)
            expect(response.headers['Content-Security-Policy']).not_to include(vite_hmr_http_url)
            expect(response.headers['Content-Security-Policy']).not_to include(vite_gitlab_url)
          end
        end
      end
    end
  end
end