Class: Brakeman::AliasProcessor

Inherits:
SexpProcessor show all
Includes:
CallConversionHelper, ProcessorHelper, SafeCallHelper, Util
Defined in:
lib/brakeman/processors/alias_processor.rb

Overview

Returns an s-expression with aliases replaced with their value. This does not preserve semantics (due to side effects, etc.), but it makes processing easier when searching for various things.

Constant Summary collapse

ARRAY_CONST =
s(:const, :Array)
HASH_CONST =
s(:const, :Hash)
RAILS_TEST =
s(:call, s(:call, s(:const, :Rails), :env), :test?)
STRING_NEW =
s(:call, s(:const, :String), :new)

Constants included from CallConversionHelper

CallConversionHelper::STRING_LENGTH_LIMIT

Constants included from Util

Util::ALL_COOKIES, Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_COOKIES, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::REQUEST_REQUEST_PARAMETERS, Util::SAFE_LITERAL, Util::SESSION, Util::SESSION_SEXP

Constants inherited from SexpProcessor

SexpProcessor::VERSION

Instance Attribute Summary collapse

Attributes inherited from SexpProcessor

#context, #env, #expected

Instance Method Summary collapse

Methods included from CallConversionHelper

#all_literals?, #join_arrays, #join_strings, #math_op, #process_array_access, #process_hash_access

Methods included from Util

#array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #cookies?, #false?, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #kwsplat?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #regexp?, #remove_kwsplat, #request_env?, #request_value?, #result?, #safe_literal, #safe_literal?, #safe_literal_target?, #set_env_defaults, #sexp?, #string?, #string_interp?, #symbol?, #template_path_to_name, #true?, #underscore

Methods included from ProcessorHelper

#current_file, #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

#initialize(tracker = nil, current_file = nil) ⇒ AliasProcessor

Returns a new AliasProcessor with an empty environment.

The recommended usage is:

AliasProcessor.new.process_safely src



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/brakeman/processors/alias_processor.rb', line 23

def initialize tracker = nil, current_file = nil
  super()
  @env = SexpProcessor::Environment.new
  @inside_if = false
  @ignore_ifs = nil
  @exp_context = []
  @tracker = tracker #set in subclass as necessary
  @helper_method_cache = {}
  @helper_method_info = Hash.new({})
  @or_depth_limit = (tracker && tracker.options[:branch_limit]) || 5 #arbitrary default
  @meth_env = nil
  @current_file = current_file
  set_env_defaults
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



16
17
18
# File 'lib/brakeman/processors/alias_processor.rb', line 16

def result
  @result
end

#trackerObject (readonly)

Returns the value of attribute tracker.



16
17
18
# File 'lib/brakeman/processors/alias_processor.rb', line 16

def tracker
  @tracker
end

Instance Method Details

#array_detect_all_literals?(exp) ⇒ Boolean

Returns:

  • (Boolean)


737
738
739
740
741
742
# File 'lib/brakeman/processors/alias_processor.rb', line 737

def array_detect_all_literals? exp
  call? exp and
  [:detect, :find].include? exp.method and
  exp.first_arg.nil? and
  all_literals? exp.target
end

#array_include_all_literals?(exp) ⇒ Boolean

Check if exp is a call to Array#include? on an array literal that contains all literal values. For example:

[1, 2, "a"].include? x

Returns:

  • (Boolean)


731
732
733
734
735
# File 'lib/brakeman/processors/alias_processor.rb', line 731

def array_include_all_literals? exp
  call? exp and
  exp.method == :include? and
  all_literals? exp.target
end

#assign_args(method_exp, args, meth_env = SexpProcessor::Environment.new) ⇒ Object



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
# File 'lib/brakeman/processors/alias_processor.rb', line 1071

def assign_args method_exp, args, meth_env = SexpProcessor::Environment.new
  formal_args = method_exp.formal_args

  formal_args.each_with_index do |arg, index|
    next if index == 0

    if arg.is_a? Symbol and sexp? args[index - 1]
      meth_env[Sexp.new(:lvar, arg)] = args[index - 1]
    end
  end

  meth_env
end

#collapse_send_call(exp, first_arg) ⇒ Object

Change x.send(:y, 1) to x.y(1)



934
935
936
937
938
939
940
941
942
943
944
945
946
947
# File 'lib/brakeman/processors/alias_processor.rb', line 934

def collapse_send_call exp, first_arg
  # Handle try(&:id)
  if node_type? first_arg, :block_pass
    first_arg = first_arg[1]
  end

  return unless symbol? first_arg or string? first_arg
  exp.method = first_arg.value.to_sym
  args = exp.args
  exp.pop # remove last arg
  if args.length > 1
    exp.arglist = args[1..-1]
  end
end

#duplicate?(exp) ⇒ Boolean

Returns:

  • (Boolean)


