1+ import org .apache .commons .text .StringSubstitutor ;
2+ import org .apache .commons .text .lookup .StringLookup ;
3+ import org .apache .commons .text .lookup .StringLookupFactory ;
4+ import org .apache .commons .text .matcher .StringMatcher ;
5+ import org .apache .commons .text .TextStringBuilder ;
6+ import java .util .HashMap ;
7+ import java .util .Map ;
8+ import java .util .Properties ;
9+
10+ class StringSubstitutorTextTest {
11+ String taint () { return "tainted" ; }
12+
13+ void sink (Object o ) {}
14+
15+ void test () throws Exception {
16+ Map <String , String > taintedMap = new HashMap <String , String >();
17+ taintedMap .put ("key" , taint ());
18+ StringLookup taintedLookup = StringLookupFactory .INSTANCE .mapStringLookup (taintedMap );
19+
20+ // Test constructors:
21+ StringSubstitutor ss1 = new StringSubstitutor (); ss1 .setVariableResolver (taintedLookup ); sink (ss1 .replace ("input" )); // $hasTaintFlow=y
22+ StringSubstitutor ss2 = new StringSubstitutor (taintedMap ); sink (ss2 .replace ("input" )); // $hasTaintFlow=y
23+ StringSubstitutor ss3 = new StringSubstitutor (taintedMap , "{" , "}" ); sink (ss3 .replace ("input" )); // $hasTaintFlow=y
24+ StringSubstitutor ss4 = new StringSubstitutor (taintedMap , "{" , "}" , ' ' ); sink (ss4 .replace ("input" )); // $hasTaintFlow=y
25+ StringSubstitutor ss5 = new StringSubstitutor (taintedMap , "{" , "}" , ' ' , "," ); sink (ss5 .replace ("input" )); // $hasTaintFlow=y
26+ StringSubstitutor ss6 = new StringSubstitutor (taintedLookup ); sink (ss6 .replace ("input" )); // $hasTaintFlow=y
27+ StringSubstitutor ss7 = new StringSubstitutor (taintedLookup , "{" , "}" , ' ' ); sink (ss7 .replace ("input" )); // $hasTaintFlow=y
28+ StringSubstitutor ss8 = new StringSubstitutor (taintedLookup , "{" , "}" , ' ' , "," ); sink (ss8 .replace ("input" )); // $hasTaintFlow=y
29+ StringSubstitutor ss9 = new StringSubstitutor (taintedLookup , (StringMatcher )null , null , ' ' ); sink (ss9 .replace ("input" )); // $hasTaintFlow=y
30+ StringSubstitutor ss10 = new StringSubstitutor (taintedLookup , (StringMatcher )null , null , ' ' , null ); sink (ss10 .replace ("input" )); // $hasTaintFlow=y
31+
32+ // Test replace overloads (tainted substitution map):
33+ StringSubstitutor taintedSubst = ss2 ;
34+ sink (taintedSubst .replace ((Object )"input" )); // $hasTaintFlow=y
35+ sink (taintedSubst .replace ("input" )); // $hasTaintFlow=y
36+ sink (taintedSubst .replace ("input" , 0 , 0 )); // $hasTaintFlow=y
37+ sink (taintedSubst .replace ("input" .toCharArray ())); // $hasTaintFlow=y
38+ sink (taintedSubst .replace ("input" .toCharArray (), 0 , 0 )); // $hasTaintFlow=y
39+ sink (taintedSubst .replace ((CharSequence )"input" )); // $hasTaintFlow=y
40+ sink (taintedSubst .replace ((CharSequence )"input" , 0 , 0 )); // $hasTaintFlow=y
41+ sink (taintedSubst .replace (new TextStringBuilder ("input" ))); // $hasTaintFlow=y
42+ sink (taintedSubst .replace (new TextStringBuilder ("input" ), 0 , 0 )); // $hasTaintFlow=y
43+ sink (taintedSubst .replace (new StringBuilder ("input" ))); // $hasTaintFlow=y
44+ sink (taintedSubst .replace (new StringBuilder ("input" ), 0 , 0 )); // $hasTaintFlow=y
45+ sink (taintedSubst .replace (new StringBuffer ("input" ))); // $hasTaintFlow=y
46+ sink (taintedSubst .replace (new StringBuffer ("input" ), 0 , 0 )); // $hasTaintFlow=y
47+
48+ // Test replace overloads (tainted input):
49+ StringSubstitutor untaintedSubst = ss1 ;
50+ sink (untaintedSubst .replace ((Object )taint ())); // $hasTaintFlow=y
51+ sink (untaintedSubst .replace (taint ())); // $hasTaintFlow=y
52+ sink (untaintedSubst .replace (taint (), 0 , 0 )); // $hasTaintFlow=y
53+ sink (untaintedSubst .replace (taint ().toCharArray ())); // $hasTaintFlow=y
54+ sink (untaintedSubst .replace (taint ().toCharArray (), 0 , 0 )); // $hasTaintFlow=y
55+ sink (untaintedSubst .replace ((CharSequence )taint ())); // $hasTaintFlow=y
56+ sink (untaintedSubst .replace ((CharSequence )taint (), 0 , 0 )); // $hasTaintFlow=y
57+ sink (untaintedSubst .replace (new TextStringBuilder (taint ()))); // $hasTaintFlow=y
58+ sink (untaintedSubst .replace (new TextStringBuilder (taint ()), 0 , 0 )); // $hasTaintFlow=y
59+ sink (untaintedSubst .replace (new StringBuilder (taint ()))); // $hasTaintFlow=y
60+ sink (untaintedSubst .replace (new StringBuilder (taint ()), 0 , 0 )); // $hasTaintFlow=y
61+ sink (untaintedSubst .replace (new StringBuffer (taint ()))); // $hasTaintFlow=y
62+ sink (untaintedSubst .replace (new StringBuffer (taint ()), 0 , 0 )); // $hasTaintFlow=y
63+
64+ // Test static replace methods:
65+ sink (StringSubstitutor .replace (taint (), new HashMap <String , String >())); // $hasTaintFlow=y
66+ sink (StringSubstitutor .replace (taint (), new HashMap <String , String >(), "{" , "}" )); // $hasTaintFlow=y
67+ sink (StringSubstitutor .replace ("input" , taintedMap )); // $hasTaintFlow=y
68+ sink (StringSubstitutor .replace ("input" , taintedMap , "{" , "}" )); // $hasTaintFlow=y
69+ Properties taintedProps = new Properties ();
70+ taintedProps .put ("key" , taint ());
71+ sink (StringSubstitutor .replace (taint (), new Properties ())); // $hasTaintFlow=y
72+ sink (StringSubstitutor .replace ("input" , taintedProps )); // $hasTaintFlow=y
73+
74+ // Test replaceIn methods:
75+ TextStringBuilder strBuilder1 = new TextStringBuilder (); taintedSubst .replaceIn (strBuilder1 ); sink (strBuilder1 .toString ()); // $hasTaintFlow=y
76+ TextStringBuilder strBuilder2 = new TextStringBuilder (); taintedSubst .replaceIn (strBuilder2 , 0 , 0 ); sink (strBuilder2 .toString ()); // $hasTaintFlow=y
77+ StringBuilder stringBuilder1 = new StringBuilder (); taintedSubst .replaceIn (stringBuilder1 ); sink (stringBuilder1 .toString ()); // $hasTaintFlow=y
78+ StringBuilder stringBuilder2 = new StringBuilder (); taintedSubst .replaceIn (stringBuilder2 , 0 , 0 ); sink (stringBuilder2 .toString ()); // $hasTaintFlow=y
79+ StringBuffer stringBuffer1 = new StringBuffer (); taintedSubst .replaceIn (stringBuffer1 ); sink (stringBuffer1 .toString ()); // $hasTaintFlow=y
80+ StringBuffer stringBuffer2 = new StringBuffer (); taintedSubst .replaceIn (stringBuffer2 , 0 , 0 ); sink (stringBuffer2 .toString ()); // $hasTaintFlow=y
81+ }
82+
83+ }
0 commit comments