git_access_spec.rb 20.1 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2
require 'spec_helper'

D
Douwe Maan 已提交
3
describe Gitlab::GitAccess, lib: true do
4 5
  let(:pull_access_check) { access.check('git-upload-pack', '_any') }
  let(:push_access_check) { access.check('git-receive-pack', '_any') }
6
  let(:access) { Gitlab::GitAccess.new(actor, project, protocol, authentication_abilities: authentication_abilities) }
7
  let(:project) { create(:project, :repository) }
D
Dmitriy Zaporozhets 已提交
8
  let(:user) { create(:user) }
9
  let(:actor) { user }
10
  let(:protocol) { 'ssh' }
11
  let(:authentication_abilities) do
K
Kamil Trzcinski 已提交
12 13 14 15 16 17
    [
      :read_project,
      :download_code,
      :push_code
    ]
  end
D
Dmitriy Zaporozhets 已提交
18

19 20 21
  describe '#check with single protocols allowed' do
    def disable_protocol(protocol)
      settings = ::ApplicationSetting.create_from_defaults
22
      settings.update_attribute(:enabled_git_access_protocol, protocol)
23 24 25 26 27 28 29 30
    end

    context 'ssh disabled' do
      before do
        disable_protocol('ssh')
      end

      it 'blocks ssh git push' do
M
Michael Kozono 已提交
31
        expect(access.check('git-receive-pack', '_any').allowed?).to be_falsey
32 33 34
      end

      it 'blocks ssh git pull' do
M
Michael Kozono 已提交
35
        expect(access.check('git-upload-pack', '_any').allowed?).to be_falsey
36 37 38 39
      end
    end

    context 'http disabled' do
M
Michael Kozono 已提交
40 41
      let(:protocol) { 'http' }

42 43 44 45 46
      before do
        disable_protocol('http')
      end

      it 'blocks http push' do
M
Michael Kozono 已提交
47
        expect(access.check('git-receive-pack', '_any').allowed?).to be_falsey
48 49 50
      end

      it 'blocks http git pull' do
M
Michael Kozono 已提交
51
        expect(access.check('git-upload-pack', '_any').allowed?).to be_falsey
