Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SafeEscape Backend Environment Variables
# Copy this file to .env and fill in your actual values

# Required Environment Variables
JWT_SECRET=your-super-secure-jwt-secret-at-least-32-characters-long
FIREBASE_PROJECT_ID=your-firebase-project-id
GOOGLE_CLOUD_PROJECT_ID=your-google-cloud-project-id
GEMINI_API_KEY=your-gemini-api-key

# Optional Environment Variables
FIREBASE_CREDENTIALS={"type":"service_account","project_id":"..."}
VERTEXAI_CREDENTIALS={"type":"service_account","project_id":"..."}
GOOGLE_MAPS_API_KEY=your-google-maps-api-key
OPENWEATHER_API_KEY=your-openweather-api-key
MONGODB_URI=mongodb://localhost:27017/safeescape

# Server Configuration
NODE_ENV=development
PORT=5000

# Google Cloud Configuration
GOOGLE_APPLICATION_CREDENTIALS=path/to/your/service-account-key.json
VERTEX_AI_LOCATION=us-central1

# Firebase Configuration (alternative to FIREBASE_CREDENTIALS)
FIREBASE_CLIENT_EMAIL=your-firebase-client-email
FIREBASE_PRIVATE_KEY=your-firebase-private-key
FIREBASE_DATABASE_URL=your-firebase-database-url

# Development/Testing
PUBSUB_EMULATOR_HOST=localhost:8085
FIREBASE_AUTH_EMULATOR_HOST=localhost:9099
132 changes: 122 additions & 10 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,132 @@

## Supported Versions

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

| Version | Supported |
| ------- | ------------------ |
| 5.1.x | :white_check_mark: |
| 5.0.x | :x: |
| 4.0.x | :white_check_mark: |
| < 4.0 | :x: |
| 1.0.x | :white_check_mark: |
| < 1.0 | :x: |

## Security Features

### Current Security Measures

- **Environment Variable Validation**: All required environment variables are validated on startup
- **Secure CORS Configuration**: Environment-specific CORS settings with restrictive production defaults
- **Rate Limiting**: Comprehensive rate limiting for different endpoint types
- **Input Validation**: All user inputs are sanitized and validated
- **Secure File Uploads**: File type validation, size limits, and content verification
- **Error Handling**: Secure error responses that prevent information disclosure
- **Authentication**: JWT-based authentication with proper token validation
- **Security Headers**: Helmet.js for security headers (CSP, XSS protection, etc.)
- **Dependency Security**: Regular npm audit checks and automatic fixes

### Security Headers

The application automatically sets the following security headers:

- `X-Content-Type-Options: nosniff`
- `X-Frame-Options: DENY`
- `X-XSS-Protection: 1; mode=block`
- `Strict-Transport-Security` (in production)

### Rate Limiting

Different endpoints have different rate limits:

- **General API**: 100 requests per 15 minutes
- **Authentication**: 5 requests per 15 minutes
- **Voice API**: 10 requests per 5 minutes
- **File Uploads**: 5 requests per 10 minutes
- **Emergency APIs**: 20 requests per minute

## Reporting a Vulnerability

Use this section to tell people how to report a vulnerability.
### How to Report

If you discover a security vulnerability in SafeEscape, please report it responsibly:

1. **DO NOT** create a public GitHub issue for security vulnerabilities
2. **DO NOT** post security issues in forums or chat rooms
3. **DO** email security reports to: [[email protected]](mailto:[email protected])

### What to Include

Please include the following information in your security report:

- **Description**: Clear description of the vulnerability
- **Steps to Reproduce**: Detailed steps to reproduce the issue
- **Impact**: Potential impact and severity assessment
- **Affected Versions**: Which versions are affected
- **Proof of Concept**: If possible, include a proof of concept (but do not exploit the vulnerability)
- **Suggested Fix**: If you have suggestions for fixing the issue

### Response Timeline

- **Acknowledgment**: We will acknowledge receipt of your report within 48 hours
- **Initial Assessment**: We will provide an initial assessment within 5 business days
- **Status Updates**: We will provide regular updates every 10 business days
- **Resolution**: We aim to resolve critical vulnerabilities within 30 days

### Responsible Disclosure

We follow responsible disclosure practices:

1. **Investigation**: We will investigate and validate the reported vulnerability
2. **Fix Development**: We will develop and test a fix
3. **Coordinated Disclosure**: We will coordinate with you on the disclosure timeline
4. **Public Disclosure**: After the fix is deployed, we will publicly disclose the vulnerability

## Security Best Practices

### For Developers

- Always validate and sanitize user inputs
- Use parameterized queries to prevent SQL injection
- Implement proper authentication and authorization
- Keep dependencies up to date
- Follow secure coding practices
- Use environment variables for sensitive configuration

### For Deployment

- Use strong, unique passwords and API keys
- Enable HTTPS in production
- Set up proper firewall rules
- Monitor logs for suspicious activity
- Regularly update server software
- Use secure environment variable management

