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 #1: Arbitrary file access during archive extraction ("Zip Slip") #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zulkhair
Copy link
Collaborator

@zulkhair zulkhair commented Sep 20, 2024

Fixes https://github.com/Argus-Labs/world-cli/security/code-scanning/1

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

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for file extraction paths during the unzip process, enhancing robustness.
    • Simplified the renaming operation for extracted files to ensure correct path handling.

…action ("Zip Slip")

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

graphite-app bot commented Sep 20, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “graphite/merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

Copy link

coderabbitai bot commented Sep 20, 2024

Walkthrough

The changes involve modifications to the unzipFile function in common/teacmd/editor.go. The extraction path handling has been improved by replacing the direct assignment of file.Name to originalDir with a call to sanitizeExtractPath, which includes error handling. Additionally, the os.Rename function now uses originalDir directly, simplifying the renaming process. These updates enhance the robustness of file extraction by ensuring proper path sanitization and error management.

Changes

File Change Summary
common/teacmd/editor.go Modified unzipFile to use sanitizeExtractPath for improved path handling and added error handling. Updated os.Rename to use originalDir directly.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant UnzipFunction
    participant PathSanitizer
    participant FileSystem

    User->>UnzipFunction: Request to unzip file
    UnzipFunction->>PathSanitizer: Call sanitizeExtractPath(file.Name)
    PathSanitizer-->>UnzipFunction: Return sanitized path or error
    alt If error occurs
        UnzipFunction-->>User: Return error
    else
        UnzipFunction->>FileSystem: Rename file to originalDir
        FileSystem-->>UnzipFunction: Confirm rename
        UnzipFunction-->>User: Return success
    end
Loading

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>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@zulkhair zulkhair changed the title Fix code scanning alert #1: Arbitrary file access during archive extraction ("Zip Slip") fix: Code scanning alert #1: Arbitrary file access during archive extraction ("Zip Slip") Sep 20, 2024
Copy link

codecov bot commented Sep 20, 2024

Codecov Report

Attention: Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 36.84%. Comparing base (b0fefd8) to head (181b054).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
common/teacmd/editor.go 25.00% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #77      +/-   ##
==========================================
+ Coverage   36.70%   36.84%   +0.13%     
==========================================
  Files          40       40              
  Lines        1749     1729      -20     
==========================================
- Hits          642      637       -5     
+ Misses        980      966      -14     
+ Partials      127      126       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@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.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 69e78c9 and 181b054.

Files selected for processing (1)
  • common/teacmd/editor.go (2 hunks)
Additional context used
GitHub Check: codecov/patch
common/teacmd/editor.go

[warning] 207-207: common/teacmd/editor.go#L207
Added line #L207 was not covered by tests

Additional comments not posted (2)
common/teacmd/editor.go (2)

205-208: Excellent work on improving the security of the file extraction process!

The introduction of the sanitizeExtractPath function to sanitize the extraction path, along with the error handling, enhances the robustness and security of the code. This change effectively mitigates potential vulnerabilities that could arise from improper path handling.

Tools
GitHub Check: codecov/patch

[warning] 207-207: common/teacmd/editor.go#L207
Added line #L207 was not covered by tests


241-241: Good simplification of the renaming operation.

Using the sanitized path (originalDir) directly in the os.Rename function call improves the clarity and maintainability of the code. It eliminates the need to reconstruct the path, reducing the chances of introducing path-related bugs or inconsistencies.

originalDir = file.Name
originalDir, err = sanitizeExtractPath(filepath.Dir(targetDir), file.Name)
if err != nil {
return err
Copy link

Choose a reason for hiding this comment

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

Consider adding test coverage for the error handling path.

The static analysis tool codecov/patch reported that line 207, which is part of the error handling block in the unzipFile function, is not covered by tests. To ensure the robustness of the code and catch potential issues, it's recommended to add test cases that trigger the error path and verify the expected behavior.

Do you want me to generate a test case for this scenario or open a GitHub issue to track this task?

Tools
GitHub Check: codecov/patch

[warning] 207-207: common/teacmd/editor.go#L207
Added line #L207 was not covered by tests

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