52 53 54 55
      end
    end
  end

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
  describe '#check_project_accessibility!' do
    context 'when the project exists' do
      context 'when actor exists' do
        context 'when actor is a DeployKey' do
          let(:deploy_key) { create(:deploy_key, user: user, can_push: true) }
          let(:actor) { deploy_key }

          context 'when the DeployKey has access to the project' do
            before { deploy_key.projects << project }

            it 'allows pull access' do
              expect(pull_access_check.allowed?).to be_truthy
            end

            it 'allows push access' do
              expect(push_access_check.allowed?).to be_truthy
            end
          end

          context 'when the Deploykey does not have access to the project' do
            it 'blocks pulls with "not found"' do
              expect(pull_access_check.allowed?).to be_falsey
              expect(pull_access_check.message).to eq('The project you were looking for could not be found.')
            end

            it 'blocks pushes with "not found"' do
              expect(push_access_check.allowed?).to be_falsey
              expect(push_access_check.message).to eq('The project you were looking for could not be found.')
            end
          end
        end

        context 'when actor is a User' do
          context 'when the User can read the project' do
            before { project.team << [user, :master] }

            it 'allows pull access' do
              expect(pull_access_check.allowed?).to be_truthy
            end

            it 'allows push access' do
              expect(push_access_check.allowed?).to be_truthy
            end
          end

          context 'when the User cannot read the project' do
            it 'blocks pulls with "not found"' do
              expect(pull_access_check.allowed?).to be_falsey
              expect(pull_access_check.message).to eq('The project you were looking for could not be found.')
            end

            it 'blocks pushes with "not found"' do
              expect(push_access_check.allowed?).to be_falsey
              expect(push_access_check.message).to eq('The project you were looking for could not be found.')
            end
          end
        end

        # For backwards compatibility
        context 'when actor is :ci' do
          let(:actor) { :ci }
          let(:authentication_abilities) { build_authentication_abilities }

          it 'allows pull access' do
            expect(pull_access_check.allowed?).to be_truthy
          end

          it 'does not block pushes with "not found"' do
            expect(push_access_check.allowed?).to be_falsey
            expect(push_access_check.message).to eq('You are not allowed to upload code for this project.')
          end
        end
      end

      context 'when actor is nil' do
        let(:actor) { nil }

        context 'when guests can read the project' do
          let(:project) { create(:project, :repository, :public) }

          it 'allows pull access' do
            expect(pull_access_check.allowed?).to be_truthy
          end

          it 'does not block pushes with "not found"' do
            expect(push_access_check.allowed?).to be_falsey
            expect(push_access_check.message).to eq('You are not allowed to upload code for this project.')
          end
        end

        context 'when guests cannot read the project' do
          it 'blocks pulls with "not found"' do
            expect(pull_access_check.allowed?).to be_falsey
            expect(pull_access_check.message).to eq('The project you were looking for could not be found.')
          end

          it 'blocks pushes with "not found"' do
            expect(push_access_check.allowed?).to be_falsey
            expect(push_access_check.message).to eq('The project you were looking for could not be found.')
          end
        end
      end
    end

    context 'when the project is nil' do
      let(:project) { nil }

      it 'blocks any command with "not found"' do
        expect(pull_access_check.allowed?).to be_falsey
        expect(pull_access_check.message).to eq('The project you were looking for could not be found.')
        expect(push_access_check.allowed?).to be_falsey
        expect(push_access_check.message).to eq('The project you were looking for could not be found.')
      end
    end
  end

  describe '#check_command_disabled!' do
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
    before { project.team << [user, :master] }

    context 'over http' do
      let(:protocol) { 'http' }

      context 'when the git-upload-pack command is disabled in config' do
        before do
          allow(Gitlab.config.gitlab_shell).to receive(:upload_pack).and_return(false)
        end

        context 'when calling git-upload-pack' do
          subject { access.check('git-upload-pack', '_any') }
          it { expect(subject.allowed?).to be_falsey }
          it { expect(subject.message).to eq('The command "git-upload-pack" is not allowed.') }
        end

        context 'when calling git-receive-pack' do
          it { expect(access.check('git-receive-pack', '_any').allowed?).to be_truthy }
        end
      end

      context 'when the git-receive-pack command is disabled in config' do
        before do
          allow(Gitlab.config.gitlab_shell).to receive(:receive_pack).and_return(false)
        end

        context 'when calling git-receive-pack' do
          subject { access.check('git-receive-pack', '_any') }
          it { expect(subject.allowed?).to be_falsey }
          it { expect(subject.message).to eq('The command "git-receive-pack" is not allowed.') }
        end

        context 'when calling git-upload-pack' do
          it { expect(access.check('git-upload-pack', '_any').allowed?).to be_truthy }
        end
      end
    end
  end

212
  describe '#check_download_access!' do
213
    subject { access.check('git-upload-pack', '_any') }
214

D
Dmitriy Zaporozhets 已提交
215 216 217 218
    describe 'master permissions' do
      before { project.team << [user, :master] }

      context 'pull code' do
219
        it { expect(subject.allowed?).to be_truthy }
D
Dmitriy Zaporozhets 已提交
220 221 222 223 224 225 226
      end
    end

    describe 'guest permissions' do
      before { project.team << [user, :guest] }

      context 'pull code' do
227
        it { expect(subject.allowed?).to be_falsey }
228
        it { expect(subject.message).to match(/You are not allowed to download code/) }
D
Dmitriy Zaporozhets 已提交
229 230 231 232 233 234 235 236 237 238
      end
    end

    describe 'blocked user' do
      before do
        project.team << [user, :master]
        user.block
      end

      context 'pull code' do
239
        it { expect(subject.allowed?).to be_falsey }
240
        it { expect(subject.message).to match(/Your account has been blocked/) }
D
Dmitriy Zaporozhets 已提交
241 242 243
      end
    end

L
Lin Jen-Shin 已提交
244
    describe 'without access to project' do
D
Dmitriy Zaporozhets 已提交
245
      context 'pull code' do
246
        it { expect(subject.allowed?).to be_falsey }
D
Dmitriy Zaporozhets 已提交
247
      end
248 249

      context 'when project is public' do
