From d16a39dc533168f58963493bfe9fd7d481d56b54 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Mon, 3 May 2021 09:21:35 +0200 Subject: [PATCH] FIX: prevents exception when text input is nil (#12922) nil was converted to "" and the matching regex would return [] and then be converted to nil with max usage. Example exception: ``` NoMethodError (undefined method `<=' for nil:NilClass) lib/text_sentinel.rb:71:in `seems_unpretentious?' lib/validators/quality_title_validator.rb:13:in `validate_each' lib/topic_creator.rb:25:in `valid?' ``` --- lib/text_sentinel.rb | 4 +++- spec/components/text_sentinel_spec.rb | 28 ++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/text_sentinel.rb b/lib/text_sentinel.rb index 72053574fb6..f720e1b5032 100644 --- a/lib/text_sentinel.rb +++ b/lib/text_sentinel.rb @@ -68,7 +68,9 @@ class TextSentinel def seems_unpretentious? return true if skipped_locale.include?(SiteSetting.default_locale) # Don't allow super long words if there is a word length maximum - @opts[:max_word_length].blank? || @text.split(/\s|\/|-|\.|:/).map(&:size).max <= @opts[:max_word_length] + + @opts[:max_word_length].blank? || + (@text.split(/\s|\/|-|\.|:/).map(&:size).max || 0) <= @opts[:max_word_length] end def seems_quiet? diff --git a/spec/components/text_sentinel_spec.rb b/spec/components/text_sentinel_spec.rb index d14a8bf6248..2676a010134 100644 --- a/spec/components/text_sentinel_spec.rb +++ b/spec/components/text_sentinel_spec.rb @@ -61,6 +61,18 @@ describe TextSentinel do end end + it "uses a sensible min entropy value when min body length is less than min entropy" do + SiteSetting.min_post_length = 3 + SiteSetting.body_min_entropy = 7 + expect(TextSentinel.body_sentinel('Yup')).to be_valid + end + + it "uses a sensible min entropy value when min pm body length is less than min entropy" do + SiteSetting.min_post_length = 5 + SiteSetting.min_personal_message_post_length = 3 + SiteSetting.body_min_entropy = 7 + expect(TextSentinel.body_sentinel('Lol', private_message: true)).to be_valid + end end context "validity" do @@ -144,19 +156,9 @@ describe TextSentinel do end - context 'body_sentinel' do - - it "uses a sensible min entropy value when min body length is less than min entropy" do - SiteSetting.min_post_length = 3 - SiteSetting.body_min_entropy = 7 - expect(TextSentinel.body_sentinel('Yup')).to be_valid - end - - it "uses a sensible min entropy value when min pm body length is less than min entropy" do - SiteSetting.min_post_length = 5 - SiteSetting.min_personal_message_post_length = 3 - SiteSetting.body_min_entropy = 7 - expect(TextSentinel.body_sentinel('Lol', private_message: true)).to be_valid + context 'seems_unpretentious?' do + it 'works with nil title' do + expect(TextSentinel.title_sentinel(nil).seems_unpretentious?).to eq(true) end end