Class: Brakeman::CheckModelAttrAccessible

Inherits:
BaseCheck show all
Defined in:
lib/brakeman/checks/check_model_attr_accessible.rb

Overview

Author: Paul Deardorff (themetric) Checks models to see if important foreign keys or attributes are exposed as attr_accessible when they probably shouldn’t be.

Constant Summary collapse

SUSP_ATTRS =
[
  [:admin, CONFIDENCE[:high]], # Very dangerous unless some Rails authorization used
  [:role, CONFIDENCE[:med]],
  [:banned, CONFIDENCE[:med]],
  [:account_id, CONFIDENCE[:high]],
  [/\S*_id(s?)\z/, CONFIDENCE[:low]] # All other foreign keys have weak/low confidence
]

Constants inherited from BaseCheck

BaseCheck::CONFIDENCE

Constants included from Util

Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::SESSION, Util::SESSION_SEXP

Constants inherited from SexpProcessor

SexpProcessor::VERSION

Instance Attribute Summary

Attributes inherited from BaseCheck

#tracker, #warnings

Attributes inherited from SexpProcessor

#context, #env, #expected

Instance Method Summary collapse

Methods inherited from BaseCheck

#add_result, inherited, #initialize, #process_call, #process_cookies, #process_default, #process_dstr, #process_if, #process_params

Methods included from Util

#array?, #block?, #call?, #camelize, #class_name, #contains_class?, #context_for, #cookies?, #false?, #file_by_name, #file_for, #github_url, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #regexp?, #relative_path, #request_env?, #request_value?, #result?, #set_env_defaults, #sexp?, #string?, #string_interp?, #symbol?, #table_to_csv, #template_path_to_name, #true?, #truncate_table, #underscore

Methods included from ProcessorHelper

#process_all, #process_all!, #process_call_args, #process_call_defn?, #process_class, #process_module

Methods inherited from SexpProcessor

#in_context, #initialize, #process, processors, #scope

Constructor Details

This class inherits a constructor from Brakeman::BaseCheck

Instance Method Details

#check_modelsObject



48
49
50
51
52
53
54
# File 'lib/brakeman/checks/check_model_attr_accessible.rb', line 48

def check_models
  tracker.models.each do |name, model|
    if !model.attr_accessible.nil?
      yield name, model
    end
  end
end

#role_limited?(model, attribute) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/brakeman/checks/check_model_attr_accessible.rb', line 42

def role_limited? model, attribute
  role_accessible = model.role_accessible
  return if role_accessible.nil?
  role_accessible.include? attribute
end

#run_checkObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/brakeman/checks/check_model_attr_accessible.rb', line 21

def run_check
  check_models do |name, model|
    model.attr_accessible.each do |attribute|
      next if role_limited? model, attribute

      SUSP_ATTRS.each do |susp_attr, confidence|
        if susp_attr.is_a?(Regexp) and susp_attr =~ attribute.to_s or susp_attr == attribute
          warn :model => name,
            :file => model.file,
            :warning_type => "Mass Assignment",
            :warning_code => :dangerous_attr_accessible,
            :message => "Potentially dangerous attribute available for mass assignment",
            :confidence => confidence,
            :code => Sexp.new(:lit, attribute)
          break # Prevent from matching single attr multiple times
        end
      end
    end
  end
end