250
        let(:public_project) { create(:project, :public, :repository) }
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        let(:guest_access) { Gitlab::GitAccess.new(nil, public_project, 'web', authentication_abilities: []) }
        subject { guest_access.check('git-upload-pack', '_any') }

        context 'when repository is enabled' do
          it 'give access to download code' do
            expect(subject.allowed?).to be_truthy
          end
        end

        context 'when repository is disabled' do
          it 'does not give access to download code' do
            public_project.project_feature.update_attribute(:repository_access_level, ProjectFeature::DISABLED)

            expect(subject.allowed?).to be_falsey
            expect(subject.message).to match(/You are not allowed to download code/)
          end
        end
      end
D
Dmitriy Zaporozhets 已提交
269
    end
270 271

    describe 'deploy key permissions' do
272
      let(:key) { create(:deploy_key, user: user) }
273
      let(:actor) { key }
274 275

      context 'pull code' do
276 277
        context 'when project is authorized' do
          before { key.projects << project }
278

279 280 281 282 283
          it { expect(subject).to be_allowed }
        end

        context 'when unauthorized' do
          context 'from public project' do
284
            let(:project) { create(:project, :public, :repository) }
285 286 287 288 289

            it { expect(subject).to be_allowed }
          end

          context 'from internal project' do
290
            let(:project) { create(:project, :internal, :repository) }
291 292 293 294 295

            it { expect(subject).not_to be_allowed }
          end

          context 'from private project' do
296
            let(:project) { create(:project, :private, :repository) }
297 298 299 300

            it { expect(subject).not_to be_allowed }
          end
        end
301 302
      end
    end
K
Kamil Trzcinski 已提交
303

304 305
    describe 'build authentication_abilities permissions' do
      let(:authentication_abilities) { build_authentication_abilities }
K
Kamil Trzcinski 已提交
306

307
      describe 'owner' do
308
        let(:project) { create(:project, :repository, namespace: user.namespace) }
309 310 311 312 313 314

        context 'pull code' do
          it { expect(subject).to be_allowed }
        end
      end

K
Kamil Trzcinski 已提交
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
      describe 'reporter user' do
        before { project.team << [user, :reporter] }

        context 'pull code' do
          it { expect(subject).to be_allowed }
        end
      end

      describe 'admin user' do
        let(:user) { create(:admin) }

        context 'when member of the project' do
          before { project.team << [user, :reporter] }

          context 'pull code' do
            it { expect(subject).to be_allowed }
          end
        end

        context 'when is not member of the project' do
          context 'pull code' do
            it { expect(subject).not_to be_allowed }
          end
        end
      end
340 341 342 343 344 345 346 347

      describe 'generic CI (build without a user)' do
        let(:actor) { :ci }

        context 'pull code' do
          it { expect(subject).to be_allowed }
        end
      end
K
Kamil Trzcinski 已提交
348
    end
D
Dmitriy Zaporozhets 已提交
349 350
  end

351
  describe '#check_push_access!' do
352
    before { merge_into_protected_branch }
353
    let(:unprotected_branch) { 'unprotected_branch' }
D
Dmitriy Zaporozhets 已提交
354

355 356
    let(:changes) do
      { push_new_branch: "#{Gitlab::Git::BLANK_SHA} 570e7b2ab refs/heads/wow",
357 358
        push_master: '6f6d7e7ed 570e7b2ab refs/heads/master',
        push_protected_branch: '6f6d7e7ed 570e7b2ab refs/heads/feature',
359 360
        push_remove_protected_branch: "570e7b2ab #{Gitlab::Git::BLANK_SHA} "\
                                      'refs/heads/feature',
361
        push_tag: '6f6d7e7ed 570e7b2ab refs/tags/v1.0.0',
362
        push_new_tag: "#{Gitlab::Git::BLANK_SHA} 570e7b2ab refs/tags/v7.8.9",
363 364
        push_all: ['6f6d7e7ed 570e7b2ab refs/heads/master', '6f6d7e7ed 570e7b2ab refs/heads/feature'],
        merge_into_protected_branch: "0b4bc9a #{merge_into_protected_branch} refs/heads/feature" }
365
    end
D
Dmitriy Zaporozhets 已提交
366

367 368
    def stub_git_hooks
      # Running the `pre-receive` hook is expensive, and not necessary for this test.
S
Sean McGivern 已提交
369 370 371
      allow_any_instance_of(GitHooksService).to receive(:execute) do |service, &block|
        block.call(service)
      end
D
Dmitriy Zaporozhets 已提交
372
    end
373

