From 991896c4eb8e791e30f482dabf5be15800dd646f Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 17 Dec 2020 13:41:19 -0600 Subject: [PATCH] tags: don't allow tags more than 170 chars long. Limit tag length to 170 chars. 170 chars was chosen because it's longer than the longest active tag on Danbooru. Tag length is limited because in some contexts we can't deal with excessively long tags. Tag autocorrect for example uses the levenshtein function in Postgres, which can't handle strings more than 255 chars long. --- app/logical/tag_name_validator.rb | 8 +++++++- test/unit/tag_test.rb | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/logical/tag_name_validator.rb b/app/logical/tag_name_validator.rb index 63690f9ec..029a8c5c0 100644 --- a/app/logical/tag_name_validator.rb +++ b/app/logical/tag_name_validator.rb @@ -1,6 +1,12 @@ class TagNameValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - case Tag.normalize_name(value) + value = Tag.normalize_name(value) + + if value.size > 170 + record.errors.add(attribute, "'#{value}' cannot be more than 255 characters long") + end + + case value when /\A_*\z/ record.errors.add(attribute, "'#{value}' cannot be blank") when /\*/ diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index 43a06af12..0c4bd3dce 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -185,6 +185,7 @@ class TagTest < ActiveSupport::TestCase should_not allow_value("café").for(:name).on(:create) should_not allow_value("東方").for(:name).on(:create) should_not allow_value("FAV:blah").for(:name).on(:create) + should_not allow_value("X"*171).for(:name).on(:create) metatags = PostQueryBuilder::METATAGS + TagCategory.mapping.keys metatags.each do |metatag|