Add rate limit logging to the payment form endpoints
What does this MR do and why?
Adds an application rate limit to the two endpoints behind the credit card form in identity verification: GET /-/subscriptions/payment_form and POST /-/subscriptions/validate_payment_method.
- The limit is per user when the user is signed in. During identity verification the limit is per IP address. The request log line has the identity verification user ID in
meta.identity_verification_user_id. - Requests over the limit are allowed and recorded in the
rate_limit_statefield of the request log line, with the_logrule name. - The
enforce_subscriptions_payment_form_rate_limitsflag (gitlab_com_derisk, off by default) rejects requests over the limit with429 Too Many Requestsand logs them inauth.log. - The limit applies on GitLab.com only. On GitLab Self-Managed and GitLab Dedicated, requests are not counted or logged.
- Requests with the rate limit bypass header are not counted or logged.
- The thresholds are application settings in the
rate_limitsJSONB column, as in !268308 (merged). No migration is needed.
| Setting | Default | Applies to |
|---|---|---|
subscriptions_payment_form_per_hour_limit |
20 per hour | payment_form |
subscriptions_validate_payment_method_per_hour_limit |
10 per hour | validate_payment_method |
Set a limit to 0 to disable it.
A 429 on payment_form or validate_payment_method shows the identity verification rate limit message: "You've reached the maximum amount of tries. Wait about 1 hour and try again."
References
- https://gitlab.com/gitlab-org/gitlab/-/work_items/637366
- Rollout issue: https://gitlab.com/gitlab-org/gitlab/-/work_items/639404
How to set up and validate locally
-
Set
GITLAB_SIMULATE_SAAS=1and restart Rails. -
As an administrator, set a low limit:
curl --request PUT --header "PRIVATE-TOKEN: <admin token>" \ "http://gdk.test:3000/api/v4/application/settings?subscriptions_payment_form_per_hour_limit=1" -
Sign in and open
http://gdk.test:3000/-/subscriptions/payment_form?id=<form id>2 times. -
Check
log/development_json.log. The second request hasapplimiter_subscriptions_payment_form:limit_subscriptions_payment_form_by_user_or_ip_log:loginrate_limit_state, and the response is not 429. -
Enable the flag in the Rails console:
Feature.enable(:enforce_subscriptions_payment_form_rate_limits). -
Repeat step 3. The second response is
429.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist.