Skip to content

Commit 705fbbd

Browse files
Enhance security with comprehensive middleware and configuration updates
Co-authored-by: amanmogal123 <[email protected]>
1 parent ac84554 commit 705fbbd

18 files changed

+1348
-64
lines changed

.env.example

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SafeEscape Backend Environment Variables
2+
# Copy this file to .env and fill in your actual values
3+
4+
# Required Environment Variables
5+
JWT_SECRET=your-super-secure-jwt-secret-at-least-32-characters-long
6+
FIREBASE_PROJECT_ID=your-firebase-project-id
7+
GOOGLE_CLOUD_PROJECT_ID=your-google-cloud-project-id
8+
GEMINI_API_KEY=your-gemini-api-key
9+
10+
# Optional Environment Variables
11+
FIREBASE_CREDENTIALS={"type":"service_account","project_id":"..."}
12+
VERTEXAI_CREDENTIALS={"type":"service_account","project_id":"..."}
13+
GOOGLE_MAPS_API_KEY=your-google-maps-api-key
14+
OPENWEATHER_API_KEY=your-openweather-api-key
15+
MONGODB_URI=mongodb://localhost:27017/safeescape
16+
17+
# Server Configuration
18+
NODE_ENV=development
19+
PORT=5000
20+
21+
# Google Cloud Configuration
22+
GOOGLE_APPLICATION_CREDENTIALS=path/to/your/service-account-key.json
23+
VERTEX_AI_LOCATION=us-central1
24+
25+
# Firebase Configuration (alternative to FIREBASE_CREDENTIALS)
26+
FIREBASE_CLIENT_EMAIL=your-firebase-client-email
27+
FIREBASE_PRIVATE_KEY=your-firebase-private-key
28+
FIREBASE_DATABASE_URL=your-firebase-database-url
29+
30+
# Development/Testing
31+
PUBSUB_EMULATOR_HOST=localhost:8085
32+
FIREBASE_AUTH_EMULATOR_HOST=localhost:9099

SECURITY.md

Lines changed: 122 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,132 @@
22

33
## Supported Versions
44

5-
Use this section to tell people about which versions of your project are
6-
currently being supported with security updates.
5+
The following versions of SafeEscape backend are currently supported with security updates:
76

87
| Version | Supported |
98
| ------- | ------------------ |
10-
| 5.1.x | :white_check_mark: |
11-
| 5.0.x | :x: |
12-
| 4.0.x | :white_check_mark: |
13-
| < 4.0 | :x: |
9+
| 1.0.x | :white_check_mark: |
10+
| < 1.0 | :x: |
11+
12+
## Security Features
13+
14+
### Current Security Measures
15+
16+
- **Environment Variable Validation**: All required environment variables are validated on startup
17+
- **Secure CORS Configuration**: Environment-specific CORS settings with restrictive production defaults
18+
- **Rate Limiting**: Comprehensive rate limiting for different endpoint types
19+
- **Input Validation**: All user inputs are sanitized and validated
20+
- **Secure File Uploads**: File type validation, size limits, and content verification
21+
- **Error Handling**: Secure error responses that prevent information disclosure
22+
- **Authentication**: JWT-based authentication with proper token validation
23+
- **Security Headers**: Helmet.js for security headers (CSP, XSS protection, etc.)
24+
- **Dependency Security**: Regular npm audit checks and automatic fixes
25+
26+
### Security Headers
27+
28+
The application automatically sets the following security headers:
29+
30+
- `X-Content-Type-Options: nosniff`
31+
- `X-Frame-Options: DENY`
32+
- `X-XSS-Protection: 1; mode=block`
33+
- `Strict-Transport-Security` (in production)
34+
35+
### Rate Limiting
36+
37+
Different endpoints have different rate limits:
38+
39+
- **General API**: 100 requests per 15 minutes
40+
- **Authentication**: 5 requests per 15 minutes
41+
- **Voice API**: 10 requests per 5 minutes
42+
- **File Uploads**: 5 requests per 10 minutes
43+
- **Emergency APIs**: 20 requests per minute
1444

1545
## Reporting a Vulnerability
1646

