1818import static org .junit .Assert .assertEquals ;
1919import static org .junit .Assert .assertFalse ;
2020import static org .junit .Assert .assertNull ;
21- import static org .junit .Assert .assertThrows ;
2221import static org .junit .Assert .assertTrue ;
2322import static org .junit .Assert .fail ;
2423
3635
3736import javax .servlet .http .Cookie ;
3837
38+ import org .junit .Rule ;
3939import org .junit .Test ;
40+ import org .junit .rules .TemporaryFolder ;
4041import org .owasp .esapi .ESAPI ;
4142import org .owasp .esapi .Encoder ;
4243import org .owasp .esapi .EncoderConstants ;
@@ -63,6 +64,9 @@ public class ValidatorTest {
6364
6465 private static final String PREFERRED_ENCODING = "UTF-8" ;
6566
67+ @ Rule
68+ public TemporaryFolder tempFolder = new TemporaryFolder ();
69+
6670 @ Test
6771 public void testAddRule () {
6872 Validator validator = ESAPI .validator ();
@@ -388,24 +392,6 @@ public void testIsValidDirectoryPath() throws IOException {
388392 }
389393 }
390394
391- private static void mkdir (String dirname ) throws IOException {
392- File file = new File ( dirname );
393-
394- if ( file .exists () && file .isDirectory () ) {
395- return ;
396- } else if ( file .exists () ) {
397- throw new IOException ("Filename " + dirname + " already exists, but is not a directory." );
398- }
399-
400- file .deleteOnExit (); // Mark the directory that we create below to be deleted when the JVM exits.
401-
402- boolean flag = file .mkdir ();
403-
404- if ( !flag ) throw new IOException ("Failed to create directory: " + dirname );
405-
406- return ;
407- }
408-
409395 // GitHub issue # xxxx - GHSL-2022-008
410396 @ Test
411397 public void testIsValidDirectoryPathGHSL_POC () throws IOException {
@@ -415,29 +401,18 @@ public void testIsValidDirectoryPathGHSL_POC() throws IOException {
415401
416402 Validator instance = ESAPI .validator ();
417403 ValidationErrorList errors = new ValidationErrorList ();
418-
419- String input = null ;
420- File parent = null ;
421-
422- boolean isWindows = (System .getProperty ("os.name" ).indexOf ("Windows" ) != -1 ) ? true : false ;
423- if (isWindows ) {
424- input = "C:/temp/esapi-test2" ;
425- parent = new File ("C:/temp/esapi-test/" ); // Note the trailing '/'.
426- } else {
427- input = "/tmp/esapi-test2" ;
428- parent = new File ("/tmp/esapi-test/" ); // Note the trailing '/'.
429- }
430-
431- // Create the 2 directories and set them to be deleted when the JVM exists.
432- mkdir ( input );
433- mkdir ( parent .getCanonicalPath () );
404+
405+ String invalidPath = tempFolder .newFolder ("esapi-test2" ).getAbsolutePath ();
406+ File parent = tempFolder .newFolder ("sibling-of-esapi-test2" );
407+ String validPath = tempFolder .newFolder ("sibling-of-esapi-test2" , "child" ).getAbsolutePath ();
434408
435409 // Before the fix, this incorrectly would return 'true' even though
436410 // 'esapi-test2' directory clearly was not within the 'esapi-test'
437411 // directory.
438412 //
439- assertFalse ( instance .isValidDirectoryPath ("GHSL-2022-008" , input , parent , false , errors ) );
413+ assertFalse ( instance .isValidDirectoryPath ("GHSL-2022-008" , invalidPath , parent , false , errors ) );
440414 assertEquals ( 1 , errors .size () );
415+ assertTrue (instance .isValidDirectoryPath ("GHSL-2022-008" , validPath , parent , false , new ValidationErrorList ()));
441416 }
442417
443418
0 commit comments