tag.rb: validate tag names.

Makes these tag names invalid when tagging posts:

* Blank names (e.g. ___).
* Non-ASCII characters (Japanese text).
* Non-printable characters (e.g. control characters < 0x20).
* Leading or trailing underscores, or consecutive underscores. Reason:
  `_foo__bar_` and `foo_bar` both render as `foo bar` in the tag list.
* Leading `-` or `~`, or '*' (-foo, ~foo, foo*bar). Previously
  these were silently stripped, but that meant tagging a post with e.g.
  `*-foo` tagged the post with the invalid tag `-foo`.
* Tag type prefixes (e.g. `character:character:hatsune_miku` no longer creates
  the literal tag `character:hatsune_miku`).
* Metatags (order:score, user:evazion, etc).
This commit is contained in:
evazion
2017-02-07 23:03:35 -06:00
parent f0a8598ba2
commit 853ddc7cda
2 changed files with 33 additions and 1 deletions

View File

@@ -6,6 +6,8 @@ class Tag < ActiveRecord::Base
attr_accessible :is_locked, :as => [:moderator, :admin]
has_one :wiki_page, :foreign_key => "title", :primary_key => "name"
validates :name, uniqueness: true, tag_name: true, on: :create
module ApiMethods
def to_legacy_json
return {
@@ -179,7 +181,7 @@ class Tag < ActiveRecord::Base
module NameMethods
def normalize_name(name)
name.mb_chars.downcase.tr(" ", "_").gsub(/\A[-~]+/, "").gsub(/\*/, "").to_s
name.to_s.mb_chars.downcase.strip.tr(" ", "_").to_s
end
def find_or_create_by_name(name, options = {})