Skip to content

Commit ef5cad5

Browse files
committed
feat(security): implement security and error handling improvements
* Add input validation for table names, dimensions, and other parameters * Create custom exception classes for better error handling * Enhance error messages for clarity and user guidance * Implement validation utilities for consistent input checks * Update methods to raise appropriate exceptions on validation failures * Add comprehensive tests for security features and error handling
1 parent 03a3b0b commit ef5cad5

File tree

8 files changed

+574
-61
lines changed

8 files changed

+574
-61
lines changed

SECURITY_IMPROVEMENTS.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Security & Stability Improvements
2+
3+
This document summarizes the critical security and stability improvements made to sqlite-vec-client.
4+
5+
## Completed Tasks
6+
7+
### Security Enhancements
8+
9+
#### 1. SQL Injection Prevention
10+
- **Table Name Validation**: Added strict validation for table names using regex pattern `^[a-zA-Z_][a-zA-Z0-9_]*$`
11+
- **Prevents**: SQL injection attacks through malicious table names
12+
- **Implementation**: `validation.py::validate_table_name()`
13+
14+
#### 2. Input Validation
15+
Added comprehensive validation for all user inputs:
16+
- **Dimension validation**: Must be positive integer
17+
- **top_k validation**: Must be positive integer
18+
- **limit validation**: Must be positive integer
19+
- **offset validation**: Must be non-negative integer
20+
- **List length matching**: Ensures texts, embeddings, and metadata lists have matching lengths
21+
22+
### Error Handling Improvements
23+
24+
#### 1. Custom Exception Classes
25+
Created a hierarchy of custom exceptions in `exceptions.py`:
26+
- `VecClientError`: Base exception for all client errors
27+
- `ValidationError`: Input validation failures
28+
- `TableNameError`: Invalid table name errors
29+
- `TableNotFoundError`: Missing table errors
30+
- `ConnectionError`: Database connection failures
31+
- `DimensionMismatchError`: Embedding dimension mismatches
32+
33+
#### 2. Enhanced Error Messages
34+
All exceptions now provide clear, actionable error messages:
35+
- Explains what went wrong
36+
- Suggests how to fix the issue
37+
- Includes relevant context (e.g., expected vs actual values)
38+
39+
#### 3. Exception Handling in Public Methods
40+
Added try-catch blocks to all critical operations:
41+
- `create_connection()`: Catches connection and extension loading errors
42+
- `similarity_search()`: Catches table not found errors
43+
- `add()`: Catches table not found errors
44+
- All methods validate inputs before execution
45+
46+
## New Files
47+
48+
1. **sqlite_vec_client/exceptions.py**: Custom exception classes
49+
2. **sqlite_vec_client/validation.py**: Input validation utilities
50+
3. **test_security.py**: Security test suite
51+
52+
## Modified Files
53+
54+
1. **sqlite_vec_client/base.py**:
55+
- Added validation calls to all public methods
56+
- Enhanced error handling with try-catch blocks
57+
- Improved docstrings with Args, Returns, and Raises sections
58+
59+
2. **sqlite_vec_client/__init__.py**:
60+
- Exported all custom exceptions for public use
61+
62+
3. **sqlite_vec_client/utils.py**:
63+
- Updated to use f-string format specifiers (code quality improvement)
64+
65+
## Testing
66+
67+
Created `test_security.py` with comprehensive tests:
68+
- Table name validation (including SQL injection attempts)
69+
- Input parameter validation
70+
- Table not found error handling
71+
72+
All tests pass successfully.
73+
74+
## Code Quality
75+
76+
- ✅ All code passes `mypy` type checking
77+
- ✅ All code passes `ruff` linting
78+
- ✅ Code formatted with `ruff format`
79+
- ✅ Compatible with Python 3.9+
80+
81+
## Usage Examples
82+
83+
### Catching Specific Exceptions
84+
85+
```python
86+
from sqlite_vec_client import (
87+
SQLiteVecClient,
88+
TableNameError,
89+
ValidationError,
90+
TableNotFoundError,
91+
)
92+
93+
# Handle invalid table name
94+
try:
95+
client = SQLiteVecClient(table="invalid-name", db_path="db.db")
96+
except TableNameError as e:
97+
print(f"Invalid table name: {e}")
98+
99+
# Handle validation errors
100+
try:
101+
client = SQLiteVecClient(table="docs", db_path="db.db")
102+
client.create_table(dim=-1) # Invalid dimension
103+
except ValidationError as e:
104+
print(f"Validation error: {e}")
105+
106+
# Handle missing table
107+
try:
108+
client = SQLiteVecClient(table="docs", db_path="db.db")
109+
client.similarity_search(embedding=[0.1, 0.2, 0.3])
110+
except TableNotFoundError as e:
111+
print(f"Table not found: {e}")
112+
```
113+
114+
## Security Best Practices
115+
116+
1. **Always validate user input**: All table names and parameters are now validated
117+
2. **Use parameterized queries**: All SQL queries use `?` placeholders (already implemented)
118+
3. **Clear error messages**: Users get helpful feedback without exposing internals
119+
4. **Fail fast**: Invalid inputs are rejected immediately before any database operations
120+
121+
## Next Steps
122+
123+
With the Critical Priority section complete, the project is now ready for:
124+
- High Priority: Test Suite expansion
125+
- High Priority: Example scripts
126+
- High Priority: Documentation improvements

TODO

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
## 🔴 Critical Priority (Security & Stability)
44

55
### Security
6-
- [ ] Fix SQL injection vulnerability (table name sanitization)
7-
- [ ] Add table name validation (alphanumeric and underscore only)
8-
- [ ] Add input validation (dim, top_k, limit, etc.)
6+
- [x] Fix SQL injection vulnerability (table name sanitization)
7+
- [x] Add table name validation (alphanumeric and underscore only)
8+
- [x] Add input validation (dim, top_k, limit, etc.)
99

1010
### Error Handling
11-
- [ ] Create custom exception classes (VecClientError, TableNotFoundError, etc.)
12-
- [ ] Add exception handling to all public methods
13-
- [ ] Write user-friendly error messages
14-
- [ ] Improve connection error handling
11+
- [x] Create custom exception classes (VecClientError, TableNotFoundError, etc.)
12+
- [x] Add exception handling to all public methods
13+
- [x] Write user-friendly error messages
14+
- [x] Improve connection error handling
1515

1616
## 🟡 High Priority (Quality & Reliability)
1717

sqlite_vec_client/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,21 @@
44
"""
55

66
from .base import SQLiteVecClient
7+
from .exceptions import (
8+
ConnectionError,
9+
DimensionMismatchError,
10+
TableNameError,
11+
TableNotFoundError,
12+
ValidationError,
13+
VecClientError,
14+
)
715

8-
__all__ = ["SQLiteVecClient"]
16+
__all__ = [
17+
"SQLiteVecClient",
18+
"VecClientError",
19+
"ValidationError",
20+
"TableNameError",
21+
"TableNotFoundError",
22+
"ConnectionError",
23+
"DimensionMismatchError",
24+
]

0 commit comments

Comments
 (0)