Files
danbooru/test/unit/wiki_page_test.rb
evazion d9dc84325f Fix #5365: Don't allow whitespace-only text submission.
Fix bug where it was possible to submit blank text in various text fields.

Caused by `String#blank?` not considering certain Unicode characters as blank. `blank?` is defined
as `match?(/\A[[:space:]]*\z/)`, where `[[:space:]]` matches ASCII spaces (space, tab, newline, etc)
and Unicode characters in the Space category ([1]). However, there are other space-like characters
not in the Space category. This includes U+200B (Zero-Width Space), and many more.

It turns out the "Default ignorable code points" [2][3] are what we're after. These are the set of 400
or so formatting and control characters that are invisible when displayed.

Note that there are other control characters that aren't invisible when rendered, instead they're
shown with a placeholder glyph. These include the ASCII C0 and C1 control codes [4], certain Unicode
control characters [5], and unassigned, reserved, and private use codepoints.

There is one outlier: the Braille pattern blank (U+2800) [6]. This character is visually blank, but is
not considered to be a space or an ignorable code point.

[1]: https://codepoints.net/search?gc[]=Z
[2]: https://codepoints.net/search?DI=1
[3]: https://www.unicode.org/review/pr-5.html
[4]: https://codepoints.net/search?gc[]=Cc
[5]: https://codepoints.net/search?gc[]=Cf
[6]: https://codepoints.net/U+2800
[7]: https://en.wikipedia.org/wiki/Whitespace_character
[8]: https://character.construction/blanks
[9]: https://invisible-characters.com
2022-12-05 01:58:34 -06:00

148 lines
6.2 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
require 'test_helper'
class WikiPageTest < ActiveSupport::TestCase
teardown do
CurrentUser.user = nil
end
context "A wiki page" do
context "updated by a regular user" do
setup do
@user = FactoryBot.create(:user)
CurrentUser.user = @user
@wiki_page = FactoryBot.create(:wiki_page, :title => "HOT POTATO", :other_names => "foo*bar baz")
end
should "search by title" do
matches = WikiPage.titled("hot potato")
assert_equal(1, matches.count)
assert_equal("hot_potato", matches.first.title)
end
should "search other names with wildcards" do
assert_search_equals(@wiki_page, other_names_match: "fo*")
end
should "create versions" do
assert_difference("WikiPageVersion.count") do
@wiki_page = FactoryBot.create(:wiki_page, :title => "xxx")
end
assert_difference("WikiPageVersion.count") do
@wiki_page.title = "yyy"
travel(1.day) do
@wiki_page.save
end
end
end
should "revert to a prior version" do
@wiki_page.title = "yyy"
travel(1.day) do
@wiki_page.save
end
version = WikiPageVersion.first
@wiki_page.revert_to!(version)
@wiki_page.reload
assert_equal("hot_potato", @wiki_page.title)
end
should "update its dtext links" do
@wiki_page.update!(body: "[[long hair]]")
assert_equal(1, @wiki_page.dtext_links.size)
assert_equal("wiki_link", @wiki_page.dtext_links.first.link_type)
assert_equal("long_hair", @wiki_page.dtext_links.first.link_target)
@wiki_page.update!(body: "http://www.google.com")
assert_equal(1, @wiki_page.dtext_links.size)
assert_equal("external_link", @wiki_page.dtext_links.first.link_type)
assert_equal("http://www.google.com", @wiki_page.dtext_links.first.link_target)
@wiki_page.update!(body: "nothing")
assert_equal(0, @wiki_page.dtext_links.size)
end
end
context "the wiki body" do
should "be normalized to NFC" do
# \u00E9: é; \u0301: acute accent
@wiki = create(:wiki_page, body: "Poke\u0301mon")
assert_equal("Pok\u00E9mon", @wiki.body)
end
should "normalize line endings and trim spaces" do
@wiki = create(:wiki_page, body: " foo\nbar\n")
assert_equal("foo\r\nbar", @wiki.body)
end
end
context "the #normalize_other_names method" do
subject { build(:wiki_page) }
should normalize_attribute(:other_names).from([" foo"]).to(["foo"])
should normalize_attribute(:other_names).from(["foo "]).to(["foo"])
should normalize_attribute(:other_names).from(["___foo"]).to(["foo"])
should normalize_attribute(:other_names).from(["foo___"]).to(["foo"])
should normalize_attribute(:other_names).from(["foo\n"]).to(["foo"])
should normalize_attribute(:other_names).from(["foo bar"]).to(["foo_bar"])
should normalize_attribute(:other_names).from(["foo bar"]).to(["foo_bar"])
should normalize_attribute(:other_names).from(["foo___bar"]).to(["foo_bar"])
should normalize_attribute(:other_names).from([" _Foo Bar_ "]).to(["Foo_Bar"])
should normalize_attribute(:other_names).from(["foo 1", "bar 2"]).to(["foo_1", "bar_2"])
should normalize_attribute(:other_names).from(["foo", nil, "", " ", "bar"]).to(["foo", "bar"])
should normalize_attribute(:other_names).from([nil, "", " "]).to([])
should normalize_attribute(:other_names).from(["pokémon".unicode_normalize(:nfd)]).to(["pokémon".unicode_normalize(:nfkc)])
should normalize_attribute(:other_names).from([""]).to(["ABC"])
should normalize_attribute(:other_names).from(["foo", "foo"]).to(["foo"])
should normalize_attribute(:other_names).from(%w[foo*bar baz baz 加賀(艦これ)]).to(%w[foo*bar baz 加賀(艦これ)])
should normalize_attribute(:other_names).from("foo foo").to(["foo"])
should normalize_attribute(:other_names).from("foo bar").to(["foo", "bar"])
should normalize_attribute(:other_names).from("_foo_ Bar").to(["foo", "Bar"])
end
context "during title validation" do
should normalize_attribute(:title).from(" foo ").to("foo")
should normalize_attribute(:title).from("~foo").to("foo")
should normalize_attribute(:title).from("_foo").to("foo")
should normalize_attribute(:title).from("foo_").to("foo")
should normalize_attribute(:title).from("FOO").to("foo")
should normalize_attribute(:title).from("foo__bar").to("foo_bar")
should normalize_attribute(:title).from("foo___bar").to("foo_bar")
should normalize_attribute(:title).from("___foo___bar___").to("foo_bar")
should normalize_attribute(:title).from("foo bar").to("foo_bar")
should normalize_attribute(:title).from(" Foo___ Bar ").to("foo_bar")
should_not allow_value("").for(:title).on(:create)
should_not allow_value(" ").for(:title).on(:create)
should_not allow_value("\u200B").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 "during body validation" do
should_not allow_value("").for(:body)
should_not allow_value(" ").for(:body)
should_not allow_value("\u200B").for(:body)
end
context "with other names" do
should "not allow artist wikis to have other names" do
tag = create(:artist_tag)
wiki = build(:wiki_page, title: tag.name, other_names: ["blah"])
assert_equal(false, wiki.valid?)
assert_equal(["An artist wiki can't have other names"], wiki.errors[:base])
end
end
end
end