forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathConcatenateString.qll
More file actions
932 lines (858 loc) · 30.4 KB
/
ConcatenateString.qll
File metadata and controls
932 lines (858 loc) · 30.4 KB
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
import csharp
/*
* This library builds the actual string value from the following string concatenation methods:
* Interpolated strings `$"https://{host}/{path}?a=b"`
* string addition `"https://" + host + "/" + path + "?a=b"`
* StringBuilder `StringBuilder sb = new StringBuilder("https://host/"); sb.Append("path").Append("?a=b")`
* StringBuilder `StringBuilder sb = new StringBuilder("https://host/"); sb.Append("path"); sb.Append("?a=b")`
* String.Concat `string.Concat("https://", host, "/", path)`
* String.Concat `string.Concat(new String[]{"https://", host, "/", path})`
* String.Format `string.format("https://{0}/path", "host")`
* String.Join `string.Join("/", new String[]{$"https://{host}", path})`
*
* TODO: String.Format for array argument
* TODO: String() constructor
* TODO: String.Insert, String.Remove, String.Replace, String.Split
* TODO: StringBuilder.Insert, StringBuilder.Remove, StringBuilder.Replace
* TODO: CastExpr for String/int/ByteType, like `(int)response.StatusCode`
* TODO: https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.concat for arrays
* TODO: nameof() -> `NameOfExpr`
* TODO: format for log messages https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loggerextensions
* TODO: String.Split, if the original string is Literal and the array needs to be concatenated later
* example: "a;b"
* note: the delimiter can be an array, and all are applied to the string
* TODO: read array element for getStringValue
*
* TODO Maybe a v2 version where it returns the nodes of concat Exprs?
* Then that can be used as the source in data flows?
* After the flow, then it can grab the relevant concatenated values, instead of all in the repo.
* - How to restrict the low-level evaluated predicates though so it doesn't concatenate the whole repo though?
*
* There are private recursive helper predicates to build the full string value for each concatenation method
*
* There are public predicates to build the full string value
*
* There is a public predicate to capture the `Literal` values of Variables/Parameters/etc.,
* It will return `<unknown>` if the value is not available, like untrusted input.
*
* The Range pattern is also used. It creates an abstract base class that is extended.
* The base class ConcatenatedStringValue::Range has predicate `concatenatedStringMatches` to filter on a specific pattern. This will apply to all extensions.
* There are extensions of ConcatenatedStringValue::Range for each concatenation method. They are type DataFlow::Node and capture the full string in predicate `getAConcatenatedString`
*
* Note: There is an artificial restriction of 10 elements on each concatenation type, or the permutations to build the full string value from variable/property/parameter assignments can exceed the 32-bit stringpool
*
* Note: There is an artificial restriction of 120 characters on the string value of a single element, or building the full string value can exceed the maxium string length
*
* Example taint flow:
* override predicate isSource(DataFlow::Node source) { source.(ConcatenatedStringValue).concatenatedStringMatches("%pattern") }
*
* Example to get all nodes where the concatenated string matches a pattern:
* string stringPattern(){ result = ["%pattern1","%pattern2"] }
*
* from DataFlow::Node node
* where node.asExpr().fromSource()
* and node.(ConcatenatedStringValue).concatenatedStringMatches(stringPattern())
* select node, node.(ConcatenatedStringValue).getAConcatenatedString()
*/
/**
* Holds for string concat method
*/
class StringConcatMethodCall extends MethodCall {
StringConcatMethodCall() {
this.fromSource() and
this.getTarget().hasFullyQualifiedName("System.String", "Concat")
}
}
/**
* Holds for string format method
*/
class StringFormatMethodCall extends MethodCall {
StringFormatMethodCall() {
this.fromSource() and
this.getTarget().hasFullyQualifiedName("System.String", "Format")
}
}
/**
* Holds for string join method
*/
class StringJoinMethodCall extends MethodCall {
StringJoinMethodCall() {
this.fromSource() and
this.getTarget().hasFullyQualifiedName("System.String", "Join")
}
}
/**
* Holds for StringBuilder constructor call
*/
class StringBuilderConstructorCall extends Call {
StringBuilderConstructorCall() {
this.fromSource() and
exists(Constructor c | this = c.getACall() |
c.hasFullyQualifiedName("System.Text.StringBuilder", "StringBuilder")
)
}
}
/**
* Holds for StringBuilder append method
*/
private class StringBuilderAppendMethodCall extends MethodCall {
StringBuilderAppendMethodCall() {
this.fromSource() and
this.getTarget().hasFullyQualifiedName("System.Text.StringBuilder", "Append")
}
}
/**
* Holds for StringBuilder appendFormat method
*/
private class StringBuilderAppendFormatMethodCall extends MethodCall {
StringBuilderAppendFormatMethodCall() {
this.fromSource() and
this.getTarget().hasFullyQualifiedName("System.Text.StringBuilder", "AppendFormat")
}
}
/**
* Holds for StringBuilder appendJoin method
*/
private class StringBuilderAppendJoinMethodCall extends MethodCall {
StringBuilderAppendJoinMethodCall() {
this.fromSource() and
this.getTarget().hasFullyQualifiedName("System.Text.StringBuilder", "AppendJoin")
}
}
/**
* Holds for StringBuilder appendLine method
*/
private class StringBuilderAppendLineMethodCall extends MethodCall {
StringBuilderAppendLineMethodCall() {
this.fromSource() and
this.getTarget().hasFullyQualifiedName("System.Text.StringBuilder", "AppendLine")
}
}
/**
* Holds for StringBuilder object creation
*/
class StringBuilderObjectCreation extends ObjectCreation {
StringBuilderObjectCreation() {
this.fromSource() and
this.getType().hasFullyQualifiedName("System.Text", "StringBuilder")
}
}
/**
* Holds for ArrayCreation of type string, int, char, byte
*/
class ArrayCreationOfInterest extends ArrayCreation {
ArrayCreationOfInterest() {
this.fromSource() and
exists(Type t | t = this.getArrayType().getElementType() |
t instanceof SimpleType
or
t instanceof StringType
or
t instanceof ByteType
)
}
}
/**
* Holds for AddExpr in source
*/
class AddExprOfInterest extends AddExpr {
AddExprOfInterest() { this.fromSource() }
}
/**
* Holds for InterpolatedStringExpr in source
*/
class IpsExprOfInterest extends InterpolatedStringExpr {
IpsExprOfInterest() { this.fromSource() }
}
/**
* Holds for string format methods
*/
class FormatMethodCall extends MethodCall {
FormatMethodCall() {
this instanceof StringFormatMethodCall
or
this instanceof StringBuilderAppendFormatMethodCall
}
}
/**
* Holds for string concatenation join methods
*/
class JoinMethodCall extends MethodCall {
JoinMethodCall() {
this instanceof StringJoinMethodCall
or
this instanceof StringBuilderAppendJoinMethodCall
}
}
/**
* Holds for string builder append methods
*/
class StringBuilderMethodCall extends MethodCall {
StringBuilderMethodCall() {
this instanceof StringBuilderAppendMethodCall
or
this instanceof StringBuilderAppendFormatMethodCall
or
this instanceof StringBuilderAppendJoinMethodCall
or
this instanceof StringBuilderAppendLineMethodCall
}
}
/**
* Get the restricted number of elements for each concatenation type
* Otherwise, the permutations to build the full string value from variable/property/parameter assignments can exceed the 32-bit stringpool
*/
int elementRestiction() { result = 10 }
/**
* Holds for the types of Expr that can be used to build the actual string value
*/
abstract private class ExtractExpr extends Expr { }
/**
* Holds when the Expr is an access to a variable, parameter, or property
*/
private class AccessExpr extends ExtractExpr {
AccessExpr() {
this instanceof VariableAccess
or
this instanceof ParameterAccess
or
this instanceof PropertyAccess
}
}
/**
* Holds when the Expr is a Literal, ByteType, InterpolatedStringExpr, or AddExpr
*/
private class ValueExpr extends ExtractExpr {
ValueExpr() {
this instanceof CharLiteral
or
this instanceof IntegerLiteral
or
this instanceof StringLiteral
or
this.getType() instanceof ByteType
or
this instanceof IpsExprOfInterest
or
this instanceof AddExprOfInterest
}
}
/**
* Holds when the `MethodCall` is one of the string concatenation calls
*/
private class MethodCallOfInterest extends MethodCall {
MethodCallOfInterest() {
this instanceof StringConcatMethodCall
or
this instanceof StringFormatMethodCall
or
this instanceof StringJoinMethodCall
or
this instanceof StringBuilderMethodCall
}
}
/**
* Gets the value of a `PropertyRead` from assigned property values
*/
Expr getValueforPropRead(PropertyRead pr) {
exists(VariableAccess va_pr, Variable v, ObjectCreation oc |
va_pr = pr.getQualifier() and
v.getInitializer() = oc and
va_pr.getTarget() = v
|
result = getAValueForProp(oc, pr.getTarget().getName())
)
}
/**
* Gets value set on the property 'prop' either with initializer of with a property setter on the object created with ObjectCreation 'create'
*/
Expr getAValueForProp(ObjectCreation create, string prop) {
// values set in object init
exists(MemberInitializer init |
init = create.getInitializer().(ObjectInitializer).getAMemberInitializer() and
init.getLValue().(PropertyAccess).getTarget().hasName(prop) and
result = init.getRValue()
)
or
// values set on var that create is assigned to
exists(Assignment propAssign |
DataFlow::localFlow(DataFlow::exprNode(create),
DataFlow::exprNode(propAssign.getLValue().(PropertyAccess).getQualifier())) and
propAssign.getLValue().(PropertyAccess).getTarget().hasName(prop) and
result = propAssign.getRValue()
)
or
// property assignment in constructor
exists(Assignment propAssign |
propAssign.getEnclosingCallable() = create.getTarget() and
propAssign.getLValue().(PropertyAccess).getTarget().hasName(prop) and
result = propAssign.getRValue()
)
}
/**
* Holds for arguments or array elements of string concatenation, except for `AddExprOfInterest` and `IpsExprOfInterest` which are recursive
*/
private Expr getExprOfInterest0() {
exists(MethodCallOfInterest mc | result = mc.getAnArgument())
or
exists(StringBuilderConstructorCall call | result = call.getAnArgument())
or
exists(ArrayCreationOfInterest arr | result = arr.getInitializer().getAnElement())
}
/**
* Holds for arguments or array elements of string concatenation
*/
private Expr getExprOfInterest() {
result = getExprOfInterest0()
or
exists(AddExprOfInterest expr | result = expr.getAnOperand())
or
exists(IpsExprOfInterest expr, int i | result = getIseChild(expr, i))
}
/**
* Holds when the Expr is an initialized, default, or assigned value
* of a variable, parameter, or property access
*/
private ValueExpr getExprFromAccess0(AccessExpr e) {
e = getExprOfInterest() and
(
result = e.(VariableAccess).getTarget().getInitializer()
or
exists(ValueExpr ve |
ve = e.(VariableAccess).getTarget().getAnAssignedValue() and
getExprFromAssignedValue(e, ve) and
result = ve
)
or
result = e.(ParameterAccess).getTarget().getDefaultValue()
or
result = e.(PropertyRead).getTarget().getInitializer()
or
result = e.(PropertyRead).getTarget().getExpressionBody()
or
e instanceof PropertyRead and
exists(ValueExpr ve |
ve = getValueforPropRead(e) and
getExprFromAssignedValue(e, ve) and
result = ve
)
)
}
/**
* Holds when the Expr is an assigned value of a variable, parameter, or property access
*/
bindingset[e, ve]
pragma[inline_late]
private predicate getExprFromAssignedValue(AccessExpr e, ValueExpr ve) {
e = getExprOfInterest() and
(
ve.getEnclosingCallable() instanceof Constructor
or
ve.getEnclosingCallable() instanceof Getter
or
ve.getEnclosingCallable() instanceof Setter
) and
not exists(ValueExpr ve2 | DataFlow::localFlow(DataFlow::exprNode(ve2), DataFlow::exprNode(e)))
or
DataFlow::localFlow(DataFlow::exprNode(ve), DataFlow::exprNode(e))
}
/**
* Return actual string value of an `AccessExpr` (if any), otherwise return `<unknown>`
*/
private string getStringFromAccessExpr(AccessExpr e) {
e = getExprOfInterest() and
(
exists(ValueExpr value | value = getExprFromAccess0(e) |
value instanceof Literal and
result = value.(Literal).getValue()
or
value.getType() instanceof ByteType and
result = value.getValue()
or
// TODO need to use this for accuracy. However, the recursive calls for `getInterpolatedStringValue` and `getAddExprValue` are causing performance issues.
// The test cases for these scenarios are also marked with TODO
// result = getStringFromValueExpr(value)
value instanceof InterpolatedStringExpr and
result = "<recursion_skipped>"
or
value instanceof AddExpr and
result = "<recursion_skipped>"
)
or
not exists(ValueExpr value | value = getExprFromAccess0(e)) and
result = "<unknown>"
)
}
/**
* Return actual string value of a `ValueExpr`
*/
private string getStringFromValueExpr(ValueExpr e) {
e = getExprOfInterest() and
(
e instanceof Literal and
result = e.(Literal).getValue()
or
e.getType() instanceof ByteType and
result = e.getValue()
or
// InterpolatedStringExpr may be used inside other string concatenation methods
e instanceof IpsExprOfInterest and
result = getInterpolatedStringValue(e)
or
// AddExpr may be used inside other string concatenation methods
e instanceof AddExprOfInterest and
result = getAddExprValue(e)
)
}
/**
* Return actual string value of an `ExtractExpr`
*/
pragma[inline]
string getStringValue(Expr e) {
exists(string s |
e instanceof ValueExpr and
s = getStringFromValueExpr(e)
or
e instanceof AccessExpr and
s = getStringFromAccessExpr(e)
or
not e instanceof ExtractExpr and
s = "<unknown>"
|
// TODO ConditionalExpr
// restrict string length or total concatenated string can hit max string length
// if s.length() <= 120 then result = s else result = s.substring(0, 120) + "<trimmed>"
result = s.substring(0, s.length().minimum(128))
)
}
/**
* Holds for `ArrayCreation` that uses a direct or local variable argument of a method call
*
* Example: `new String[]{"a", "b", "c"}` from `string.Concat(new String[]{"a", "b", "c"})`
* Example: `new String[]{"a", "b", "c"}` from `var arr = new String[]{"a", "b", "c"}; string.Concat(arr)`
*/
pragma[inline]
private ArrayCreationOfInterest getAnArrayCreationArg(MethodCallOfInterest mc) {
result = mc.getAnArgument()
or
// Array variable
exists(Variable v | mc.getAnArgument() = v.getAnAccess() |
result = v.getInitializer()
or
result = v.getAnAssignedValue()
)
}
/**
* Concentate an array with actual string values
*
* Example: `abc` from `new String[]{var_a, "b", "c"}`
*/
string getConcatenatedArrayValue(ArrayCreationOfInterest arr) {
// restrict elements or number of permutations with `getStringFromAccessExpr` can exceed 32-bit stringpool
if arr.getInitializer().getNumberOfElements() < elementRestiction()
then
result =
concat(string s, int i | s = getStringValue(arr.getInitializer().getElement(i)) | s order by i) and
result.length() > 0
else result = "exceeds elements limitation"
}
/**
* Concentate an array with actual string values using a separator
*
* Example: `a;b;c` from `new String[]{var_a, "b", "c"}` and separator `;`
*/
pragma[inline]
string getConcatenatedArrayValueWithSeparator(ArrayCreationOfInterest arr, ExtractExpr separator) {
// restrict elements or number of permutations with `getStringFromAccessExpr` can exceed 32-bit stringpool
if arr.getInitializer().getNumberOfElements() < elementRestiction()
then
result =
concat(string s, int i |
s = getStringValue(arr.getInitializer().getElement(i))
|
s, getStringValue(separator) order by i
) and
result.length() > 0
else result = "exceeds elements limitation"
}
/**
* Regex for a valid insert.
*
* Example: `{0}`
*
* Copied from semmle.code.csharp.frameworks.Format
*/
private string getFormatInsertRegex() { result = "\\{(\\d+)\\s*(,\\s*-?\\d+\\s*)?(:[^{}]+)?\\}" }
/**
* Regex for a valid token in the string.
*
* Note that the format string can be tokenised using this regex.
*
* Copied from semmle.code.csharp.frameworks.Format
*/
private string getValidFormatTokenRegex() {
result = "[^{}]|\\{\\{|\\}\\}|" + getFormatInsertRegex()
}
/**
* Gets the token at the given position in the string.
*
* Example: from `{0]/{1}` it will return `{0}`, `/`, `{1}`
*
* Based on semmle.code.csharp.frameworks.Format.ValidFormatString
*/
bindingset[formatString]
pragma[inline_late]
private string getToken(string formatString, int outPosition) {
result = formatString.regexpFind(getValidFormatTokenRegex(), _, outPosition)
}
/**
* Gets the insert number at the given position in the string.
*
* Based on semmle.code.csharp.frameworks.Format.ValidFormatString
*/
bindingset[formatString]
pragma[inline_late]
private int getInsert(string formatString, int position) {
result = getToken(formatString, position).regexpCapture(getFormatInsertRegex(), 1).toInt()
}
/**
* Gets any insert number in the string.
*
* Based on semmle.code.csharp.frameworks.Format.ValidFormatString
*/
bindingset[formatString]
pragma[inline_late]
int getAnInsert(string formatString) { result = getInsert(formatString, _) }
/**
* Concatenates the full format string with actual string values
*
* Example: `https://host/path?a=b` from `string.Format("https://{0}/{1}?a=b", "host", "path")`
*/
string getFormatStringValue(FormatMethodCall mc) {
// restrict elements or number of permutations with `getStringFromAccessExpr` can exceed 32-bit stringpool
// Do not count format string argument
if mc.getNumberOfArguments() - 1 < elementRestiction()
then
exists(string formatString | getStringValue(mc.getArgumentForName("format")) = formatString |
result =
concat(string token, int tokenInt, string s |
token = getToken(formatString, tokenInt) and
if token.charAt(0) = "{"
then
if exists(Expr e | e = mc.getArgumentForName("provider"))
then
// If there is an IFormatProvider argument, get insert from 2 args ahead (skip IFormatProvider, formatString)
exists(int insertInt | insertInt = getInsert(formatString, tokenInt) |
s = getStringValue(mc.getArgument(insertInt + 2))
)
else
// otherwise, get insert from 1 arg ahead (skip formatString)
exists(int insertInt | insertInt = getInsert(formatString, tokenInt) |
s = getStringValue(mc.getArgument(insertInt + 1))
)
else s = token
|
s order by tokenInt
)
)
else result = "exceeds elements limitation"
}
private Expr getIseChild(IpsExprOfInterest ise, int i) {
if ise.getChild(i) instanceof InterpolatedStringInsertExpr
then result = ise.getChild(i).(InterpolatedStringInsertExpr).getInsert()
else result = ise.getChild(i)
}
/**
* Concatenates `InterpolatedStringExpr` with actual string value
* Example: `abcde` from `$"a{b}c{d}e"`
*/
language[monotonicAggregates]
string getInterpolatedStringValue(IpsExprOfInterest ise) {
if ise.getNumberOfChildren() < elementRestiction()
then
result =
strictconcat(int i | exists(ise.getChild(i)) | getStringValue(getIseChild(ise, i)) order by i)
else result = "exceeds elements limitation"
}
/**
* Count of chained `AddExpr`
*/
private int getAddExprCount(AddExprOfInterest ae) {
result = count(AddExprOfInterest ae2 | ae2 = ae.getLeftOperand+() | ae2)
}
/**
* Recursively concatenates `AddExpr` children with actual string values
* Example: `a`, `ab`, `abc` from `"a" + "b" + "c"`
*/
private string getAnAddExprValue(AddExprOfInterest e) {
// restrict elements or number of permutations with `getStringFromAccessExpr` can exceed 32-bit stringpool
if getAddExprCount(e) < elementRestiction()
then
// Base case when there are no more AddExpr on the left
not e.getLeftOperand() instanceof AddExprOfInterest and
result = getStringValue(e.getLeftOperand()) + getStringValue(e.getRightOperand())
or
// Recursive case
e.getLeftOperand() instanceof AddExprOfInterest and
result = getAnAddExprValue(e.getLeftOperand()) + getStringValue(e.getRightOperand())
else result = "exceeds elements limitation"
}
/**
* Concatenates `AddExpr` with actual string values
* Example: `abc` from `"a" + "b" + "c"`
*/
string getAddExprValue(AddExprOfInterest e) {
not exists(AddExprOfInterest parent | e = parent.getLeftOperand()) and
result = getAnAddExprValue(e)
}
/**
* Get StringBuilder constructor argument with actual string values
*
* Example: `a` from `new StringBuilder("a")`
*/
private LocalVariable getAStringBuilderLocalVariable(StringBuilderObjectCreation oc) {
exists(Assignment a | a.getRValue() = oc |
result = a.getLValue().(LocalVariableAccess).getTarget()
)
}
/**
* Get StringBuilder constructor argument with actual string values
*
* Example: `a` from `new StringBuilder("a")`
*/
private string getAStringBuilderConstructorValue(LocalVariable v) {
exists(StringBuilderObjectCreation oc |
oc.getTarget().getAParameter().hasName("value") and
v.getAnAssignedValue() = oc
|
result = getStringValue(oc.getArgument(0))
)
}
/**
* Get StringBuilder.Append argument with actual string values
*
* Example: `b` from `sb.Append("b")`
*/
private string getAStringBuilderAppendValue(StringBuilderAppendMethodCall mc) {
(
result = getStringValue(mc.getAnArgument())
or
exists(ArrayCreationOfInterest arr | arr = getAnArrayCreationArg(mc) |
// zero-index array
result = getConcatenatedArrayValue(arr)
)
// or
// TODO exists stringbuilder, recurse
)
}
/**
* Get StringBuilder.AppendLine argument with actual string values
*
* Example: `b` from `sb.AppendLine("b")`
*/
private string getAStringBuilderAppendLineValue(StringBuilderAppendLineMethodCall mc) {
result = getStringValue(mc.getAnArgument()) + "/n"
// or
// TODO exists stringbuilder, recurse
}
/**
* Get StringBuilder method argument with actual string values
*/
private string getAStringBuilderMethodValue(StringBuilderMethodCall mc) {
result = getAStringBuilderAppendValue(mc)
or
result = getFormatStringValue(mc)
or
result = getStringJoinValue(mc)
or
result = getAStringBuilderAppendLineValue(mc)
}
/**
* Count of StringBuilder qualifier chain
*/
private int getAStringBuilderQualifierCount(StringBuilderMethodCall mc) {
result = count(StringBuilderMethodCall mc2 | mc2 = mc.getQualifier+() | mc2)
}
/**
* Concatenate StringBuilder append chain with actual string values
*
* Example: `b`, `cd`, `e;f`, `ab`, `abcd`, `abcde;f`
* from `StringBuilder sb = new StringBuilder("a");`
* `sb.Append("b").AppendFormat("c{0}", "d").AppendJoin(";", new String[]{"e", "f"});`
*/
private string getAStringBuilderAppendChainValue(StringBuilderMethodCall mc) {
// restrict elements or number of permutations with `getStringFromAccessExpr` can exceed 32-bit stringpool
if getAStringBuilderQualifierCount(mc) < elementRestiction()
then
// base case
exists(LocalVariableAccess va | va = mc.getQualifier() |
result = getAStringBuilderConstructorValue(va.getTarget()) + getAStringBuilderMethodValue(mc)
or
not exists(string s | s = getAStringBuilderConstructorValue(va.getTarget())) and
result = getAStringBuilderMethodValue(mc)
)
or
// recursive case
exists(StringBuilderMethodCall qualifier | qualifier = mc.getQualifier() |
result = getAStringBuilderAppendChainValue(qualifier) + getAStringBuilderMethodValue(mc)
)
else result = "exceeds elements limitation"
}
/**
* Concatenate StringBuilder append chain with actual string values
*
* Example: `abcde;f`
* from `StringBuilder sb = new StringBuilder("a");`
* `sb.Append("b").AppendFormat("c{0}", "d").AppendJoin(";", new String[]{"e", "f"})`
*/
private string getStringBuilderAppendChainValue(StringBuilderObjectCreation oc) {
exists(LocalVariableAccess va, StringBuilderMethodCall mc |
va = getAStringBuilderLocalVariable(oc).getAnAccess() and
va = mc.getQualifier+() and
// filter on the last append in the chain
not va = mc.getQualifier() and
not exists(StringBuilderMethodCall mc2 | mc = mc2.getQualifier()) and
result = getAStringBuilderAppendChainValue(mc)
)
}
/**
* Holds for `MethodCall` of discrete StringBuilder append calls
*
* Holds for `sb.Append("b")`, `sb.AppendFormat("c{0}", "d")`,
* `sb.AppendJoin(";", new String[]{"e", "f"})`
*
* Does not hold for `sb.Append("b").sb.Append("c")`, `sb.ToString()`
*/
private StringBuilderMethodCall getAStringBuilderMethodCall(StringBuilderObjectCreation oc) {
exists(LocalVariable v, StringBuilderMethodCall mc |
v = getAStringBuilderLocalVariable(oc) and
mc.getQualifier() = v.getAnAccess() and
// filter out append chain
not exists(StringBuilderMethodCall mc2 | mc = mc2.getQualifier()) and
result = mc
)
}
/**
* Get StringBuilder qualifiers chain count
*/
private int getAStringBuilderMethodCallCount(StringBuilderObjectCreation oc) {
result = count(StringBuilderMethodCall mc2 | mc2 = getAStringBuilderMethodCall(oc) | mc2)
}
/**
* Concatenate discrete StringBuilder append calls with actual string values
*
* Example: `abcde;f`
* from `StringBuilder sb = new StringBuilder("a"); sb.Append("b");`
* `sb.AppendFormat("c{0}", "d"); sb.AppendJoin(";", new String[]{"e", "f"})`
*/
private string getStringBuilderAppendValue(StringBuilderObjectCreation oc) {
// restrict elements or number of permutations with `getStringFromAccessExpr` can exceed 32-bit stringpool
if getAStringBuilderMethodCallCount(oc) < elementRestiction()
then
exists(LocalVariableAccess va, string appendValueString |
va = getAStringBuilderLocalVariable(oc).getAnAccess() and
appendValueString =
concat(string s, StringBuilderMethodCall mc |
mc = getAStringBuilderMethodCall(oc) and s = getAStringBuilderMethodValue(mc)
|
s order by mc.getLocation().getStartLine()
)
|
result = getAStringBuilderConstructorValue(va.getTarget()) + appendValueString
or
not exists(string s | s = getAStringBuilderConstructorValue(va.getTarget())) and
result = appendValueString
) and
result.length() > 0
else result = "exceeds elements limitation"
}
/**
* Concatenate StringBuilder append chain with actual string values
*/
string getStringBuilderValue(StringBuilderObjectCreation oc) {
result = getStringBuilderAppendChainValue(oc)
or
result = getStringBuilderAppendValue(oc)
}
/**
* Concatenates `string.Concate` arguments with actual string values
*
* Example: `abc` from `string.Concat("a", "b", "c")`
* Example: `abc` from `string.Concat(new String[]{"a", "b", "c"})`
*/
string getStringConcatValue(StringConcatMethodCall mc) {
not exists(ArrayCreationOfInterest arr | arr = getAnArrayCreationArg(mc)) and
if mc.getNumberOfArguments() < elementRestiction()
then
result = concat(string s, int i | s = getStringValue(mc.getArgument(i)) | s order by i) and
result.length() > 0
else result = "exceeds elements limitation"
or
exists(ArrayCreationOfInterest arr | arr = getAnArrayCreationArg(mc) |
// zero-index array
result = getConcatenatedArrayValue(arr)
)
}
/**
* Concatenates `string.Join` arguments with actual string values and separator
*
* Example: `abc` from `string.Join("", new String[]{"a" + "b" + "c"})`
* Example: `a-b-c` from `string.Join("-", new String[]{"a" + "b" + "c"})`
*/
string getStringJoinValue(JoinMethodCall mc) {
exists(ArrayCreationOfInterest arr |
arr = getAnArrayCreationArg(mc) and
// zero-index array
result = getConcatenatedArrayValueWithSeparator(arr, mc.getArgument(0))
)
}
class ConcatenatedStringValue extends DataFlow::Node instanceof ConcatenatedStringValue::Range {
final string getAConcatenatedString() { result = super.getAConcatenatedString() }
bindingset[pattern]
predicate concatenatedStringMatches(string pattern) { super.concatenatedStringMatches(pattern) }
}
/** Provides a class for modeling string construction. */
module ConcatenatedStringValue {
abstract class Range extends DataFlow::Node {
/** Gets the concatenated string replaced with actual values from variables/etc. */
abstract string getAConcatenatedString();
bindingset[pattern]
predicate concatenatedStringMatches(string pattern) {
this.getAConcatenatedString().matches(pattern)
}
}
}
private class ConcatenateInterpolatedString extends ConcatenatedStringValue::Range, DataFlow::Node {
ConcatenateInterpolatedString() {
exists(IpsExprOfInterest ips | ips = this.asExpr() |
not ips = getExprOfInterest0() and
not exists(AddExprOfInterest ae | ips = ae.getAnOperand())
)
}
override string getAConcatenatedString() { result = getInterpolatedStringValue(this.asExpr()) }
}
private class ConcatenateFormatString extends ConcatenatedStringValue::Range, DataFlow::Node {
ConcatenateFormatString() { exists(StringFormatMethodCall mc | mc = this.asExpr()) }
override string getAConcatenatedString() { result = getFormatStringValue(this.asExpr()) }
}
private class ConcatenateAddExpr extends ConcatenatedStringValue::Range, DataFlow::Node {
ConcatenateAddExpr() {
exists(AddExprOfInterest ae | ae = this.asExpr() |
not ae = getExprOfInterest0() and
not exists(IpsExprOfInterest ips | ae = ips.getAnInsert())
)
}
override string getAConcatenatedString() { result = getAddExprValue(this.asExpr()) }
}
private class ConcatenateStringConcat extends ConcatenatedStringValue::Range, DataFlow::Node {
ConcatenateStringConcat() { exists(StringConcatMethodCall mc | mc = this.asExpr()) }
override string getAConcatenatedString() { result = getStringConcatValue(this.asExpr()) }
}
private class ConcatenateStringBuilder extends ConcatenatedStringValue::Range, DataFlow::Node {
ConcatenateStringBuilder() { exists(StringBuilderObjectCreation oc | oc = this.asExpr()) }
override string getAConcatenatedString() { result = getStringBuilderValue(this.asExpr()) }
}
private class ConcatenateStringJoin extends ConcatenatedStringValue::Range, DataFlow::Node {
ConcatenateStringJoin() { exists(StringJoinMethodCall mc | mc = this.asExpr()) }
override string getAConcatenatedString() { result = getStringJoinValue(this.asExpr()) }
}