Skip to content

A platform for community discussion. Free, open, simple.

FIX: Do not allow blank chat messages (#21968)

Martin Brennan482ef0782dba
diff --git a/plugins/chat/app/models/chat/message.rb b/plugins/chat/app/models/chat/message.rb
index 49171e2807c..b9911713458 100644
--- a/plugins/chat/app/models/chat/message.rb
+++ b/plugins/chat/app/models/chat/message.rb
@@ -76,6 +76,9 @@ module Chat
def self.polymorphic_class_mapping = { "ChatMessage" => Chat::Message }
def validate_message(has_uploads:)
+ self.message =
+ TextCleaner.clean(self.message, strip_whitespaces: true, strip_zero_width_spaces: true)
+
WatchedWordsValidator.new(attributes: [:message]).validate(self)
if self.new_record? || self.changed.include?("message")
diff --git a/plugins/chat/spec/components/chat/message_creator_spec.rb b/plugins/chat/spec/components/chat/message_creator_spec.rb
index ba4f15ef830..91055313cee 100644
--- a/plugins/chat/spec/components/chat/message_creator_spec.rb
+++ b/plugins/chat/spec/components/chat/message_creator_spec.rb
@@ -65,6 +65,18 @@ describe Chat::MessageCreator do
)
end
+ it "errors when a blank message is sent" do
+ creator =
+ described_class.create(chat_channel: public_chat_channel, user: user1, content: " ")
+ expect(creator.failed?).to eq(true)
+ expect(creator.error.message).to match(
+ I18n.t(
+ "chat.errors.minimum_length_not_met",
+ { count: SiteSetting.chat_minimum_message_length },
+ ),
+ )
+ end
+
it "errors when length is greater than `chat_maximum_message_length`" do
SiteSetting.chat_maximum_message_length = 100
creator =
diff --git a/plugins/chat/spec/components/chat/message_updater_spec.rb b/plugins/chat/spec/components/chat/message_updater_spec.rb
index b42f067c974..5e587d54b0b 100644
--- a/plugins/chat/spec/components/chat/message_updater_spec.rb
+++ b/plugins/chat/spec/components/chat/message_updater_spec.rb
@@ -85,6 +85,27 @@ describe Chat::MessageUpdater do
expect(chat_message.reload.message).to eq(og_message)
end
+ it "errors when a blank message is sent" do
+ og_message = "This won't be changed!"
+ chat_message = create_chat_message(user1, og_message, public_chat_channel)
+ new_message = " "
+
+ updater =
+ Chat::MessageUpdater.update(
+ guardian: guardian,
+ chat_message: chat_message,
+ new_content: new_message,
+ )
+ expect(updater.failed?).to eq(true)
+ expect(updater.error.message).to match(
+ I18n.t(
+ "chat.errors.minimum_length_not_met",
+ { count: SiteSetting.chat_minimum_message_length },
+ ),
+ )
+ expect(chat_message.reload.message).to eq(og_message)
+ end
+
it "errors if a user other than the message user is trying to edit the message" do
og_message = "This won't be changed!"
chat_message = create_chat_message(user1, og_message, public_chat_channel)
diff --git a/plugins/chat/spec/requests/chat/incoming_webhooks_controller_spec.rb b/plugins/chat/spec/requests/chat/incoming_webhooks_controller_spec.rb
index b011abfdae1..f6b18f8aabd 100644
--- a/plugins/chat/spec/requests/chat/incoming_webhooks_controller_spec.rb
+++ b/plugins/chat/spec/requests/chat/incoming_webhooks_controller_spec.rb
@@ -117,7 +117,7 @@ RSpec.describe Chat::IncomingWebhooksController do
Chat::Message.where(chat_channel: chat_channel).count
}.by(1)
expect(Chat::Message.last.message).to eq(
- "New alert: \"[StatusCake] https://www.test_notification.com (StatusCake Test Alert): Down,\" [46353](https://eu.opsg.in/a/i/test/blahguid)\nTags: ",
+ "New alert: \"[StatusCake] https://www.test_notification.com (StatusCake Test Alert): Down,\" [46353](https://eu.opsg.in/a/i/test/blahguid)\nTags:",
)
expect {
post "/chat/hooks/#{webhook.key}/slack.json", params: { payload: payload_data }
@@ -142,7 +142,7 @@ RSpec.describe Chat::IncomingWebhooksController do
post "/chat/hooks/#{webhook.key}/slack.json", params: { payload: payload_data.to_json }
}.to change { Chat::Message.where(chat_channel: chat_channel).count }.by(1)
expect(Chat::Message.last.message).to eq(
- "New alert: \"[StatusCake] https://www.test_notification.com (StatusCake Test Alert): Down,\" [46353](https://eu.opsg.in/a/i/test/blahguid)\nTags: ",
+ "New alert: \"[StatusCake] https://www.test_notification.com (StatusCake Test Alert): Down,\" [46353](https://eu.opsg.in/a/i/test/blahguid)\nTags:",
)
end
end