fix: add path traversal protection for project file operations#3
Draft
semgrep-code-lahiruramesh[bot] wants to merge 1 commit into
Draft
fix: add path traversal protection for project file operations#3semgrep-code-lahiruramesh[bot] wants to merge 1 commit into
semgrep-code-lahiruramesh[bot] wants to merge 1 commit into
Conversation
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).
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.
Add validation to prevent path traversal attacks when copying project files to GitHub repositories.
Changes
Why
The
project_idparameter was used directly to construct a file path without validation. An attacker could supply a malicious project ID like../../../etc/passwdto access files outside the intended./projects/directory. By resolving both paths and usingis_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
:pathannotation 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.