1+ import java .io .IOException ;
2+ import java .io .File ;
3+ import java .io .InputStream ;
4+ import static java .io .File .separatorChar ;
5+ import java .nio .file .Files ;
6+
7+
8+ public class PartialPathTraversalTest {
9+ public void esapiExample (File dir , File parent ) throws IOException {
10+ if (!dir .getCanonicalPath ().startsWith (parent .getCanonicalPath ())) {
11+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
12+ }
13+ }
14+
15+ @ SuppressWarnings ("ResultOfMethodCallIgnored" )
16+ void foo1 (File dir , File parent ) throws IOException {
17+ (dir .getCanonicalPath ()).startsWith ((parent .getCanonicalPath ()));
18+ }
19+
20+ void foo2 (File dir , File parent ) throws IOException {
21+ dir .getCanonicalPath ();
22+ if ("potato" .startsWith (parent .getCanonicalPath ())) {
23+ System .out .println ("Hello!" );
24+ }
25+ }
26+
27+ void foo3 (File dir , File parent ) throws IOException {
28+ String parentPath = parent .getCanonicalPath ();
29+ if (!dir .getCanonicalPath ().startsWith (parentPath )) {
30+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
31+ }
32+ }
33+
34+ void foo4 (File dir ) throws IOException {
35+ if (!dir .getCanonicalPath ().startsWith ("/usr" + "/dir" )) {
36+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
37+ }
38+ }
39+
40+ void foo5 (File dir , File parent ) throws IOException {
41+ String canonicalPath = dir .getCanonicalPath ();
42+ if (!canonicalPath .startsWith (parent .getCanonicalPath ())) {
43+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
44+ }
45+ }
46+
47+ void foo6 (File dir , File parent ) throws IOException {
48+ String canonicalPath = dir .getCanonicalPath ();
49+ if (!canonicalPath .startsWith (parent .getCanonicalPath ())) {
50+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
51+ }
52+ String canonicalPath2 = dir .getCanonicalPath ();
53+ if (!canonicalPath2 .startsWith (parent .getCanonicalPath ())) {
54+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
55+ }
56+ }
57+
58+ void foo7 (File dir , File parent ) throws IOException {
59+ String canonicalPath = dir .getCanonicalPath ();
60+ String canonicalPath2 = dir .getCanonicalPath ();
61+ if (!canonicalPath .startsWith (parent .getCanonicalPath ())) {
62+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
63+ }
64+ if (!canonicalPath2 .startsWith (parent .getCanonicalPath ())) {
65+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
66+ }
67+ }
68+
69+ File getChild () {
70+ return null ;
71+ }
72+
73+ void foo8 (File parent ) throws IOException {
74+ String canonicalPath = getChild ().getCanonicalPath ();
75+ if (!canonicalPath .startsWith (parent .getCanonicalPath ())) {
76+ throw new IOException ("Invalid directory: " + getChild ().getCanonicalPath ());
77+ }
78+ }
79+
80+ void foo9 (File dir , File parent ) throws IOException {
81+ if (!dir .getCanonicalPath ().startsWith (parent .getCanonicalPath () + File .separator )) {
82+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
83+ }
84+ }
85+
86+ void foo10 (File dir , File parent ) throws IOException {
87+ if (!dir .getCanonicalPath ().startsWith (parent .getCanonicalPath () + File .separatorChar )) {
88+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
89+ }
90+ }
91+
92+ void foo11 (File dir , File parent ) throws IOException {
93+ String parentCanonical = parent .getCanonicalPath ();
94+ if (!dir .getCanonicalPath ().startsWith (parentCanonical )) {
95+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
96+ }
97+ }
98+
99+ void foo12 (File dir , File parent ) throws IOException {
100+ String parentCanonical = parent .getCanonicalPath ();
101+ String parentCanonical2 = parent .getCanonicalPath ();
102+ if (!dir .getCanonicalPath ().startsWith (parentCanonical )) {
103+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
104+ }
105+ if (!dir .getCanonicalPath ().startsWith (parentCanonical2 )) {
106+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
107+ }
108+ }
109+
110+ void foo13 (File dir , File parent ) throws IOException {
111+ String parentCanonical = parent .getCanonicalPath () + File .separatorChar ;
112+ if (!dir .getCanonicalPath ().startsWith (parentCanonical )) {
113+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
114+ }
115+ }
116+
117+ void foo14 (File dir , File parent ) throws IOException {
118+ String parentCanonical = parent .getCanonicalPath () + separatorChar ;
119+ if (!dir .getCanonicalPath ().startsWith (parentCanonical )) {
120+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
121+ }
122+ }
123+
124+ void foo15 (File dir , File parent ) throws IOException {
125+ String parentCanonical = parent .getCanonicalPath () + File .separatorChar ;
126+ String parentCanonical2 = parent .getCanonicalPath () + File .separatorChar ;
127+ if (!dir .getCanonicalPath ().startsWith (parentCanonical )) {
128+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
129+ }
130+ if (!dir .getCanonicalPath ().startsWith (parentCanonical2 )) {
131+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
132+ }
133+ }
134+
135+ void foo16 (File dir , File parent ) throws IOException {
136+ String parentCanonical = parent .getCanonicalPath () + File .separator ;
137+ if (!dir .getCanonicalPath ().startsWith (parentCanonical )) {
138+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
139+ }
140+ }
141+
142+ @ SuppressWarnings ({
143+ "IfStatementWithIdenticalBranches" ,
144+ "MismatchedStringCase" ,
145+ "UnusedAssignment" ,
146+ "ResultOfMethodCallIgnored"
147+ })
148+ void foo17 (File dir , File parent , boolean branch ) throws IOException {
149+ String parentCanonical = null ;
150+ "test " .startsWith ("somethingElse" );
151+ if (branch ) {
152+ parentCanonical = parent .getCanonicalPath () + File .separatorChar ;
153+ } else {
154+ parentCanonical = parent .getCanonicalPath () + File .separatorChar ;
155+ }
156+ if (!dir .getCanonicalPath ().startsWith (parentCanonical )) {
157+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
158+ }
159+ }
160+
161+ void foo18 (File dir , File parent , boolean branch ) throws IOException {
162+ String parentCanonical = parent .getCanonicalPath ();
163+ if (branch ) {
164+ parentCanonical = parent .getCanonicalPath () + File .separatorChar ;
165+ }
166+ if (!dir .getCanonicalPath ().startsWith (parentCanonical )) {
167+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
168+ }
169+ }
170+
171+ void foo19 (File dir , File parent ) throws IOException {
172+ String parentCanonical = parent .getCanonicalPath () + "/potato" ;
173+ if (!dir .getCanonicalPath ().startsWith (parentCanonical )) {
174+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
175+ }
176+ }
177+
178+ private File cacheDir ;
179+
180+ InputStream foo20 (String ... path ) {
181+ StringBuilder sb = new StringBuilder ();
182+ sb .append (cacheDir .getAbsolutePath ());
183+ for (String p : path ) {
184+ sb .append (File .separatorChar );
185+ sb .append (p );
186+ }
187+ sb .append (".gz" );
188+ String filePath = sb .toString ();
189+ File encodedFile = new File (filePath );
190+ try {
191+ if (!encodedFile .getCanonicalPath ().startsWith (cacheDir .getCanonicalPath ())) {
192+ return null ;
193+ }
194+ return Files .newInputStream (encodedFile .toPath ());
195+ } catch (Exception e ) {
196+ return null ;
197+ }
198+ }
199+
200+ void foo21 (File dir , File parent ) throws IOException {
201+ String parentCanonical = parent .getCanonicalPath ();
202+ if (!dir .getCanonicalPath ().startsWith (parentCanonical + File .separator )) {
203+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
204+ }
205+ }
206+
207+ void foo22 (File dir , File dir2 , File parent , boolean conditional ) throws IOException {
208+ String canonicalPath = conditional ? dir .getCanonicalPath () : dir2 .getCanonicalPath ();
209+ if (!canonicalPath .startsWith (parent .getCanonicalPath ())) {
210+ throw new IOException ("Invalid directory: " + dir .getCanonicalPath ());
211+ }
212+ }
213+
214+ public void doesNotFlag () {
215+ "hello" .startsWith ("goodbye" );
216+ }
217+
218+ }
0 commit comments