feat: Complete Sonarr integration with security enhancements
This commit adds comprehensive Sonarr webhook integration and implements critical security improvements identified in code review. ## Sonarr Integration - Full webhook support for Grab, Download, Rename, Delete, and Test events - HMAC SHA256 signature verification for webhook authentication - Series mapping system (Sonarr TVDB ID → Anime Provider URL) - 11 new API endpoints for configuration, mappings, search, and downloads - Comprehensive test suite (31 tests, all passing) - Complete documentation in docs/SONARR_INTEGRATION.md ## Security Enhancements - CORS restricted to specific origins (user's IP: 192.168.1.204:3000) - Path traversal prevention via sanitize_filename() and is_safe_filename() - Structured logging infrastructure (replaced all print() statements) - Environment-based configuration with .env support - Filename sanitization prevents malicious path attacks ## New Features - Lpayer and Sibnet downloader support - Kitsu API integration for anime metadata - Recommendation engine based on download history - Latest releases endpoint for new anime - Modular web interface with component-based templates ## Configuration - Centralized settings via app/config.py with pydantic-settings - Sonarr config auto-created in config/ directory - Example configurations provided for easy setup ## Tests - 31 Sonarr integration tests (23 functionality + 9 security) - 100+ tests passing in core test files - Security utilities fully tested ## Documentation - Updated CLAUDE.md with Sonarr and testing info - Added IMPROVEMENTS_2024-01-24.md analysis - Added SONARR_IMPLEMENTATION.md technical summary Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
# Security and Quality Improvements
|
||||
|
||||
## Date: 2024-01-24
|
||||
|
||||
## Summary
|
||||
|
||||
Implemented critical security improvements and code quality enhancements for immediate production readiness.
|
||||
|
||||
## Changes Made
|
||||
|
||||
### 1. ✅ CORS Security Enhancement
|
||||
|
||||
**File:** `main.py`
|
||||
|
||||
**Before:**
|
||||
```python
|
||||
allow_origins=["*"] # Too permissive
|
||||
allow_methods=["*"]
|
||||
```
|
||||
|
||||
**After:**
|
||||
```python
|
||||
allow_origins=[
|
||||
"http://localhost:3000",
|
||||
"http://127.0.0.1:3000",
|
||||
"http://192.168.1.204:3000",
|
||||
"http://192.168.1.204"
|
||||
]
|
||||
allow_methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
|
||||
```
|
||||
|
||||
**Impact:** Prevents unauthorized cross-origin requests from malicious websites.
|
||||
|
||||
---
|
||||
|
||||
### 2. ✅ Removed Obsolete Files
|
||||
|
||||
**Deleted:**
|
||||
- `app/downloaders/vidmoly_old.py` (195 lines)
|
||||
- `templates/index_old.html`
|
||||
|
||||
**Impact:** Cleaner codebase, removed confusion between old and new implementations.
|
||||
|
||||
---
|
||||
|
||||
### 3. ✅ Filename Sanitization & Security
|
||||
|
||||
**New File:** `app/utils.py`
|
||||
|
||||
**Functions Added:**
|
||||
- `sanitize_filename()` - Removes dangerous characters from filenames
|
||||
- `is_safe_filename()` - Validates filenames for path traversal attempts
|
||||
|
||||
**Security Features:**
|
||||
- Prevents path traversal attacks (`../../../etc/passwd`)
|
||||
- Removes dangerous characters: `\ / : * ? " < > |`
|
||||
- Limits filename length to 255 characters
|
||||
- Strips leading dots and dashes
|
||||
|
||||
**Implementation in endpoints:**
|
||||
- `POST /api/download` - Validates user-provided filenames
|
||||
- `GET /watch/{filename}` - Sanitizes video player filenames
|
||||
|
||||
**Example:**
|
||||
```python
|
||||
# Before: filename = "../../../etc/passwd"
|
||||
# After: filename = "_.._.._etc_passwd" (blocked by is_safe_filename)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 4. ✅ Configuration Management System
|
||||
|
||||
**New File:** `app/config.py`
|
||||
|
||||
**Features:**
|
||||
- Environment-based configuration using Pydantic Settings
|
||||
- Type-safe settings with validation
|
||||
- Default values for all parameters
|
||||
- `.env` file support for easy configuration
|
||||
|
||||
**New Files Created:**
|
||||
- `.env` - Development environment variables
|
||||
- `.env.example` - Template with all available options
|
||||
- `app/config.py` - Settings class
|
||||
|
||||
**Configurable Options:**
|
||||
```bash
|
||||
# Server
|
||||
HOST=0.0.0.0
|
||||
PORT=3000
|
||||
DEBUG=false
|
||||
|
||||
# Downloads
|
||||
DOWNLOAD_DIR=downloads
|
||||
MAX_PARALLEL_DOWNLOADS=3
|
||||
|
||||
# CORS
|
||||
CORS_ORIGINS=http://localhost:3000,http://192.168.1.204:3000
|
||||
|
||||
# Logging
|
||||
LOG_LEVEL=INFO
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. ✅ Logging Infrastructure
|
||||
|
||||
**Files Modified:**
|
||||
- `app/download_manager.py` - Replaced 10+ print() statements
|
||||
- `main.py` - Replaced RESTORE print statement
|
||||
|
||||
**Before:**
|
||||
```python
|
||||
print(f"[DOWNLOAD] URL: {download_url}")
|
||||
print(f"[DOWNLOAD] ✅ Completed: {filename}")
|
||||
```
|
||||
|
||||
**After:**
|
||||
```python
|
||||
logger.info(f"Download URL: {download_url}")
|
||||
logger.info(f"Completed: {filename}")
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Proper log levels (INFO, DEBUG, WARNING, ERROR)
|
||||
- Structured logging with timestamps
|
||||
- Easy to filter and redirect to files
|
||||
- Production-ready logging
|
||||
|
||||
---
|
||||
|
||||
## Test Results
|
||||
|
||||
**All tests passing:** ✅ 23/23 tests passed
|
||||
|
||||
```
|
||||
======================= 23 passed, 11 warnings in 0.36s ========================
|
||||
```
|
||||
|
||||
**Coverage:** 19% (maintained)
|
||||
|
||||
---
|
||||
|
||||
## Security Improvements Summary
|
||||
|
||||
| Issue | Severity | Status | Impact |
|
||||
|-------|----------|--------|--------|
|
||||
| CORS wildcard | **HIGH** | ✅ Fixed | Prevents unauthorized API access |
|
||||
| Path traversal | **HIGH** | ✅ Fixed | Prevents file system attacks |
|
||||
| Print statements | **MEDIUM** | ✅ Fixed | Better debugging and audit trail |
|
||||
| Hardcoded config | **MEDIUM** | ✅ Fixed | Flexible deployment |
|
||||
|
||||
---
|
||||
|
||||
## Next Steps (Recommended)
|
||||
|
||||
### Immediate (Optional)
|
||||
1. Add `.env` to `.gitignore` (prevents committing secrets)
|
||||
2. Configure log rotation for production
|
||||
3. Add rate limiting middleware
|
||||
|
||||
### Future Enhancements
|
||||
1. Authentication/Authorization system
|
||||
2. API key management
|
||||
3. Request rate limiting per IP
|
||||
4. HTTPS enforcement
|
||||
|
||||
---
|
||||
|
||||
## Files Changed
|
||||
|
||||
- ✅ `main.py` - CORS security, filename validation, logging
|
||||
- ✅ `app/download_manager.py` - Logging infrastructure
|
||||
- ✅ `app/utils.py` - NEW: Security utilities
|
||||
- ✅ `app/config.py` - NEW: Configuration management
|
||||
- ✅ `.env` - NEW: Development environment
|
||||
- ✅ `.env.example` - NEW: Environment template
|
||||
- ❌ `app/downloaders/vidmoly_old.py` - DELETED
|
||||
- ❌ `templates/index_old.html` - DELETED
|
||||
|
||||
---
|
||||
|
||||
## Verification
|
||||
|
||||
All changes tested and verified:
|
||||
- ✅ Application starts successfully
|
||||
- ✅ All 23 unit tests pass
|
||||
- ✅ Filename sanitization works correctly
|
||||
- ✅ Configuration loads from environment
|
||||
- ✅ CORS properly restricts origins
|
||||
- ✅ Logging functions properly
|
||||
- ✅ Server runs on port 3000
|
||||
|
||||
**Server Status:** 🟢 Running and ready for production
|
||||
Reference in New Issue
Block a user