Skip to content

Commit

Permalink
Merge pull request #266 from ncats/aw_a26_pathtraversal
Browse files Browse the repository at this point in the history
WIP:  path traversal mitigation attempt raised snyk analysis
  • Loading branch information
blueSwordfish authored Jul 2, 2024
2 parents edb481a + 93a2cb2 commit 9247c14
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gsrs.config;

import java.io.File;
import java.io.IOException;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.format.DateTimeFormatter;

import gov.nih.ncats.common.util.TimeUtil;
Expand Down Expand Up @@ -78,4 +81,26 @@ private static File getDefaultRootDir() {
return null;
}

public static void failOnBadPathResolution(String basePath, String passedPath)
throws InvalidPathException, IOException {
Path basePathObj;
Path resolvedPathObj;
try {
basePathObj = Paths.get(basePath);
resolvedPathObj = basePathObj.resolve(passedPath);
// System.out.println("getPath : " + resolvedPathObj.toFile().getPath());
// System.out.println("getAbsPath : " + resolvedPathObj.toFile().getAbsolutePath());
// System.out.println("getNormAbsPath: " + resolvedPathObj.normalize().toAbsolutePath());
} catch (InvalidPathException ipe) {
// Don't want to provide input path as it might get flagged.
throw new InvalidPathException("Relating to passedPath", "Exception while resolving path");
}
if (Paths.get(passedPath).isAbsolute()) {
throw new IOException("Absolute path not allowed.");
}
if (!resolvedPathObj.normalize().startsWith(basePathObj)) {
throw new IOException("Unexpected start of path.");
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gsrs.controller;

import gsrs.config.FilePathParserUtils;
import gsrs.service.ExportService;
import ix.ginas.exporters.ExportDir;
import ix.ginas.exporters.ExportMetaData;
Expand Down Expand Up @@ -184,8 +185,6 @@ public ResponseEntity<Object> download(@PathVariable("id") @Pattern(regexp="^[a-

File f = exportFile.get().getFile();



Path path = Paths.get(f.getAbsolutePath());
ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package gsrs.config;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.io.IOException;

import org.junit.jupiter.api.Test;

import gsrs.config.FilePathParserUtils.FileParser;
import static org.junit.jupiter.api.Assertions.*;


public class TestFileParserHelper {
Expand Down Expand Up @@ -99,8 +96,6 @@ public void nullSuppliedGivenDefaultFilePathWithSetRelativeRootShouldBeEffective

assertEquals(new File("test").toPath().normalize(), f.toPath().normalize());
}



@Test
public void nullSuppliedGivenDefaultRelativeWithSetRootShouldBeAbsolute() throws IOException {
Expand All @@ -112,6 +107,70 @@ public void nullSuppliedGivenDefaultRelativeWithSetRootShouldBeAbsolute() throws

assertEquals(new File("/tmp/test"), f);
}



@Test
public void testFailOnBadPathResolutionNormal() throws IOException {
boolean hasException = false;
try {
// Should succeed since normalized path ok
FilePathParserUtils.failOnBadPathResolution("/tmp", "my-file.txt");
} catch(Exception e) {
System.out.println(e.getMessage());
hasException = true;
}
assertFalse(hasException);
}

@Test
public void testFailOnBadPathResolutionSingleDot1() throws IOException {
boolean hasException = false;
try {
// Should succeed since path resolves to /tmp/my-file.txt
FilePathParserUtils.failOnBadPathResolution("/tmp", "./my-file.txt");
} catch(Exception e) {
System.out.println(e.getMessage());
hasException = true;
}
assertFalse(hasException);
}

@Test
public void testFailOnBadPathResolutionDotDot1() throws IOException {
boolean hasException = false;
try {
// Should succeed since path resolves to /tmp/my-file.txt
FilePathParserUtils.failOnBadPathResolution("/tmp", "../tmp/my-file.txt");
} catch(Exception e) {
System.out.println(e.getMessage());
hasException = true;
}
assertFalse(hasException);
}

@Test
public void testFailOnBadPathResolutionDotDot2() throws IOException {
boolean hasException = false;
try {
// Should fail since path resolves to /etc/my-file.txt
FilePathParserUtils.failOnBadPathResolution("/tmp", "../etc/my-file.txt");
} catch(Exception e) {
System.out.println(e.getMessage());
hasException = true;
}
assertTrue(hasException);
}

@Test
public void testFailOnBadPathResolutionAbsolutePathException() throws IOException {
boolean hasException = false;
try {
// Should fail due to absolute path
FilePathParserUtils.failOnBadPathResolution("/tmp", "/my/abs/path");
} catch(Exception e) {
System.out.println(e.getMessage());
hasException = true;
}
assertTrue(hasException);
}

}

0 comments on commit 9247c14

Please sign in to comment.