1094
1095
1096
1097
1098
1099
1100
# File 'lib/brakeman/processors/alias_processor.rb', line 1094

def duplicate? exp
  @exp_context[0..-2].reverse_each do |e|
    return true if exp == e
  end

  false
end

#early_return?(exp) ⇒ Boolean

Returns:

  • (Boolean)


814
815
816
817
818
819
820
821
822
823
824
# File 'lib/brakeman/processors/alias_processor.rb', line 814

def early_return? exp
  return true if node_type? exp, :return
  return true if call? exp and [:fail, :raise].include? exp.method

  if node_type? exp, :block, :rlist
    node_type? exp.last, :return or
      (call? exp and [:fail, :raise].include? exp.method)
  else
    false
  end
end

#find_method(*args) ⇒ Object



1102
1103
1104
# File 'lib/brakeman/processors/alias_processor.rb', line 1102

def find_method *args
  nil
end

#find_push_target(exp) ⇒ Object

Finds the inner most call target which is not the target of a call to <<



1086
1087
1088
1089
1090
1091
1092
# File 'lib/brakeman/processors/alias_processor.rb', line 1086

def find_push_target exp
  if call? exp and exp.method == :<<
    find_push_target exp.target
  else
    exp
  end
end

#get_call_value(call) ⇒ Object



986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/brakeman/processors/alias_processor.rb', line 986

def get_call_value call
  method_name = call.method

  #Look for helper methods and see if we can get a return value
  if found_method = find_method(method_name, @current_class)
    helper = found_method[:method]

    if sexp? helper
      value = process_helper_method helper, call.args
      value.line(call.line)
      return value
    else
      raise "Unexpected value for method: #{found_method}"
    end
  else
    call
  end
end

#get_rhs(exp) ⇒ Object

Handles x = y = z = 1



447
448
449
450
451
452
453
# File 'lib/brakeman/processors/alias_processor.rb', line 447

def get_rhs exp
  if node_type? exp, :lasgn, :iasgn, :gasgn, :attrasgn, :safe_attrasgn, :cvdecl, :cdecl
    get_rhs(exp.rhs)
  else
    exp
  end
end

#join_item(item, join_value) ⇒ Object



340
341
342
343
344
345
346
347
348
# File 'lib/brakeman/processors/alias_processor.rb', line 340

def join_item item, join_value
  if item.is_a? String
    "#{item}#{join_value}"
  elsif string? item or symbol? item or number? item
    s(:str, "#{item.value}#{join_value}")
  else
    s(:evstr, item)
  end
end

#merge_if_branch(branch_env) ⇒ Object



905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
# File 'lib/brakeman/processors/alias_processor.rb', line 905

def merge_if_branch branch_env
  branch_env.each do |k, v|
    next if v.nil?

    current_val = env[k]

    if current_val
      unless same_value?(current_val, v)
        if too_deep? current_val
          # Give up branching, start over with latest value
          env[k] = v
        else
          env[k] = current_val.combine(v, k.line)
        end
      end
    else
      env[k] = v
    end
  end
end

#meth_envObject



425
426
427
428
429
430
431
432
433
434
435
# File 'lib/brakeman/processors/alias_processor.rb', line 425

def meth_env
  begin
    env.scope do
      set_env_defaults
      @meth_env = env.current
      yield
    end
  ensure
    @meth_env = nil
  end
end

#new_string?(exp) ⇒ Boolean

String.new ?

Returns:

  • (Boolean)


1207
1208
1209
# File 'lib/brakeman/processors/alias_processor.rb', line 1207

def new_string? exp
  exp == STRING_NEW
end

#only_ivars(include_request_vars = false, lenv = nil) ⇒ Object

Returns a new SexpProcessor::Environment containing only instance variables. This is useful, for example, when processing views.



951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
# File 'lib/brakeman/processors/alias_processor.rb', line 951

def only_ivars include_request_vars = false, lenv = nil
  lenv ||= env
  res = SexpProcessor::Environment.new

  if include_request_vars
    lenv.all.each do |k, v|
      #TODO Why would this have nil values?
      if (k.node_type == :ivar or request_value? k) and not v.nil?
        res[k] = v.dup
      end
    end
  else
    lenv.all.each do |k, v|
      #TODO Why would this have nil values?
      if k.node_type == :ivar and not v.nil?
        res[k] = v.dup
      end
    end
  end

  res
end

#only_request_varsObject



974
975
976
977
978
979
980
981
982
983
984
# File 'lib/brakeman/processors/alias_processor.rb', line 974

def only_request_vars
  res = SexpProcessor::Environment.new

  env.all.each do |k, v|
    if request_value? k and not v.nil?
      res[k] = v.dup
    end
  end

  res
end

#process_array_join(array, join_str) ⇒ Object

