This document summarizes the critical security and stability improvements made to sqlite-vec-client.
- Table Name Validation: Added strict validation for table names using regex pattern
^[a-zA-Z_][a-zA-Z0-9_]*$ - Prevents: SQL injection attacks through malicious table names
- Implementation:
validation.py::validate_table_name()
Added comprehensive validation for all user inputs:
- Dimension validation: Must be positive integer
- top_k validation: Must be positive integer
- limit validation: Must be positive integer
- offset validation: Must be non-negative integer
- List length matching: Ensures texts, embeddings, and metadata lists have matching lengths
Created a hierarchy of custom exceptions in exceptions.py:
VecClientError: Base exception for all client errorsValidationError: Input validation failuresTableNameError: Invalid table name errorsTableNotFoundError: Missing table errorsConnectionError: Database connection failuresDimensionMismatchError: Embedding dimension mismatches
All exceptions now provide clear, actionable error messages:
- Explains what went wrong
- Suggests how to fix the issue
- Includes relevant context (e.g., expected vs actual values)
Added try-catch blocks to all critical operations:
create_connection(): Catches connection and extension loading errorssimilarity_search(): Catches table not found errorsadd(): Catches table not found errors- All methods validate inputs before execution
- sqlite_vec_client/exceptions.py: Custom exception classes
- sqlite_vec_client/validation.py: Input validation utilities
- test_security.py: Security test suite
-
sqlite_vec_client/base.py:
- Added validation calls to all public methods
- Enhanced error handling with try-catch blocks
- Improved docstrings with Args, Returns, and Raises sections
-
sqlite_vec_client/init.py:
- Exported all custom exceptions for public use
-
sqlite_vec_client/utils.py:
- Updated to use f-string format specifiers (code quality improvement)
Created test_security.py with comprehensive tests:
- Table name validation (including SQL injection attempts)
- Input parameter validation
- Table not found error handling
All tests pass successfully.
- ✅ All code passes
mypytype checking - ✅ All code passes
rufflinting - ✅ Code formatted with
ruff format - ✅ Compatible with Python 3.9+
from sqlite_vec_client import (
SQLiteVecClient,
TableNameError,
ValidationError,
TableNotFoundError,
)
# Handle invalid table name
try:
client = SQLiteVecClient(table="invalid-name", db_path="db.db")
except TableNameError as e:
print(f"Invalid table name: {e}")
# Handle validation errors
try:
client = SQLiteVecClient(table="docs", db_path="db.db")
client.create_table(dim=-1) # Invalid dimension
except ValidationError as e:
print(f"Validation error: {e}")
# Handle missing table
try:
client = SQLiteVecClient(table="docs", db_path="db.db")
client.similarity_search(embedding=[0.1, 0.2, 0.3])
except TableNotFoundError as e:
print(f"Table not found: {e}")- Always validate user input: All table names and parameters are now validated
- Use parameterized queries: All SQL queries use
?placeholders (already implemented) - Clear error messages: Users get helpful feedback without exposing internals
- Fail fast: Invalid inputs are rejected immediately before any database operations
With the Critical Priority section complete, the project is now ready for:
- High Priority: Test Suite expansion
- High Priority: Example scripts
- High Priority: Documentation improvements