Class: Brakeman::CheckMassAssignment

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

Overview

Checks for mass assignments to models.

See guides.rubyonrails.org/security.html#mass-assignment for details

Constant Summary collapse

LITERALS =

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, #process_call, #process_cookies, #process_default, #process_dstr, #process_if, #process_params

Methods included from Util

#array?, #block?, #call?, #camelize, #class_name, #constant?, #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, #process, processors, #scope

Constructor Details

#initializeCheckMassAssignment

Returns a new instance of CheckMassAssignment.



12
13
14
15
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 12

def initialize(*)
  super
  @mass_assign_calls = nil
end

Instance Method Details

#all_literal_args?(exp) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 132

def all_literal_args? exp
  if call? exp
    exp.each_arg do |arg|
      return false unless literal? arg
    end

    true
  else
    exp.all? do |arg|
      literal? arg
    end
  end

end

#check_call(call) ⇒ Object

Want to ignore calls to Model.new that have no arguments



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 110

def check_call call
  process_call_args call

  if call.method == :update
    arg = call.second_arg
  else
    arg = call.first_arg
  end

  if arg.nil? #empty new()
    false
  elsif hash? arg and not include_user_input? arg
    false
  elsif all_literal_args? call
    false
  else
    true
  end
end

#check_mass_assignmentObject



53
54
55
56
57
58
59
60
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 53

def check_mass_assignment
  return if mass_assign_disabled?

  Brakeman.debug "Processing possible mass assignment calls"
  find_mass_assign_calls.each do |result|
    process_result result
  end
end

#check_permit!Object

Look for and warn about uses of Parameters#permit! for mass assignment



160
161
162
163
164
165
166
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 160

def check_permit!
  tracker.find_call(:method => :permit!).each do |result|
    if params? result[:call].target and not result[:chain].include? :slice
      warn_on_permit! result
    end
  end
end

#find_mass_assign_callsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 22

def find_mass_assign_calls
  return @mass_assign_calls if @mass_assign_calls

  models = []
  tracker.models.each do |name, m|
    if m.is_a? Hash
      p m
    end
    if m.unprotected_model?
      models << name
    end
  end

  return [] if models.empty?

  Brakeman.debug "Finding possible mass assignment calls on #{models.length} models"
  @mass_assign_calls = tracker.find_call :chained => true, :targets => models, :methods => [:new,
    :attributes=,
    :update_attributes,
    :update_attributes!,
    :create,
    :create!,
    :build,
    :first_or_create,
    :first_or_create!,
    :first_or_initialize!,
    :assign_attributes,
    :update
  ]
end

#literal?(exp) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
156
157
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 147

def literal? exp
  if sexp? exp
    if exp.node_type == :hash
      all_literal_args? exp
    else
      LITERALS.include? exp.node_type
    end
  else
    true
  end
end

#process_result(res) ⇒ Object

All results should be Model.new(…) or Model.attributes=() calls



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 63

def process_result res
  call = res[:call]

  check = check_call call

  if check and original? res

    model = tracker.models[res[:chain].first]

    attr_protected = (model and model.attr_protected)

    if attr_protected and tracker.options[:ignore_attr_protected]
      return
    elsif input = include_user_input?(call.arglist)
      first_arg = call.first_arg

      if call? first_arg and (first_arg.method == :slice or first_arg.method == :only)
        return
      elsif not node_type? first_arg, :hash
        if attr_protected
          confidence = CONFIDENCE[:med]
        else
          confidence = CONFIDENCE[:high]
        end
      else
        confidence = CONFIDENCE[:low]
      end
    elsif node_type? call.first_arg, :lit, :str
      return
    else
      confidence = CONFIDENCE[:low]
      input = nil
    end

    warn :result => res,
      :warning_type => "Mass Assignment",
      :warning_code => :mass_assign_call,
      :message => "Unprotected mass assignment",
      :code => call,
      :user_input => input,
      :confidence => confidence
  end

  res
end

#run_checkObject



17
18
19
20
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 17

def run_check
  check_mass_assignment
  check_permit!
end

#subsequent_mass_assignment?(result) ⇒ Boolean

Look for actual use of params in mass assignment to avoid warning about uses of Parameters#permit! without any mass assignment or when mass assignment is restricted by model instead.

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
179
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 171

def subsequent_mass_assignment? result
  location = result[:location]
  line = result[:call].line
  find_mass_assign_calls.any? do |call|
    call[:location] == location and
    params? call[:call].first_arg and
    call[:call].line >= line
  end
end

#warn_on_permit!(result) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 181

def warn_on_permit! result
  return unless original? result

  confidence = if subsequent_mass_assignment? result
                 CONFIDENCE[:high]
               else
                 CONFIDENCE[:med]
               end

  warn :result => result,
    :warning_type => "Mass Assignment",
    :warning_code => :mass_assign_permit!,
    :message => "Parameters should be whitelisted for mass assignment",
    :confidence => confidence
end