Add rate limit to project webhook creation API

What does this MR do and why?

Adds a rate limit to POST /projects/:id/hooks, which until now could be called without a bound. The limit is 5 requests per minute per user per project. It is configurable through a new project_hooks_create_limit application setting, and 0 disables it.

This builds on !245299 (merged), which added the "Webhook rate limits" admin section, the docs page and the web_hook_*_limit settings. This MR extends those files and creates none.

Closes #605597

Changes

  • Setting: project_hooks_create_limit (default 5) added to the model, application_setting_implementation.rb defaults, the rate-limits JSON schema, the admin helper's visible_attributes and lib/api/settings.rb.
  • Rule: project_hooks_create in labkit_adapter/supported_rate_limits.rb (characteristics project, user, period 1 minute). There is no feature flag and no migration. Setting the limit to 0 is the kill switch.
  • Endpoint: lib/api/project_hooks.rb calls check_rate_limit!(:project_hooks_create, scope: { project:, user: }) at the top of the create block. It also adds 429 to the failure list and a rate-limit note to the endpoint description.
  • Admin UI: a third number field (min: 0) in the "Webhook rate limits" section, with updated block text and i18n strings in locale/gitlab.pot.
  • Docs: rate-limit-on-webhook-operations.md, project_webhooks.md, settings.md, and openapi_v2.yaml and openapi_v3.yaml.
  • Specs: a new rate-limit block in spec/requests/api/project_hooks_spec.rb, plus the setting added to the model and helper specs.

Notes for reviewers

  • action: :limit: the issue proposed action: :block. The Labkit version in this checkout only accepts :limit, :log or :skip, so I used :limit, like the sibling rules.
  • Scope format: the scope is a Hash keyed by characteristic names, as validate_scope! requires. resend_hook.rb still passes an array.
  • Redis cleanup in the spec: :clean_gitlab_redis_rate_limiting is applied to all of project_hooks_spec.rb. The existing create examples share one let_it_be user and project, so they would otherwise exceed 5 per minute and get 429s.
  • OpenAPI files: I patched these by hand. The generators produced large unrelated diffs locally (about 14k lines for v2). Please check that CI's generator output matches.
  • Out of scope:
    • 401 and 403 responses are not counted, because authenticate! and authorize! run before the endpoint body. This change bounds 201 and 422 responses only.
    • Group webhook creation (lib/api/group_hooks.rb) has the same unbounded path. A follow-up issue is needed: .

How to verify

  1. As a maintainer, send six POST /projects/:id/hooks requests within one minute. The first five return 201 and the sixth returns 429.
  2. A different user, or a different project, is not affected by the first user's count.
  3. Set project_hooks_create_limit to 0 under Admin > Settings > Network > Webhook rate limits or through PUT /application/settings. Creation is no longer limited.

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.

Related to #605597

Edited by Carla Drago

Merge request reports

Loading
Loading