<commit_before>require 'rails_sanitize'

module XssTerminate
  def self.included(base)
    base.extend(ClassMethods)
    # sets up default of stripping tags for all fields
    base.send(:xss_terminate)
    base.class_inheritable_reader :xss_terminate_options
    base.send :include, InstanceMethods
  end

  module ClassMethods
    def xss_terminate(options = {})
      write_inheritable_attribute(:xss_terminate_options, {
        :except => (options[:except] || []),
        :html5lib_sanitize => (options[:html5lib_sanitize] || []),
        :html5lib_options => (options[:html5lib_options] || {})
      })
    end
  end
  
  module InstanceMethods

    def sanitize_fields
      # fix a bug with Rails internal AR::Base models that get loaded before
      # the plugin, like CGI::Sessions::ActiveRecordStore::Session
      return if xss_terminate_options.nil?
      
      self.class.columns.each do |column|
        next unless (column.type == :string || column.type == :text)
        
        field = column.name.to_sym
        value = self[field]

        next if value.nil? || !value.is_a?(String)
        
        if xss_terminate_options[:except].include?(field)
          next
        elsif xss_terminate_options[:html5lib_sanitize].include?(field)
          if  xss_terminate_options[:html5lib_options].empty?
            self[field] = RailsSanitize.white_list_sanitizer.sanitize(value)
          elsif xss_terminate_options[:html5lib_options].include?(:whitelist_attrs)<commit_msg>Removing deprecated code to work with Rails 3.2<commit_after>require 'rails_sanitize'

module XssTerminate
  def self.included(base)
    base.extend(ClassMethods)
    # sets up default of stripping tags for all fields
    base.send(:xss_terminate)
    base.send :include, InstanceMethods
  end

  module ClassMethods
    def xss_terminate(options = {})
      class_attribute :xss_terminate_options
      self.xss_terminate_options = {
        :except => (options[:except] || []),
        :html5lib_sanitize => (options[:html5lib_sanitize] || []),
        :html5lib_options => (options[:html5lib_options] || {})
      }
    end
  end
  
  module InstanceMethods

    def sanitize_fields
      # fix a bug with Rails internal AR::Base models that get loaded before
      # the plugin, like CGI::Sessions::ActiveRecordStore::Session
      return if xss_terminate_options.nil?
      
      self.class.columns.each do |column|
        next unless (column.type == :string || column.type == :text)
        
        field = column.name.to_sym
        value = self[field]

        next if value.nil? || !value.is_a?(String)
        
        if xss_terminate_options[:except].include?(field)
          next
        elsif xss_terminate_options[:html5lib_sanitize].include?(field)
          if  xss_terminate_options[:html5lib_options].empty?
            self[field] = RailsSanitize.white_list_sanitizer.sanitize(value)
          elsif xss_terminate_options[:html5lib_options].include?(:whitelist_attrs)
{'fraction_non_alphanumeric': 0.09919571045576407, 'fraction_numerical': 0.005361930294906166, 'mean_word_length': 3.134349030470914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '878272', 'n_tokens_mistral': 914, 'n_tokens_neox': 919, 'n_words': 180}