Temporary Nuclear option on topic titles - disallow non ascii characters so we

can stay on top of the trolls. This is meant to be replaced soon with a more
permissive and safe method of sanitizing.
This commit is contained in:
Robin Ward 2013-02-06 15:47:36 -05:00
parent f1a3e76d2b
commit 2d2b907d2c
3 changed files with 31 additions and 0 deletions

View File

@ -24,6 +24,7 @@ class Topic < ActiveRecord::Base
serialize :meta_data, ActiveRecord::Coders::Hstore
validate :unique_title
validate :nuclear_option
belongs_to :category
has_many :posts
@ -112,6 +113,21 @@ class Topic < ActiveRecord::Base
errors.add(:title, I18n.t(:has_already_been_used)) if finder.exists?
end
# This is bad, but people are screwing us on try.discourse.org - soon we'll replace with
# a much more sane validation of odd characters to allow for other languages and such.
def nuclear_option
# Let presence validation catch it if it's blank
return if title.blank?
title.each_char do |c|
unless (20..126).include?(c.ord)
errors.add(:title, I18n.t(:invalid_characters))
return
end
end
end
def new_version_required?
return true if title_changed?

View File

@ -8,6 +8,7 @@ en:
too_many_links: "has too many links"
just_posted_that: "is too similar to what you recently posted"
has_already_been_used: "has already been used"
invalid_characters: "contains invalid characters"
activerecord:
attributes:

View File

@ -1,3 +1,5 @@
# encoding: UTF-8
require 'spec_helper'
describe Topic do
@ -24,6 +26,18 @@ describe Topic do
it { should rate_limit }
context 'topic title content' do
it "allows a regular title with a few ascii characters" do
Fabricate.build(:topic, title: "hello this is my cool topic! welcome: all;").should be_valid
end
it "doesn't allow non standard ascii" do
Fabricate.build(:topic, title: "Iñtërnâtiônàlizætiøn").should_not be_valid
end
end
context 'topic title uniqueness' do