Unsanitized user input in file path

Description

Using user input to dynamically construct file paths without proper sanitization introduces a high security risk. This practice can allow attackers to manipulate file paths to access or alter sensitive files, potentially leading to data breaches or system compromise. It is essential to sanitize user input before using it in file system operations to prevent path traversal attacks.

Remediations

  • Do not use unsanitized user input directly in file path construction. This can lead to path traversal vulnerabilities.
  • Do hash or replace user input with a system-generated unique identifier when constructing file paths. This approach minimizes the risk of path manipulation.
  • Do use filepath.Base to extract the filename from a path, discarding any directory information. This helps prevent directory traversal attacks.
    safeFilename := filepath.Base(userInput)
  • Do validate paths before accessing files to ensure they are within the intended directory. This validation acts as a safeguard against unauthorized file access.

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=go_gosec_filesystem_filereadtaint

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

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