Painful conversion of Array#join into string interpolation



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/brakeman/processors/alias_processor.rb', line 290

def process_array_join array, join_str
  result = s()

  join_value = if string? join_str
                 join_str.value
               else
                 nil
               end

  array[1..-2].each do |e|
    result << join_item(e, join_value)
  end

  result << join_item(array.last, nil)

  # Combine the strings at the beginning because that's what RubyParser does
  combined_first = ""
  result.each do |e|
    if string? e
      combined_first << e.value
    elsif e.is_a? String
      combined_first << e
    else
      break
    end
  end

  # Remove the strings at the beginning
  result.reject! do |e|
    if e.is_a? String or string? e
      true
    else
      break
    end
  end

  result.unshift combined_first

  # Have to fix up strings that follow interpolation
  result.reduce(s(:dstr)) do |memo, e|
    if string? e and node_type? memo.last, :evstr
      e.value = "#{join_value}#{e.value}"
    elsif join_value and node_type? memo.last, :evstr and node_type? e, :evstr
      memo << s(:str, join_value)
    end

    memo << e
  end
end

#process_attrasgn(exp) ⇒ Object

‘Attribute’ assignment x.y = 1 or x = 1



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/brakeman/processors/alias_processor.rb', line 526

def process_attrasgn exp
  tar_variable = exp.target
  target = process(exp.target)
  method = exp.method
  index_arg = exp.first_arg
  value_arg = exp.second_arg

  if method == :[]=
    index = exp.first_arg = process(index_arg)
    value = exp.second_arg = process(value_arg)
    match = Sexp.new(:call, target, :[], index)

    set_value match, value

    if hash? target
      env[tar_variable] = hash_insert target.deep_clone, index, value
    end

    unless node_type? target, :hash
      exp.target = target
    end
  elsif method.to_s[-1,1] == "="
    exp.first_arg = process(index_arg)
    value = get_rhs(exp)
    #This is what we'll replace with the value
    match = Sexp.new(:call, target, method.to_s[0..-2].to_sym)

    set_value match, value
    exp.target = target
  else
    raise "Unrecognized assignment: #{exp}"
  end
  exp
end

#process_block(exp) ⇒ Object

Start new scope for block.



411
412
413
414
415
# File 'lib/brakeman/processors/alias_processor.rb', line 411

def process_block exp
  env.scope do
    process_default exp
  end
end

#process_bracket_call(exp) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/brakeman/processors/alias_processor.rb', line 95

def process_bracket_call exp
  r = replace(exp)

  if r != exp
    return r
  end

  exp.arglist = process_default(exp.arglist)

  r = replace(exp)

  if r != exp
    return r
  end

  t = process(exp.target.deep_clone)

  # sometimes t[blah] has a match in the env
  # but we don't want to actually set the target
  # in case the target is big...which is what this
  # whole method is trying to avoid
  if t != exp.target
    e = exp.deep_clone
    e.target = t

    r = replace(e)

    if r != e
      return r
    end
  else
    t = nil
  end

  if hash? t
    if v = process_hash_access(t, exp.first_arg)
      v.deep_clone(exp.line)
    else
      case t.node_type
      when :params
        exp.target = PARAMS_SEXP.deep_clone(exp.target.line)
      when :session
        exp.target = SESSION_SEXP.deep_clone(exp.target.line)
      when :cookies
        exp.target = COOKIES_SEXP.deep_clone(exp.target.line)
      end

      exp
    end
  elsif array? t
    if v = process_array_access(t, exp.args)
      v.deep_clone(exp.line)
    else
      exp
    end
  elsif t
    exp.target = t
    exp
  else
    if exp.target # `self` target is reported as `nil` https://github.com/seattlerb/ruby_parser/issues/250
      exp.target = process_default exp.target
    end

    exp
  end
end

#process_call(exp) ⇒ Object

Process a method call.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/brakeman/processors/alias_processor.rb', line 167

