mirror of
https://github.com/discourse/discourse.git
synced 2025-06-16 03:11:19 +08:00
FIX: Do not allow blank chat messages (#21968)
This fixes an issue where a user could send an empty string as a chat message .e.g ' ' and the message would be posted. We don't want this, we need to strip the message first before validating for length etc.
This commit is contained in:
@ -76,6 +76,9 @@ module Chat
|
|||||||
def self.polymorphic_class_mapping = { "ChatMessage" => Chat::Message }
|
def self.polymorphic_class_mapping = { "ChatMessage" => Chat::Message }
|
||||||
|
|
||||||
def validate_message(has_uploads:)
|
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)
|
WatchedWordsValidator.new(attributes: [:message]).validate(self)
|
||||||
|
|
||||||
if self.new_record? || self.changed.include?("message")
|
if self.new_record? || self.changed.include?("message")
|
||||||
|
@ -65,6 +65,18 @@ describe Chat::MessageCreator do
|
|||||||
)
|
)
|
||||||
end
|
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
|
it "errors when length is greater than `chat_maximum_message_length`" do
|
||||||
SiteSetting.chat_maximum_message_length = 100
|
SiteSetting.chat_maximum_message_length = 100
|
||||||
creator =
|
creator =
|
||||||
|
@ -85,6 +85,27 @@ describe Chat::MessageUpdater do
|
|||||||
expect(chat_message.reload.message).to eq(og_message)
|
expect(chat_message.reload.message).to eq(og_message)
|
||||||
end
|
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
|
it "errors if a user other than the message user is trying to edit the message" do
|
||||||
og_message = "This won't be changed!"
|
og_message = "This won't be changed!"
|
||||||
chat_message = create_chat_message(user1, og_message, public_chat_channel)
|
chat_message = create_chat_message(user1, og_message, public_chat_channel)
|
||||||
|
Reference in New Issue
Block a user