Repository navigation
Conversation
- Inject Illuminate\Contracts\Cache\Repository and TicketTypeManager via IoC into RateLimiter - Cache rolling ticket and appeal quota counts with 5-minute TTL to eliminate SQL COUNT(*) overhead - Automatically invalidate user quota cache on ticket creation and status transitions - Add unit tests for RateLimiter cache hit, miss, and key eviction Co-authored-by: Claude <noreply@anthropic.com>
- Add TicketCreationThrottler implementing Flarum's ThrottleApi middleware convention - Enforce 10-second anti-flood cooldown between ticket/reply submissions per user or IP - Automatically bypass staff and users holding postWithoutThrottle permission - Register throttler in extend.php using ThrottleApi extender - Add integration tests for ticket creation, replies, staff bypass, and guest IP throttling Co-authored-by: Claude <noreply@anthropic.com>
karl-bullock
left a comment
There was a problem hiding this comment.
Thanks! The quota cache and the throttle both work (I ran them on a test site: staff are exempt, quotas still apply, and the 429 shows a clear message in the UI). A few changes before merging:
- Only successful submissions should start the cooldown.
TicketCreationThrottlercallshit()before the controller runs, so a request that fails validation (400/422, e.g. an empty subject) or a quota refusal (403) still burns the 10 s window. On the test site: an empty subject → 400, then the corrected ticket right after → 429 with no explanation. Please count a hit only after the create succeeds. - Please drop the reply throttle and keep it for new tickets only. One reply per 10 s gets in the way of a normal back-and-forth with staff, and ticket creation is where flooding actually hurts (the description also only mentions creation endpoints).
- Clear the quota cache after commit, and on the other paths that change counts. In
SupportTicketResource::create(~line 586) the cache is cleared inside the transaction, before commit, so a concurrent check can re-cache the old count. Please clear it after commit, and also on soft-delete / restore / force-delete and when a ticket's category or type changes. - The 429 response body has no message (
{"errors":[{"status":"429","code":"too_many_requests"}]}); the UI text comes from the client. Adetailwith a short message would help API clients. Optional.
Also, this conflicts with #68 in SupportServiceProvider.php (this branch still calls recordChanges, which #68 removes). I plan to merge #68 first, so please rebase onto main once that lands.
Thanks again!
…he after commit - Drop reply throttling in TicketCreationThrottler so only ticket creation is rate-limited - Record cooldown hits in SupportTicketResource::create after transaction commit so validation/quota failures do not burn the 10s window - Throw FloodingException with translated too_many_requests detail on 429 responses - Clear user quota cache after commit on create and on status, category, deleted_at, delete/restore, and category ticket_type changes Co-authored-by: Claude <noreply@anthropic.com>
|
All fixed! |
…ttings repository in throttle test - Restore RateLimiter parameter in SupportServiceProvider::boot so afterCommit quota cache invalidation does not call clearUserQuotaCache() on null - Update TicketThrottleTest::quota_refusal_does_not_burn_throttle_cooldown to update SettingsRepositoryInterface on the booted app container Co-authored-by: Claude <noreply@anthropic.com>
…Type change Co-authored-by: Claude <noreply@anthropic.com>
|
Thanks, I rechecked the head at 1-3 are done and I am satisfied with how: the hit lands in Item 4 does not work as written, and the test hides it. One regression comes with the cache. Smaller ones:
The cache and the throttle are per-server: Flarum binds the cache to the file store, so with more than one app server each node keeps its own stale count and cooldown. Fine for a single host, but worth one line in the PR. Thanks! |
Summary
This PR introduces native caching for ticket and appeal quotas to eliminate repetitive
COUNT(*)queries on ticket submission, and implements an HTTP-level anti-flood throttler via Flarum'sThrottleApiextender to prevent rapid-fire ticket and reply spam.Key Changes
1. Accelerated Quota Rate Limiting via Cache (
RateLimiter)Illuminate\Contracts\Cache\RepositoryintoRateLimiterto store rolling count checks (general quota window, appeal window, open appeals count) with a 5-minute TTL (300s).clearUserQuotaCache()to automatically evict a user's cached quota keys:SupportTicketResource::creating().SupportTicket::updatedinSupportServiceProvider.tests/unit/RateLimiterCacheTest.phptesting cache hit, miss, quota limit evaluations, and key invalidation.2. Anti-Flood Throttler (
TicketCreationThrottler)TicketCreationThrottlerregistered inextend.phpvia(new Extend\ThrottleApi)->set('linkrobinsSupportTicketTimeout', TicketCreationThrottler::class).429 Too Many Requestsbefore any controller logic or database write begins.linkrobins_support.throttle.ticket.*vslinkrobins_support.throttle.reply.*) so opening a ticket does not lock a user out of follow-up replies.sha1(ipAddress)).lr-support.handle_ticketspermission or Flarum core'spostWithoutThrottlepermission.3. Full HTTP Integration Tests (
TicketThrottleTest)tests/integration/api/TicketThrottleTest.phpextendingTestCase:normal_user_rapid_ticket_creation_is_throttled: Verifies first ticket returns201and immediate second ticket returns429with zero database writes.normal_user_rapid_reply_is_throttled: Verifies repeat replies are blocked with429.guests_are_throttled_by_ip: Verifies guest IP cooldown and IP isolation.unrelated_routes_are_not_throttled: Confirms non-creation routes (e.g. categories index) remain unthrottled.staff_with_handle_tickets_permission_is_exempt_from_throttle&user_with_post_without_throttle_permission_is_exempt: Verifies bypass permissions.general_quota_exceeded_returns_403_forbidden: Verifies business quota boundaries (403 Forbiddenwith localized error messages) distinct from anti-flood rate limits (429).allowedRepeatedQueries()for N+1 query regression monitoring.