class Sqreen::Rules::BindingAccessorMatcherCB

Callback that match on a list or matcher+binding accessor

Constants

MAX_LENGTH

Attributes

rules[R]

Public Class Methods

new(klass, method, rule_hash) click to toggle source
Calls superclass method
# File lib/sqreen/rules/binding_accessor_matcher_cb.rb, line 26
def initialize(klass, method, rule_hash)
  super(klass, method, rule_hash)
  @rules = []
  if @data.empty? || @data['values'].nil? || @data['values'].empty?
    msg = "no rules in data (had #{@data.keys})"
    raise Sqreen::Exception, msg
  end
  prepare_rules(@data['values'])
end

Public Instance Methods

pre(inst, args, budget = nil, &_block) click to toggle source
# File lib/sqreen/rules/binding_accessor_matcher_cb.rb, line 53
def pre(inst, args, budget = nil, &_block)
  finish = budget + Sqreen.time unless budget.nil?
  resol_cache = Hash.new do |hash, accessor|
    hash[accessor] = accessor.resolve(binding, framework, inst, args)
  end
  @rules.each do |id, accessors, matcher, matcher_ref|
    accessors.each do |accessor|
      val = resol_cache[accessor]
      val = [val] if val.is_a?(String)
      next unless val.respond_to?(:each)
      next if val.respond_to?(:seek)
      val.each do |v|
        return nil if !budget.nil? && Sqreen.time > finish
        next if !v.is_a?(String) || (!matcher.min_size.nil? && v.size < matcher.min_size)
        next if v.size > MAX_LENGTH
        next if matcher.match(v).nil?
        infos = {
          'id' => id,
          'binding_accessor' => accessor.expression,
          'matcher' => matcher_ref,
          'found' => v,
        }
        record_event(infos)
        return advise_action(:raise, :infos => infos)
      end
    end
  end
  nil
end
prepare_rules(rules) click to toggle source
# File lib/sqreen/rules/binding_accessor_matcher_cb.rb, line 36
def prepare_rules(rules)
  accessors = Hash.new do |hash, key|
    hash[key] = BindingAccessor.new(key, true)
  end
  @rules = rules.map do |r|
    if r['binding_accessor'].empty?
      raise Sqreen::Exception, "no accessors #{r['id']}"
    end
    [
      r['id'],
      r['binding_accessor'].map { |expression| accessors[expression] },
      MatcherElem.new(r['matcher']),
      r['matcher']['value'],
    ]
  end
end