DEV: add maxlength limits to chat messages and revisions (#23530)

Add additional limits to text columns for chat.
This commit is contained in:
David Battersby 2023-09-12 18:02:04 +08:00 committed by GitHub
parent b7d7099d08
commit 16ccf30abc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 0 deletions

View File

@ -73,6 +73,7 @@ module Chat
before_save { ensure_last_editor_id } before_save { ensure_last_editor_id }
validates :cooked, length: { maximum: 20_000 }
validate :validate_message validate :validate_message
def self.polymorphic_class_mapping = { "ChatMessage" => Chat::Message } def self.polymorphic_class_mapping = { "ChatMessage" => Chat::Message }

View File

@ -4,6 +4,9 @@ module Chat
class MessageRevision < ActiveRecord::Base class MessageRevision < ActiveRecord::Base
self.table_name = "chat_message_revisions" self.table_name = "chat_message_revisions"
validates :old_message, length: { maximum: 50_000 }
validates :new_message, length: { maximum: 50_000 }
belongs_to :chat_message, class_name: "Chat::Message" belongs_to :chat_message, class_name: "Chat::Message"
belongs_to :user belongs_to :user
end end

View File

@ -0,0 +1,6 @@
# frozen_string_literal: true
RSpec.describe Chat::MessageRevision do
it { is_expected.to validate_length_of(:old_message).is_at_most(50_000) }
it { is_expected.to validate_length_of(:new_message).is_at_most(50_000) }
end

View File

@ -7,6 +7,12 @@ describe Chat::Message do
it { is_expected.to have_many(:chat_mentions).dependent(:destroy) } it { is_expected.to have_many(:chat_mentions).dependent(:destroy) }
describe "validations" do
subject(:message) { described_class.new(message: "") }
it { is_expected.to validate_length_of(:cooked).is_at_most(20_000) }
end
describe ".cook" do describe ".cook" do
it "does not support HTML tags" do it "does not support HTML tags" do
cooked = described_class.cook("<h1>test</h1>") cooked = described_class.cook("<h1>test</h1>")