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_state field of the request log line, with the _log rule name.
  • The enforce_subscriptions_payment_form_rate_limits flag (gitlab_com_derisk, off by default) rejects requests over the limit with 429 Too Many Requests and logs them in auth.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_limits JSONB 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

How to set up and validate locally

  1. Set GITLAB_SIMULATE_SAAS=1 and restart Rails.

  2. 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"
  3. Sign in and open http://gdk.test:3000/-/subscriptions/payment_form?id=<form id> 2 times.

  4. Check log/development_json.log. The second request has applimiter_subscriptions_payment_form:limit_subscriptions_payment_form_by_user_or_ip_log:log in rate_limit_state, and the response is not 429.

  5. Enable the flag in the Rails console: Feature.enable(:enforce_subscriptions_payment_form_rate_limits).

  6. Repeat step 3. The second response is 429.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

Edited by Roy Zwambag

Merge request reports

Loading
Loading