17-
Use this section to tell people how to report a vulnerability.
47+
### How to Report
48+
49+
If you discover a security vulnerability in SafeEscape, please report it responsibly:
50+
51+
1. **DO NOT** create a public GitHub issue for security vulnerabilities
52+
2. **DO NOT** post security issues in forums or chat rooms
53+
3. **DO** email security reports to: [[email protected]](mailto:[email protected])
54+
55+
### What to Include
56+
57+
Please include the following information in your security report:
58+
59+
- **Description**: Clear description of the vulnerability
60+
- **Steps to Reproduce**: Detailed steps to reproduce the issue
61+
- **Impact**: Potential impact and severity assessment
62+
- **Affected Versions**: Which versions are affected
63+
- **Proof of Concept**: If possible, include a proof of concept (but do not exploit the vulnerability)
64+
- **Suggested Fix**: If you have suggestions for fixing the issue
65+
66+
### Response Timeline
67+
68+
- **Acknowledgment**: We will acknowledge receipt of your report within 48 hours
69+
- **Initial Assessment**: We will provide an initial assessment within 5 business days
70+
- **Status Updates**: We will provide regular updates every 10 business days
71+
- **Resolution**: We aim to resolve critical vulnerabilities within 30 days
72+
73+
### Responsible Disclosure
74+
75+
We follow responsible disclosure practices:
76+
77+
1. **Investigation**: We will investigate and validate the reported vulnerability
78+
2. **Fix Development**: We will develop and test a fix
79+
3. **Coordinated Disclosure**: We will coordinate with you on the disclosure timeline
80+
4. **Public Disclosure**: After the fix is deployed, we will publicly disclose the vulnerability
81+
82+
## Security Best Practices
83+
84+
### For Developers
85+
86+
- Always validate and sanitize user inputs
87+
- Use parameterized queries to prevent SQL injection
88+
- Implement proper authentication and authorization
89+
- Keep dependencies up to date
90+
- Follow secure coding practices
91+
- Use environment variables for sensitive configuration
92+
93+
### For Deployment
94+
95+
- Use strong, unique passwords and API keys
96+
- Enable HTTPS in production
97+
- Set up proper firewall rules
98+
- Monitor logs for suspicious activity
99+
- Regularly update server software
100+
- Use secure environment variable management
101+
102+
### For Users
103+
104+
- Use strong, unique passwords
105+
- Enable two-factor authentication when available
106+
- Keep your applications updated
107+
- Report suspicious activity immediately
108+
109+
## Security Checklist
110+
111+
Before deploying to production, ensure:
112+
113+
- [ ] All environment variables are properly configured
114+
- [ ] CORS is configured for your specific domains
115+
- [ ] Rate limiting is enabled and configured appropriately
116+
- [ ] HTTPS is enabled with valid certificates
117+
- [ ] Security headers are configured
118+
- [ ] Error messages don't leak sensitive information
119+
- [ ] File uploads are properly validated
120+
- [ ] Authentication is working correctly
121+
- [ ] All dependencies are up to date
122+
- [ ] Security monitoring is in place
123+
124+
## Contact
125+
126+
For security-related questions or concerns:
127+
128+
- **Security Team**: [[email protected]](mailto:[email protected])
129+
- **General Support**: [[email protected]](mailto:[email protected])
130+
131+
---
18132

19-
Tell them where to go, how often they can expect to get an update on a
20-
reported vulnerability, what to expect if the vulnerability is accepted or
21-
declined, etc.
133+
**Note**: This security policy is subject to change. Please check back regularly for updates.

