Skip to content

fix: add path traversal protection for project file operations#3

Draft
semgrep-code-lahiruramesh[bot] wants to merge 1 commit into
masterfrom
semgrep-autofix/1774428861
Draft

fix: add path traversal protection for project file operations#3
semgrep-code-lahiruramesh[bot] wants to merge 1 commit into
masterfrom
semgrep-autofix/1774428861

Conversation

@semgrep-code-lahiruramesh

Copy link
Copy Markdown

Add validation to prevent path traversal attacks when copying project files to GitHub repositories.

Changes

  • Resolve the projects base directory to an absolute canonical path
  • Resolve the user-provided project path and validate it stays within the projects directory
  • Return a 400 error if the project ID contains path traversal sequences

Why

The project_id parameter was used directly to construct a file path without validation. An attacker could supply a malicious project ID like ../../../etc/passwd to access files outside the intended ./projects/ directory. By resolving both paths and using is_relative_to(), we ensure the final path cannot escape the allowed 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 FastAPI apps, consider using the Starlette :path annotation in the route declaration to automatically sanitize paths and filenames.

@46934971 requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.fastapi.file.tainted-path-traversal-stdlib-fastapi.tainted-path-traversal-stdlib-fastapi.


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

Add validation to prevent path traversal attacks when copying project files to GitHub repositories.

## Changes
- Resolve the projects base directory to an absolute canonical path
- Resolve the user-provided project path and validate it stays within the projects directory
- Return a 400 error if the project ID contains path traversal sequences

## Why
The `project_id` parameter was used directly to construct a file path without validation. An attacker could supply a malicious project ID like `../../../etc/passwd` to access files outside the intended `./projects/` directory. By resolving both paths and using `is_relative_to()`, we ensure the final path cannot escape the allowed 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 FastAPI apps, consider using the Starlette `:path` annotation in the route declaration to automatically sanitize paths and filenames.

@46934971 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/sylonik/findings/702806323) from the detection rule [python.fastapi.file.tainted-path-traversal-stdlib-fastapi.tainted-path-traversal-stdlib-fastapi](https://semgrep.dev/r/python.fastapi.file.tainted-path-traversal-stdlib-fastapi.tainted-path-traversal-stdlib-fastapi).
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.

0 participants