Loading
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(default5) added to the model,application_setting_implementation.rbdefaults, the rate-limits JSON schema, the admin helper'svisible_attributesandlib/api/settings.rb. - Rule:
project_hooks_createinlabkit_adapter/supported_rate_limits.rb(characteristicsproject,user, period 1 minute). There is no feature flag and no migration. Setting the limit to0is the kill switch. - Endpoint:
lib/api/project_hooks.rbcallscheck_rate_limit!(:project_hooks_create, scope: { project:, user: })at the top of the create block. It also adds429to 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 inlocale/gitlab.pot. - Docs:
rate-limit-on-webhook-operations.md,project_webhooks.md,settings.md, andopenapi_v2.yamlandopenapi_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 proposedaction: :block. The Labkit version in this checkout only accepts:limit,:logor: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.rbstill passes an array. - Redis cleanup in the spec:
:clean_gitlab_redis_rate_limitingis applied to all ofproject_hooks_spec.rb. The existing create examples share onelet_it_beuser 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!andauthorize!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: .
- 401 and 403 responses are not counted, because
How to verify
- As a maintainer, send six
POST /projects/:id/hooksrequests within one minute. The first five return201and the sixth returns429. - A different user, or a different project, is not affected by the first user's count.
- Set
project_hooks_create_limitto0under Admin > Settings > Network > Webhook rate limits or throughPUT /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