def process_call exp
  return exp if process_call_defn? exp
  target_var = exp.target
  target_var &&= target_var.deep_clone
  if exp.node_type == :safe_call
    exp.node_type = :call
  end

  if exp.method == :[]
    return process_bracket_call exp
  else
    exp = process_default exp
  end

  #In case it is replaced with something else
  unless call? exp
    return exp
  end

  target = exp.target
  method = exp.method
  first_arg = exp.first_arg

  if method == :send or method == :try
    collapse_send_call exp, first_arg
  end

  if node_type? target, :or and [:+, :-, :*, :/].include? method
    res = process_or_simple_operation(exp)
    return res if res
  elsif target == ARRAY_CONST and method == :new
    return Sexp.new(:array, *exp.args)
  elsif target == HASH_CONST and method == :new and first_arg.nil? and !node_type?(@exp_context.last, :iter)
    return Sexp.new(:hash)
  elsif exp == RAILS_TEST
    return Sexp.new(:false)
  end

  #See if it is possible to simplify some basic cases
  #of addition/concatenation.
  case method
  when :+
    if array? target and array? first_arg
      exp = join_arrays(target, first_arg, exp)
    elsif string? first_arg
      exp = join_strings(target, first_arg, exp)
    elsif number? first_arg
      exp = math_op(:+, target, first_arg, exp)
    end
  when :-, :*, :/
    exp = math_op(method, target, first_arg, exp)
  when :[]
    if array? target
      exp = process_array_access(target, exp.args, exp)
    elsif hash? target
      exp = process_hash_access(target, first_arg, exp)
    end
  when :merge!, :update
    if hash? target and hash? first_arg
       target = process_hash_merge! target, first_arg
       env[target_var] = target
       return target
    end
  when :merge
    if hash? target and hash? first_arg
      return process_hash_merge(target, first_arg)
    end
  when :<<
    if string? target and string? first_arg
      target.value << first_arg.value
      env[target_var] = target
      return target
    elsif string? target and string_interp? first_arg
      exp = Sexp.new(:dstr, target.value + first_arg[1]).concat(first_arg[2..-1])
      env[target_var] = exp
    elsif string? first_arg and string_interp? target
      if string? target.last
        target.last.value << first_arg.value
      elsif target.last.is_a? String
        target.last << first_arg.value
      else
        target << first_arg
      end
      env[target_var] = target
      return first_arg
    elsif new_string? target
      env[target_var] = first_arg
      return first_arg
    elsif array? target
      target << first_arg
      env[target_var] = target
      return target
    else
      target = find_push_target(target_var)
      env[target] = exp unless target.nil? # Happens in TemplateAliasProcessor
    end
  when :first
    if array? target and first_arg.nil? and sexp? target[1]
      exp = target[1]
    end
  when :freeze
    unless target.nil?
      exp = target
    end
  when :dup
    unless target.nil?
      exp = target
    end
  when :join
    if array? target and target.length > 2 and (string? first_arg or first_arg.nil?)
      exp = process_array_join(target, first_arg)
    end
  when :!
    #  Convert `!!a` to boolean
    if call? target and target.method == :!
      exp = s(:or, s(:true).line(exp.line), s(:false).line(exp.line)).line(exp.line)
    end
  end

  exp
end

#process_case(exp) ⇒ Object



833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# File 'lib/brakeman/processors/alias_processor.rb', line 833

def process_case exp
  if @ignore_ifs.nil?
    @ignore_ifs = @tracker && @tracker.options[:ignore_ifs]
  end

  if @ignore_ifs
    process_default exp
    return exp
  end

  branch_scopes = []
  was_inside = @inside_if
  @inside_if = true

  exp[1] = process exp[1] if exp[1]

  case_value = if node_type? exp[1], :lvar, :ivar, :call
    exp[1].deep_clone
  end

  exp.each_sexp do |e|
    if node_type? e, :when
      scope do
        @branch_env = env.current

        # set value of case var if possible
        if case_value and simple_when? e
          @branch_env[case_value] = e[1][1]
        end

        # when blocks aren't blocks, they are lists of expressions
        process_default e

        branch_scopes << env.current

        @branch_env = nil
      end
    end
  end

  # else clause
  if sexp? exp.last
    scope do
      @branch_env = env.current

      process_default exp[-1]

      branch_scopes << env.current

      @branch_env = nil
    end
  end

  @inside_if = was_inside

  branch_scopes.each do |s|
    merge_if_branch s
  end

  exp
end

#process_cdecl(exp) ⇒ Object

Constant assignments like BIG_CONSTANT = 234810983



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/brakeman/processors/alias_processor.rb', line 701

def process_cdecl exp
  if sexp? exp.rhs
    exp.rhs = process exp.rhs
  end

  if @tracker
    @tracker.add_constant exp.lhs,
      exp.rhs,
      :file => @current_file,
      :module => @current_module,
      :class => @current_class,
      :method => @current_method
  end

  if exp.lhs.is_a? Symbol
    match = Sexp.new(:const, exp.lhs)
  else
    match = exp.lhs
  end

  env[match] = get_rhs(exp)

  exp
end

#process_cvdecl(exp) ⇒ Object

Class variable assignment @@x = 1



512
513
514
515
516
517
518
519
520
# File 'lib/brakeman/processors/alias_processor.rb', line 512

def process_cvdecl exp
  match = Sexp.new(:cvar, exp.lhs)
  exp.rhs = process(exp.rhs)
  value = get_rhs(exp)

  set_value match, value

  exp
end

#process_default(exp) ⇒ Object

Process a Sexp. If the Sexp has a value associated with it in the environment, that value will be returned.



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
# File 'lib/brakeman/processors/alias_processor.rb', line 56

