-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fix for path traversal vulenerability #61
Conversation
WalkthroughA crucial update has been made to enhance the security of the Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# A fix for path traversal vulenerability. | ||
# An attacker may summon this endpoint with ../../etc/passwd and recover the password file of your PC (in linux) or access any other file on the PC | ||
if ".." in file_name: | ||
raise HTTPException(status_code=404, detail=".. in the file name! Are you kidding me?") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The security check added to prevent path traversal attacks by blocking requests containing ".." in the file name is a crucial improvement. However, the approach can be further refined to enhance security. Specifically, the check only looks for the presence of ".." which might not be sufficient to catch all types of path traversal attempts. For example, URL-encoded representations (%2e%2e/
) or more complex sequences that might bypass simple substring checks.
Consider using a more robust method to sanitize the input file path. This could involve:
- Resolving the absolute path of the requested file against a base directory and ensuring it still lies within the intended directory.
- Using a whitelist approach for allowed file names or patterns.
- Employing existing libraries or functions designed for path sanitization.
Here's a suggested approach using pathlib
to ensure the requested file is within the allowed directory:
- if ".." in file_name:
+ base_path = Path(XTTS.speaker_folder).resolve()
+ full_path = (base_path / file_name).resolve()
+ if not str(full_path).startswith(str(base_path)):
raise HTTPException(status_code=404, detail="Invalid file path")
This method resolves the full path of the requested file and checks if it starts with the base path of the speaker folder, ensuring the request cannot traverse outside the intended directory.
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
# A fix for path traversal vulenerability. | |
# An attacker may summon this endpoint with ../../etc/passwd and recover the password file of your PC (in linux) or access any other file on the PC | |
if ".." in file_name: | |
raise HTTPException(status_code=404, detail=".. in the file name! Are you kidding me?") | |
# A fix for path traversal vulenerability. | |
# An attacker may summon this endpoint with ../../etc/passwd and recover the password file of your PC (in linux) or access any other file on the PC | |
base_path = Path(XTTS.speaker_folder).resolve() | |
full_path = (base_path / file_name).resolve() | |
if not str(full_path).startswith(str(base_path)): | |
raise HTTPException(status_code=404, detail="Invalid file path") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very legit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ParisNeo: Thank you for the feedback! If you have any more questions or need further assistance, feel free to ask.
Hi, I'm still just learning so thanks for your PR, I'll be aware :) |
Don't worry. We are all learning :) |
Hi, I have spotted a path traversal vulenerability in the XTTS server that allows an attacker to access any file on the host.
I have added the following to the endpoint to forbid the recovery of files outside the served folder.
Summary by CodeRabbit
get_sample
endpoint to prevent path traversal attacks.