Total Pageviews

2017/12/11

[Fortify] Fix Path Manipulation

Problem


Before
Original code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    public static List<String> readFile(String file) throws TdccInitException, IOException {
        List<String> rows = new ArrayList<>();

        if (!StringUtils.isNotEmpty(file)) {
            throw new TdccInitException("Please assign file, must not be empty or null");
        }
        Boolean isFileExisted = Files.exists(Paths.get(file));
        if (!isFileExisted) {
            throw new TdccInitException(MessageFormat.format(wrongFileErr, file));
        } else {
            rows = com.google.common.io.Files.readLines(new File(file), Charsets.UTF_8);
        }
        return rows;
    }


After
Add dependency in your pom.xml
1
2
3
4
5
6
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>


Updated code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    public static List<String> readFile(String file) throws TdccInitException, IOException {
        List<String> rows = new ArrayList<>();

        if (!StringUtils.isNotEmpty(file)) {
            throw new TdccInitException("Please assign file, must not be empty or null");
        }
        file = FilenameUtils.normalize(file);
        Boolean isFileExisted = Files.exists(Paths.get(file));
        if (!isFileExisted) {
            throw new TdccInitException(MessageFormat.format(wrongFileErr, file));
        } else {
            rows = com.google.common.io.Files.readLines(new File(file), Charsets.UTF_8);
        }
        return rows;
    }



No comments: