Skip to content

fix: parameterize database filters and limits - #166

Merged
robmarkcole merged 2 commits into
robmarkcole:masterfrom
dajiaohuang:audit/hass-data-detective-20260922
Oct 7, 2026
Merged

robmarkcole merged 2 commits into
robmarkcole:masterfrom
dajiaohuang:audit/hass-data-detective-20260922

Conversation

@dajiaohuang

Copy link
Copy Markdown
Contributor

Summary

The sensor and statistics helpers previously interpolated caller-provided sensor IDs and limits directly into SQL text. This allowed malformed values to alter the generated query and made normal identifiers with quotes unsafe.

This change:

  • binds sensor IDs and limits as SQL parameters
  • validates non-empty sensor identifiers and non-negative integer limits
  • preserves support for statistics identifiers derived from entity IDs
  • makes default Home Assistant config discovery robust when APPDATA is absent on Windows
  • emits portable SQLite URLs with forward slashes
  • adds regression coverage for injection-shaped sensor IDs and invalid limits

Validation

  • python -m pytest -q tests passed: 16 passed
  • python -m black --check detective/core.py detective/config.py tests/test_core.py passed
  • python -m compileall -q detective tests passed
  • git diff --check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The focused changes correctly address unsafe SQL interpolation and cross-platform path handling without introducing unresolved issues.

Review effort: Balanced
Findings: None

What changed in this PR

Secures database queries and improves cross-platform configuration handling.

Changes:

  • Parameterizes sensor IDs and query limits with validation.
  • Produces portable SQLite paths and handles missing Windows APPDATA.
  • Adds regression tests for SQL injection-shaped IDs and invalid limits.
File Description
detective/​core.py Adds validated SQL parameter binding.
detective/​config.py Improves Windows paths and SQLite URLs.
tests/​test_core.py Adds security and validation regression tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@robmarkcole

Copy link
Copy Markdown
Owner

@dajiaohuang thanks for the PR, please fix the test

@dajiaohuang

Copy link
Copy Markdown
Contributor Author

Fixed the failing tests.

@robmarkcole
robmarkcole merged commit 94ad195 into robmarkcole:master Oct 7, 2026
1 check failed
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.

3 participants