Rate limit package uploads authorized to a final store path

What does this MR do and why?

Adds a per-actor rate limit of 1,500 requests a minute to package upload authorizations that use a final store path.

The key is a staged_rule, so it only logs until the package_final_store_path_authorize_rate_limit flag is enabled.

Details

Where the check runs. In each case it runs after the permission checks and before the pending entry is written:

Route Check
Maven PUT .../authorize After protect_package!. Skipped for .sha1 and .md5, which get a temporary path and write no entry.
Generic and Terraform module PUT .../authorize In authorize_workhorse!, after authorize_upload!, only when use_final_store_path is true. Only these two routes pass it.
Maven virtual registry GET and HEAD In workhorse_upload_url, which runs only on a cache miss. Cache hits are not counted.

Limit. One shared key covers all four routes, so the limit applies to an actor across all of them, not to each route separately.

Actor. On these routes current_user can be a DeployToken. A deploy token is counted under deploy_token:, not user:. The Redis key holds only the primary key, so deploy token 5 and user 5 would otherwise share one counter.

Bypass header. The helper calls Gitlab::ApplicationRateLimiter.throttled? directly, not check_rate_limit!. check_rate_limit! passes the throttle bypass header through, and a request with that header set skips the rule.

Flag actor. enforce checks the flag against scope[:user]. A deploy-token request has no user, so it is enforced only when the flag is fully enabled, not by actor or percentage rollout.

Limit lambda. The limit is ->(ctx) { ctx&.dig(:threshold) || 1_500 }, the form project_repositories_archive uses, so specs can lower it per call. Production callers never pass a threshold.

No changelog. Nothing changes for users while the flag is off. The MR that removes the flag will add Changelog: added.

References

Screenshots or screen recordings

Not applicable. There is no UI change.

How to set up and validate locally

  1. In the Rails console, check log mode. The flag is disabled by default:

    user = User.find_by_username('root')
    key = :package_final_store_path_authorize
    2.times.map { Gitlab::ApplicationRateLimiter.throttled?(key, scope: { user: user }, threshold: 1) }
    # => [false, false]: over the limit, but only logged
  2. Enable the flag and repeat:

    Feature.enable(:package_final_store_path_authorize_rate_limit)
    2.times.map { Gitlab::ApplicationRateLimiter.throttled?(key, scope: { user: user }, threshold: 1) }
    # => [false, true]: the second call is rejected

    The enforcing rule counts separately from the log rule, so it starts from zero.

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 Tarun Raja

Merge request reports

Loading
Loading