Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

Merge commit from fork

Sony Mathew5a80844877d6
diff --git a/app/controllers/concerns/attachment_concern.rb b/app/controllers/concerns/attachment_concern.rb
index 2652f04be02..bf5cf2d600d 100644
--- a/app/controllers/concerns/attachment_concern.rb
+++ b/app/controllers/concerns/attachment_concern.rb
@@ -20,13 +20,20 @@ module AttachmentConcern
private
def process_attachment_action(action, record, blobs)
- blob_id = action[:action_params].first
- blob = ActiveStorage::Blob.find_signed(blob_id.to_s)
-
- return action.merge(action_params: [blob.id]).tap { blobs << blob } if blob.present?
- return action if blob_already_attached?(record, blob_id)
+ blob_ids = action[:action_params].map do |blob_id|
+ blob = ActiveStorage::Blob.find_signed(blob_id.to_s)
+
+ if blob.present?
+ blobs << blob
+ blob.id
+ elsif blob_already_attached?(record, blob_id)
+ blob_id
+ else
+ return nil
+ end
+ end
- nil
+ action.merge(action_params: blob_ids) if blob_ids.present?
end
def blob_already_attached?(record, blob_id)
diff --git a/app/services/automation_rules/action_service.rb b/app/services/automation_rules/action_service.rb
index 5d3d4d03395..d8d7cd62aeb 100644
--- a/app/services/automation_rules/action_service.rb
+++ b/app/services/automation_rules/action_service.rb
@@ -25,9 +25,7 @@ class AutomationRules::ActionService < ActionService
def send_attachment(blob_ids)
return if conversation_a_tweet?
- return unless @rule.files.attached?
-
- blobs = ActiveStorage::Blob.where(id: blob_ids)
+ blobs = @rule.files.blobs.where(id: blob_ids)
return if blobs.blank?
diff --git a/app/services/macros/execution_service.rb b/app/services/macros/execution_service.rb
index 6a32ac0789b..c66404d695f 100644
--- a/app/services/macros/execution_service.rb
+++ b/app/services/macros/execution_service.rb
@@ -50,9 +50,7 @@ class Macros::ExecutionService < ActionService
def send_attachment(blob_ids)
return if conversation_a_tweet?
- return unless @macro.files.attached?
-
- blobs = ActiveStorage::Blob.where(id: blob_ids)
+ blobs = @macro.files.blobs.where(id: blob_ids)
return if blobs.blank?
diff --git a/spec/controllers/api/v1/accounts/automation_rules_controller_spec.rb b/spec/controllers/api/v1/accounts/automation_rules_controller_spec.rb
index 3ddbf5df9c4..1b56c44f04a 100644
--- a/spec/controllers/api/v1/accounts/automation_rules_controller_spec.rb
+++ b/spec/controllers/api/v1/accounts/automation_rules_controller_spec.rb
@@ -428,6 +428,51 @@ RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
end
end
+ describe 'attachment authorization' do
+ let(:blob) { get_blob_for('spec/assets/avatar.png', 'image/png') }
+ let(:new_blob) { get_blob_for('spec/assets/avatar.png', 'image/png') }
+ let(:foreign_message) { create(:message, :with_attachment) }
+ let(:foreign_blob) { foreign_message.attachments.first.file.blob }
+ let(:automation_rule) do
+ create(:automation_rule, account: account, actions: [{ action_name: 'send_attachment', action_params: [blob.id] }])
+ end
+
+ before { automation_rule.files.attach(blob) }
+
+ it 'rejects a foreign raw blob id after an existing attachment without changing the rule' do
+ patch "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
+ params: { actions: [{ action_name: 'send_attachment', action_params: [blob.id, foreign_blob.id] }] },
+ headers: administrator.create_new_auth_token, as: :json
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(response.parsed_body['error']).to eq(I18n.t('errors.attachments.invalid'))
+ expect(automation_rule.reload.actions.first['action_params']).to eq([blob.id])
+ expect(automation_rule.files.blobs.pluck(:id)).to eq([blob.id])
+ end
+
+ it 'rejects a foreign raw blob id after a signed upload when creating a rule' do
+ expect do
+ post "/api/v1/accounts/#{account.id}/automation_rules",
+ params: { name: 'Attachments', event_name: 'conversation_created', conditions: [],
+ actions: [{ action_name: 'send_attachment', action_params: [new_blob.signed_id, foreign_blob.id] }] },
+ headers: administrator.create_new_auth_token, as: :json
+ end.not_to change(AutomationRule, :count)
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(response.parsed_body['error']).to eq(I18n.t('errors.attachments.invalid'))
+ end
+
+ it 'retains existing attachments and attaches every signed upload in an action' do
+ patch "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
+ params: { actions: [{ action_name: 'send_attachment', action_params: [blob.id, new_blob.signed_id] }] },
+ headers: administrator.create_new_auth_token, as: :json
+
+ expect(response).to have_http_status(:success)
+ expect(automation_rule.reload.actions.first['action_params']).to eq([blob.id, new_blob.id])
+ expect(automation_rule.files.blobs.pluck(:id)).to contain_exactly(blob.id, new_blob.id)
+ end
+ end
+
describe 'DELETE /api/v1/accounts/{account.id}/automation_rules/{automation_rule.id}' do
let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
diff --git a/spec/controllers/api/v1/accounts/macros_controller_spec.rb b/spec/controllers/api/v1/accounts/macros_controller_spec.rb
index 10d687d3e0f..10c6f039b75 100644
--- a/spec/controllers/api/v1/accounts/macros_controller_spec.rb
+++ b/spec/controllers/api/v1/accounts/macros_controller_spec.rb
@@ -298,6 +298,61 @@ RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do
end
end
+ describe 'attachment authorization' do
+ let(:blob) { get_blob_for('spec/assets/avatar.png', 'image/png') }
+ let(:new_blob) { get_blob_for('spec/assets/avatar.png', 'image/png') }
+ let(:foreign_message) { create(:message, :with_attachment) }
+ let(:foreign_blob) { foreign_message.attachments.first.file.blob }
+ let(:macro) do
+ create(:macro, account: account, created_by: agent, updated_by: agent,
+ actions: [{ action_name: 'send_attachment', action_params: [blob.id] }])
+ end
+
+ before { macro.files.attach(blob) }
+
+ it 'rejects a foreign raw blob id after an existing attachment without changing the macro' do
+ put "/api/v1/accounts/#{account.id}/macros/#{macro.id}",
+ params: { actions: [{ action_name: 'send_attachment', action_params: [blob.id, foreign_blob.id] }] },
+ headers: agent.create_new_auth_token, as: :json
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(response.parsed_body['error']).to eq(I18n.t('errors.attachments.invalid'))
+ expect(macro.reload.actions.first['action_params']).to eq([blob.id])
+ expect(macro.files.blobs.pluck(:id)).to eq([blob.id])
+ end
+
+ it 'rejects a foreign raw blob id after a signed upload when creating a macro' do
+ expect do
+ post "/api/v1/accounts/#{account.id}/macros",
+ params: { name: 'Attachments', visibility: 'personal',
+ actions: [{ action_name: 'send_attachment', action_params: [new_blob.signed_id, foreign_blob.id] }] },
+ headers: agent.create_new_auth_token, as: :json
+ end.not_to change(Macro, :count)
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(response.parsed_body['error']).to eq(I18n.t('errors.attachments.invalid'))
+ end
+
+ it 'retains existing attachments and attaches every signed upload in an action' do
+ put "/api/v1/accounts/#{account.id}/macros/#{macro.id}",
+ params: { actions: [{ action_name: 'send_attachment', action_params: [blob.id, new_blob.signed_id] }] },
+ headers: agent.create_new_auth_token, as: :json
+
+ expect(response).to have_http_status(:success)
+ expect(macro.reload.actions.first['action_params']).to eq([blob.id, new_blob.id])
+ expect(macro.files.blobs.pluck(:id)).to contain_exactly(blob.id, new_blob.id)
+ end
+
+ it 'rejects attachment actions without any file ids' do
+ put "/api/v1/accounts/#{account.id}/macros/#{macro.id}",
+ params: { actions: [{ action_name: 'send_attachment', action_params: [] }] },
+ headers: agent.create_new_auth_token, as: :json
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(response.parsed_body['error']).to eq(I18n.t('errors.attachments.invalid'))
+ end
+ end
+
describe 'GET /api/v1/accounts/{account.id}/macros/{macro.id}' do
let!(:macro) { create(:macro, account: account, created_by: administrator, updated_by: administrator) }
diff --git a/spec/services/automation_rules/action_service_spec.rb b/spec/services/automation_rules/action_service_spec.rb
index f24617d29e6..64cc787def1 100644
--- a/spec/services/automation_rules/action_service_spec.rb
+++ b/spec/services/automation_rules/action_service_spec.rb
@@ -12,6 +12,39 @@ RSpec.describe AutomationRules::ActionService do
])
end
+ describe 'attachment authorization for stored actions' do
+ before do
+ rule.files.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
+ end
+
+ it 'only sends rule files when stored actions also reference another account attachment' do
+ foreign_message = create(:message, :with_attachment)
+ foreign_blob = foreign_message.attachments.first.file.blob
+ own_blob = rule.files.first.blob
+ rule.update!(actions: [{ action_name: 'send_attachment', action_params: [own_blob.id, foreign_blob.id] }])
+
+ described_class.new(rule, account, conversation).perform
+
+ expect(conversation.messages.last.attachments.map { |attachment| attachment.file.blob_id }).to eq([own_blob.id])
+ end
+
+ it 'does not send another account attachment when no requested blob belongs to the rule' do
+ foreign_message = create(:message, :with_attachment)
+ foreign_blob = foreign_message.attachments.first.file.blob
+ rule.update!(actions: [{ action_name: 'send_attachment', action_params: [foreign_blob.id] }])
+
+ expect { described_class.new(rule, account, conversation).perform }.not_to change(conversation.messages, :count)
+ end
+
+ it 'does not send files belonging to another rule in the same account' do
+ other_rule = create(:automation_rule, account: account)
+ other_rule.files.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
+ rule.update!(actions: [{ action_name: 'send_attachment', action_params: [other_rule.files.first.blob_id] }])
+
+ expect { described_class.new(rule, account, conversation).perform }.not_to change(conversation.messages, :count)
+ end
+ end
+
describe '#perform' do
context 'when actions are defined in the rule' do
it 'will call the actions' do
diff --git a/spec/services/macros/execution_service_spec.rb b/spec/services/macros/execution_service_spec.rb
index d44f7793a6c..cbb75799b94 100644
--- a/spec/services/macros/execution_service_spec.rb
+++ b/spec/services/macros/execution_service_spec.rb
@@ -156,6 +156,33 @@ RSpec.describe Macros::ExecutionService, type: :service do
message = Message.last
expect(message.attachments).to be_present
end
+
+ it 'only sends macro files when stored actions also reference another account attachment' do
+ foreign_message = create(:message, :with_attachment)
+ foreign_blob = foreign_message.attachments.first.file.blob
+ own_blob = macro.files.first.blob
+ macro.update!(actions: [{ action_name: 'send_attachment', action_params: [own_blob.id, foreign_blob.id] }])
+
+ service.perform
+
+ expect(conversation.messages.last.attachments.map { |attachment| attachment.file.blob_id }).to eq([own_blob.id])
+ end
+
+ it 'does not send another account attachment when no requested blob belongs to the macro' do
+ foreign_message = create(:message, :with_attachment)
+ foreign_blob = foreign_message.attachments.first.file.blob
+ macro.update!(actions: [{ action_name: 'send_attachment', action_params: [foreign_blob.id] }])
+
+ expect { service.perform }.not_to change(conversation.messages, :count)
+ end
+
+ it 'does not send files belonging to another macro in the same account' do
+ other_macro = create(:macro, account: account)
+ other_macro.files.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
+ macro.update!(actions: [{ action_name: 'send_attachment', action_params: [other_macro.files.first.blob_id] }])
+
+ expect { service.perform }.not_to change(conversation.messages, :count)
+ end
end
context 'when conversation is a tweet or macro has no files attached' do