Environment File Security
Protecting the .env File
The.env file contains sensitive configuration including database credentials, API keys, and encryption keys.
Critical Actions:
Environment Variables Checklist
Ensure these are properly configured in production:Generating Secure Keys
Database Security
Database User Permissions
Create a dedicated database user with minimal privileges:Database Connection Security
If database is on a separate server:- Use SSL/TLS for database connections
- Configure firewall to allow only application server IP
- Use strong authentication (certificates when possible)
Protecting Against SQL Injection
Laravel’s Eloquent ORM and Query Builder provide automatic protection: Safe (parameterized queries):HTTPS/SSL Enforcement
Force HTTPS in Application
Laravel automatically enforces HTTPS whenAPP_URL uses https://.
Additional enforcement in app/Providers/AppServiceProvider.php:
HTTP Strict Transport Security (HSTS)
Already configured in the Nginx example, but verify:SSL Certificate Best Practices
- Use TLS 1.2 or TLS 1.3 only (disable older versions)
- Keep certificates up to date (Let’s Encrypt auto-renews every 60 days)
- Use strong cipher suites
- Enable OCSP stapling for performance
CSRF Protection
Laravel provides automatic CSRF protection for all POST, PUT, PATCH, and DELETE requests.How It Works
- Laravel generates a unique token for each user session
- The token must be included in forms and AJAX requests
- Requests without valid tokens are rejected
Implementation with Inertia
Inertia automatically handles CSRF tokens. Ensure the middleware is active inbootstrap/app.php:
Manual CSRF Token Usage
For custom forms:XSS Prevention
Laravel and React provide automatic XSS protection.Blade Templates (if used)
Laravel’s Blade engine escapes output by default:React Components
React escapes content by default:Content Security Policy (CSP)
Add CSP headers in Nginx:'unsafe-eval'. For production, you may tighten this.
File Upload Security
Validation Rules
Always validate uploaded files:Secure File Storage
Store uploaded files outside the public directory:File Type Validation
Don’t rely solely on file extensions. Validate MIME types:Prevent File Execution
Ensure storage directories are not executable:Authentication Security
Laravel Fortify Configuration
Nguhöe EHR uses Laravel Fortify for authentication. Key features:- Password hashing (bcrypt with configurable rounds)
- Rate limiting on login attempts
- Email verification
- Two-factor authentication (2FA)
- Password confirmation for sensitive actions
Two-Factor Authentication Setup
Enable 2FA inconfig/fortify.php:
Password Requirements
Enforce strong passwords in your Form Request validation:Rate Limiting
Laravel includes rate limiting for authentication routes. Configure inconfig/fortify.php:
app/Providers/FortifyServiceProvider.php:
Session Security
Session Configuration
In.env:
SESSION_SECURE_COOKIE=true: Cookies only sent over HTTPSSESSION_SAME_SITE=strict: Prevents CSRF attacksSESSION_LIFETIME=120: Sessions expire after 2 hours of inactivity
Session Database Storage
Using database sessions (already configured) provides:- Better scalability for multiple servers
- Session invalidation on logout
- Activity tracking
API Security
If you expose APIs:Rate Limiting
Laravel provides built-in API rate limiting:API Authentication
For API endpoints, consider using Laravel Sanctum:Security Headers
All critical security headers are included in the Nginx configuration:Permission and Role Management
Nguhöe EHR uses Spatie Laravel Permission for role-based access control (RBAC).Best Practices
- Assign users the minimum permissions required
- Use roles to group permissions logically (e.g., Doctor, Nurse, Admin)
- Regularly audit user permissions
- Remove access immediately when employees leave
Implementation Example
Logging and Monitoring
Security Event Logging
Log important security events:Monitor Logs for Suspicious Activity
Security Checklist
Before going live, verify:-
APP_DEBUG=falsein production -
APP_ENV=production - HTTPS enforced with valid SSL certificate
-
.envfile permissions set to 600 - Database user has minimal required privileges
- Strong passwords enforced for user accounts
- Two-factor authentication enabled and encouraged
- File upload validation in place
- CSRF protection active on all forms
- Security headers configured in web server
- Rate limiting enabled on authentication routes
- Session cookies configured securely
- Storage directories not publicly accessible
- Regular security updates scheduled
- Security logging and monitoring active
- Backup encryption enabled
Regular Security Maintenance
Update Dependencies
Security Scanning
Consider using:- Laravel Security Checker:
composer audit - npm audit:
npm audit fix - OWASP ZAP: Web application security scanner
- Server security audits: Lynis, OpenVAS
Stay Informed
- Subscribe to Laravel Security Advisories
- Monitor PHP security updates
- Review OWASP Top 10 regularly
Incident Response
If a security breach is suspected:- Isolate: Take affected systems offline if necessary
- Investigate: Review logs to determine extent of breach
- Contain: Change passwords, revoke tokens, update credentials
- Recover: Restore from clean backups if needed
- Document: Record timeline and actions taken
- Improve: Update security measures to prevent recurrence
- Notify: Inform affected users if personal data was compromised