Skip to content

Fix path traversal vulnerability in actions.py using secure_filename#7

Merged
studentsca023-rgb merged 1 commit into
masterfrom
semgrep-autofix/1773730814
Mar 17, 2026
Merged

Fix path traversal vulnerability in actions.py using secure_filename#7
studentsca023-rgb merged 1 commit into
masterfrom
semgrep-autofix/1773730814

Conversation

@semgrep-code-studentsca023-rgb

Copy link
Copy Markdown

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 from the detection rule python.flask.file.tainted-path-traversal-stdlib-flask.tainted-path-traversal-stdlib-flask.


⚠️ Review carefully before merging. This PR was generated by AI and may cause breaking changes or introduce new vulnerabilities.

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).
@studentsca023-rgb studentsca023-rgb marked this pull request as ready for review March 17, 2026 07:02
@studentsca023-rgb studentsca023-rgb merged commit eb08bad into master Mar 17, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant