Rate limit the merge ref API endpoint per user or IP address
What does this MR do and why?
Each call to GET /projects/:id/merge_requests/:merge_request_iid/merge_ref runs a mergeability recheck, which can merge to the merge ref in Gitaly. Nothing bounded how often one caller could do that.
This merge request adds the merge_requests_merge_ref application rate limit, checked with check_rate_limit_by_user_or_ip! before the merge request lookup. The new merge_requests_merge_ref_limit application setting sets the limit for each user, or for each IP address for unauthenticated requests. It defaults to 0, which disables it, like other recent rate limits. GitLab.com sets its value through the application settings API. Over the limit, the endpoint returns 429.
https://gitlab.com/gitlab-org/gitlab/-/work_items/637079
References
Screenshots or screen recordings
| Before | After |
|---|---|
How to set up and validate locally
- Set a low limit:
ApplicationSetting.current.update!(merge_requests_merge_ref_limit: 1). - Call
GET /api/v4/projects/:id/merge_requests/:merge_request_iid/merge_reftwice in one minute. The second call returns429.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.