Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix: Use server-scoped encryption key (n8n pattern) instead of password-derived
trohitg6ed686ccec06
diff --git a/CLAUDE.md b/CLAUDE.mdindex 721ddb02..986c85d6 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -2562,15 +2562,17 @@ email-validator>=2.0.0## Encrypted Credentials System### Overview-API keys and OAuth tokens are stored in a separate encrypted database (`credentials.db`) using Fernet encryption (AES-128-CBC + HMAC-SHA256). The encryption key is derived from the user's login password using PBKDF2, ensuring credentials are only accessible during an authenticated session.+API keys and OAuth tokens are stored in a separate encrypted database (`credentials.db`) using Fernet encryption (AES-128-CBC + HMAC-SHA256). Following the n8n pattern, the encryption key is derived from a server-scoped config key (`API_KEY_ENCRYPTION_KEY` in `.env`) using PBKDF2, initialized at startup and persisting across restarts.### Security Architecture```-User Login (password)+Server Startup↓-PBKDF2HMAC (SHA256, 600K iterations) + Salt+API_KEY_ENCRYPTION_KEY (from .env) + Salt (from credentials.db)↓-Fernet Key (in-memory only)+PBKDF2HMAC (SHA256, 600K iterations)+ ↓+Fernet Key (in-memory for application lifetime)↓EncryptionService.encrypt()/decrypt()↓@@ -2578,9 +2580,9 @@ credentials.db (encrypted ciphertext)```**Key Security Properties:**-- Encryption key derived from user password (never stored)-- Key exists only in memory during authenticated session-- Cleared on logout via `EncryptionService.clear()`+- Server-scoped encryption key from `API_KEY_ENCRYPTION_KEY` in `.env` (n8n pattern)+- Key initialized at startup, persists across application lifetime+- Not tied to user sessions -- survives server restarts with valid JWT- Salt stored in credentials database (not the main database)- OWASP 2024 compliant: 600,000 PBKDF2 iterationsdiff --git a/server/core/encryption.py b/server/core/encryption.pyindex 118f600b..c0878ab8 100644--- a/server/core/encryption.py+++ b/server/core/encryption.py@@ -1,12 +1,13 @@"""Field-level encryption using Fernet (AES-128-CBC + HMAC-SHA256).This module provides secure encryption for API keys and OAuth tokens stored in-the credentials database. Uses PBKDF2HMAC for key derivation from user password.+the credentials database. Uses PBKDF2HMAC for key derivation from a server-scoped+encryption key (API_KEY_ENCRYPTION_KEY in .env), following the n8n pattern.Security:- Fernet: AES-128-CBC with PKCS7 padding + HMAC-SHA256 for authentication- PBKDF2: SHA256, 600,000 iterations (OWASP 2024 recommendation)-- Key derived from user's login password, stored only in memory+- Key derived from server config, initialized at startup, persists across restarts"""import base64@@ -23,20 +24,19 @@ logger = logging.getLogger(__name__)class EncryptionService:"""- Fernet-based encryption service with key derivation from user password.+ Fernet-based encryption service with PBKDF2 key derivation.- The encryption key is derived from the user's login password using PBKDF2- and stored only in memory. The key is cleared on logout.+ Uses a server-scoped encryption key (API_KEY_ENCRYPTION_KEY from .env)+ following the n8n pattern. Initialized once at startup, persists across+ the application lifecycle. Not tied to user sessions.Usage:encryption = EncryptionService()salt = await credentials_db.get_or_create_salt()- encryption.initialize(user_password, salt)+ encryption.initialize(server_key, salt)encrypted = encryption.encrypt("my-api-key")decrypted = encryption.decrypt(encrypted)-- encryption.clear() # On logout"""# OWASP recommended iterations for PBKDF2-SHA256 (2024)@@ -49,11 +49,11 @@ class EncryptionService:def derive_key_from_password(self, password: str, salt: bytes) -> bytes:"""- Derive Fernet-compatible key from password using PBKDF2.+ Derive Fernet-compatible key from passphrase using PBKDF2.Args:- password: User's login password- salt: Random salt (should be stored in credentials DB)+ password: Server-scoped encryption key (API_KEY_ENCRYPTION_KEY)+ salt: Random salt (stored in credentials DB)Returns:Base64-encoded 32-byte key suitable for Fernet@@ -70,13 +70,12 @@ class EncryptionService:def initialize(self, password: str, salt: bytes) -> None:"""- Initialize Fernet cipher with key derived from password.+ Initialize Fernet cipher with key derived from passphrase.- Must be called after successful user login before any encrypt/decrypt- operations. The derived key is stored only in memory.+ Called once at server startup. The derived key is stored only in memory.Args:- password: User's login password (plaintext)+ password: Server-scoped encryption key (API_KEY_ENCRYPTION_KEY)salt: Random salt from credentials database"""key = self.derive_key_from_password(password, salt)@@ -98,7 +97,7 @@ class EncryptionService:RuntimeError: If encryption service not initialized"""if not self._fernet:- raise RuntimeError("Encryption service not initialized - user must be logged in")+ raise RuntimeError("Encryption service not initialized - check server startup")token = self._fernet.encrypt(plaintext.encode())return token.decode() # Fernet tokens are already base64@@ -117,7 +116,7 @@ class EncryptionService:ValueError: If decryption fails (wrong key or corrupted data)"""if not self._fernet:- raise RuntimeError("Encryption service not initialized - user must be logged in")+ raise RuntimeError("Encryption service not initialized - check server startup")try:plaintext = self._fernet.decrypt(ciphertext.encode())return plaintext.decode()@@ -133,8 +132,8 @@ class EncryptionService:"""Clear encryption key from memory.- Should be called on user logout to ensure keys don't persist- in memory longer than necessary.+ Generally not needed in normal operation since the key persists+ for the application lifecycle (server-scoped, not session-scoped)."""self._fernet = Noneself._salt = Nonediff --git a/server/main.py b/server/main.pyindex e38c6f44..3d7362c1 100644--- a/server/main.py+++ b/server/main.py@@ -75,14 +75,12 @@ async def lifespan(app: FastAPI):salt = await credentials_db.initialize()logger.info("Credentials database initialized")- # When auth is disabled, auto-initialize encryption with a default key- # This allows saving API keys without user login- if settings.vite_auth_enabled == "false":- encryption = container.encryption_service()- if not encryption.is_initialized():- # Use api_key_encryption_key as the default password- encryption.initialize(settings.api_key_encryption_key, salt)- logger.info("Encryption service auto-initialized (auth disabled)")+ # Initialize encryption with server-scoped key (n8n pattern)+ # Key from .env persists across restarts, not tied to user sessions+ encryption = container.encryption_service()+ if not encryption.is_initialized():+ encryption.initialize(settings.api_key_encryption_key, salt)+ logger.info("Encryption service initialized")# Initialize event waiter with cache service for Redis Streams supportfrom services import event_waiterdiff --git a/server/services/user_auth.py b/server/services/user_auth.pyindex 9bf40129..bffccb46 100644--- a/server/services/user_auth.py+++ b/server/services/user_auth.py@@ -17,12 +17,7 @@ logger = logging.getLogger(__name__)class UserAuthService:- """Handles user authentication, registration, and JWT token management.-- On login, this service initializes the EncryptionService with a key derived- from the user's password. This enables decryption of API keys and OAuth tokens- stored in the separate credentials database.- """+ """Handles user authentication, registration, and JWT token management."""def __init__(self,@@ -107,10 +102,6 @@ class UserAuthService:logger.info(f"User registered: {email} (owner={is_owner})")- # Initialize encryption for the new user- # This ensures credentials can be saved immediately after registration- await self._initialize_encryption(password)-return user, Noneasync def login(@@ -119,9 +110,6 @@ class UserAuthService:"""Authenticate user and return user object.Returns (user, None) on success, (None, error_message) on failure.-- On successful login, initializes the EncryptionService with a key- derived from the user's password for decrypting stored credentials."""user = await self.get_user_by_email(email)if not user:@@ -141,35 +129,12 @@ class UserAuthService:db_user.last_login = datetime.now(timezone.utc)await session.commit()- # Initialize encryption with user's password- await self._initialize_encryption(password)-logger.info(f"User logged in: {email}")return user, None- async def _initialize_encryption(self, password: str) -> None:- """- Initialize encryption service with key derived from password.-- Gets or creates the salt from the credentials database and uses it- with the password to derive the encryption key via PBKDF2.- """- # Ensure credentials database is initialized and get salt- salt = await self.credentials_db.initialize()-- # Initialize encryption service with derived key- self.encryption.initialize(password, salt)- logger.debug("Encryption service initialized for user session")-def logout(self) -> None:- """- Clear encryption key on logout.-- Clears the encryption service's in-memory key to ensure credentials- cannot be decrypted after user logout.- """- self.encryption.clear()- logger.debug("Encryption service cleared on logout")+ """Log out user. Encryption key persists (server-scoped, not session-scoped)."""+ logger.debug("User logged out")def create_access_token(self, user: User) -> str:"""Create JWT access token for user."""