Cap new Service Desk external participants per day
What does this MR do and why?
Caps how many distinct new external email addresses a top-level namespace can add as Service Desk participants per day, so one namespace can't reach an unbounded number of new external addresses through Service Desk. All subgroups and projects under the top-level namespace share the cap.
-
Counted: addresses added by group members or the support bot:
/add_email, CC on inbound email (when "Add external participants from CC" is on), and/convert_to_ticket. -
Not counted: ticket authors who email in, ticket moves, and addresses already on the ticket. An address already counted today doesn't count again, and stays addable to other tickets after the cap is reached. A refused address isn't counted either.
-
Over the cap: the participant is not created.
/add_emailand/convert_to_ticketreturn an error. Skipped CC addresses get one internal note per ticket per hour, so agents know. -
Caps: hard-coded per plan for now, while they're validated on GitLab.com. They'll move to a
plan_limitscolumn once confirmed.Plan Daily cap Free, Premium trial, Ultimate trial (and legacy Early Adopter) 20 Premium, Open Source (and legacy Bronze, Silver) 50 Ultimate, Ultimate trial paid customer (and legacy Gold) 150 Any other plan, including the self-managed defaultplanNo cap -
Feature flag:
service_desk_external_participant_limit(wip, disabled by default), checked per top-level namespace.
Implementation notes
-
ServiceDesk::ExternalParticipantLimiter(lib/service_desk/, EE override for the caps) uses a new Labkitcount_distinctrule withadmit_counted_values(gitlab-labkit 5.9.0), one Redis set per top-level namespace per day. One atomic script lets an address already in the set through, adds a new one only while the set is below the cap, and never adds a refused one. The member is a keyed digest of the lowercased address (Gitlab::CryptoHelper.sha256), so addresses never reach Redis. There's no database query for the count. Blocked addresses are logged with the root namespace, without the address. -
One Redis call per address, at most 6 per request. Labkit checks one
count_distinctvalue per call (one atomicEVALSHA) and has no multi-value check, so the limiter checks each address on its own. That stays cheap because the number of checks is capped:Path Limiter checks per request /add_emailat most 6 ( MAX_NUMBER_OF_EMAILS)CC on inbound email at most 6, for the same reason /convert_to_ticket1, or 0 when the address is already a participant Addresses that are malformed, already on the ticket, or over the ticket's 10 participant slots are filtered out before the check, so they never reach Redis. For comparison, each added address already costs one uniqueness query on save and one Redis write to enqueue its "new participant" email. A batch check would need a new Labkit API and another cross-SDK contract change for little gain.
-
ApplicationRateLimiter.throttled?gets adistinct_value:keyword, an alternative toresource:socount_distinctrules can count values that aren't ActiveRecord models. Passing both raises. -
ServiceDesk::PostNoticeServiceis extracted fromServiceDesk::EmailRateLimiter#post_suppression_notice, so the existing suppression notice and the new CC notice share the "internal note as the support bot, throttled per ticket" logic.post_suppression_noticebehaves the same. -
IssueEmailParticipants::CreateServicenow works in steps: deduplicate, keep valid emails that fit the ticket's 10 slots, check the cap once per email, charge the email rate limiter only for emails about to be added, then create. A separate commit fixesdeduplicate_and_limit_emails, which never removed emails already on the ticket or case-insensitive duplicates (the uniqueness validation was catching them). Uniqueness is still queried once per email, through the existing(issue_id, lower(email))unique index.
Known behaviors worth reviewing
- The window is 24 hours from the first address, not a calendar day.
- CC can use up a project's cap. On a project with "Add external participants from CC" enabled, anyone who emails the Service Desk address can use up the namespace's daily cap, after which agents can't
/add_emailnew addresses until the window expires. - The
above_limit_countlog (emails over a ticket's 10 slots) no longer counts malformed addresses or addresses already on the ticket.
References
- Issue: https://gitlab.com/gitlab-org/gitlab/-/work_items/632306
- Labkit option: gitlab-org/ruby/gems/labkit-ruby!404 (merged) (contract: gitlab-org/quality/tooling/labkit-spec!52 (merged))
gitlab-labkit5.9.0 upgrade: !271317 (merged)- Documentation will follow in a separate MR before the flag is enabled.
Screenshots or screen recordings
Not applicable: backend change. The user-facing change is the quick action message, for example:
Added user@example.com. Couldn't add other@example.com because the namespace reached its daily limit for new email participants. Try again later.
How to set up and validate locally
-
Start GDK in SaaS mode (
export GITLAB_SIMULATE_SAAS=1) and make sure a top-level group is on the Free plan, with a project that has Service Desk enabled. -
In the Rails console, enable the flag for that group:
Feature.enable(:service_desk_external_participant_limit, Group.find_by_full_path('my-group')) -
On an issue in the project, run
/add_emailwith new addresses until 20 have been added (6 per command, 10 per issue, so use a few issues). -
Run
/add_email new-21@example.com. It returns the "daily limit" message and adds nothing. -
Run
/add_emailwith an address you added earlier, on a different issue. It's added, because addresses counted earlier in the day stay addable after the cap. -
Disable the flag and repeat: every address is added.
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.