Rate limit merge request notes polling

What does this MR do and why?

Adds a rate limit of 60 requests per minute per IP for anonymous callers of merge request notes polling (GET /:namespace/:project/noteable/merge_request/:id/notes), under the merge_request_notes_index_unauthenticated key.

Admins can change the limit through the application settings API with merge_request_notes_index_unauthenticated_limit (default 60, 0 disables it).

The limit is a staged rule. Until the merge_request_notes_index_unauthenticated_rate_limit flag is enabled, requests over the limit are logged but not blocked, so the threshold can be checked against GitLab.com traffic before enforcing.

Signed-in users, and issue, commit, and snippet notes polling, are not affected.

Context and design choices
  • Callers choose X-Last-Fetched-At, so each request can return up to 100 notes. Nothing limited how often an anonymous caller on a public project could repeat this.
  • The check runs after the noteable lookup and authorize_read_note!, so only requests that would read notes count.
  • Signed-in callers are left out: they are attributable and blockable, and a per-user threshold has no traffic data behind it yet. A per-user staged rule can follow if logs show abuse from accounts.
  • The flag uses the :instance actor because anonymous callers have no user to gate on, like the merge request diff limits.
  • The threshold is an application setting in the rate_limits JSONB column (no migration), like the neighbouring merge request diff limits. If the log stage shows legitimate traffic over the default (for example shared NAT), admins can raise it without a deploy.
  • When a request is throttled, the frontend ignores the 429 and keeps its last timestamp, so the next update event fetches the missed notes. If no further event arrives, the viewer sees them after a reload.

Out of scope

  • A byte budget per response, with a continuation point. Follow-up MR.
  • Ignoring html=1 for merge request targets. No first-party client sends it, and it renders each note's whole thread.

References

Screenshots or screen recordings

Not applicable, no UI change.

How to set up and validate locally

  1. Pick a public project with a merge request, and note the merge request's database ID.

  2. Signed out, poll the endpoint 61 times within one minute:

    for i in $(seq 1 61); do
      curl -s -o /dev/null -w "%{http_code}\n" -H "X-Last-Fetched-At: 1" -H "Accept: application/json" \
        "http://gdk.test:8080/<group>/<project>/noteable/merge_request/<mr_id>/notes"
    done | sort | uniq -c

    All return 200. In log/development_json.log, the 61st request has rate_limit_state ending in _by_ip_log:log.

  3. In the Rails console, enable the flag:

    Feature.enable(:merge_request_notes_index_unauthenticated_rate_limit)
  4. After a minute, repeat step 2. The 61st request returns 429.

  5. Issue notes on the same project (noteable/issue/<issue_id>/notes) still return 200.

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.

Generated with Claude Code

Edited by Sincheol (David) Kim

Merge request reports

Loading
Loading