def process_default exp
  @exp_context.push exp

  begin
    exp.map! do |e|
      if sexp? e and not e.empty?
        process e
      else
        e
      end
    end
  rescue => err
    if @tracker
      @tracker.error err
    else
      raise err
    end
  end

  result = replace(exp)

  @exp_context.pop

  result
end

#process_defn(exp) ⇒ Object

Process a method definition.



418
419
420
421
422
423
# File 'lib/brakeman/processors/alias_processor.rb', line 418

def process_defn exp
  meth_env do
    exp.body = process_all! exp.body
  end
  exp
end

#process_defs(exp) ⇒ Object

Process a method definition on self.



438
439
440
441
442
443
444
# File 'lib/brakeman/processors/alias_processor.rb', line 438

def process_defs exp
  env.scope do
    set_env_defaults
    exp.body = process_all! exp.body
  end
  exp
end

#process_gasgn(exp) ⇒ Object

Global assignment $x = 1



496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/brakeman/processors/alias_processor.rb', line 496

def process_gasgn exp
  match = Sexp.new(:gvar, exp.lhs)
  exp.rhs = process(exp.rhs)
  value = get_rhs(exp)

  if value
    value.line = exp.line

    set_value match, value
  end

  exp
end

#process_hash(exp) ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/brakeman/processors/alias_processor.rb', line 605

def process_hash exp
  exp = process_default(exp)

  # Handle { **kw }
  if node_type? exp, :hash
    if exp.any? { |e| node_type? e, :kwsplat and node_type? e.value, :hash }
      kwsplats, rest = exp.partition { |e| node_type? e, :kwsplat and node_type? e.value, :hash }
      exp = Sexp.new.concat(rest).line(exp.line)

      kwsplats.each do |e|
        exp = process_hash_merge! exp, e.value
      end
    end
  end

  exp
end

#process_hash_merge(hash, args) ⇒ Object

Return a new hash Sexp with the given values merged into it.

args should be a hash Sexp as well.



639
640
641
642
643
644
645
# File 'lib/brakeman/processors/alias_processor.rb', line 639

def process_hash_merge hash, args
  hash = hash.deep_clone
  hash_iterate args do |key, replacement|
    hash_insert hash, key, replacement
  end
  hash
end

#process_hash_merge!(hash, args) ⇒ Object

Merge values into hash when processing

h.merge! :something => “value”



626
627
628
629
630
631
632
633
634
# File 'lib/brakeman/processors/alias_processor.rb', line 626

def process_hash_merge! hash, args
  hash = hash.deep_clone
  hash_iterate args do |key, replacement|
    hash_insert hash, key, replacement
    match = Sexp.new(:call, hash, :[], key)
    env[match] = replacement
  end
  hash
end

#process_helper_method(method_exp, args) ⇒ Object



1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
# File 'lib/brakeman/processors/alias_processor.rb', line 1005

def process_helper_method method_exp, args
  method_name = method_exp.method_name
  Brakeman.debug "Processing method #{method_name}"

  info = @helper_method_info[method_name]

  #If method uses instance variables, then include those and request
  #variables (params, etc) in the method environment. Otherwise,
  #only include request variables.
  if info[:uses_ivars]
    meth_env = only_ivars(:include_request_vars)
  else
    meth_env = only_request_vars
  end

  #Add arguments to method environment
  assign_args method_exp, args, meth_env


  #Find return values if method does not depend on environment/args
  values = @helper_method_cache[method_name]

  unless values
    #Serialize environment for cache key
    meth_values = meth_env.instance_variable_get(:@env).to_a
    meth_values.sort!
    meth_values = meth_values.to_s

    digest = Digest::SHA1.new.update(meth_values << method_name.to_s).to_s.to_sym

    values = @helper_method_cache[digest]
  end

  if values
    #Use values from cache
    values[:ivar_values].each do |var, val|
      env[var] = val
    end

    values[:return_value]
  else
    #Find return value for method
    frv = Brakeman::FindReturnValue.new
    value = frv.get_return_value(method_exp.body_list, meth_env)

    ivars = {}

    only_ivars(false, meth_env).all.each do |var, val|
      env[var] = val
      ivars[var] = val
    end

    if not frv.uses_ivars? and args.length == 0
      #Store return value without ivars and args if they are not used
      @helper_method_cache[method_exp.method_name] = { :return_value => value, :ivar_values => ivars }
    else
      @helper_method_cache[digest] = { :return_value => value, :ivar_values => ivars }
    end

    #Store information about method, just ivar usage for now
    @helper_method_info[method_name] = { :uses_ivars => frv.uses_ivars? }

    value
  end
end

#process_iasgn(exp) ⇒ Object

Instance variable assignment



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/brakeman/processors/alias_processor.rb', line 476

