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

Methods included from Util

#array?, #block?, #call?, #camelize, #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, #regexp?, #relative_path, #request_env?, #request_value?, #result?, #set_env_defaults, #sexp?, #string?, #symbol?, #table_to_csv, #template_path_to_name, #true?, #truncate_table, #underscore

Methods included from ProcessorHelper

#class_name, #process_all, #process_all!, #process_call_args, #process_class, #process_module

Methods inherited from SexpProcessor

#error_handler, #in_context, #initialize, #process, #process_dummy, #scope

Constructor Details

This class inherits a constructor from Brakeman::BaseCheck

Instance Method Details

#all_literal_args?(exp) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 125

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 103

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



45
46
47
48
49
50
51
52
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 45

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



153
154
155
156
157
158
159
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 153

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

#find_mass_assign_callsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 17

def find_mass_assign_calls
  return @mass_assign_calls if @mass_assign_calls

  models = []
  tracker.models.each do |name, m|
    if unprotected_model? m
      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)


140
141
142
143
144
145
146
147
148
149
150
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 140

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



55
56
57
58
59
60
61
62
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
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 55

def process_result res
  call = res[:call]

  check = check_call call

  if check and not call.original_line and not duplicate? res
    add_result res

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

    attr_protected = (model and model[:options][: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
        user_input = input.match
      else
        confidence = CONFIDENCE[:low]
        user_input = input.match
      end
    else
      confidence = CONFIDENCE[:low]
      user_input = nil
    end

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

  res
end

#run_checkObject



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

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)


164
165
166
167
168
169
170
171
172
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 164

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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/brakeman/checks/check_mass_assignment.rb', line 174

def warn_on_permit! result
  return if duplicate? result or result[:call].original_line
  add_result 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