Sha256: d29506c7a0037df3e2671c02ea6d6e5e35f08fd64d118629a2f64d74a43d15f2

Contents?: true

Size: 1.48 KB

Versions: 27

Compression:

Stored size: 1.48 KB

Contents

require 'checks/base_check'

#Reports any calls to +validates_format_of+ which do not use +\A+ and +\z+
#as anchors in the given regular expression.
#
#For example:
#
# #Allows anything after new line
# validates_format_of :user_name, :with => /^\w+$/
class CheckValidationRegex < BaseCheck
  Checks.add self

  WITH = Sexp.new(:lit, :with)

  def run_check
    tracker.models.each do |name, model|
      @current_model = name
      format_validations = model[:options][:validates_format_of]
      if format_validations
        format_validations.each do |v|
          process_validator v
        end
      end
    end
  end

  #Check validates_format_of
  def process_validator validator
    hash_iterate(validator[-1]) do |key, value|
      if key == WITH
        check_regex value, validator
      end
    end
  end

  #Issue warning if the regular expression does not use
  #+\A+ and +\z+
  def check_regex value, validator
    return unless regexp? value

    regex = value[1].inspect
    if regex =~ /[^\A].*[^\z]\/(m|i|x|n|e|u|s|o)*\z/
      warn :model => @current_model,
        :warning_type => "Format Validation", 
        :message => "Insufficient validation for '#{get_name validator}' using #{value[1].inspect}. Use \\A and \\z as anchors",
        :line => value.line,
        :confidence => CONFIDENCE[:high] 
    end
  end

  #Get the name of the attribute being validated.
  def get_name validator
    name = validator[1]
    if sexp? name
      name[1]
    else
      name
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
brakeman-0.6.0 lib/checks/check_validation_regex.rb
brakeman-min-0.5.2 lib/checks/check_validation_regex.rb
brakeman-0.5.2 lib/checks/check_validation_regex.rb
brakeman-min-0.5.1 lib/checks/check_validation_regex.rb
brakeman-0.5.1 lib/checks/check_validation_regex.rb
brakeman-min-0.5.0 lib/checks/check_validation_regex.rb
brakeman-0.5.0 lib/checks/check_validation_regex.rb
brakeman-0.4.1 lib/checks/check_validation_regex.rb
brakeman-min-0.4.1 lib/checks/check_validation_regex.rb
brakeman-min-0.4.0 lib/checks/check_validation_regex.rb
brakeman-0.4.0 lib/checks/check_validation_regex.rb
brakeman-min-0.3.2 lib/checks/check_validation_regex.rb
brakeman-0.3.2 lib/checks/check_validation_regex.rb
brakeman-min-0.3.1 lib/checks/check_validation_regex.rb
brakeman-0.3.1 lib/checks/check_validation_regex.rb
brakeman-min-0.3.0 lib/checks/check_validation_regex.rb
brakeman-0.3.0 lib/checks/check_validation_regex.rb
brakeman-min-0.2.2 lib/checks/check_validation_regex.rb
brakeman-0.2.2 lib/checks/check_validation_regex.rb
brakeman-min-0.2.1 lib/checks/check_validation_regex.rb