SonarQube Vulnerability while using File class in Java

Pratim Singha

Objective: The batch application needs to read a file from the server. The exact directory might change in future and so we should make it dynamic.

Approach taken: The file path (directories) and the file name will be provided in the application yaml file as follows:

file path: /application/directory

file name: test.csv

Code for the same: Initial version:

File file = new File(filePath,filename);

SonarQube showed it vulnerable by suggesting this: A file is opened to read its content. The filename comes from an input parameter. If an unfiltered parameter is passed to this file API, files from an arbitrary filesystem location could be read. This rule identifies potential path traversal vulnerabilities. In many cases, the constructed file path cannot be controlled by the user. If that is the case, the reported instance is a false positive.

Vulnerable Code:

@GET
@Path("/images/{image}")
@Produces("images/*")
public Response getImage(@javax.ws.rs.PathParam("image") String image) {
    File file = new File("resources/images/", image); //Weak point

    if (!file.exists()) {
        return Response.status(Status.NOT_FOUND).build();
    }

    return Response.ok().entity(new FileInputStream(file)).build();
}

Solution:

import org.apache.commons.io.FilenameUtils;

@GET
@Path("/images/{image}")
@Produces("images/*")
public Response getImage(@javax.ws.rs.PathParam("image") String image) {
    File file = new File("resources/images/", FilenameUtils.getName(image)); //Fix

    if (!file.exists()) {
        return Response.status(Status.NOT_FOUND).build();
    }

    return Response.ok().entity(new FileInputStream(file)).build();
}

I have changed my code accordingly:

File file = new File(filePath,FilenameUtils.getName(fileName));

Still I am getting the same error message.

Pratim Singha

Yes it is a False Positive.

I removed this by using //NOSONAR at the end of the statement

File file = new File("resources/images/", FilenameUtils.getName(image));  //NOSONAR

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

SonarQube: Unsupported class file major version 58 error when using Java 11

From Java

Error while using the load class from Java to import property file

From Java

Can we specify directory path with the property file while using ResourceBundle class in Java?

From Java

Java - Compile using .class file

From Dev

Manipulating a class to alter another class while using CSV file

From Dev

Could not find the class while using the java class inside the c++

From Dev

{php} is not working in latest smarty while using SmartyBC class file

From Dev

Java cannot find the class file, while javap can

From Java

Reading a .txt file using Scanner class in Java

From Dev

Java: No Such File Or Directory (Using Scanner Class)

From Dev

Unzip Zip File Using JAVA ZipFile Class

From Dev

The Findbugs configuration file is not valid Error While uploading rule file to sonarQube

From Dev

Can append to file using Java class but not using Java Servlet

From Dev

Java: Cannot Find Object while using Comparable Class

From Dev

Using reflection to instantiate a Java class from a .class file

From Dev

Unable to write to the file, Created in java using File class

From Java

Skip blank lines while reading .csv file using opencsv (java)

From Java

How to preserve newlines while reading a file using stream - java 8

From Java

Copy a directory while preserving symlinks using Java File/IO APIs

From Dev

Getting error while using ObjC class include in .cpp file (unqualified-id @class)

From Dev

Compiled javascript file have the duplicate variable declaration in the class constructor while using access modifiers in typescript class

From Dev

Project with both java and javascript in sonarqube using gradle

From Dev

Declare custom rule type as Vulnerability in SonarQube 6.3

From Dev

SonarQube - "The class '...' could not be matched to its original source file."

From Dev

Fixing Cross site scripting vulnerability in java using OWASP

From Java

Java 1.9 Module: Different output for same class using java while overwriting java.net.InetAddress

From Dev

Immutable class while using a cache

From Dev

Find the avg of a set and the class using a text file in java

From Dev

Executing a Java class file using the command line, but from the "wrong" directory

Related Related

  1. 1

    SonarQube: Unsupported class file major version 58 error when using Java 11

  2. 2

    Error while using the load class from Java to import property file

  3. 3

    Can we specify directory path with the property file while using ResourceBundle class in Java?

  4. 4

    Java - Compile using .class file

  5. 5

    Manipulating a class to alter another class while using CSV file

  6. 6

    Could not find the class while using the java class inside the c++

  7. 7

    {php} is not working in latest smarty while using SmartyBC class file

  8. 8

    Java cannot find the class file, while javap can

  9. 9

    Reading a .txt file using Scanner class in Java

  10. 10

    Java: No Such File Or Directory (Using Scanner Class)

  11. 11

    Unzip Zip File Using JAVA ZipFile Class

  12. 12

    The Findbugs configuration file is not valid Error While uploading rule file to sonarQube

  13. 13

    Can append to file using Java class but not using Java Servlet

  14. 14

    Java: Cannot Find Object while using Comparable Class

  15. 15

    Using reflection to instantiate a Java class from a .class file

  16. 16

    Unable to write to the file, Created in java using File class

  17. 17

    Skip blank lines while reading .csv file using opencsv (java)

  18. 18

    How to preserve newlines while reading a file using stream - java 8

  19. 19

    Copy a directory while preserving symlinks using Java File/IO APIs

  20. 20

    Getting error while using ObjC class include in .cpp file (unqualified-id @class)

  21. 21

    Compiled javascript file have the duplicate variable declaration in the class constructor while using access modifiers in typescript class

  22. 22

    Project with both java and javascript in sonarqube using gradle

  23. 23

    Declare custom rule type as Vulnerability in SonarQube 6.3

  24. 24

    SonarQube - "The class '...' could not be matched to its original source file."

  25. 25

    Fixing Cross site scripting vulnerability in java using OWASP

  26. 26

    Java 1.9 Module: Different output for same class using java while overwriting java.net.InetAddress

  27. 27

    Immutable class while using a cache

  28. 28

    Find the avg of a set and the class using a text file in java

  29. 29

    Executing a Java class file using the command line, but from the "wrong" directory

HotTag

Archive