Moved 25 files from root to organized subdirectories while keeping essential scripts (install.sh, setup.sh, start*.sh, stop*.sh) in root. ## Changes: ### New Directory Structure: - Created `/scripts/` for utility scripts - Created `/docs/guides/` for user guides - Created `/docs/development/` for dev documentation - Created `/docs/archive/` for historical/legacy docs ### Moved Files: **To `/scripts/` (7 files):** - backup_claude_os.sh - restore_claude_os.sh - run_tests.sh - setup_native.sh - start_mcp_server.sh - start_redis_workers.sh - entrypoint.sh **To `/docs/guides/` (7 files):** - AUTH_SETUP.md - BACKUP_RESTORE_GUIDE.md - HOOK_SETUP_QUICK_START.md - SHARING_GUIDE.md - START_SCRIPTS_GUIDE.md - PORTS_GUIDE.md - WHAT_IS_CLAUDE_OS.md **To `/docs/development/` (4 files):** - COMPREHENSIVE_REVIEW.md - SECURITY_FIXES.md - CLAUDE_OS_DEVELOPER_GUIDE.md - PROJECT_SETUP_AND_COMMANDS.md **To `/docs/archive/` (10 files):** - HYBRID_INDEXING_COMPLETE.md - INITIALIZE_PROJECT_HOOKS_FIX.md - INSTALL_ENHANCEMENTS.md - INSTALL_FIXES.md - PISTN_TEST_RESULTS.md - SESSION_SUMMARY.md - TEST_COVERAGE.md - TEST_RESULTS.md - TESTING_STATUS.md - README_TESTING.md ### Updated References: **Fixed broken documentation links in:** - docs/QUICK_REFERENCE.md - docs/PRODUCTION_DEPLOYMENT.md - docs/SCRIPTS_GUIDE.md - docs/development/SECURITY_FIXES.md - docs/guides/BACKUP_RESTORE_GUIDE.md - docs/development/CLAUDE_OS_DEVELOPER_GUIDE.md - tests/TESTING_GUIDE.md - tests/TEST_COVERAGE.md **Script references updated to:** - `./scripts/backup_claude_os.sh` - `./scripts/restore_claude_os.sh` - `./scripts/run_tests.sh` - `./scripts/setup_native.sh` - `./scripts/start_mcp_server.sh` - `./scripts/start_redis_workers.sh` ### Remaining in Root (9 essential files): - install.sh - setup.sh - start.sh - start_all_services.sh - stop_all_services.sh - restart_services.sh - README.md - CLAUDE.md - requirements.txt (+ GEMINI_DIAGRAM_PROMPT.md) ## Benefits: ✅ Cleaner root directory (9 files vs 34 files) ✅ Organized documentation by purpose ✅ Scripts grouped in dedicated directory ✅ Essential startup commands still in root ✅ All references updated and verified ✅ No broken links Version: 2.0.2
9 KiB
🔒 Security Fixes & Improvements
Date: 2025-11-04 Version: 2.0.1 Severity: CRITICAL → SECURE
🚨 Critical Security Vulnerabilities Fixed
1. SQL Injection Vulnerability ⚠️ CRITICAL
Status: ✅ FIXED
Location: mcp_server/server.py (lines 666, 679)
Problem:
# BEFORE: PostgreSQL-style parameterized queries (WRONG for SQLite!)
cur.execute(
"SELECT id FROM knowledge_bases WHERE name = %s",
(kb_name,)
)
Impact:
- SQL injection attacks possible
- Malicious queries could corrupt or expose database
- Authentication bypass potential
Solution:
# AFTER: SQLite-style parameterized queries
cur.execute(
"SELECT id FROM knowledge_bases WHERE name = ?",
(kb_name,)
)
Files Changed:
mcp_server/server.py:- Line 666: Changed
%s→? - Line 679: Changed
%s→?andmetadata->>'filename'→json_extract(metadata, '$.filename')
- Line 666: Changed
2. CORS Security Risk ⚠️ CRITICAL
Status: ✅ FIXED
Location: mcp_server/server.py (lines 51-57)
Problem:
# BEFORE: Allows requests from ANY origin!
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # ❌ DANGEROUS!
allow_credentials=True,
)
Impact:
- Cross-Site Request Forgery (CSRF) attacks
- Malicious websites can make authenticated requests
- API credentials exposed to any origin
Solution:
# AFTER: Restricted to specific origins
allowed_origins_str = os.getenv(
"ALLOWED_ORIGINS",
"http://localhost:5173,http://localhost:8051,http://127.0.0.1:5173,http://127.0.0.1:8051"
)
allowed_origins = [origin.strip() for origin in allowed_origins_str.split(",") if origin.strip()]
app.add_middleware(
CORSMiddleware,
allow_origins=allowed_origins, # ✅ SECURE!
allow_credentials=True,
)
Configuration:
- Added
ALLOWED_ORIGINSenvironment variable - Updated
.env.examplewith secure defaults - Production deployments should set their specific domains
Files Changed:
mcp_server/server.py: Added configurable CORS middleware.env.example: AddedALLOWED_ORIGINSconfiguration
🛡️ Additional Security Enhancements
3. Rate Limiting Protection
Status: ✅ ADDED
Feature: API rate limiting to prevent abuse and DoS attacks
Implementation:
# Added slowapi for rate limiting
from slowapi import Limiter, _rate_limit_exceeded_handler
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
@app.post("/api/kb/{kb_name}/chat")
@limiter.limit("20/minute") # 20 queries per minute per IP
async def api_chat(...):
...
Protection:
- Prevents DoS attacks
- Limits resource exhaustion
- 20 queries per minute per IP for chat endpoints
Files Changed:
requirements.txt: Addedslowapi>=0.1.9mcp_server/server.py: Added rate limiting middleware and decorators
4. Environment Variable Validation
Status: ✅ ADDED
Feature: Startup validation of critical configuration
Implementation:
# In Config class
@classmethod
def validate_config(cls) -> None:
"""Validate required configuration and environment variables."""
errors = []
# Validate Ollama host, database path, ports, RAG config, etc.
if not cls.OLLAMA_HOST:
errors.append("OLLAMA_HOST must be set")
# ... more validations ...
if errors:
error_msg = "Configuration validation failed:\n" + "\n".join(f" - {e}" for e in errors)
raise ValueError(error_msg)
Benefits:
- Early detection of configuration errors
- Prevents silent failures
- Clear error messages for debugging
Files Changed:
app/core/config.py: Addedvalidate_config()methodmcp_server/server.py: Call validation inmain()
🔧 Code Quality Improvements
5. Removed Hardcoded User Paths
Status: ✅ FIXED
Location: frontend/src/pages/MainApp.tsx
Before:
const [newProjectPath, setNewProjectPath] = useState('/Users/iamanmp/Projects');
After:
const [newProjectPath, setNewProjectPath] = useState(''); // User selects via DirectoryPicker
Impact: Now works for all users, not just "iamanmp"
6. Removed Debug Console Logs
Status: ✅ FIXED
Removed from:
frontend/src/pages/MainApp.tsx: Line 65frontend/src/components/DirectoryPicker.tsx: Lines 41, 48
Why: Production code should not contain debug logs
7. Made Reranker Configurable
Status: ✅ IMPROVED
Before: Hardcoded disabled with comment "enable in code if needed"
After:
# Configurable via environment variable
ENABLE_RERANKER = os.getenv("ENABLE_RERANKER", "false").lower() == "true"
if Config.ENABLE_RERANKER:
self.reranker = SentenceTransformerRerank(...)
Configuration:
# .env
ENABLE_RERANKER=false # Set to true to enable
RERANK_MODEL=cross-encoder/mmarco-mMiniLMv2-L12-H384
RERANK_TOP_K=10
Files Changed:
app/core/config.py: AddedENABLE_RERANKERconfigapp/core/rag_engine.py: Made reranker conditional.env.example: Added reranker configuration
8. Fixed Typo in Main Instructions
Status: ✅ FIXED
File: CLAUDE.md
Before: "You are the greates AI coding assitant that there ever way"
After: "You are the greatest AI coding assistant that there ever was"
📊 Security Status Summary
| Issue | Severity | Status | Files Changed |
|---|---|---|---|
| SQL Injection | CRITICAL | ✅ FIXED | mcp_server/server.py |
| CORS Vulnerability | CRITICAL | ✅ FIXED | mcp_server/server.py, .env.example |
| Rate Limiting | HIGH | ✅ ADDED | mcp_server/server.py, requirements.txt |
| Config Validation | MEDIUM | ✅ ADDED | app/core/config.py, mcp_server/server.py |
| Hardcoded Paths | LOW | ✅ FIXED | frontend/src/pages/MainApp.tsx |
| Debug Logs | LOW | ✅ FIXED | frontend/src (multiple files) |
🚀 Deployment Recommendations
For Production Deployments:
-
Set ALLOWED_ORIGINS to your specific domain(s):
ALLOWED_ORIGINS=https://yourdomain.com,https://api.yourdomain.com -
Enable HTTPS: Never run in production without TLS/SSL
-
Configure Rate Limits: Adjust based on your usage patterns
-
Enable Reranker (if performance allows):
ENABLE_RERANKER=true -
Set Strong Authentication:
CLAUDE_OS_EMAIL=admin@yourdomain.com CLAUDE_OS_PASSWORD=<use-strong-password> -
Validate Configuration on startup (now automatic)
-
Monitor Logs for security events:
tail -f logs/mcp_server.log | grep "rate limit\|failed\|error"
🔐 Security Best Practices
Implemented ✅
- SQL injection prevention (parameterized queries)
- CORS restrictions (origin whitelist)
- Rate limiting (DoS protection)
- Configuration validation
- No debug logs in production
- No hardcoded credentials
Recommended for Future 📋
- Database migrations with Alembic
- HTTPS/TLS certificate setup
- API authentication tokens with expiration
- Audit logging for security events
- Regular security updates
- Dependency vulnerability scanning
📝 Testing Security Fixes
SQL Injection Testing:
# Test with malicious input (should be safely escaped)
curl -X POST http://localhost:8051/api/kb/test' OR '1'='1/chat \
-H "Content-Type: application/json" \
-d '{"query": "test"}'
# Should return 404 (KB not found) not a SQL error
CORS Testing:
# Test from unauthorized origin (should fail)
curl -X GET http://localhost:8051/api/kb \
-H "Origin: https://evil.com"
# Should return CORS error or no Access-Control-Allow-Origin header
Rate Limiting Testing:
# Send 25 requests rapidly (should get rate limited after 20)
for i in {1..25}; do
curl -X POST http://localhost:8051/api/kb/test/chat \
-H "Content-Type: application/json" \
-d '{"query": "test"}'
done
# Should get 429 Too Many Requests after 20 requests
📚 Related Documentation
- Comprehensive Review - Full analysis of findings
- API Reference - Complete API documentation
- Auth Setup Guide - Authentication configuration
- Production Deployment - Deployment best practices
✅ Verification Checklist
Before deploying, verify:
- All SQL queries use
?placeholders (not%s) ALLOWED_ORIGINSis set to production domains- Rate limiting is configured appropriately
- Configuration validation passes on startup
- No console.log statements in frontend
- HTTPS/TLS is configured
- Strong authentication credentials are set
- Database backups are configured
- Monitoring and alerting are set up
Security Contact: For security issues, please review the codebase or consult security documentation.
Last Updated: 2025-11-04 Version: 2.0.1