diff --git a/lib/regex_helper.rb b/lib/regex_helper.rb index 5edfa99f4..46830d33f 100644 --- a/lib/regex_helper.rb +++ b/lib/regex_helper.rb @@ -4,7 +4,9 @@ module RegexHelper # the following regext needs atleast one character which should be # valid unicode letter, unicode number, underscore, hyphen # shouldn't start with a underscore or hyphen - UNICODE_CHARACTER_NUMBER_HYPHEN_UNDERSCORE = Regexp.new('\A[\p{L}\p{N}]+[\p{L}\p{N}_-]+\Z') + # \z (not \Z) anchors strictly to the end of the string -- \Z would also accept a single + # trailing "\n", which would let a title like "hello_world\n" pass this validation. + UNICODE_CHARACTER_NUMBER_HYPHEN_UNDERSCORE = Regexp.new('\A[\p{L}\p{N}]+[\p{L}\p{N}_-]+\z') # Regex to match mention markdown links and extract display names # Matches: [@display name](mention://user|team/id/url_encoded_name) # Captures: 1) @display name (including emojis), 2) url_encoded_name diff --git a/spec/models/label_spec.rb b/spec/models/label_spec.rb index ee5e7b108..ab5a00d1a 100644 --- a/spec/models/label_spec.rb +++ b/spec/models/label_spec.rb @@ -26,6 +26,13 @@ RSpec.describe Label do expect(label.valid?).to be true end + it 'would not let you use a title with a trailing newline' do + # Regression test: the format validator used to anchor on \Z instead of \z, and \Z tolerates + # a single trailing newline, so 'hello_world' + "\n" incorrectly passed validation. + label = FactoryBot.build(:label, title: "hello_world\n") + expect(label.valid?).to be false + end + it 'converts uppercase letters to lowercase' do label = FactoryBot.build(:label, title: 'Hello_World') expect(label.valid?).to be true