### For Users

- Use strong, unique passwords
- Enable two-factor authentication when available
- Keep your applications updated
- Report suspicious activity immediately

## Security Checklist

Before deploying to production, ensure:

- [ ] All environment variables are properly configured
- [ ] CORS is configured for your specific domains
- [ ] Rate limiting is enabled and configured appropriately
- [ ] HTTPS is enabled with valid certificates
- [ ] Security headers are configured
- [ ] Error messages don't leak sensitive information
- [ ] File uploads are properly validated
- [ ] Authentication is working correctly
- [ ] All dependencies are up to date
- [ ] Security monitoring is in place

## Contact

For security-related questions or concerns:

- **Security Team**: [[email protected]](mailto:[email protected])
- **General Support**: [[email protected]](mailto:[email protected])

---

Tell them where to go, how often they can expect to get an update on a
reported vulnerability, what to expect if the vulnerability is accepted or
declined, etc.
**Note**: This security policy is subject to change. Please check back regularly for updates.
113 changes: 113 additions & 0 deletions SECURITY_AUDIT_REPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Security Audit Report - SafeEscape Backend

## Executive Summary
This report documents the security audit conducted on the SafeEscape backend application, identifying critical security vulnerabilities and providing fixes for each issue.

## Critical Security Issues Identified

### 1. **CRITICAL: Missing JWT Secret Environment Variable**
**Severity**: Critical
**Location**: `middleware/auth/auth.js:10`
**Issue**: The application uses `process.env.JWT_SECRET` without validation, which could cause authentication failures if the environment variable is not set.
**Risk**: Authentication bypass, application crashes

### 2. **HIGH: Overly Permissive CORS Configuration**
**Severity**: High
**Location**: `server.js:72`, `server-core.js:37`, `bot/app.js:16`
**Issue**: CORS is configured with `origin: '*'` allowing any domain to make requests
**Risk**: Cross-origin attacks, data theft

### 3. **HIGH: Missing Input Validation**
**Severity**: High
**Location**: Multiple route handlers
**Issue**: No input validation on request bodies, params, or query parameters
**Risk**: Injection attacks, data corruption

### 4. **HIGH: Unsafe JSON.parse() Usage**
**Severity**: High
**Location**: Multiple files including `config/firebase-config.js:23`
**Issue**: JSON.parse() used without try-catch blocks in several places
**Risk**: Application crashes, DoS attacks

### 5. **MEDIUM: Missing Rate Limiting**
**Severity**: Medium
**Location**: Server configuration
**Issue**: No rate limiting implemented despite express-rate-limit being installed
**Risk**: DoS attacks, resource exhaustion

### 6. **MEDIUM: Information Disclosure in Error Messages**
**Severity**: Medium
**Location**: `server.js:254`, error handlers
**Issue**: Detailed error messages exposed in production
**Risk**: Information leakage

### 7. **MEDIUM: Insecure File Upload Configuration**
**Severity**: Medium
**Location**: `bot/app.js:30`, `routes/aiRoutes.js:4`
**Issue**: File uploads without proper validation and sanitization
**Risk**: Malicious file uploads, path traversal

### 8. **LOW: Excessive Console Logging**
**Severity**: Low
**Location**: Multiple files
**Issue**: Sensitive information logged to console
**Risk**: Information disclosure in logs

## Dependency Vulnerabilities
✅ **FIXED**: All npm audit vulnerabilities have been resolved by running `npm audit fix`

## Fixes Applied

### 1. Environment Variable Validation
Created a comprehensive environment validation system.

### 2. Secure CORS Configuration
Implemented environment-specific CORS settings.

### 3. Input Validation Middleware
Added comprehensive input validation.

### 4. Rate Limiting Implementation
Configured rate limiting for API endpoints.

### 5. Secure Error Handling
Implemented secure error responses.

### 6. File Upload Security
Enhanced file upload validation and sanitization.

### 7. Logging Security
Implemented secure logging practices.

## Recommendations

### Immediate Actions Required:
1. Set up proper environment variables for all deployments
2. Configure CORS for specific allowed origins
3. Implement comprehensive input validation
4. Add rate limiting to all API endpoints
5. Review and sanitize all error messages

### Long-term Security Improvements:
1. Implement API authentication for all endpoints
2. Add request/response encryption
3. Set up security monitoring and alerting
4. Regular security audits and penetration testing
5. Implement Content Security Policy (CSP)

## Security Checklist
- [x] Dependency vulnerabilities fixed
- [x] Environment variable validation added
- [x] CORS configuration secured
- [x] Input validation implemented
- [x] Rate limiting configured
- [x] Error handling secured
- [x] File upload validation enhanced
- [x] Logging security improved

## Conclusion
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.

---
*Security Audit completed on: $(date)*
*Next audit recommended: Every 3 months*
Loading
Loading