Unsanitized user input in file path

Description

Unsanitized user input in file paths can compromise your system's security. This vulnerability arises when user input is directly used to construct file names or paths without proper sanitization, potentially leading to path manipulation. Attackers could exploit this to access files or directories outside the intended scope, posing a significant security risk.

Remediations

  • Do not construct file paths directly with user input. This practice can inadvertently allow attackers to manipulate paths to access unauthorized files.
  • Do use path normalization utilities to safely handle user input in file paths. These utilities help eliminate dangerous path sequences.
    public class Cls extends HttpServlet
    {
    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
    {
    String image = request.getParameter("user_profile_picture");

    // normalize path
    Path imagePath = Paths.get("user/profile/" + FilenameUtils.getName(image)).normalize();
    File file = new File(imagePath.toString());
    }
    }

References

Associated CWE

OWASP Top 10

Configuration

To skip this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --skip-rule=java_lang_path_traversal

To run only this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --only-rule=java_lang_path_traversal