Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code scanning alert no. 26: Arbitrary file write during tarfile extraction #154

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gitworkflows
Copy link
Contributor

@gitworkflows gitworkflows commented Oct 23, 2024

Fixes https://github.com/khulnasoft/ThreatMatrix/security/code-scanning/26

To fix the problem, we need to ensure that the paths of the files within the tar archive do not contain any directory traversal elements (e.g., ..). This can be done by checking each file path before extraction and raising an error if any path is found to be unsafe.

The best way to fix the problem without changing existing functionality is to add a validation step before extracting the files. We will iterate through the members of the tar archive, check their paths, and only extract them if they are safe.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Summary by Sourcery

Bug Fixes:

  • Ensure safe extraction of tar files by validating file paths to prevent directory traversal vulnerabilities.

…xtraction

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: gitworkflows <[email protected]>
Copy link

sourcery-ai bot commented Oct 23, 2024

Reviewer's Guide by Sourcery

This pull request addresses a code scanning alert related to arbitrary file write during tarfile extraction. The changes implement a security fix by adding validation to ensure that extracted files do not contain directory traversal elements.

Sequence diagram for tarfile extraction with validation

sequenceDiagram
    participant User
    participant MaxmindDBManager
    participant TarFile
    participant Settings
    participant Logger

    User->>MaxmindDBManager: Call _extract_db_to_media_root(tar_db_path)
    MaxmindDBManager->>Logger: Log "Started extracting..."
    MaxmindDBManager->>TarFile: Open tarfile (tar_db_path)
    loop for each member in tarfile
        MaxmindDBManager->>TarFile: Get member
        MaxmindDBManager->>MaxmindDBManager: Calculate member_path
        MaxmindDBManager->>MaxmindDBManager: Check _is_within_directory
        alt if not within directory
            MaxmindDBManager->>User: Raise AnalyzerRunException
        end
    end
    MaxmindDBManager->>TarFile: Extract all to MEDIA_ROOT
    MaxmindDBManager->>Logger: Log "Finished extracting..."
Loading

File-Level Changes

Change Details Files
Implement secure tarfile extraction
  • Add a new method '_is_within_directory' to validate file paths
  • Modify '_extract_db_to_media_root' method to check each member of the tar archive before extraction
  • Raise an AnalyzerRunException if an unsafe path is detected
api_app/analyzers_manager/observable_analyzers/maxmind.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

deepsource-io bot commented Oct 23, 2024

Here's the code health analysis summary for commits c03f603..8c6c886. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Python LogoPython✅ SuccessView Check ↗
DeepSource Docker LogoDocker✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

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