def process_iasgn exp
  self_assign = self_assign?(exp.lhs, exp.rhs)
  exp.rhs = process exp.rhs
  ivar = Sexp.new(:ivar, exp.lhs).line(exp.line)

  if self_assign
    if env[ivar].nil? and @meth_env
      @meth_env[ivar] = get_rhs(exp)
    else
      env[ivar] = get_rhs(exp)
    end
  else
    set_value ivar, get_rhs(exp)
  end

  exp
end

#process_if(exp) ⇒ Object

Sets @inside_if = true



745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# File 'lib/brakeman/processors/alias_processor.rb', line 745

def process_if exp
  if @ignore_ifs.nil?
    @ignore_ifs = @tracker && @tracker.options[:ignore_ifs]
  end

  condition = exp.condition = process exp.condition

  #Check if a branch is obviously going to be taken
  if true? condition
    no_branch = true
    exps = [exp.then_clause, nil]
  elsif false? condition
    no_branch = true
    exps = [nil, exp.else_clause]
  else
    no_branch = false
    exps = [exp.then_clause, exp.else_clause]
  end

  if @ignore_ifs or no_branch
    exps.each_with_index do |branch, i|
      exp[2 + i] = process_if_branch branch
    end
  else
    # Translate `if !...` into `unless ...`
    # Technically they are different but that's only if someone overrides `!`
    if call? condition and condition.method == :!
      condition = condition.target
      exps.reverse!
    end

    was_inside = @inside_if
    @inside_if = true

    branch_scopes = []
    exps.each_with_index do |branch, i|
      scope do
        @branch_env = env.current
        branch_index = 2 + i # s(:if, condition, then_branch, else_branch)
        if i == 0 and array_include_all_literals? condition
          # If the condition is ["a", "b"].include? x
          # set x to "a" inside the true branch
          var = condition.first_arg
          previous_value = env.current[var]
          env.current[var] = safe_literal(var.line)
          exp[branch_index] = process_if_branch branch
          env.current[var] = previous_value
        elsif i == 1 and array_include_all_literals? condition and early_return? branch
          var = condition.first_arg
          env.current[var] = safe_literal(var.line)
          exp[branch_index] = process_if_branch branch
        else
          exp[branch_index] = process_if_branch branch
        end
        branch_scopes << env.current
        @branch_env = nil
      end
    end

    @inside_if = was_inside

    branch_scopes.each do |s|
      merge_if_branch s
    end
  end

  exp
end

#process_if_branch(exp) ⇒ Object



895
896
897
898
899
900
901
902
903
# File 'lib/brakeman/processors/alias_processor.rb', line 895

def process_if_branch exp
  if sexp? exp
    if block? exp
      process_default exp
    else
      process exp
    end
  end
end

#process_iter(exp) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/brakeman/processors/alias_processor.rb', line 350

def process_iter exp
  @exp_context.push exp
  exp[1] = process exp.block_call
  if array_detect_all_literals? exp[1]
    return safe_literal(exp.line)
  end

  @exp_context.pop

  env.scope do
    call = exp.block_call
    block_args = exp.block_args

    if call? call and [:each, :map].include? call.method and all_literals? call.target and block_args.length == 2 and block_args.last.is_a? Symbol
      # Iterating over an array of all literal values
      local = Sexp.new(:lvar, block_args.last)
      env.current[local] = safe_literal(exp.line)
    else
      block_args.each do |e|
        #Force block arg(s) to be local
        if node_type? e, :lasgn
          env.current[Sexp.new(:lvar, e.lhs)] = Sexp.new(:lvar, e.lhs)
        elsif node_type? e, :kwarg
          env.current[Sexp.new(:lvar, e[1])] = e[2]
        elsif node_type? e, :masgn, :shadow
          e[1..-1].each do |var|
            local = Sexp.new(:lvar, var)
            env.current[local] = local
          end
        elsif e.is_a? Symbol
          local = Sexp.new(:lvar, e)
          env.current[local] = local
        elsif e.nil? # trailing comma, argument destructuring
          next # Punt for now
        else
          raise "Unexpected value in block args: #{e.inspect}"
        end
      end
    end

    block = exp.block

    if block? block
      process_all! block
    else
      exp[3] = process block
    end
  end

  exp
end

#process_lasgn(exp) ⇒ Object

Local assignment x = 1



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/brakeman/processors/alias_processor.rb', line 457

def process_lasgn exp
  self_assign = self_assign?(exp.lhs, exp.rhs)
  exp.rhs = process exp.rhs if sexp? exp.rhs
  return exp if exp.rhs.nil?

  local = Sexp.new(:lvar, exp.lhs).line(exp.line || -2)

  if self_assign
    # Skip branching
    env[local] = get_rhs(exp)
  else
    set_value local, get_rhs(exp)
  end

  exp
end

#process_masgn(exp) ⇒ Object

Multiple/parallel assignment:

