The audit trail system was vulnerable to IP spoofing attacks because it trusted x-forwarded-for headers without proper proxy validation.
audit-trail.service.tsline 256: Usedx-forwarded-forheader directlylogging.interceptor.tsline 36: Usedx-forwarded-forheader directly- No trust proxy configuration in Express/NestJS
// Configure trust proxy for IP extraction
// Only trust specific proxy IPs from environment variable
const trustedProxies = process.env.TRUSTED_PROXIES?.split(',')?.map(ip => ip.trim()) || [];
if (trustedProxies.length > 0) {
app.set('trust proxy', trustedProxies);
} else {
// Default: trust no proxies (disable x-forwarded-for processing)
app.set('trust proxy', false);
}private getClientIp(): string | undefined {
if (!this.request) return undefined;
// Use req.ip which respects trust proxy configuration
// Falls back to socket remoteAddress for direct connections
return this.request.ip || this.request.socket?.remoteAddress;
}ip: request.ip, // Only uses req.ip which respects trust proxy settingsTRUSTED_PROXIES=127.0.0.1,10.0.0.1,192.168.1.100# No TRUSTED_PROXIES needed - defaults to false (secure)- IP Spoofing Prevention: Direct clients cannot spoof their IP address
- Trusted Proxy Support: Only configured proxies can forward real client IPs
- Forensic Integrity: Audit logs contain accurate client IP addresses
- Backward Compatibility: Works with both direct and proxied connections
- ✅ Direct connection with spoofed headers (should use real IP)
- ✅ Trusted proxy with valid headers (should use forwarded IP)
- ✅ Multiple proxy chain handling
- ✅ Fallback to socket.remoteAddress
- ✅ Attack scenarios (basic spoofing, CF header spoofing)
npm test -- src/audit/services/audit-trail.service.spec.ts- ✅ Spoofed forwarded header from direct client is ignored
- ✅ Only trusted proxies can forward real client IPs
- ✅ Audit logs contain accurate IP addresses
- ✅ Tests verify spoofing attempts are blocked
- No Breaking Changes: Existing functionality preserved
- Environment Variable: Add
TRUSTED_PROXIESfor production if using load balancers - Default Secure: No configuration needed for direct deployments
- Audit Log Integrity: Existing logs maintain their recorded IPs
- Set
TRUSTED_PROXIESenvironment variable if using load balancers/reverse proxies - Test with actual proxy infrastructure
- Verify audit logs show correct IPs in production
- Run security tests to confirm spoofing protection