From 160732c07d6b23d8ea2bea1114b4a411c5b50b8d Mon Sep 17 00:00:00 2001 From: Sony Mathew Date: Mon, 20 Jul 2026 20:41:39 +0530 Subject: [PATCH] fix: rate limit agent management APIs (#15081) # Pull Request Template Bring agent create and delete requests under rack attack throttling Related to https://linear.app/chatwoot/issue/CW-7637 Co-authored-by: Vishnu Narayanan --- config/initializers/rack_attack.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index e08a6a6e1..41ccae971 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -212,6 +212,24 @@ class Rack::Attack match_data[:account_id] if match_data.present? end + ## Prevent abuse of agent create APIs (per account, covers bulk_create) + throttle('/api/v1/accounts/:account_id/agents POST', + limit: ENV.fetch('RATE_LIMIT_AGENT_CREATE', '100').to_i, period: 1.day) do |req| + next unless req.post? + + match_data = %r{\A/api/v1/accounts/(?\d+)/agents(?:/bulk_create)?/?\z}.match(req.path_without_extensions) + match_data[:account_id] if match_data.present? + end + + ## Prevent abuse of agent delete API (per account) + throttle('/api/v1/accounts/:account_id/agents/:id DELETE', + limit: ENV.fetch('RATE_LIMIT_AGENT_DELETE', '50').to_i, period: 1.day) do |req| + next unless req.delete? + + match_data = %r{\A/api/v1/accounts/(?\d+)/agents/(?\d+)/?\z}.match(req.path_without_extensions) + match_data[:account_id] if match_data.present? + end + ## Prevent Abuse of attachment upload APIs ## throttle('/api/v1/accounts/:account_id/upload', limit: 60, period: 1.hour) do |req| match_data = %r{/api/v1/accounts/(?\d+)/upload}.match(req.path)