From 2d2b907d2c505125462f094e585d489bc2e1f9ce Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 6 Feb 2013 15:47:36 -0500 Subject: [PATCH] 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. --- app/models/topic.rb | 16 ++++++++++++++++ config/locales/en.yml | 1 + spec/models/topic_spec.rb | 14 ++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/app/models/topic.rb b/app/models/topic.rb index 13a93a661ac..327c910d3d0 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -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? diff --git a/config/locales/en.yml b/config/locales/en.yml index 43049288062..a86b81ffcbc 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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: diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index a65d0350d6d..e50030ba002 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -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