Unsanitized dynamic input in file path
- Rule ID: python_lang_path_traversal
- Languages: python
- Source: path_traversal.yml
Description
Using unsanitized dynamic input to determine file paths can allow attackers to gain access to files and folders outside of the intended scope. This vulnerability occurs when input provided by users is directly used to access the filesystem without proper validation or sanitization.
Remediations
- Do not directly use external input to construct file paths. This can lead to unauthorized file access.
- Do use a safelist to define accessible paths or directories. Only allow user input to influence file paths within these predefined, safe boundaries.
- Do use absolute path checks to confirm that the constructed path is within the expected directory
BASE_DIRECTORY = '/path/to/safe/directory'
my_path = os.path.abspath(os.path.join(BASE_DIRECTORY, dynamic_input))
if my_path.startswith(BASE_DIRECTORY):
open(my_path)
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=python_lang_path_traversal
To run only this rule during a scan, use the following flag
bearer scan /path/to/your-project/ --only-rule=python_lang_path_traversal