374 375 376 377 378
    def merge_into_protected_branch
      @protected_branch_merge_commit ||= begin
        stub_git_hooks
        project.repository.add_branch(user, unprotected_branch, 'feature')
        target_branch = project.repository.lookup('feature')
379
        source_branch = project.repository.create_file(
L
Lin Jen-Shin 已提交
380
          user,
D
Douwe Maan 已提交
381
          'filename',
382 383
          'This is the file content',
          message: 'This is a good commit message',
384
          branch_name: unprotected_branch)
385 386
        rugged = project.repository.rugged
        author = { email: "email@example.com", time: Time.now, name: "Example Git User" }
387

388 389
        merge_index = rugged.merge_commits(target_branch, source_branch)
        Rugged::Commit.create(rugged, author: author, committer: author, message: "commit message", parents: [target_branch, source_branch], tree: merge_index.write_tree(rugged))
390 391
      end
    end
392

393
    # Run permission checks for a user
394 395
    def self.run_permission_checks(permissions_matrix)
      permissions_matrix.keys.each do |role|
396
        describe "#{role} access" do
T
Timothy Andrew 已提交
397 398 399 400 401 402
          before do
            if role == :admin
              user.update_attribute(:admin, true)
            else
              project.team << [user, role]
            end
S
Sean McGivern 已提交
403 404 405 406 407
          end

          permissions_matrix[role].each do |action, allowed|
            context action do
              subject { access.send(:check_push_access!, changes[action]) }
408

S
Sean McGivern 已提交
409 410 411 412 413 414
              it do
                if allowed
                  expect { subject }.not_to raise_error
                else
                  expect { subject }.to raise_error(Gitlab::GitAccess::UnauthorizedError)
                end
415
              end
416 417 418 419 420
            end
          end
        end
      end
    end
421 422

    permissions_matrix = {
T
Timothy Andrew 已提交
423 424 425 426 427 428 429 430 431 432 433
      admin: {
        push_new_branch: true,
        push_master: true,
        push_protected_branch: true,
        push_remove_protected_branch: false,
        push_tag: true,
        push_new_tag: true,
        push_all: true,
        merge_into_protected_branch: true
      },

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
      master: {
        push_new_branch: true,
        push_master: true,
        push_protected_branch: true,
        push_remove_protected_branch: false,
        push_tag: true,
        push_new_tag: true,
        push_all: true,
        merge_into_protected_branch: true
      },

      developer: {
        push_new_branch: true,
        push_master: true,
        push_protected_branch: false,
        push_remove_protected_branch: false,
        push_tag: false,
        push_new_tag: true,
        push_all: false,
        merge_into_protected_branch: false
      },

      reporter: {
        push_new_branch: false,
        push_master: false,
        push_protected_branch: false,
        push_remove_protected_branch: false,
        push_tag: false,
        push_new_tag: false,
        push_all: false,
        merge_into_protected_branch: false
      },

      guest: {
        push_new_branch: false,
        push_master: false,
        push_protected_branch: false,
        push_remove_protected_branch: false,
        push_tag: false,
        push_new_tag: false,
        push_all: false,
        merge_into_protected_branch: false
      }
    }

D
Douwe Maan 已提交
479
    [%w(feature exact), ['feat*', 'wildcard']].each do |protected_branch_name, protected_branch_type|
480 481 482 483 484 485
      context do
        before { create(:protected_branch, name: protected_branch_name, project: project) }

        run_permission_checks(permissions_matrix)
      end

486 487
      context "when developers are allowed to push into the #{protected_branch_type} protected branch" do
        before { create(:protected_branch, :developers_can_push, name: protected_branch_name, project: project) }
488 489 490 491

        run_permission_checks(permissions_matrix.deep_merge(developer: { push_protected_branch: true, push_all: true, merge_into_protected_branch: true }))
      end

492 493
      context "developers are allowed to merge into the #{protected_branch_type} protected branch" do
        before { create(:protected_branch, :developers_can_merge, name: protected_branch_name, project: project) }
494 495 496 497

        context "when a merge request exists for the given source/target branch" do
          context "when the merge request is in progress" do
            before do
498
              create(:merge_request, source_project: project, source_branch: unprotected_branch, target_branch: 'feature',
499
                                     state: 'locked', in_progress_merge_commit_sha: merge_into_protected_branch)
500 501
            end

502 503
            run_permission_checks(permissions_matrix.deep_merge(developer: { merge_into_protected_branch: true }))
          end
