Introduce a daily cap on non-channel outbound emails to prevent abuse. Fixes https://linear.app/chatwoot/issue/CW-6418/ses-incident-jan-28 ## Type of change - [x] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality not to work as expected) ## Summary - Adds a Redis-based daily counter to rate limit outbound emails per account, preventing email abuse - Covers continuity emails (WebWidget/API), conversation transcripts, and agent notifications - Email channel replies are excluded (paid feature, not abusable) - Adds account suspension check in `ConversationReplyMailer` to block already-queued emails for suspended accounts ## Limit Resolution Hierarchy 1. Per-account override (`account.limits['emails']`) — SuperAdmin configurable 2. Enterprise plan-based (`ACCOUNT_EMAILS_PLAN_LIMITS` InstallationConfig) 3. Global default (`ACCOUNT_EMAILS_LIMIT` InstallationConfig, default: 100) 4. Fallback (`ChatwootApp.max_limit` — effectively unlimited) ## Enforcement Points | Path | Where | Behavior | |------|-------|----------| | WebWidget/API continuity | `SendEmailNotificationService#should_send_email_notification?` | Silently skipped | | Widget transcript | `Widget::ConversationsController#transcript` | Returns 429 | | API transcript | `ConversationsController#transcript` | Returns 429 | | Agent notifications | `Notification::EmailNotificationService#perform` | Silently skipped | | Email channel replies | Not rate limited | Paid feature | | Suspended accounts | `ConversationReplyMailer` | Blocked at mailer level |
101 lines
2.9 KiB
Ruby
101 lines
2.9 KiB
Ruby
class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController
|
|
include Events::Types
|
|
before_action :render_not_found_if_empty, only: [:toggle_typing, :toggle_status, :set_custom_attributes, :destroy_custom_attributes]
|
|
|
|
def index
|
|
@conversation = conversation
|
|
end
|
|
|
|
def create
|
|
ActiveRecord::Base.transaction do
|
|
process_update_contact
|
|
@conversation = create_conversation
|
|
conversation.messages.create!(message_params)
|
|
# TODO: Temporary fix for message type cast issue, since message_type is returning as string instead of integer
|
|
conversation.reload
|
|
end
|
|
end
|
|
|
|
def process_update_contact
|
|
@contact = ContactIdentifyAction.new(
|
|
contact: @contact,
|
|
params: { email: contact_email, phone_number: contact_phone_number, name: contact_name },
|
|
retain_original_contact_name: true,
|
|
discard_invalid_attrs: true
|
|
).perform
|
|
end
|
|
|
|
def update_last_seen
|
|
head :ok && return if conversation.nil?
|
|
|
|
conversation.contact_last_seen_at = DateTime.now.utc
|
|
conversation.save!
|
|
::Conversations::UpdateMessageStatusJob.perform_later(conversation.id, conversation.contact_last_seen_at)
|
|
head :ok
|
|
end
|
|
|
|
def transcript
|
|
return head :too_many_requests unless conversation.present? && conversation.account.within_email_rate_limit?
|
|
|
|
send_transcript_email
|
|
head :ok
|
|
end
|
|
|
|
def toggle_typing
|
|
case permitted_params[:typing_status]
|
|
when 'on'
|
|
trigger_typing_event(CONVERSATION_TYPING_ON)
|
|
when 'off'
|
|
trigger_typing_event(CONVERSATION_TYPING_OFF)
|
|
end
|
|
|
|
head :ok
|
|
end
|
|
|
|
def toggle_status
|
|
return head :forbidden unless @web_widget.end_conversation?
|
|
|
|
unless conversation.resolved?
|
|
conversation.status = :resolved
|
|
conversation.save!
|
|
end
|
|
head :ok
|
|
end
|
|
|
|
def set_custom_attributes
|
|
conversation.update!(custom_attributes: permitted_params[:custom_attributes])
|
|
end
|
|
|
|
def destroy_custom_attributes
|
|
conversation.custom_attributes = conversation.custom_attributes.excluding(params[:custom_attribute])
|
|
conversation.save!
|
|
render json: conversation
|
|
end
|
|
|
|
private
|
|
|
|
def send_transcript_email
|
|
return if conversation.contact&.email.blank?
|
|
|
|
ConversationReplyMailer.with(account: conversation.account).conversation_transcript(
|
|
conversation,
|
|
conversation.contact.email
|
|
)&.deliver_later
|
|
conversation.account.increment_email_sent_count
|
|
end
|
|
|
|
def trigger_typing_event(event)
|
|
Rails.configuration.dispatcher.dispatch(event, Time.zone.now, conversation: conversation, user: @contact)
|
|
end
|
|
|
|
def render_not_found_if_empty
|
|
return head :not_found if conversation.nil?
|
|
end
|
|
|
|
def permitted_params
|
|
params.permit(:id, :typing_status, :website_token, :email, contact: [:name, :email, :phone_number],
|
|
message: [:content, :referer_url, :timestamp, :echo_id],
|
|
custom_attributes: {})
|
|
end
|
|
end
|