Merge Request acceptance race condition bypasses execution of pipeline and requirement of pipeline success
Summary
There exists a race condition where if a merge request is created and accepted before the pipeline starts, you can bypass "Only allow merge requests to be merged if the pipeline succeeds" setting of the pipeline.
Steps to reproduce
Prerequisites:
- Ensure Project has pipeline configured
- "Only allow merge requests to be merged if the pipeline succeeds" enabled in project settings.
Via script calling the api (we are talking about sub second chain of operations):
- branch off master
- add/update a file
- create merge request
- accept merge request with {merge_when_pipeline_succeeds: true}
Unfortunately I cannot easily provide POC code I discovered this while developing in Angular6.
What is the current bug behavior?
Instead of the pipeline running, and eventually passing and merge request being automatically accepted, the merge request is immediately approved and merged.
When introducing a delay of 100 miliseconds between creating the merge request and accepting it with "Only allow merge requests to be merged if the pipeline succeeds" causes the MR to "be merged automatically when the pipeline succeeds "
What is the expected correct behavior?
When a pipeline is configured and required to succeed for a merge request to be approved, it should ALWAYS be this way.
Results of GitLab environment info
Expand for output related to GitLab environment info
(For installations with omnibus-gitlab package run and paste the output of:
sudo gitlab-rake gitlab:env:info
) 11:54 ~$ docker exec -it gitlab gitlab-rake gitlab:env:infoSystem information System: Current User: git Using RVM: no Ruby Version: 2.4.5p335 Gem Version: 2.7.6 Bundler Version:1.16.6 Rake Version: 12.3.1 Redis Version: 3.2.12 Git Version: 2.18.1 Sidekiq Version:5.2.1 Go Version: unknown
GitLab information Version: 11.5.3 Revision: aa1173 Directory: /opt/gitlab/embedded/service/gitlab-rails DB Adapter: postgresql URL: https://gitlab.mysite.com HTTP Clone URL: https://gitlab.mysite.com/some-group/some-project.git SSH Clone URL: ssh://git@gitlab.mysite.com:222/some-group/some-project.git Using LDAP: no Using Omniauth: yes Omniauth Providers:
GitLab Shell Version: 8.4.1 Repository storage paths:
- default: /var/opt/gitlab/git-data/repositories Hooks: /opt/gitlab/embedded/service/gitlab-shell/hooks Git: /opt/gitlab/embedded/bin/git
Results of GitLab application Check
Expand for output related to the GitLab application check
(For installations with omnibus-gitlab package run and paste the output of:
sudo gitlab-rake gitlab:check SANITIZE=true
)12:00 $ docker exec -it gitlab gitlab-rake gitlab:check SANITIZE=true Checking GitLab Shell ...
GitLab Shell version >= 8.4.1 ? ... OK (8.4.1) hooks directories in repos are links: ... 2/3 ... repository is empty 5/4 ... ok 4/5 ... ok 5/6 ... ok 3/7 ... repository is empty 5/8 ... ok 18/9 ... repository is empty 4/10 ... ok 24/12 ... ok 25/15 ... ok 25/16 ... ok 25/17 ... ok 31/19 ... ok 31/20 ... ok 31/21 ... ok 31/22 ... ok 4/23 ... ok 4/25 ... ok 25/26 ... ok 2/27 ... ok 31/28 ... ok 10/29 ... ok 25/32 ... ok 2/33 ... ok 51/35 ... ok 10/36 ... ok 3/37 ... ok 10/38 ... ok 31/39 ... ok 4/40 ... ok 31/41 ... ok 31/42 ... ok 25/44 ... ok 25/46 ... ok 29/47 ... ok 25/48 ... ok 3/49 ... ok 25/50 ... ok 31/51 ... ok 31/52 ... ok 62/53 ... ok 10/54 ... ok 10/55 ... ok 62/57 ... ok 25/59 ... ok 63/60 ... ok Running /opt/gitlab/embedded/service/gitlab-shell/bin/check Check GitLab API access: OK Redis available via internal API: OK
Access to /var/opt/gitlab/.ssh/authorized_keys: OK gitlab-shell self-check successful
Checking GitLab Shell ... Finished
Checking Gitaly ...
default ... OK
Checking Gitaly ... Finished
Checking Sidekiq ...
Running? ... yes Number of Sidekiq processes ... 1
Checking Sidekiq ... Finished
Reply by email is disabled in config/gitlab.yml Checking LDAP ...
LDAP is disabled in config/gitlab.yml
Checking LDAP ... Finished
Checking GitLab ...
Git configured correctly? ... yes Database config exists? ... yes All migrations up? ... yes Database contains orphaned GroupMembers? ... no GitLab config exists? ... yes GitLab config up to date? ... yes Log directory writable? ... yes Tmp directory writable? ... yes Uploads directory exists? ... yes Uploads directory has correct permissions? ... yes Uploads directory tmp has correct permissions? ... yes Init script exists? ... skipped (omnibus-gitlab has no init script) Init script up-to-date? ... skipped (omnibus-gitlab has no init script) Projects have namespace: ... 2/3 ... yes 5/4 ... yes 4/5 ... yes 5/6 ... yes 3/7 ... yes 5/8 ... yes 18/9 ... yes 4/10 ... yes 24/12 ... yes 25/15 ... yes 25/16 ... yes 25/17 ... yes 31/19 ... yes 31/20 ... yes 31/21 ... yes 31/22 ... yes 4/23 ... yes 4/25 ... yes 25/26 ... yes 2/27 ... yes 31/28 ... yes 10/29 ... yes 25/32 ... yes 2/33 ... yes 51/35 ... yes 10/36 ... yes 3/37 ... yes 10/38 ... yes 31/39 ... yes 4/40 ... yes 31/41 ... yes 31/42 ... yes 25/44 ... yes 25/46 ... yes 29/47 ... yes 25/48 ... yes 3/49 ... yes 25/50 ... yes 31/51 ... yes 31/52 ... yes 62/53 ... yes 10/54 ... yes 10/55 ... yes 62/57 ... yes 25/59 ... yes 63/60 ... yes Redis version >= 2.8.0? ... yes Ruby version >= 2.3.5 ? ... yes (2.4.5) Git version >= 2.9.5 ? ... yes (2.18.1) Git user has default SSH configuration? ... yes Active users: ... 54
Checking GitLab ... Finished
Possible fixes
(If you can, link to the line of code that might be responsible for the problem)
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/api/merge_requests.rb#L375-383 I suspect merge_request.head_pipeline.active is false when the accept is triggered several milliseconds after the merge request is created.