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.diff and .patch, and the same formats of the /reports tab (Projects::MergeRequestsController#show and #reports, which serve them through show_merge_request)
  • GET /:namespace/:project/-/merge_requests/:iid/diffs.diff and .patch, which the routes send to Projects::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_limit application setting (0 turns it off).
  • Checked before the Workhorse send-data header is set, the last point where the stream can be refused. A throttled request gets 429 and no stream.
  • Only diff and patch formats are counted. The HTML and JSON merge request pages and diffs aren't.
  • Log mode first: the rule is a staged_rule behind the merge_request_diff_download_rate_limit flag (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

Screenshots or screen recordings

Not applicable: no UI changes.

How to set up and validate locally

  1. In the Rails console, set a low limit:

    ApplicationSetting.current.update!(merge_request_diff_download_request_limit: 2)
  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"
    done

    With the flag disabled, all three return 200, and the third request's log line has rate_limit_state containing limit_mr_diff_downloads_by_user_or_ip_log:log.

  3. Enable the flag and repeat after a minute:

    Feature.enable(:merge_request_diff_download_rate_limit)

    The third request returns 429 with no Gitlab-Workhorse-Send-Data header. …/reports.patch counts 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.

Edited by Eugenia Grieff

Merge request reports

Loading
Loading