x, y = z, w



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/brakeman/processors/alias_processor.rb', line 564

def process_masgn exp
  exp[2] = process exp[2] if sexp? exp[2]

  if node_type? exp[2], :to_ary and array? exp[2][1]
    exp[2] = exp[2][1]
  end

  unless array? exp[1] and array? exp[2] and exp[1].length == exp[2].length
    return process_default(exp)
  end

  vars = exp[1].dup
  vals = exp[2].dup

  vars.shift
  vals.shift

  # Call each assignment as if it is normal
  vars.each_with_index do |var, i|
    val = vals[i]
    if val

      # This happens with nested destructuring like
      #   x, (a, b) = blah
      if node_type? var, :masgn
        # Need to add value to masgn exp
        m = var.dup
        m[2] = s(:to_ary, val)

        process_masgn m
      else
        assign = var.dup
        assign.rhs = val
        process assign
      end
    end
  end

  exp
end

#process_op_asgn1(exp) ⇒ Object

Assignments like this x ||= 1



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/brakeman/processors/alias_processor.rb', line 649

def process_op_asgn1 exp
  target_var = exp[1]
  target_var &&= target_var.deep_clone

  target = exp[1] = process(exp[1])
  index = exp[2][1] = process(exp[2][1])
  value = exp[4] = process(exp[4])
  match = Sexp.new(:call, target, :[], index)

  if exp[3] == :"||"
    unless env[match]
      if request_value? target
        env[match] = match.combine(value)
      else
        env[match] = value
      end
    end
  else
    new_value = process s(:call, s(:call, target_var, :[], index), exp[3], value)

    env[match] = new_value
  end

  exp
end

#process_op_asgn2(exp) ⇒ Object

Assignments like this x.y ||= 1



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/brakeman/processors/alias_processor.rb', line 677

def process_op_asgn2 exp
  return process_default(exp) if exp[3] != :"||"

  target = exp[1] = process(exp[1])
  value = exp[4] = process(exp[4])
  method = exp[2]

  match = Sexp.new(:call, target, method.to_s[0..-2].to_sym)

  unless env[match]
    env[match] = value
  end

  exp
end

#process_or_simple_operation(exp) ⇒ Object

If possible, distribute operation over both sides of an or. For example,

(1 or 2) * 5

Becomes

(5 or 10)

Only works for strings and numbers right now.



1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
# File 'lib/brakeman/processors/alias_processor.rb', line 1249

def process_or_simple_operation exp
  arg = exp.first_arg
  return nil unless string? arg or number? arg

  target = exp.target
  lhs = process_or_target(target.lhs, exp.dup)
  rhs = process_or_target(target.rhs, exp.dup)

  if lhs and rhs
    if same_value? lhs, rhs
      lhs
    else
      exp.target.lhs = lhs
      exp.target.rhs = rhs
      exp.target
    end
  else
    nil
  end
end

#process_or_target(value, copy) ⇒ Object



1270
1271
1272
1273
1274
1275
1276
1277
# File 'lib/brakeman/processors/alias_processor.rb', line 1270

def process_or_target value, copy
  if string? value or number? value
    copy.target = value
    process copy
  else
    false
  end
end

#process_safely(src, set_env = nil, current_file = @current_file) ⇒ Object

This method processes the given Sexp, but copies it first so the original argument will not be modified.

set_env should be an instance of SexpProcessor::Environment. If provided, it will be used as the starting environment.

This method returns a new Sexp with variables replaced with their values, where possible.



46
47
48
49
50
51
52
# File 'lib/brakeman/processors/alias_processor.rb', line 46

def process_safely src, set_env = nil, current_file = @current_file
  @current_file = current_file
  @env = set_env || SexpProcessor::Environment.new
  @result = src.deep_clone
  process @result
  @result
end

#process_scope(exp) ⇒ Object

Process a new scope.



403
404
405
406
407
408
# File 'lib/brakeman/processors/alias_processor.rb', line 403

def process_scope exp
  env.scope do
    process exp.block
  end
  exp
end

#process_svalue(exp) ⇒ Object

