Rate limit /internal/allowed per actor across repositories

What does this MR do and why?

Adds a per-actor rate limit to POST /api/v4/internal/allowed, the endpoint gitlab-shell calls for every Git-over-SSH operation and Gitaly's pre-receive hook calls for every push.

The existing gitlab_shell_operation rule counts each combination of Git action, repository path, and actor separately, so one actor can multiply the existing limit by spreading operations over many repositories.

This MR adds:

  • A new Labkit rule, gitlab_shell_operation_per_actor, with characteristics user, deploy_key, and deploy_token and no action or repo_path slot, so one actor shares a single bucket across every repository and Git action. The actor is GitAccessActor#resolved_identity: the user for SSH keys, SSH certificates, and Gitaly hook calls; the deploy key for deploy keys; the deploy token for deploy tokens. Deploy keys and deploy tokens get their own counters rather than sharing their creator's.
  • The check runs first in check_allowed, before the two existing per-repository checks, so an actor over its overall limit stops incrementing the per-repository counters.
  • A new application setting, gitlab_shell_operation_per_actor_limit (default 6000, 0 disables). It is exposed in the Application settings REST API and as a second field under Admin > Settings > Network > Git SSH operations rate limits. The setting is in beta.
  • The check is behind the rate_limit_gitlab_shell_operation_per_actor feature flag (gitlab_com_derisk, disabled by default) with the counted identity as the flag actor: the user, or a Feature::ActorWrapper around the deploy key or deploy token, since those are not FeatureGate models. A percentage-of-actors rollout therefore buckets exactly the actors the rule counts.

Notes for reviewers:

  • A push over SSH calls /internal/allowed twice (gitlab-shell, then the Gitaly pre-receive hook), and both calls land in the same bucket, so an SSH push counts as two operations. Pushes over HTTP and changes made from the UI or API reach this endpoint only through the hook and count once. The documentation says so.
  • No production caller currently delivers a deploy token to this endpoint (gitlab-shell cannot, and Gitaly's parseAndSetGLID has no deploy-token- case), so the deploy_token slot is covered by a synthetic request in the specs.

References

Screenshots or screen recordings

Before After
image image

How to set up and validate locally

  1. Enable the feature flag in a Rails console:

    Feature.enable(:rate_limit_gitlab_shell_operation_per_actor)
  2. Lower the new limit so it is easy to hit. In Admin > Settings > Network > Git SSH operations rate limits, set Maximum number of Git operations per minute for each user to 3, or in a Rails console:

    Gitlab::CurrentSettings.update!(gitlab_shell_operation_per_actor_limit: 3)

    Application settings are cached in memory for up to a minute, so wait about 60 seconds before testing.

  3. Run the checks below with SSH keys registered to your user.

Note

A blocked operation does not fail right away. Rails returns 429 with Retry-After: 60, and gitlab-shell retries /internal/allowed after waiting that long. So the Git client hangs for about 60 seconds, then succeeds when the window resets. To confirm that a request was blocked, look for a 429 on /api/v4/internal/allowed in log/api_json.log, and an Application_Rate_Limiter_Request entry with env: gitlab_shell_operation_per_actor_request_limit in log/auth_json.log. Other gitlab-shell rate limits on this endpoint retry the same way.

Validation

  • One key, two projects: alternate git fetch between two projects. The 4th fetch within a minute is blocked by the per-actor rule (it hangs for about 60 seconds), even though neither project was fetched more than twice.
  • Two keys, same user: alternate fetches across two SSH keys and two projects. The 4th fetch is blocked, which confirms both keys share one counter.
  • Limit set to 0: five fetches in a row all succeed immediately.
  • Feature flag disabled (limit 3): five fetches in a row all succeed immediately, and the per-actor rule never fires.
  • API read: GET /api/v4/application/settings returns gitlab_shell_operation_per_actor_limit.
  • API write: PUT /api/v4/application/settings?gitlab_shell_operation_per_actor_limit=6000 changes the value.
  • API validation: PUT ...?gitlab_shell_operation_per_actor_limit=-1 returns 400 with must be greater than or equal to 0.
  • Admin UI: the second field appears under Git SSH operations rate limits, with a default of 6000 (see screenshots).

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 Fred Reinink

Merge request reports

Loading
Loading