This document outlines the security enhancements made to the Credit Scoring with XAI application.
- Feature Dictionary Size Limit: Maximum 300 features to prevent DoS attacks
- Value Validation: All feature values must be numeric, non-NaN, and non-Infinite
- Type Checking: Strict type validation using Pydantic models
- Empty Input Protection: Prevents empty feature dictionaries
- API Key Authentication: Optional API key-based authentication via
X-API-Keyheader - Environment Variable Configuration: API key stored in environment variable
API_KEY - Development Mode: Authentication can be disabled by not setting
API_KEYenvironment variable
Usage:
# Enable authentication
export API_KEY="your-secure-api-key"
# Make authenticated request
curl -X POST http://localhost:8000/predict \
-H "X-API-Key: your-secure-api-key" \
-H "Content-Type: application/json" \
-d '{"features": {...}}'- Generic Error Messages: Clients receive generic error messages
- Detailed Internal Logging: Full error details logged internally for debugging
- Structured Logging: Using Python's logging module with timestamps and levels
- Audit Trail: All predictions logged with relevant metadata (without sensitive data)
- CORS Configuration: Configurable allowed origins via
ALLOWED_ORIGINSenvironment variable - Method Restrictions: Only POST and GET methods allowed
- Credentials Support: Proper CORS credentials handling
- Dedicated User: Application runs as non-root user
appuser(UID 1000) - File Permissions: All application files owned by
appuser - Privilege Separation: Reduces attack surface and privilege escalation risks
- Built-in Health Endpoint:
/healthendpoint for container orchestration - Docker Health Check: Automated health monitoring with curl
- Configuration: 30s interval, 10s timeout, 3 retries
- Slim Base Image: Using Python 3.10-slim to reduce image size
- Minimal Dependencies: Only required system packages installed
- Clean Package Cache: APT cache cleaned to reduce image size
- Build Arguments: Model artifact path configurable via Docker build arg
- Environment Variables: Runtime model path configurable via
MODEL_PATH
Usage:
# Build with custom model path
docker build --build-arg MODEL_ARTIFACT_PATH=path/to/model -t credit-api .
# Run with environment variables
docker run -e API_KEY="secret" -e MODEL_PATH="/app/model" -p 8000:8000 credit-api- All Dependencies Pinned: Version ranges specified for all dependencies
- Regular Updates: Dependencies should be reviewed and updated regularly
- Security Scanning: Use
pip-auditorsafetyto scan for vulnerabilities
Scan for vulnerabilities:
pip install pip-audit
pip-audit -r requirements_serving.txt- requirements.txt: Training dependencies
- requirements_serving.txt: Production serving dependencies (minimal footprint)
- Log Levels: INFO, WARNING, ERROR with appropriate usage
- Timestamps: All logs include timestamps
- Context Information: Logs include relevant context without sensitive data
- Status Check:
/healthendpoint returns application status - Model Validation: Confirms model and explainer are loaded
- Integration: Can be used with monitoring systems (Prometheus, DataDog, etc.)
-
Enable Authentication
export API_KEY="$(openssl rand -hex 32)"
-
Configure CORS
export ALLOWED_ORIGINS="https://yourdomain.com,https://app.yourdomain.com"
-
Use HTTPS/TLS
- Deploy behind a reverse proxy (nginx, traefik) with TLS
- Never expose the API directly without HTTPS
-
Rate Limiting
- Implement rate limiting at reverse proxy level
- Consider using services like Cloudflare, AWS WAF, or nginx rate limiting
-
Secrets Management
- Use secrets management systems (AWS Secrets Manager, HashiCorp Vault)
- Never commit
.envfiles to version control - Rotate API keys regularly
-
Monitoring
- Set up log aggregation (ELK Stack, Splunk, CloudWatch)
- Monitor for unusual access patterns
- Set up alerts for failed authentication attempts
-
Regular Security Audits
- Run dependency vulnerability scans regularly
- Perform penetration testing
- Review access logs
-
Data Privacy
- Ensure GDPR/CCPA compliance for credit data
- Implement data retention policies
- Consider data anonymization for logs
Internet β [Cloudflare/WAF] β [Load Balancer] β [Reverse Proxy with TLS] β [Credit API Container]
β
[Monitoring & Logging]
- Enable API key authentication (
API_KEYenvironment variable set) - Configure CORS with specific allowed origins
- Set up HTTPS/TLS termination
- Implement rate limiting
- Configure log aggregation and monitoring
- Set up health check monitoring
- Scan dependencies for vulnerabilities
- Review and test error handling
- Document incident response procedures
- Regular dependency updates and vulnerability scans
- Log review and analysis (weekly/monthly)
- API key rotation (quarterly)
- Security audit (annually)
- Backup and disaster recovery testing
- Performance and capacity monitoring
- Compliance reviews (GDPR, PCI-DSS if applicable)
- No Built-in Rate Limiting: Must be implemented at reverse proxy level
- Simple API Key Auth: Consider upgrading to OAuth2/JWT for production
- No Request Size Limit: Should be configured at reverse proxy level
- No Model Versioning: Consider implementing model version tracking
- No Adversarial Attack Detection: Consider adding input anomaly detection
- OWASP API Security Top 10
- FastAPI Security Documentation
- Docker Security Best Practices
- NIST Cybersecurity Framework
This document should be reviewed and updated whenever:
- New security features are added
- Vulnerabilities are discovered and fixed
- Production deployment architecture changes
- Compliance requirements change
Last Updated: 2024 Version: 1.0