This is the right hand side value of a multiple assignment, like ‘x = y, z`



695
696
697
# File 'lib/brakeman/processors/alias_processor.rb', line 695

def process_svalue exp
  exp.value
end

#raise?(exp) ⇒ Boolean

Returns:

  • (Boolean)


1200
1201
1202
# File 'lib/brakeman/processors/alias_processor.rb', line 1200

def raise? exp
  call? exp and exp.method == :raise
end

#replace(exp, int = 0) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/brakeman/processors/alias_processor.rb', line 82

def replace exp, int = 0
  return exp if int > 3


  if replacement = env[exp] and not duplicate? replacement
    replace(replacement.deep_clone(exp.line), int + 1)
  elsif tracker and replacement = tracker.constant_lookup(exp) and not duplicate? replacement
    replace(replacement.deep_clone(exp.line), int + 1)
  else
    exp
  end
end

#same_value?(lhs, rhs) ⇒ Boolean

Return true if lhs == rhs or lhs is an or expression and rhs is one of its values

Returns:

  • (Boolean)


1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/brakeman/processors/alias_processor.rb', line 1108

def same_value? lhs, rhs
  if lhs == rhs
    true
  elsif node_type? lhs, :or
    lhs.rhs == rhs or lhs.lhs == rhs
  else
    false
  end
end

#self_assign?(var, value) ⇒ Boolean

Returns:

  • (Boolean)


1118
1119
1120
# File 'lib/brakeman/processors/alias_processor.rb', line 1118

def self_assign? var, value
  self_assign_var?(var, value) or self_assign_target?(var, value)
end

#self_assign_target?(var, value) ⇒ Boolean

Return true for x = x.blah

Returns:

  • (Boolean)


1131
1132
1133
1134
1135
1136
1137
1138
1139
# File 'lib/brakeman/processors/alias_processor.rb', line 1131

def self_assign_target? var, value
  target = top_target(value)

  if node_type? target, :lvar, :ivar
    target = target.value
  end

  var == target
end

#self_assign_var?(var, value) ⇒ Boolean

Return true if for x = blah or @x = blah

Returns:

  • (Boolean)


1123
1124
1125
1126
1127
1128
# File 'lib/brakeman/processors/alias_processor.rb', line 1123

def self_assign_var? var, value
  call? value and
  value.method == :+ and
  node_type? value.target, :lvar, :ivar and
  value.target.value == var
end

#set_value(var, value) ⇒ Object

Set variable to given value. Creates “branched” versions of values when appropriate. Avoids creating multiple branched versions inside same if branch.



1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
# File 'lib/brakeman/processors/alias_processor.rb', line 1215

def set_value var, value
  if node_type? value, :if
    value = value_from_if(value)
  elsif node_type? value, :case
    value = value_from_case(value)
  end

  if @ignore_ifs or not @inside_if
    if @meth_env and node_type? var, :ivar and env[var].nil?
      @meth_env[var] = value
    else
      env[var] = value
    end
  elsif env.current[var]
    env.current[var] = value
  elsif @branch_env and @branch_env[var]
    @branch_env[var] = value
  elsif @branch_env and @meth_env and node_type? var, :ivar
    @branch_env[var] = value
  else
    env.current[var] = value
  end
end

#simple_when?(exp) ⇒ Boolean

Returns:

  • (Boolean)


826
827
828
829
830
831
# File 'lib/brakeman/processors/alias_processor.rb', line 826

def simple_when? exp
  node_type? exp[1], :array and
    not node_type? exp[1][1], :splat, :array and
    (exp[1].length == 2 or
     exp[1].all? { |e| e.is_a? Symbol or node_type? e, :lit, :str })
end

#too_deep?(exp) ⇒ Boolean

Returns:

  • (Boolean)


926
927
928
929
930
931
# File 'lib/brakeman/processors/alias_processor.rb', line 926

def too_deep? exp
  @or_depth_limit >= 0 and
  node_type? exp, :or and
  exp.or_depth and
  exp.or_depth >= @or_depth_limit
end

#top_target(exp, last = nil) ⇒ Object

Returns last non-nil target in a call chain



1142
1143
1144
1145
1146
1147
1148
1149
1150
# File 'lib/brakeman/processors/alias_processor.rb', line 1142

def top_target exp, last = nil
  if call? exp
    top_target exp.target, exp
  elsif node_type? exp, :iter
    top_target exp.block_call, last
  else
    exp || last
  end
end

#value_from_case(exp) ⇒ Object



1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
# File 'lib/brakeman/processors/alias_processor.rb', line 1174

def value_from_case exp
  result = []

  exp.each do |e|
    if node_type? e, :when
      result << e.last
    end
  end

  result << exp.last if exp.last # else

  result.reduce do |c, e|
    if c.nil?
      e
    elsif node_type? e, :if
      c.combine(value_from_if e)
    elsif raise? e
      c # ignore exceptions
    elsif e
      c.combine e
    else # when e is nil
      c
    end
  end
end

#value_from_if(exp) ⇒ Object



1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
# File 'lib/brakeman/processors/alias_processor.rb', line 1152

def value_from_if exp
  if block? exp.else_clause or block? exp.then_clause
    #If either clause is more than a single expression, just use entire
    #if expression for now
    exp
  elsif exp.else_clause.nil?
    exp.then_clause
  elsif exp.then_clause.nil?
    exp.else_clause
  else
    condition = exp.condition

    if true? condition
      exp.then_clause
    elsif false? condition
      exp.else_clause
    else
      exp.then_clause.combine(exp.else_clause, exp.line)
    end
  end
end