fix: Erase null bytes from incoming Twilio messages (#7901)

We've had some messages come in from a few different phone numbers that had null bytes in them. I don't know how this happens. They don't seem to be malicious.

They currently cause the Postgres gem to raise an error when Chatwoot attempts to save the message body to the database:

ArgumentError (string contains null byte)

Related Rails GitHub issue: rails/rails#26891
This commit is contained in:
Jordan Brough
2023-09-12 19:07:18 -06:00
committed by GitHub
parent cb07ac16d1
commit 4f5c5e9f85
2 changed files with 18 additions and 1 deletions

View File

@@ -9,7 +9,7 @@ class Twilio::IncomingMessageService
set_contact
set_conversation
@message = @conversation.messages.create!(
content: params[:Body],
content: message_body,
account_id: @inbox.account_id,
inbox_id: @inbox.id,
message_type: :incoming,
@@ -46,6 +46,10 @@ class Twilio::IncomingMessageService
TelephoneNumber.parse(phone_number).international_number
end
def message_body
params[:Body].delete("\u0000")
end
def set_contact
contact_inbox = ::ContactInboxWithContactBuilder.new(
source_id: params[:From],

View File

@@ -24,6 +24,19 @@ describe Twilio::IncomingMessageService do
expect(conversation.reload.messages.last.content).to eq('testing3')
end
it 'removes null bytes' do
params = {
SmsSid: 'SMxx',
From: '+12345',
AccountSid: 'ACxxx',
MessagingServiceSid: twilio_channel.messaging_service_sid,
Body: "remove\u0000 null bytes\u0000"
}
described_class.new(params: params).perform
expect(conversation.reload.messages.last.content).to eq('remove null bytes')
end
it 'creates a new conversation' do
params = {
SmsSid: 'SMxx',