Unsanitized user input in file path

Description

Unsanitized user input in file path resolution can lead to security vulnerabilities. This issue arises when an application directly uses input from the user to determine file paths or names without proper validation or sanitization. Attackers can exploit this to access unauthorized files or directories, leading to data breaches or other security compromises.

Remediations

  • Do not directly use user input in file paths without sanitization. This prevents attackers from manipulating file paths to access or manipulate unauthorized files.
  • Do use a safelist to define accessible paths or directories. Only allow user input to influence file paths within these predefined, safe boundaries.
  • Do sanitize user input used in file path resolution. For example, use methods like FilenameUtils.getName() to safely extract only the intended file name from the user input, removing any path manipulation attempts.
    public class Cls extends HttpServlet
    {
    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
    {
    String image = request.getParameter("user_profile_picture");
    File file = new File("user/profile/" + FilenameUtils.getName(image));
    }
    }

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_using_user_input

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

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