Report rate-limit calls that silently share or skip a limit

What does this MR do and why?

Gitlab::ApplicationRateLimiter now reports two calls that quietly weaken a limit. It raises in development and test, and tracks with Gitlab::ErrorTracking in production. Production behaviour and Redis keys do not change.

Call What happens today After this MR
Scope has no value for any of the rule's characteristics, e.g. { user: nil } on a %i[user] rule labkit fills every slot with _unknown_, so all such callers share one counter InvalidScopeError in dev/test, tracked in prod
resource: passed for a key whose rule has no count_distinct throttled? returns false, so the limit never applies InvalidResourceError in dev/test, tracked in prod, still returns false

A partly filled scope still passes, e.g. { project: project, user: nil } on a %i[project group user] rule. "No value" uses the same test as labkit's _unknown_ fallback: nil, an empty string, or an unsaved record.

ci_lint and notes_create keep their shared anonymous counter. Anonymous callers reach both with user: nil: lint on a public project, and comment endpoints that check the limit before the access check. That counter is their only app-level cap, so the check skips these two keys when every value is nil. An empty string or an unsaved record still reports.

Design notes
  • Both checks live in ApplicationRateLimiter, next to the existing validate_scope_keys!, which already raises in dev/test and tracks in prod for unknown scope keys. No labkit-ruby change.
  • When a scope has unknown keys, only that error is reported, so one bad call gives one event.
  • The resource: check sits after the allowlist and zero threshold/interval early returns, so it does not fire for those calls. Any normal call to the same key still reports it.
  • SHARED_ANONYMOUS_COUNTER_KEYS holds the two exempt keys. Moving their anonymous callers to an IP-keyed rule, like test_report_show_unauthenticated, is a decision for each owner.
  • Labels::DestroyService has no anonymous caller, but its EE audit spec passes a nil user. That spec now calls execute_without_rate_limiting.
  • The three keys that take resource: today (ai_users_per_ip, unique_project_downloads_for_application, unique_project_downloads_for_namespace) all have count_distinct.

References

Resolves https://gitlab.com/gitlab-org/gitlab/-/work_items/637551

How to set up and validate locally

bin/rspec spec/lib/gitlab/application_rate_limiter_spec.rb spec/lib/gitlab/application_rate_limiter/

Locally this gives 165 examples, 0 failures (FOSS_ONLY=1). With the old application_rate_limiter.rb, the four nil-scope and resource: raise/track specs fail.

Review checklist

  • The all-nil check does not reject a partly filled scope
  • Production path tracks and does not raise, for both checks
  • No change to Redis key shape or to the return value of throttled?
  • Error tracking volume: a tier-3 run found anonymous callers for ci_lint and notes_create only, and both are exempt. Any other all-nil caller would report on every call.
  • The exemption covers nil only, not an empty string or an unsaved record
  • Teammate review done before maintainer review

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 Nidhey Indurkar

Merge request reports

Loading
Loading