wikis: force wiki names to follow same rules as tag names.

Don't allow wiki pages to have invalid names.

This incidentally means that you can't create wiki pages for pools. For
example, you can't create a wiki titled "pool:almost_heart-warming".
This is not a valid tag name, so it's not a valid wiki name either. This
was done in a handful of cases to translate Pixiv tags to Danbooru pools
(see: <https://danbooru.donmai.us/wiki_page_versions?search[title_like]=pool:*>)

Also fix it so that titles are normalized before validation, not before save.
This commit is contained in:
evazion
2020-12-19 17:51:11 -06:00
parent 7708e2e08f
commit a129eb4251
3 changed files with 28 additions and 14 deletions

View File

@@ -3,14 +3,13 @@ class WikiPage < ApplicationRecord
META_WIKIS = ["list_of_", "tag_group:", "pool_group:", "howto:", "about:", "help:", "template:"] META_WIKIS = ["list_of_", "tag_group:", "pool_group:", "howto:", "about:", "help:", "template:"]
before_save :normalize_title before_validation :normalize_title
before_save :normalize_other_names before_validation :normalize_other_names
before_save :update_dtext_links, if: :dtext_links_changed? before_save :update_dtext_links, if: :dtext_links_changed?
after_save :create_version after_save :create_version
validates_uniqueness_of :title, :case_sensitive => false validates :title, tag_name: true, presence: true, uniqueness: true, if: :title_changed?
validates_presence_of :title validates :body, presence: true, unless: -> { is_deleted? || other_names.present? }
validates_presence_of :body, :unless => -> { is_deleted? || other_names.present? }
validate :validate_rename validate :validate_rename
validate :validate_other_names validate :validate_other_names
@@ -149,8 +148,7 @@ class WikiPage < ApplicationRecord
end end
def self.normalize_title(title) def self.normalize_title(title)
return if title.blank? title.to_s.downcase.delete_prefix("~").gsub(/[[:space:]]+/, "_").gsub(/__/, "_").gsub(/\A_|_\z/, "")
title.downcase.delete_prefix("~").gsub(/[[:space:]]+/, "_").gsub(/__/, "_").gsub(/\A_|_\z/, "")
end end
def normalize_title def normalize_title

View File

@@ -109,13 +109,6 @@ class WikiPagesControllerTest < ActionDispatch::IntegrationTest
assert_response 404 assert_response 404
end end
should "render for a negated tag" do
as(@user) { @wiki_page.update(title: "-aaa") }
get wiki_page_path(@wiki_page.id)
assert_redirected_to wiki_page_path(@wiki_page.title)
end
should "work for a title containing dots" do should "work for a title containing dots" do
as(@user) { create(:wiki_page, title: "...") } as(@user) { create(:wiki_page, title: "...") }

View File

@@ -78,6 +78,29 @@ class WikiPageTest < ActiveSupport::TestCase
end end
end end
context "during title validation" do
# these values are allowed because they're normalized first
should allow_value(" foo ").for(:title).on(:create)
should allow_value("~foo").for(:title).on(:create)
should allow_value("_foo").for(:title).on(:create)
should allow_value("foo_").for(:title).on(:create)
should allow_value("foo__bar").for(:title).on(:create)
should allow_value("FOO").for(:title).on(:create)
should allow_value("foo bar").for(:title).on(:create)
should_not allow_value("").for(:title).on(:create)
should_not allow_value("___").for(:title).on(:create)
should_not allow_value("-foo").for(:title).on(:create)
should_not allow_value("/foo").for(:title).on(:create)
should_not allow_value("foo*bar").for(:title).on(:create)
should_not allow_value("foo,bar").for(:title).on(:create)
should_not allow_value("foo\abar").for(:title).on(:create)
should_not allow_value("café").for(:title).on(:create)
should_not allow_value("東方").for(:title).on(:create)
should_not allow_value("FAV:blah").for(:title).on(:create)
should_not allow_value("X"*171).for(:title).on(:create)
end
context "with other names" do context "with other names" do
should "not allow artist wikis to have other names" do should "not allow artist wikis to have other names" do
tag = create(:artist_tag) tag = create(:artist_tag)