/
alias_processor.rb
1228 lines (1052 loc) · 20.7 KB
/
alias_processor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
44
45
46
47
48
49
50
51
52
53
54
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
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
161
162
163
164
165
166
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
288
289
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
339
340
341
342
343
344
345
346
347
348
349
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
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
560
561
562
563
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require_relative '../test'
class AliasProcessorTests < Minitest::Test
def assert_alias expected, original, full = false
original_sexp = RubyParser.new.parse original
expected_sexp = RubyParser.new.parse expected
processed_sexp = Brakeman::AliasProcessor.new.process_safely original_sexp
if full
assert_equal expected_sexp, processed_sexp
else
assert_equal expected_sexp, processed_sexp.last
end
end
def assert_output input, output
assert_alias output, input, true
end
def test_addition
assert_alias '10', <<-RUBY
x = 1 + 2 + 3
x += 4
x
RUBY
end
def test_simple_math
assert_alias '42', <<-RUBY
x = 8 * 5
y = 32 / 8
y -= 2
x += y
x
RUBY
end
def test_divide_by_zero
assert_alias '1 / 0', <<-RUBY
x = 1 / 0
x
RUBY
end
def test_infinity
e = RubyParser.new.parse "x = 1.0 / 0; x"
a = Brakeman::AliasProcessor.new.process_safely e
assert_equal Sexp.new(:lit, 1.0 / 0), a.last
end
def test_concatentation
assert_alias "'Hello world!'", <<-RUBY
x = "Hello"
y = x + " "
z = y + "world!"
z
RUBY
end
def test_string_append
assert_alias "'hello world'", <<-RUBY
x = ""
x << "hello" << " " << "world"
x
RUBY
end
def test_string_new_append
assert_alias "'hello world'", <<-RUBY
x = String.new
x << "hello" << " " << "world"
x
RUBY
end
def test_string_append_call
assert_alias "'hello ' << params[:x]", <<-RUBY
x = ""
x << 'hello ' << params[:x]
x
RUBY
end
def test_string_interp_concat
assert_alias '"#{y}\nthing"', <<-'RUBY'
x = "#{y}\n"
x << "thing"
x
RUBY
end
def test_string_concat_interp
assert_alias '"hello\n#{world}"', <<-'RUBY'
x = ""
x << "hello"
x << "\n#{world}"
x
RUBY
end
def test_array_index
assert_alias "'cookie'", <<-RUBY
dessert = ["fruit", "pie", "ice cream"]
dessert << "cookie"
dessert[1] = "cake"
dessert[1]
index = 2
index = index + 1
dessert[index]
RUBY
end
def test_array_negative_index
assert_alias "'ice cream'", <<-RUBY
dessert = ["fruit", "pie", "ice cream"]
dessert << "cookie"
dessert[1] = "cake"
dessert[1]
index = -3
index = 1 + index
dessert[index]
RUBY
end
def test_array_fetch
assert_alias '3', <<-RUBY
x = [1, 2, 3]
x.fetch(2)
RUBY
end
def test_array_append
assert_alias '[1, 2, 3]', <<-RUBY
x = [1]
x << 2 << 3
x
RUBY
end
def test_array_new_append
assert_alias '[1, 2, 3]', <<-RUBY
x = Array.new
x << 1 << 2 << 3
x
RUBY
end
def test_array_new_init_append
assert_alias '[1, 2, 3]', <<-RUBY
x = Array.new(1)
x << 2 << 3
x
RUBY
end
def test_array_detect
assert_alias ':BRAKEMAN_SAFE_LITERAL', <<-RUBY
x = [1,2,3].detect { |x| x.odd? }
x
RUBY
end
def test_array_first
assert_alias '1', <<-RUBY
x = [1, 2, 3]
y = x.first
y
RUBY
end
def test_array_plus_equals
assert_alias '[1, 2, 3]', <<-RUBY
x = [1]
x += [2, 3]
x
RUBY
end
def test_array_plus
assert_alias '[1, 2, 3]', <<-RUBY
x = [1]
y = x + [2, 3]
y
RUBY
end
def test_array_plus_no_lines
a1 = s(:array, s(:lit, 1))
a2 = s(:array, s(:lit, 2))
joined = Brakeman::AliasProcessor.new.join_arrays(a1, a2)
expected = s(:array,
s(:lit, 1),
s(:lit, 2))
assert_equal expected, joined
end
def test_hash_index
assert_alias "'You say goodbye, I say :hello'", <<-RUBY
x = {:goodbye => "goodbye cruel world" }
x[:hello] = "hello world"
x.merge! :goodbye => "You say goodbye, I say :hello"
x[:goodbye]
RUBY
end
def test_hash_new_index
assert_alias "'You say goodbye, I say :hello'", <<-RUBY
x = Hash.new
x[:hello] = "hello world"
x.merge! :goodbye => "You say goodbye, I say :hello"
x[:goodbye]
RUBY
end
def test_hash_fetch
assert_alias '1', <<-RUBY
x = { a: 0, b: 1, c: 3 }
x.fetch(:b)
RUBY
end
def test_hash_update
assert_alias "2", <<-RUBY
@foo = {
:denominator => 0
}
@foo[:denominator] += 2
x = @foo[:denominator]
x
RUBY
end
def test_hash_double_splat
assert_alias "1", <<-RUBY
a = { a: 1, b: "two", c: :three }
b = { **a, d: 4 }
b[:a]
RUBY
end
def test_splat_array_args
assert_alias 'x(1, b, :c)', <<-RUBY
a = b
args = [1, a, :c]
x(*args)
RUBY
end
def test_obvious_if
assert_alias "'Yes!'", <<-RUBY
condition = true
if condition
x = "Yes!"
else
x = "No!"
end
x
RUBY
end
def test_skip_obvious_if
assert_output <<-INPUT, <<-OUTPUT
condition = false
if condition
x = true
else
x = false
end
INPUT
condition = false
if false
else
x = false
end
OUTPUT
end
def test_skip_rails_env_test
assert_alias "'No!'", <<-RUBY
if Rails.env.test?
x = "Yes!"
else
x = "No!"
end
x
RUBY
end
def test_if
assert_alias "'Awesome!' or 'Else awesome!'", <<-RUBY
if something
x = "Awesome!"
elsif something_else
x = "Else awesome!"
end
x
RUBY
end
def test_or_equal
assert_alias '10', <<-RUBY
x.y = 10
x.y ||= "not this!"
x.y
RUBY
end
def test_safe_or_equal
assert_alias '10', <<-RUBY
x&.y ||= 10
x&.y ||= "not this!"
x&.y
RUBY
end
def test_unknown_hash
assert_alias '1', <<-RUBY
some_hash[:x] = 1
some_hash[:x]
RUBY
end
def test_global
assert_alias '1', <<-RUBY
$x = 1
$x
RUBY
end
def test_class_var
assert_alias '1', <<-RUBY
@@x = 1
@@x
RUBY
end
def test_constant
assert_alias '1', <<-RUBY
X = 1
X
RUBY
end
def test_addition_chained
assert_alias 'y + 5', <<-RUBY
x = y + 2 + 3
x
RUBY
end
def test_send_collapse
assert_alias 'x.y(1)', <<-RUBY
z = x.send(:y, 1)
z
RUBY
end
def test_send_collapse_with_no_target
assert_alias 'y(1)', <<-RUBY
x = send(:y, 1)
x
RUBY
end
def test_safe_send_collapse
assert_alias 'x.y(1)', <<-RUBY
z = x&.send(:y, 1)
z
RUBY
end
def test_safe___send__
assert_alias 'X.y(:z)', <<-RUBY
a = X.__send__(:y, :z)
a
RUBY
end
def test_try_collapse
assert_alias 'x.y', <<-RUBY
z = x.try(:y)
z
RUBY
end
def test_try_symbol_to_proc_collapse
assert_alias 'x.y', <<-RUBY
z = x.try(&:y)
z
RUBY
end
def test_multiple_assignments_in_if
assert_alias "1 or 4", <<-RUBY
x = 1
if y
x = 2
x = 3
x = 4
end
x
RUBY
end
def test_assignments_both_branches
assert_alias "'1234' or 6", <<-RUBY
if y
x = '1'
x += '2'
x += '3'
x += '4'
else
x = 5
x = 6
end
x
RUBY
end
def test_assignments_in_forced_branch
assert_alias "4", <<-RUBY
x = 1
if true
x = 2
x = 3
x = 4
else
x = 5
x = 6
end
x
RUBY
end
def test_assignments_inside_branch_are_isolated
assert_alias "5", <<-RUBY
x = 1
if something
x = 2
x = 3
else
x = 4
x = 5
y = x
end
y
RUBY
end
def test_simple_if_branch_replacement
assert_alias "1", <<-RUBY
x = 1
y = true ? x : z
y
RUBY
end
def test_simple_or_operation_compaction
assert_alias "[0, 4, (4 || 8)]", <<-RUBY
x = 1
if z
x += 1
y = 2
w = 10
else
x += 2
y = 4
w = 5
end
w = w * 0
y = y * 2
[w, x, y]
RUBY
end
def test_assignment_of_simple_if_expression
assert_alias "1 or 2", <<-RUBY
x = (test ? 1 : 2)
x
RUBY
end
def test_assignment_of_forced_if_expression
assert_alias "1", <<-RUBY
x = (true ? 1 : 2)
x
RUBY
end
def test_default_branch_limit_5
assert_alias "(6 or 7) or 8", <<-RUBY
x = 0
if something
x = 1
else
x = 2
end
x = 3 unless this
x = 4 if that
if another_thing
x = 5
else
x = 6
end
x = 7 if getting_crazy
x = 8 if so_crazy
x
RUBY
end
def test_default_branch_limit_not_reached
assert_alias "(((1 or 2) or 3) or 4)", <<-RUBY
x = 1
if something
x = 2
else
x = 3
end
x = 4 unless this
x
RUBY
end
def test_default_branch_limit_before_reset_with_option
expected_y = RubyParser.new.parse "((((0 or 1) or 2) or 3) or 4)"
expected_x = RubyParser.new.parse "5 or 6"
original_sexp = RubyParser.new.parse <<-RUBY
x = 0
if something
x = 1
else
x = 2
end
if another_thing
x = 3
else
x = 4
end
y = x
x = 5 if getting_crazy
x = 6 if so_crazy
[x, y]
RUBY
tracker = BrakemanTester.new_tracker(:branch_limit => 4)
assert_equal 4, tracker.options[:branch_limit]
processed_sexp = Brakeman::AliasProcessor.new(tracker).process_safely original_sexp
result = processed_sexp.last
assert_equal expected_x, result[1]
assert_equal expected_y, result[2]
end
def test_simple_block_args
assert_alias '1', <<-RUBY
y = 1
x do |y|
y = 2
end
y
RUBY
end
def test_block_arg_assignment
assert_alias '1 + z', <<-RUBY
y = 1
blah do |y = 3, x = 2|
y = 2
z = x
end
y + z
RUBY
end
def test_block_arg_destructing
assert_alias '1', <<-RUBY
y = 1
blah do |(x, y)|
y = 2
end
y
RUBY
end
def test_block_args_trailing_comma
# Just testing it doesn't blow up
assert_alias '1', <<-RUBY
blah do |x,|
end
1
RUBY
end
def test_block_with_local
assert_output <<-INPUT, <<-OUTPUT
def a
if b
c = nil
ds.each do |d|
e = T.new
c = e.map
end
r("f" + c.name)
else
g
end
end
INPUT
def a
if b
c = nil
ds.each do |d|
e = T.new
c = T.new.map
end
r("f" + T.new.map.name)
else
g
end
end
OUTPUT
end
def test_shadowed_block_arg
assert_output <<-INPUT, <<-OUTPUT
def a
y = 1
x do |w; y, z|
y = 2
end
puts y
end
INPUT
def a
y = 1
x do |w; y, z|
y = 2
end
puts 1
end
OUTPUT
end
def test_block_in_class_scope
# Make sure blocks in class do not mess up instance variable scope
# for subsequent methods
assert_output <<-INPUT, <<-OUTPUT
class A
x do
@a = 1
end
def b
@a
end
end
INPUT
class A
x do
@a = 1
end
def b
@a
end
end
OUTPUT
end
def test_instance_method_scope_in_block
# Make sure instance variables set inside blocks are set at the method
# scope
assert_output <<-INPUT, <<-OUTPUT
class A
def b
x do
@a = 1
end
@a
end
end
INPUT
class A
def b
x do
@a = 1
end
1
end
end
OUTPUT
end
def test_instance_method_scope_in_if_with_blocks
# Make sure instance variables set inside if expressions are set at the
# method scope after being combined
assert_output <<-INPUT, <<-OUTPUT
class A
def b
if something
x do
@a = 1
end
else
y do
@a = 2
end
end
@a
end
end
INPUT
class A
def b
if something
x do
@a = 1
end
else
y do
@a = 2
end
end
(1 or 2)
end
end
OUTPUT
end
def test_branch_env_is_closed_after_if_statement
assert_output <<-'INPUT', <<-'OUTPUT'
def a
if b
return unless c # this was causing problems
@d = D.find(1)
@d
end
end
INPUT
def a
if b
return unless c
@d = D.find(1)
D.find(1)
end
end
OUTPUT
end
def test_no_branch_for_plus_equals_with_string
assert_alias '"abc"', <<-INPUT
x = "a"
x += "b" if something
x += "c" if something_else
x
INPUT
end
def test_no_branch_for_plus_equals_with_string_in_ivar
assert_alias '"abc"', <<-INPUT
@x = "a"
@x += "b" if something
@x += "c" if something_else
@x
INPUT
end
#We could do better, but this prevents some Sexp explosions and retains
#information about the values
def test_no_branch_for_plus_equals_with_interpolated_string
assert_alias '"a" + "#{b}" + "c"', <<-'INPUT'
x = "a"
x += "#{b}" if something
x += "c" if something_else
x
INPUT
end
#Unfortunate to go to this behavior which loses information
#but I can't think of a scenario in which the several integers
#in ORs would be handled right anyway
def test_no_branch_for_plus_equals_with_number
assert_alias '6', <<-INPUT
x = 1
x += 2 if something
x += 3 if something_else
x
INPUT
end
def test_keywords_in_blocks
assert_output <<-'INPUT', <<-'OUTPUT'
x do |y: 1, z: "2"|
puts y, z
end
INPUT
x do |y: 1, z: "2"|
puts 1, "2"
end
OUTPUT
end
def test_multiple_assignment
assert_output <<-INPUT, <<-OUTPUT
x, $y = 1, 2
x
$y
INPUT
x, $y = 1, 2
1
2
OUTPUT
end
def test_chained_assignment
assert_alias '1', <<-INPUT
x = y = 1
x
INPUT
assert_alias '1', <<-INPUT
@x = @y = 1
@x
INPUT
assert_alias '1', <<-INPUT
$x = $y = 1
$x
INPUT
assert_alias '1', <<-INPUT
X = Y = 1
X
INPUT
assert_alias '1', <<-INPUT
@@x = @@y = 1
@@x
INPUT
assert_alias '1', <<-INPUT
w.x = x.y = 1
w.x
INPUT
assert_alias '5', <<-INPUT
A = @b = @@c = $D = e.f = 1
z = A + @b + @@c + $D + e.f
z
INPUT
end
def test_branch_with_self_assign_target
assert_alias 'a.w.y', <<-INPUT
x = a
x = x.w if thing
x = x.y if other_thing
x
INPUT
end
def test_branch_array_include
assert_alias 'x', <<-INPUT
if [1,2,3].include? x
stuff
end
x
INPUT
assert_output <<-INPUT, <<-OUTPUT
if [1,2,3].include? x
y = x + 2
p y
end
x
INPUT
if [1,2,3].include? x
y = :BRAKEMAN_SAFE_LITERAL
p :BRAKEMAN_SAFE_LITERAL
end
x
OUTPUT
assert_output <<-INPUT, <<-OUTPUT
x = params[:x].presence
if ['a','b'].include? x
User.send x
end
x
INPUT
x = params[:x].presence
if ['a','b'].include? params[:x].presence
User.BRAKEMAN_SAFE_LITERAL
end
params[:x].presence
OUTPUT
end
def test_branch_array_include_return
assert_output <<-INPUT, <<-OUTPUT
return unless ['a', 'b'].include? x
x
INPUT
return unless ['a', 'b'].include? x
:BRAKEMAN_SAFE_LITERAL
OUTPUT
end
def test_branch_array_include_return_more
assert_output <<-INPUT, <<-OUTPUT
x = params[:thing].first
y = ['a', 'b']
unless y.include? x
do_stuff
return
end
x
INPUT
x = params[:thing].first
y = ['a', 'b']
unless ['a', 'b'].include? params[:thing].first
do_stuff
return
end
:BRAKEMAN_SAFE_LITERAL
OUTPUT
end
def test_branch_array_include_fail
assert_output <<-INPUT, <<-OUTPUT
fail unless ['a', 'b'].include? x
x
INPUT
fail unless ['a', 'b'].include? x
:BRAKEMAN_SAFE_LITERAL
OUTPUT
end
def test_branch_array_include_raise
assert_output <<-INPUT, <<-OUTPUT
raise Z unless ['a', 'b'].include? x
x
INPUT
raise Z unless ['a', 'b'].include? x
:BRAKEMAN_SAFE_LITERAL
OUTPUT
end