504

505 506 507
          context "when the merge request is not in progress" do
            before do
              create(:merge_request, source_project: project, source_branch: unprotected_branch, target_branch: 'feature', in_progress_merge_commit_sha: nil)
508
            end
509 510

            run_permission_checks(permissions_matrix.deep_merge(developer: { merge_into_protected_branch: false }))
511
          end
512

513
          context "when a merge request does not exist for the given source/target branch" do
514 515 516 517 518
            run_permission_checks(permissions_matrix.deep_merge(developer: { merge_into_protected_branch: false }))
          end
        end
      end

519 520
      context "when developers are allowed to push and merge into the #{protected_branch_type} protected branch" do
        before { create(:protected_branch, :developers_can_merge, :developers_can_push, name: protected_branch_name, project: project) }
521 522 523

        run_permission_checks(permissions_matrix.deep_merge(developer: { push_protected_branch: true, push_all: true, merge_into_protected_branch: true }))
      end
524

T
Timothy Andrew 已提交
525 526
      context "when no one is allowed to push to the #{protected_branch_name} protected branch" do
        before { create(:protected_branch, :no_one_can_push, name: protected_branch_name, project: project) }
527

T
Timothy Andrew 已提交
528 529 530 531
        run_permission_checks(permissions_matrix.deep_merge(developer: { push_protected_branch: false, push_all: false, merge_into_protected_branch: false },
                                                            master: { push_protected_branch: false, push_all: false, merge_into_protected_branch: false },
                                                            admin: { push_protected_branch: false, push_all: false, merge_into_protected_branch: false }))
      end
532 533
    end
  end
534

535
  shared_examples 'pushing code' do |can|
K
Kamil Trzcinski 已提交
536
    subject { access.check('git-receive-pack', '_any') }
537

K
Kamil Trzcinski 已提交
538
    context 'when project is authorized' do
K
Kamil Trzcinski 已提交
539
      before { authorize }
540

541
      it { expect(subject).public_send(can, be_allowed) }
K
Kamil Trzcinski 已提交
542 543 544 545
    end

    context 'when unauthorized' do
      context 'to public project' do
546
        let(:project) { create(:project, :public, :repository) }
547

548 549
        it { expect(subject).not_to be_allowed }
      end
550

K
Kamil Trzcinski 已提交
551
      context 'to internal project' do
552
        let(:project) { create(:project, :internal, :repository) }
553

K
Kamil Trzcinski 已提交
554 555
        it { expect(subject).not_to be_allowed }
      end
556

K
Kamil Trzcinski 已提交
557
      context 'to private project' do
558
        let(:project) { create(:project, :private, :repository) }
559

K
Kamil Trzcinski 已提交
560
        it { expect(subject).not_to be_allowed }
561 562
      end
    end
D
Dmitriy Zaporozhets 已提交
563
  end
K
Kamil Trzcinski 已提交
564

565 566
  describe 'build authentication abilities' do
    let(:authentication_abilities) { build_authentication_abilities }
K
Kamil Trzcinski 已提交
567

568
    it_behaves_like 'pushing code', :not_to do
K
Kamil Trzcinski 已提交
569 570 571 572
      def authorize
        project.team << [user, :reporter]
      end
    end
K
Kamil Trzcinski 已提交
573 574 575
  end

  describe 'deploy key permissions' do
576
    let(:key) { create(:deploy_key, user: user, can_push: can_push) }
K
Kamil Trzcinski 已提交
577 578
    let(:actor) { key }

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
    context 'when deploy_key can push' do
      let(:can_push) { true }

      it_behaves_like 'pushing code', :to do
        def authorize
          key.projects << project
        end
      end
    end

    context 'when deploy_key cannot push' do
      let(:can_push) { false }

      it_behaves_like 'pushing code', :not_to do
        def authorize
          key.projects << project
        end
K
Kamil Trzcinski 已提交
596 597
      end
    end
K
Kamil Trzcinski 已提交
598 599 600 601
  end

  private

602
  def build_authentication_abilities
K
Kamil Trzcinski 已提交
603 604 605 606 607
    [
      :read_project,
      :build_download_code
    ]
  end
608

609
  def full_authentication_abilities
610 611 612 613 614 615
    [
      :read_project,
      :download_code,
      :push_code
    ]
  end
D
Dmitriy Zaporozhets 已提交
616
end