Skip to content

fix: secure SQLite database paths to prevent path traversal#134

Closed
MasumRab wants to merge 7 commits intoscientificfrom
fix/initial-attempt-and-reset
Closed

fix: secure SQLite database paths to prevent path traversal#134
MasumRab wants to merge 7 commits intoscientificfrom
fix/initial-attempt-and-reset

Conversation

@MasumRab
Copy link
Copy Markdown
Owner

@MasumRab MasumRab commented Oct 10, 2025

Summary by Sourcery

Secure SQLite database path resolution in SmartFilterManager by anchoring database file paths to the project root and preventing path traversal

Bug Fixes:

  • Prevent path traversal by securing SQLite database path resolution within SmartFilterManager

Enhancements:

  • Introduce ROOT_DIR constant and update init to resolve db_path as an absolute or project-root-relative path

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Oct 10, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

SmartFilterManager now secures SQLite database paths by introducing a ROOT_DIR constant and resolving any unspecified or relative db_path against it, ensuring all database files reside within the project root and preventing path traversal.

Class diagram for updated SmartFilterManager initialization

classDiagram
class SmartFilterManager {
  -db_path: str
  -logger
  -conn
  +__init__(db_path: str = None)
}
class EmailFilter
SmartFilterManager --> EmailFilter
Loading

Flow diagram for secure SQLite database path resolution

flowchart TD
    A["__init__ called with db_path"] --> B{Is db_path None?}
    B -- Yes --> C["Set db_path to ROOT_DIR/smart_filters.db"]
    B -- No --> D{Is db_path absolute?}
    D -- Yes --> E["Use db_path as is"]
    D -- No --> F["Set db_path to ROOT_DIR/db_path"]
    C --> G["Assign db_path"]
    E --> G
    F --> G
Loading

File-Level Changes

Change Details Files
Secure database path resolution to prevent path traversal
  • Define ROOT_DIR constant for project root directory
  • Change init default db_path from literal to None
  • Default to ROOT_DIR/smart_filters.db when db_path is None
  • Prefix non-absolute db_path values with ROOT_DIR
backend/python_nlp/smart_filters.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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 10, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/initial-attempt-and-reset

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `backend/python_nlp/smart_filters.py:95-96` </location>
<code_context>
-    def __init__(self, db_path: str = "smart_filters.db"):
+    def __init__(self, db_path: str = None):
         """Initializes the SmartFilterManager."""
+        if db_path is None:
+            db_path = os.path.join(ROOT_DIR, "smart_filters.db")
+        elif not os.path.isabs(db_path):
+            db_path = os.path.join(ROOT_DIR, db_path)
</code_context>

<issue_to_address>
**suggestion:** Defaulting to a project-root-based database path may cause unexpected file placement.

Using the project root for the database file may cause permission problems and clutter. A dedicated data directory or configurable path is recommended.
</issue_to_address>

### Comment 2
<location> `backend/python_nlp/smart_filters.py:97-98` </location>
<code_context>
         """Initializes the SmartFilterManager."""
+        if db_path is None:
+            db_path = os.path.join(ROOT_DIR, "smart_filters.db")
+        elif not os.path.isabs(db_path):
+            db_path = os.path.join(ROOT_DIR, db_path)
         self.db_path = db_path
         self.logger = logging.getLogger(__name__)
</code_context>

<issue_to_address>
**question:** Relative db_path resolution may not match user expectations if called from different working directories.

Please document that relative paths are resolved against the project root, or add a parameter to allow users to specify the resolution behavior.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

MasumRab added a commit that referenced this pull request Oct 27, 2025
…l attacks

- Add path validation logic to SmartFilterManager.__init__()
- Resolve relative paths relative to DATA_DIR to prevent directory traversal
- Preserve absolute paths for backward compatibility
- Update docstring to clarify path resolution behavior

This completes the security fix for PR #134 by properly implementing the path traversal prevention that was identified in the code review.
@MasumRab MasumRab closed this Oct 27, 2025
MasumRab added a commit that referenced this pull request Oct 29, 2025
…l attacks

- Add path validation logic to SmartFilterManager.__init__()
- Resolve relative paths relative to DATA_DIR to prevent directory traversal
- Preserve absolute paths for backward compatibility
- Update docstring to clarify path resolution behavior

This completes the security fix for PR #134 by properly implementing the path traversal prevention that was identified in the code review.
MasumRab added a commit that referenced this pull request Oct 30, 2025
…l attacks

- Add path validation logic to SmartFilterManager.__init__()
- Resolve relative paths relative to DATA_DIR to prevent directory traversal
- Preserve absolute paths for backward compatibility
- Update docstring to clarify path resolution behavior

This completes the security fix for PR #134 by properly implementing the path traversal prevention that was identified in the code review.
@MasumRab MasumRab deleted the fix/initial-attempt-and-reset branch November 1, 2025 06:10
MasumRab added a commit that referenced this pull request Nov 7, 2025
…l attacks

- Add path validation logic to SmartFilterManager.__init__()
- Resolve relative paths relative to DATA_DIR to prevent directory traversal
- Preserve absolute paths for backward compatibility
- Update docstring to clarify path resolution behavior

This completes the security fix for PR #134 by properly implementing the path traversal prevention that was identified in the code review.
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