Rate limit merge request .diff and .patch downloads
What does this MR do and why?
Downloading a merge request as .diff or .patch has no application rate limit. Rails only sets the Workhorse send-data header, and Workhorse then streams Gitaly RawDiff or RawPatch for the whole merge request, with no byte or time cap. Anyone can do this without signing in on a public project.
This adds a merge_request_diff_download rate limit to:
GET /:namespace/:project/-/merge_requests/:iid.diffand.patch, and the same formats of the/reportstab (Projects::MergeRequestsController#showand#reports, which serve them throughshow_merge_request)GET /:namespace/:project/-/merge_requests/:iid/diffs.diffand.patch, which the routes send toProjects::MergeRequests::DiffsController#show
The filter in Projects::MergeRequestsController also lists #diffs, which calls show_merge_request too. Today its .diff and .patch formats are routed to DiffsController first, so this only guards against a future route change.
Details:
- 120 requests per minute, counted per user for signed-in callers and per IP address for anonymous callers. It's the same design as the REST diff routes in https://gitlab.com/gitlab-org/gitlab/-/work_items/606717.
- Configurable with the new
merge_request_diff_download_request_limitapplication setting (0turns it off). - Checked before the Workhorse send-data header is set, the last point where the stream can be refused. A throttled request gets
429and no stream. - Only
diffandpatchformats are counted. The HTML and JSON merge request pages and diffs aren't. - Log mode first: the rule is a
staged_rulebehind themerge_request_diff_download_rate_limitflag (gitlab_com_derisk, off by default). It's checked against the user when signed in, and the instance otherwise. While the flag is disabled, requests over the limit are logged but not blocked.
References
- Issue: https://gitlab.com/gitlab-org/gitlab/-/work_items/635697
- Same design for REST
changes,diffs,raw_diffs: https://gitlab.com/gitlab-org/gitlab/-/work_items/606717 staged_rulepattern: !269290 (merged)- Rollout issue: #639589
Screenshots or screen recordings
Not applicable: no UI changes.
How to set up and validate locally
-
In the Rails console, set a low limit:
ApplicationSetting.current.update!(merge_request_diff_download_request_limit: 2) -
Without signing in, download a public merge request's patch three times:
for i in 1 2 3; do curl -s -o /dev/null -w "%{http_code}\n" "http://gdk.test:3000/<namespace>/<project>/-/merge_requests/<iid>.patch" doneWith the flag disabled, all three return
200, and the third request's log line hasrate_limit_statecontaininglimit_mr_diff_downloads_by_user_or_ip_log:log. -
Enable the flag and repeat after a minute:
Feature.enable(:merge_request_diff_download_rate_limit)The third request returns
429with noGitlab-Workhorse-Send-Dataheader.…/reports.patchcounts toward the same limit. A signed-in user gets their own bucket.
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.