Fix path traversal vulnerability in actions.py using secure_filename#7
Merged
Merged
Conversation
Sanitize user-supplied filename to prevent path traversal attacks in the `/message` endpoint.
## Changes
- Added import for `werkzeug.utils.secure_filename`
- Applied `secure_filename()` to sanitize `filename_param` before constructing the file path
- Replaced string concatenation with `Path` operator for cleaner path construction
## Why
The `filename_param` parameter was taken directly from user input without validation. An attacker could supply a malicious filename like `../../etc/passwd` to write files outside the intended `data/{user_id}/` directory. Using `secure_filename()` strips path separators and other dangerous characters, ensuring the file is always written within the intended directory.
## Semgrep Finding Details
The application builds a file path from potentially untrusted data, which can lead to a path traversal vulnerability. An attacker can manipulate the path which the application uses to access files. If the application does not validate user input and sanitize file paths, sensitive files such as configuration or user data can be accessed, potentially creating or overwriting files. In Flask apps, consider using the Werkzeug util `werkzeug.utils.secure_filename()` to sanitize paths and filenames.
@267212124 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/studentsca023_personal_org/findings/722169011) from the detection rule [python.flask.file.tainted-path-traversal-stdlib-flask.tainted-path-traversal-stdlib-flask](https://semgrep.dev/r/python.flask.file.tainted-path-traversal-stdlib-flask.tainted-path-traversal-stdlib-flask).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sanitize user-supplied filename to prevent path traversal attacks in the
/messageendpoint.Changes
werkzeug.utils.secure_filenamesecure_filename()to sanitizefilename_parambefore constructing the file pathPathoperator for cleaner path constructionWhy
The
filename_paramparameter was taken directly from user input without validation. An attacker could supply a malicious filename like../../etc/passwdto write files outside the intendeddata/{user_id}/directory. Usingsecure_filename()strips path separators and other dangerous characters, ensuring the file is always written within the intended directory.Semgrep Finding Details
The application builds a file path from potentially untrusted data, which can lead to a path traversal vulnerability. An attacker can manipulate the path which the application uses to access files. If the application does not validate user input and sanitize file paths, sensitive files such as configuration or user data can be accessed, potentially creating or overwriting files. In Flask apps, consider using the Werkzeug util
werkzeug.utils.secure_filename()to sanitize paths and filenames.@267212124 requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.flask.file.tainted-path-traversal-stdlib-flask.tainted-path-traversal-stdlib-flask.