Skip to content
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

Merged
merged 1 commit into from
Feb 20, 2024
Merged

Conversation

ParisNeo
Copy link
Contributor

@ParisNeo ParisNeo commented Feb 20, 2024

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.

    # 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?") 

Summary by CodeRabbit

  • Security Improvements
    • Enhanced security for the get_sample endpoint to prevent path traversal attacks.

Copy link
Contributor

coderabbitai bot commented Feb 20, 2024

Walkthrough

A crucial update has been made to enhance the security of the xtts_api_server by fortifying the get_sample endpoint. This measure effectively blocks path traversal attacks by rejecting any file name requests that include the potentially malicious ".." sequence, ensuring users cannot access unauthorized files.

Changes

File Path Change Summary
.../server.py Added a security check to prevent path traversal in the get_sample endpoint.

🐇✨
In the code's burrow, deep and vast,
A tiny change was made, not fast.
To keep the danger far at bay,
".." in paths, we say "nay".
Safe and sound beneath the moon,
Our server hums a safer tune.
🌟🐾

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?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3720e1c and 50683bb.
Files selected for processing (1)
  • xtts_api_server/server.py (1 hunks)

Comment on lines +176 to +179
# 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?")
Copy link
Contributor

@coderabbitai coderabbitai bot Feb 20, 2024

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.

Suggested change
# 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")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very legit

Copy link
Contributor

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.

@daswer123
Copy link
Owner

Hi, I'm still just learning so thanks for your PR, I'll be aware :)

@daswer123 daswer123 merged commit 203d2fa into daswer123:main Feb 20, 2024
@ParisNeo
Copy link
Contributor Author

Hi, I'm still just learning so thanks for your PR, I'll be aware :)

Don't worry. We are all learning :)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants