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: drop deprecated import #16

Merged
merged 2 commits into from
Nov 20, 2024
Merged

fix: drop deprecated import #16

merged 2 commits into from
Nov 20, 2024

Conversation

JarbasAl
Copy link
Member

@JarbasAl JarbasAl commented Nov 20, 2024

https://github.com/OpenVoiceOS/ovos_skill_installer has been archived for a LONG time

Summary by CodeRabbit

  • New Features

    • Introduced new functions for downloading and extracting files from URLs, enhancing model management capabilities.
    • Added functionality to handle tar and zip file downloads and extractions.
  • Chores

    • Removed the ovos_skill_installer dependency from requirements, streamlining the project.

Copy link

coderabbitai bot commented Nov 20, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The changes involve significant updates to the ovos_stt_plugin_vosk/__init__.py file, introducing new functions for downloading and extracting files from URLs. Specifically, three functions—download, download_extract_tar, and download_extract_zip—are added to facilitate file handling, including error management and temporary file creation. The existing ModelContainer class remains largely unchanged, and some redundant imports have been removed to streamline the code. Additionally, the requirements.txt file has been modified to remove the ovos_skill_installer dependency.

Changes

File Change Summary
ovos_stt_plugin_vosk/init.py Added functions: download(url, file=None, session=None), download_extract_tar(...), and download_extract_zip(...). Removed redundant import statements.
requirements/requirements.txt Removed dependency: ovos_skill_installer. Added newline at the end of the file.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Plugin
    participant URL
    participant FileSystem

    User->>Plugin: Request to download file
    Plugin->>URL: Fetch file from URL
    URL-->>Plugin: Return file data
    Plugin->>FileSystem: Save file to specified location
    Plugin->>FileSystem: If tar file, extract contents
    Plugin->>FileSystem: If zip file, extract contents
    FileSystem-->>Plugin: Confirm extraction
    Plugin-->>User: Download complete
Loading

🐇 "In the code where functions grow,
New ways to fetch and extract flow.
With tar and zip, we now can play,
Downloading models, hip-hip-hooray!
A hop, a skip, through files we glide,
In the world of code, we take great pride!" 🐇

Possibly related PRs

  • fix: drop deprecated import #16: This PR is related as it modifies the same file (ovos_stt_plugin_vosk/__init__.py) and includes the addition of the same functions (download, download_extract_tar, and download_extract_zip), enhancing the functionality for downloading and managing files.

Warning

Rate limit exceeded

@github-actions[bot] has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 0 minutes and 39 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 56f3e81 and 0912d8f.


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

@github-actions github-actions bot added fix and removed fix labels Nov 20, 2024
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: 3

🧹 Outside diff range and nitpick comments (2)
ovos_stt_plugin_vosk/__init__.py (2)

266-269: Simplify conditional assignment with a ternary operator

The if-else block can be simplified using a ternary operator for readability and conciseness.

Apply this diff to simplify the code:

-if session:
-    content = session.get(url).content
-else:
-    content = requests.get(url).content
+content = session.get(url).content if session else requests.get(url).content
🧰 Tools
🪛 Ruff

266-269: Use ternary operator content = session.get(url).content if session else requests.get(url).content instead of if-else-block

Replace if-else-block with content = session.get(url).content if session else requests.get(url).content

(SIM108)


279-347: Refactor to eliminate code duplication

The download_extract_tar and download_extract_zip functions share similar logic. Consider refactoring to reduce redundancy and improve maintainability.

You could create a generic extraction function:

def download_and_extract(url, folder, filename='', archive_type='zip', skill_folder_name=None, session=None):
    # Common code for downloading
    ...

    if archive_type == 'zip':
        with zipfile.ZipFile(filename, 'r') as archive:
            # Extraction logic with security checks
            ...
    elif archive_type == 'tar':
        with tarfile.open(filename) as archive:
            # Extraction logic with security checks
            ...
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 432bdd0 and 56f3e81.

📒 Files selected for processing (1)
  • ovos_stt_plugin_vosk/__init__.py (2 hunks)
🧰 Additional context used
🪛 Ruff
ovos_stt_plugin_vosk/__init__.py

264-264: Use a context manager for opening files

(SIM115)


266-269: Use ternary operator content = session.get(url).content if session else requests.get(url).content instead of if-else-block

Replace if-else-block with content = session.get(url).content if session else requests.get(url).content

(SIM108)

Comment on lines +301 to +303
with tarfile.open(tar_filename) as tar:
tar.extractall(path=folder)

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical security issue: Prevent path traversal in tar extraction

Using tar.extractall without validation can introduce path traversal vulnerabilities. Malicious tar files may overwrite arbitrary files on the filesystem.

Modify the extraction to safely validate file paths:

with tarfile.open(tar_filename) as tar:
    def is_within_directory(directory, target):
        abs_directory = os.path.abspath(directory)
        abs_target = os.path.abspath(os.path.join(directory, target))
        return abs_target.startswith(abs_directory)

    def safe_extract(tar, path=".", members=None):
        for member in tar.getmembers():
            member_path = os.path.join(path, member.name)
            if not is_within_directory(path, member_path):
                raise Exception("Attempted Path Traversal in Tar File")
        tar.extractall(path, members)

    safe_extract(tar, path=folder)

Comment on lines +336 to +338
with zipfile.ZipFile(zip_filename, 'r') as zip_ref:
zip_ref.extractall(folder)

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical security issue: Prevent path traversal in zip extraction

Similar to tar extraction, using zip_ref.extractall without validation can lead to path traversal attacks.

Update the extraction logic to validate file paths:

with zipfile.ZipFile(zip_filename, 'r') as zip_ref:
    for member in zip_ref.namelist():
        member_path = os.path.join(folder, member)
        if not os.path.abspath(member_path).startswith(os.path.abspath(folder)):
            raise Exception("Attempted Path Traversal in Zip File")
    zip_ref.extractall(folder)

"""

if isinstance(file, str):
file = open(file, 'wb')
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Use a context manager when opening files

To ensure files are properly closed even if an error occurs, it's recommended to use a context manager when opening files.

Apply this diff to use a context manager:

-if isinstance(file, str):
-    file = open(file, 'wb')
+if isinstance(file, str):
+    with open(file, 'wb') as f:
+        if session:
+            content = session.get(url).content
+        else:
+            content = requests.get(url).content
+        f.write(content)
+        return

Adjust the rest of the code to handle when file is a file-like object or None.

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Ruff

264-264: Use a context manager for opening files

(SIM115)

@JarbasAl JarbasAl merged commit 9cfc63f into dev Nov 20, 2024
5 of 6 checks passed
@JarbasAl JarbasAl deleted the JarbasAl-patch-1 branch November 20, 2024 13:42
@github-actions github-actions bot added fix and removed fix labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant