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

tag_check_spec.rb « checks « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b1fbc7e797f8e46d3ef5c451b193190809b6ba9 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Checks::TagCheck, feature_category: :source_code_management do
  include_context 'change access checks context'

  describe '#validate!' do
    let(:ref) { 'refs/tags/v1.0.0' }

    it 'raises an error when user does not have access' do
      allow(user_access).to receive(:can_do_action?).with(:admin_tag).and_return(false)

      expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, 'You are not allowed to change existing tags on this project.')
    end

    context "prohibited tags check" do
      it 'prohibits tags name that include refs/heads at the head' do
        allow(subject).to receive(:tag_name).and_return("refs/heads/foo")

        expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, "You cannot create a tag with a prohibited pattern.")
      end

      it "prohibits tag names that include refs/tags/ at the head" do
        allow(subject).to receive(:tag_name).and_return("refs/tags/foo")

        expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, "You cannot create a tag with a prohibited pattern.")
      end

      it "doesn't prohibit a nested refs/tags/ string in a tag name" do
        allow(subject).to receive(:tag_name).and_return("fix-for-refs/tags/foo")

        expect { subject.validate! }.not_to raise_error
      end

      context "deleting a refs/tags headed tag" do
        let(:newrev) { "0000000000000000000000000000000000000000" }
        let(:ref) { "refs/tags/refs/tags/267208abfe40e546f5e847444276f7d43a39503e" }

        it "doesn't prohibit the deletion of a refs/tags/ tag name" do
          expect { subject.validate! }.not_to raise_error
        end
      end

      it "prohibits tag names that include characters incompatible with UTF-8" do
        allow(subject).to receive(:tag_name).and_return("v6.0.0-\xCE.BETA")

        expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, "Tag names must be valid when converted to UTF-8 encoding")
      end

      it "doesn't prohibit UTF-8 compatible characters" do
        allow(subject).to receive(:tag_name).and_return("v6.0.0-Ü.BETA")

        expect { subject.validate! }.not_to raise_error
      end

      context "when prohibited_tag_name_encoding_check feature flag is disabled" do
        before do
          stub_feature_flags(prohibited_tag_name_encoding_check: false)
          allow(subject).to receive(:validate_tag_name_not_sha_like!)
        end

        it "doesn't prohibit tag names that include characters incompatible with UTF-8" do
          allow(subject).to receive(:tag_name).and_return("v6.0.0-\xCE.BETA")

          expect { subject.validate! }.not_to raise_error
        end

        it "doesn't prohibit UTF-8 compatible characters" do
          allow(subject).to receive(:tag_name).and_return("v6.0.0-Ü.BETA")

          expect { subject.validate! }.not_to raise_error
        end
      end

      it "forbids SHA-1 values" do
        allow(subject)
          .to receive(:tag_name)
          .and_return("267208abfe40e546f5e847444276f7d43a39503e")

        expect { subject.validate! }.to raise_error(
          Gitlab::GitAccess::ForbiddenError,
          "You cannot create a tag with a SHA-1 or SHA-256 tag name."
        )
      end

      it "forbids SHA-256 values" do
        allow(subject)
          .to receive(:tag_name)
          .and_return("09b9fd3ea68e9b95a51b693a29568c898e27d1476bbd83c825664f18467fc175")

        expect { subject.validate! }.to raise_error(
          Gitlab::GitAccess::ForbiddenError,
          "You cannot create a tag with a SHA-1 or SHA-256 tag name."
        )
      end

      it "forbids '{SHA-1}{+anything}' values" do
        allow(subject)
          .to receive(:tag_name)
          .and_return("267208abfe40e546f5e847444276f7d43a39503e-")

        expect { subject.validate! }.to raise_error(
          Gitlab::GitAccess::ForbiddenError,
          "You cannot create a tag with a SHA-1 or SHA-256 tag name."
        )
      end

      it "forbids '{SHA-256}{+anything} values" do
        allow(subject)
          .to receive(:tag_name)
          .and_return("09b9fd3ea68e9b95a51b693a29568c898e27d1476bbd83c825664f18467fc175-")

        expect { subject.validate! }.to raise_error(
          Gitlab::GitAccess::ForbiddenError,
          "You cannot create a tag with a SHA-1 or SHA-256 tag name."
        )
      end

      it "allows SHA-1 values to be appended to the tag name" do
        allow(subject)
          .to receive(:tag_name)
          .and_return("fix-267208abfe40e546f5e847444276f7d43a39503e")

        expect { subject.validate! }.not_to raise_error
      end

      it "allows SHA-256 values to be appended to the tag name" do
        allow(subject)
          .to receive(:tag_name)
          .and_return("fix-09b9fd3ea68e9b95a51b693a29568c898e27d1476bbd83c825664f18467fc175")

        expect { subject.validate! }.not_to raise_error
      end
    end

    context 'with protected tag' do
      let!(:protected_tag) { create(:protected_tag, project: project, name: 'v*') }

      context 'as maintainer' do
        before do
          project.add_maintainer(user)
        end

        context 'deletion' do
          let(:oldrev) { 'be93687618e4b132087f430a4d8fc3a609c9b77c' }
          let(:newrev) { '0000000000000000000000000000000000000000' }

          context 'via web interface' do
            let(:protocol) { 'web' }

            it 'is allowed' do
              expect { subject.validate! }.not_to raise_error
            end
          end

          context 'via SSH' do
            it 'is prevented' do
              expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, /only delete.*web interface/)
            end
          end
        end

        context 'update' do
          let(:oldrev) { 'be93687618e4b132087f430a4d8fc3a609c9b77c' }
          let(:newrev) { '54fcc214b94e78d7a41a9a8fe6d87a5e59500e51' }

          it 'is prevented' do
            expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, /cannot be updated/)
          end
        end
      end

      context 'as developer' do
        before do
          project.add_developer(user)
        end

        context 'deletion' do
          let(:oldrev) { 'be93687618e4b132087f430a4d8fc3a609c9b77c' }
          let(:newrev) { '0000000000000000000000000000000000000000' }

          it 'is prevented' do
            expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, /not allowed to delete/)
          end
        end
      end

      context 'creation' do
        let(:oldrev) { '0000000000000000000000000000000000000000' }
        let(:newrev) { '54fcc214b94e78d7a41a9a8fe6d87a5e59500e51' }
        let(:ref) { 'refs/tags/v9.1.0' }

        it 'prevents creation below access level' do
          expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, /allowed to create this tag as it is protected/)
        end

        context 'when user has access' do
          let!(:protected_tag) { create(:protected_tag, :developers_can_create, project: project, name: 'v*') }

          it 'allows tag creation' do
            expect { subject.validate! }.not_to raise_error
          end

          context 'when tag name is the same as default branch' do
            let(:ref) { "refs/tags/#{project.default_branch}" }

            it 'is prevented' do
              expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, /cannot use default branch name to create a tag/)
            end
          end
        end
      end
    end
  end
end