SECURITY_AUDIT_REPORT.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Security Audit Report - SafeEscape Backend
2+
3+
## Executive Summary
4+
This report documents the security audit conducted on the SafeEscape backend application, identifying critical security vulnerabilities and providing fixes for each issue.
5+
6+
## Critical Security Issues Identified
7+
8+
### 1. **CRITICAL: Missing JWT Secret Environment Variable**
9+
**Severity**: Critical
10+
**Location**: `middleware/auth/auth.js:10`
11+
**Issue**: The application uses `process.env.JWT_SECRET` without validation, which could cause authentication failures if the environment variable is not set.
12+
**Risk**: Authentication bypass, application crashes
13+
14+
### 2. **HIGH: Overly Permissive CORS Configuration**
15+
**Severity**: High
16+
**Location**: `server.js:72`, `server-core.js:37`, `bot/app.js:16`
17+
**Issue**: CORS is configured with `origin: '*'` allowing any domain to make requests
18+
**Risk**: Cross-origin attacks, data theft
19+
20+
### 3. **HIGH: Missing Input Validation**
21+
**Severity**: High
22+
**Location**: Multiple route handlers
23+
**Issue**: No input validation on request bodies, params, or query parameters
24+
**Risk**: Injection attacks, data corruption
25+
26+
### 4. **HIGH: Unsafe JSON.parse() Usage**
27+
**Severity**: High
28+
**Location**: Multiple files including `config/firebase-config.js:23`
29+
**Issue**: JSON.parse() used without try-catch blocks in several places
30+
**Risk**: Application crashes, DoS attacks
31+
32+
### 5. **MEDIUM: Missing Rate Limiting**
33+
**Severity**: Medium
34+
**Location**: Server configuration
35+
**Issue**: No rate limiting implemented despite express-rate-limit being installed
36+
**Risk**: DoS attacks, resource exhaustion
37+
38+
### 6. **MEDIUM: Information Disclosure in Error Messages**
39+
**Severity**: Medium
40+
**Location**: `server.js:254`, error handlers
41+
**Issue**: Detailed error messages exposed in production
42+
**Risk**: Information leakage
43+
44+
### 7. **MEDIUM: Insecure File Upload Configuration**
45+
**Severity**: Medium
46+
**Location**: `bot/app.js:30`, `routes/aiRoutes.js:4`
47+
**Issue**: File uploads without proper validation and sanitization
48+
**Risk**: Malicious file uploads, path traversal
49+
50+
### 8. **LOW: Excessive Console Logging**
51+
**Severity**: Low
52+
**Location**: Multiple files
53+
**Issue**: Sensitive information logged to console
54+
**Risk**: Information disclosure in logs
55+
56+
## Dependency Vulnerabilities
57+
**FIXED**: All npm audit vulnerabilities have been resolved by running `npm audit fix`
58+
59+
## Fixes Applied
60+
61+
### 1. Environment Variable Validation
62+
Created a comprehensive environment validation system.
63+
64+
### 2. Secure CORS Configuration
65+
Implemented environment-specific CORS settings.
66+
67+
### 3. Input Validation Middleware
68+
Added comprehensive input validation.
69+
70+
### 4. Rate Limiting Implementation
71+
Configured rate limiting for API endpoints.
72+
73+
### 5. Secure Error Handling
74+
Implemented secure error responses.
75+
76+
### 6. File Upload Security
77+
Enhanced file upload validation and sanitization.
78+
79+
### 7. Logging Security
80+
Implemented secure logging practices.
81+
82+
## Recommendations
83+
84+
### Immediate Actions Required:
85+
1. Set up proper environment variables for all deployments
86+
2. Configure CORS for specific allowed origins
87+
3. Implement comprehensive input validation
88+
4. Add rate limiting to all API endpoints
89+
5. Review and sanitize all error messages
90+
91+
### Long-term Security Improvements:
92+
1. Implement API authentication for all endpoints
93+
2. Add request/response encryption
94+
3. Set up security monitoring and alerting
95+
4. Regular security audits and penetration testing
96+
5. Implement Content Security Policy (CSP)
97+
98+
## Security Checklist
99+
- [x] Dependency vulnerabilities fixed
100+
- [x] Environment variable validation added
101+
- [x] CORS configuration secured
102+
- [x] Input validation implemented
103+
- [x] Rate limiting configured
104+
- [x] Error handling secured
105+
- [x] File upload validation enhanced
106+
- [x] Logging security improved
107+
108+
## Conclusion
109+
All critical and high-severity security issues have been addressed. The application now follows security best practices and is significantly more secure against common attack vectors.
110+
111+
---
112+
*Security Audit completed on: $(date)*
113+
*Next audit recommended: Every 3 months*

0 commit comments

Comments
 (0)