[CRITICAL] Fix OpenRouter API key fallback to prevent silent configuration#856
Conversation
…ation failures - Remove dummy-key fallback in chatRoutes.js OpenAI client initialization - Make OPENROUTER_API_KEY required in backend/utils/env.js validation - Make OPENROUTER_API_KEY required in backend/config.js validation - Add explicit API key validation at module initialization in chatRoutes.js - Add comprehensive error handling for OpenAI API errors (401, 429, 500, 502, 503) - Add response structure validation to prevent crashes on malformed responses - Add security documentation explaining the vulnerability and fix - Import HttpError for proper error handling This fix prevents the application from starting with invalid AI API configuration, eliminating the risk of silent configuration failures, potential API abuse, and unauthorized access. The server now fails explicitly with clear error messages if OPENROUTER_API_KEY is not configured.
|
Someone is attempting to deploy a commit to the durdana3105's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThis PR enforces OPENROUTER_API_KEY as a required environment variable, removes the dummy-key fallback from the chat router initialization, and adds comprehensive error handling to the ChangesAPI Key Requirement and Error Handling
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
@durdana3105 I have made pr , review it and merge |
Security Fix Implementation Plan
OpenRouter API Key Fallback Vulnerability
Executive Summary
Vulnerability: Critical configuration security issue with silent API key fallback
Severity: Critical
Status: ✅ Implemented
Branch:
api/fixFiles Modified: 3 files (68 insertions, 11 deletions)
1. Vulnerability Description
1.1 Issue
The OpenAI client initialization in backend/routers/chatRoutes.js used a fallback to "dummy-key" when the
OPENROUTER_API_KEYenvironment variable was not configured. This silent fallback allowed the application to start with invalid configuration, potentially enabling:1.2 Attack Vector
OPENROUTER_API_KEYenvironment variable is missing or misconfigured in production1.3 Impact
2. Solution Approach
2.1 Strategy
Implement defense-in-depth approach with multiple validation layers:
2.2 Design Principles
3. Detailed Changes
3.1 File: backend/utils/env.js
Change 1: Make OPENROUTER_API_KEY Required
Before:
After:
Changes:
z.string().optional()toz.string().min(1)OPENROUTER_API_KEYis not set3.2 File: backend/config.js
Change 2: Make OPENROUTER_API_KEY Required
Before:
After:
Changes:
z.string().optional()toz.string().min(1)3.3 File: backend/routers/chatRoutes.js
Change 3: Remove Dummy-Key Fallback
Before:
After:
Changes:
|| "dummy-key"fallbackChange 4: Add HttpError Import
Before:
After:
Changes:
HttpErrorimport for proper error handlingChange 5: Add Comprehensive Error Handling
Before:
After:
Changes:
4. Security Improvements
4.1 Configuration Validation
4.2 Module-Level Validation
4.3 Error Handling
4.4 Response Validation
5. Testing Recommendations
5.1 Configuration Tests
5.2 Integration Tests
5.3 Error Handling Tests
6. Deployment Considerations
6.1 Prerequisites
OPENROUTER_API_KEYenvironment variable must be set6.2 Rollout Strategy
6.3 Monitoring
/api/chat7. Rollback Plan
7.1 Rollback Procedure
If issues arise after deployment:
7.2 Rollback Triggers
8. Future Enhancements
8.1 Additional Security Measures
8.2 Performance Optimizations
8.3 Operational Improvements
9. Code Review Checklist
10. Summary
This fix addresses a critical configuration security vulnerability by removing the silent fallback to "dummy-key" and implementing multiple layers of validation. The server now fails explicitly with clear error messages if the
OPENROUTER_API_KEYis not configured, preventing silent configuration failures, potential API abuse, and unauthorized access. The implementation includes comprehensive error handling, response validation, and security documentation, making the codebase more robust and maintainable.Fixes : #835
Summary by CodeRabbit
Bug Fixes
Chores