From 688faaee41448c11504adb3e94ab4d63413b7ffa Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Tue, 11 May 2010 12:03:49 -0300 Subject: [PATCH] Fields verification filters malformed html correctly --- app/models/article.rb | 2 +- app/models/comment.rb | 2 +- app/models/community.rb | 4 ++-- app/models/consumption.rb | 2 +- app/models/environment.rb | 2 +- app/models/event.rb | 3 ++- app/models/folder.rb | 2 +- app/models/organization.rb | 2 +- app/models/product.rb | 2 +- app/models/profile.rb | 4 ++-- app/models/text_article.rb | 2 +- app/models/tiny_mce_article.rb | 2 +- app/models/validation_info.rb | 2 +- test/unit/article_test.rb | 16 ++++++++++++++++ test/unit/comment_test.rb | 22 ++++++++++++++++++++++ test/unit/community_test.rb | 17 ++++++++++++++++- test/unit/consumption_test.rb | 10 +++++++--- test/unit/environment_test.rb | 16 ++++++++++++++++ test/unit/event_test.rb | 28 ++++++++++++++++++++++++++++ test/unit/folder_test.rb | 28 +++++++++++++++++++--------- test/unit/organization_test.rb | 20 ++++++++++++++++++++ test/unit/product_test.rb | 18 ++++++++++++++++++ test/unit/profile_test.rb | 38 ++++++++++++++++++++++++++++++++++++++ test/unit/text_article_test.rb | 13 +++++++++++++ test/unit/validation_info_test.rb | 10 ++++++++++ vendor/plugins/xss_terminate/lib/xss_terminate.rb | 39 ++++++++++++++++++++++++++++++++++----- 26 files changed, 273 insertions(+), 33 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index 6c65a7b..489c462 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -26,7 +26,7 @@ class Article < ActiveRecord::Base article.published_at = article.created_at if article.published_at.nil? end - xss_terminate :only => [ :name ] + xss_terminate :only => [ :name ], :on => 'validation' named_scope :in_category, lambda { |category| {:include => 'categories', :conditions => { 'categories.id' => category.id }} diff --git a/app/models/comment.rb b/app/models/comment.rb index 2ef2928..c07e003 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -17,7 +17,7 @@ class Comment < ActiveRecord::Base end end - xss_terminate :only => [ :body, :title, :name ] + xss_terminate :only => [ :body, :title, :name ], :on => 'validation' def author_name if author diff --git a/app/models/community.rb b/app/models/community.rb index 5fca3ad..9a1e98b 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -5,8 +5,6 @@ class Community < Organization settings_items :language settings_items :zip_code, :city, :state, :country - xss_terminate :only => [ :name, :address, :contact_phone, :description ] - before_create do |community| community.moderated_articles = true if community.environment.enabled?('organizations_are_moderated_by_default') end @@ -22,6 +20,8 @@ class Community < Organization community end + xss_terminate :only => [ :name, :address, :contact_phone, :description ], :on => 'validation' + FIELDS = %w[ city state diff --git a/app/models/consumption.rb b/app/models/consumption.rb index f2d0778..86a1e70 100644 --- a/app/models/consumption.rb +++ b/app/models/consumption.rb @@ -4,6 +4,6 @@ class Consumption < ActiveRecord::Base validates_uniqueness_of :product_category_id, :scope => :profile_id - xss_terminate :only => [ :aditional_specifications ] + xss_terminate :only => [ :aditional_specifications ], :on => 'validation' end diff --git a/app/models/environment.rb b/app/models/environment.rb index 0d0f345..2fedac7 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -471,7 +471,7 @@ class Environment < ActiveRecord::Base validates_format_of :contact_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |record| ! record.contact_email.blank? }) - xss_terminate :only => [ :message_for_disabled_enterprise ], :with => 'white_list' + xss_terminate :only => [ :message_for_disabled_enterprise ], :with => 'white_list', :on => 'validation' # ################################################# diff --git a/app/models/event.rb b/app/models/event.rb index c304881..f33fdc1 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -6,7 +6,8 @@ class Event < Article settings_items :link, :type => :string settings_items :address, :type => :string - xss_terminate :only => [ :description, :link, :address ], :with => 'white_list' + xss_terminate :only => [ :link ], :on => 'validation' + xss_terminate :only => [ :description, :link, :address ], :with => 'white_list', :on => 'validation' validates_presence_of :title, :start_date diff --git a/app/models/folder.rb b/app/models/folder.rb index 91656bd..06da77f 100644 --- a/app/models/folder.rb +++ b/app/models/folder.rb @@ -4,7 +4,7 @@ class Folder < Article settings_items :view_as, :type => :string, :default => 'folder' - xss_terminate :only => [ :body ], :with => 'white_list' + xss_terminate :only => [ :body ], :with => 'white_list', :on => 'validation' def self.select_views [[_('Folder'), 'folder'], [_('Image gallery'), 'image_gallery']] diff --git a/app/models/organization.rb b/app/models/organization.rb index 60dd027..b3ab0cb 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -77,7 +77,7 @@ class Organization < Profile validates_format_of :contact_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |org| !org.contact_email.blank? }) - xss_terminate :only => [ :acronym, :contact_person, :contact_email, :legal_form, :economic_activity, :management_information ] + xss_terminate :only => [ :acronym, :contact_person, :contact_email, :legal_form, :economic_activity, :management_information ], :on => 'validation' # Yes, organizations have members. # diff --git a/app/models/product.rb b/app/models/product.rb index 84529f2..3b4e218 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -31,7 +31,7 @@ class Product < ActiveRecord::Base acts_as_searchable :fields => [ :name, :description, :category_full_name ] - xss_terminate :only => [ :name, :description ] + xss_terminate :only => [ :name, :description ], :on => 'validation' acts_as_mappable diff --git a/app/models/profile.rb b/app/models/profile.rb index 1c37053..769d11d 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -294,8 +294,8 @@ class Profile < ActiveRecord::Base self.save_without_validation! end - xss_terminate :only => [ :name, :nickname, :address, :contact_phone, :description ] - xss_terminate :only => [ :custom_footer, :custom_header ], :with => 'white_list' + xss_terminate :only => [ :name, :nickname, :address, :contact_phone, :description ], :on => 'validation' + xss_terminate :only => [ :custom_footer, :custom_header ], :with => 'white_list', :on => 'validation' # returns the contact email for this profile. # diff --git a/app/models/text_article.rb b/app/models/text_article.rb index c919b5e..97050ad 100644 --- a/app/models/text_article.rb +++ b/app/models/text_article.rb @@ -1,5 +1,5 @@ # a base class for all text article types. class TextArticle < Article - xss_terminate :only => [ :name, :abstract, :body ] + xss_terminate :only => [ :name, :abstract, :body ], :on => 'validation' end diff --git a/app/models/tiny_mce_article.rb b/app/models/tiny_mce_article.rb index 355b28d..489661f 100644 --- a/app/models/tiny_mce_article.rb +++ b/app/models/tiny_mce_article.rb @@ -9,6 +9,6 @@ class TinyMceArticle < TextArticle end xss_terminate :except => [ :abstract, :body ] - xss_terminate :only => [ :abstract, :body ], :with => 'white_list' + xss_terminate :only => [ :abstract, :body ], :with => 'white_list', :on => 'validation' end diff --git a/app/models/validation_info.rb b/app/models/validation_info.rb index 890cd69..d78ccbd 100644 --- a/app/models/validation_info.rb +++ b/app/models/validation_info.rb @@ -3,5 +3,5 @@ class ValidationInfo < ActiveRecord::Base belongs_to :organization - xss_terminate :only => [ :validation_methodology, :restrictions ] + xss_terminate :only => [ :validation_methodology, :restrictions ], :on => 'validation' end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 0f6859c..346b291 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -859,4 +859,20 @@ class ArticleTest < Test::Unit::TestCase assert_no_match /" + article.valid? + + assert article.errors.invalid?(:name) + end + + should 'escape malformed html tags' do + article = Article.new + article.name = "

> html >< tag" + article.valid? + + assert_no_match /[<>]/, article.name + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index ac2b238..43708a5 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -187,4 +187,26 @@ class CommentTest < Test::Unit::TestCase assert_no_match(/'}) - folder.save! - assert_no_match(/" + folder.valid? + + assert_no_match /(