This module provides comprehensive compliance features for LGPD (Brazilian General Data Protection Law), GDPR (General Data Protection Regulation), and OWASP (Open Web Application Security Project) security standards.
- Record and track user consent for different data processing purposes
- Support for multiple consent types (essential, analytics, marketing, third-party)
- Audit trail for consent changes
- Easy consent revocation
- Right to Access: Users can request their data
- Right to Rectification: Users can correct their data
- Right to Erasure: Users can request deletion (right to be forgotten)
- Right to Data Portability: Users can export their data
- Right to Restriction: Users can restrict data processing
- Right to Objection: Users can object to data processing
- Email anonymization
- Phone number anonymization
- Name anonymization
- Pseudonymization for analytics
- Irreversible hashing for sensitive data
All responses include OWASP-recommended security headers:
X-Frame-Options: Prevents clickjackingX-Content-Type-Options: Prevents MIME sniffingContent-Security-Policy: Controls resource loadingStrict-Transport-Security: Enforces HTTPSReferrer-Policy: Controls referrer informationPermissions-Policy: Controls browser features
- IP-based rate limiting
- Configurable limits per endpoint
- Rate limit headers in responses
- Protection against brute force attacks
- Minimum length: 12 characters
- Requires uppercase, lowercase, digits, and special characters
- Common password detection
- Password strength validation
- SQL injection prevention
- XSS (Cross-Site Scripting) prevention
- HTML sanitization
- Email validation
- Token generation and validation
- Protection against Cross-Site Request Forgery attacks
Comprehensive audit logging for compliance:
- Authentication events (login, logout, password changes)
- Data access events (read, create, update, delete)
- Privacy events (consent changes, data requests)
- Security events (suspicious activity, alerts)
- System events (configuration changes, tenant operations)
All audit logs include:
- Who: User ID and performer
- What: Action and resource
- When: Timestamp
- Where: IP address, user agent, endpoint
- Why/How: Purpose and legal basis
- Context: Trace ID for correlation with application logs
Automatic data retention management based on data category:
- Short-term: 30 days (sessions, cache)
- Medium-term: 1 year (preferences, logs)
- Long-term: 7 years (transactions, contracts, audit logs)
- Permanent: Never deleted (consent records)
- AES-256 encryption for sensitive data
- Key management with master key encryption
- Key rotation support
- Secure key storage
from src.compliance.data_privacy import ConsentManager, ConsentRequest, ConsentType
# Record consent
consent_request = ConsentRequest(
user_id="user123",
consent_type=ConsentType.MARKETING,
granted=True,
ip_address="192.168.1.1",
user_agent="Mozilla/5.0...",
consent_text="I agree to receive marketing emails"
)
await ConsentManager.record_consent(db, consent_request)
# Check consent
has_consent = await ConsentManager.check_consent(
db, "user123", ConsentType.MARKETING
)
# Revoke consent
await ConsentManager.revoke_consent(db, "user123", ConsentType.MARKETING)from src.compliance.data_privacy import (
DataSubjectRightsManager,
DataSubjectRequestCreate,
DataSubjectRight
)
# Create a data subject request
request = DataSubjectRequestCreate(
user_id="user123",
request_type=DataSubjectRight.ERASURE,
notes="User requested account deletion"
)
await DataSubjectRightsManager.create_request(db, request)
# Export user data
user_data = await DataSubjectRightsManager.get_user_data(db, "user123")
# Anonymize user data
await DataSubjectRightsManager.anonymize_user_data(db, "user123")from src.compliance.data_privacy import DataAnonymizer
# Anonymize email
anonymized = DataAnonymizer.anonymize_email("john.doe@example.com")
# Result: "j*****e@example.com"
# Anonymize phone
anonymized = DataAnonymizer.anonymize_phone("+1-555-123-4567")
# Result: "***4567"
# Anonymize name
anonymized = DataAnonymizer.anonymize_name("John Michael Doe")
# Result: "J*** ******* D**"
# Hash data (irreversible)
hashed = DataAnonymizer.hash_data("sensitive_data", salt="random_salt")
# Pseudonymize data
pseudo = DataAnonymizer.pseudonymize("data", "user123")from src.compliance.audit_log import AuditLogger, AuditAction, AuditSeverity
# Log authentication
await AuditLogger.log_authentication(
db=db,
action=AuditAction.LOGIN,
user_id="user123",
ip_address="192.168.1.1",
success=True
)
# Log data access
await AuditLogger.log_data_access(
db=db,
action=AuditAction.DATA_READ,
user_id="user123",
resource_type="account",
resource_id="acc456",
tenant_id="tenant1"
)
# Log privacy event
await AuditLogger.log_privacy_event(
db=db,
action=AuditAction.CONSENT_GRANTED,
user_id="user123",
performed_by="user123",
details="Marketing consent granted"
)
# Log security event
await AuditLogger.log_security_event(
db=db,
action=AuditAction.SUSPICIOUS_ACTIVITY,
user_id="user123",
ip_address="192.168.1.1",
severity=AuditSeverity.WARNING,
details="Multiple failed login attempts"
)from src.compliance.data_retention import (
DataRetentionManager,
DataCategory
)
# Track data for retention
await DataRetentionManager.track_data(
db=db,
resource_type="account",
resource_id="acc123",
category=DataCategory.PERSONAL_DATA
)
# Get expired data
expired = await DataRetentionManager.get_expired_data(db)
# Mark as deleted
for record in expired:
await DataRetentionManager.mark_as_deleted(
db, record, reason="retention_period_expired"
)from src.compliance.data_retention import DataEncryption
# Initialize with master key
encryptor = DataEncryption(master_key="your-master-key")
# Encrypt data
encrypted = encryptor.encrypt("sensitive data")
# Decrypt data
decrypted = encryptor.decrypt(encrypted)
# Generate new encryption key
new_key = DataEncryption.generate_key()The security middleware is automatically applied to all requests:
from fastapi import FastAPI
from src.compliance.security import SecurityHeadersMiddleware, RateLimitMiddleware
app = FastAPI()
# Add security headers
app.add_middleware(SecurityHeadersMiddleware)
# Add rate limiting
app.add_middleware(RateLimitMiddleware, requests_per_minute=60)from src.compliance.security import PasswordPolicy
# Validate password
is_valid, error_message = PasswordPolicy.validate("MyP@ssw0rd123")
if not is_valid:
print(f"Password invalid: {error_message}")
# Check if password is common
if PasswordPolicy.check_common_passwords("password123"):
print("Password is too common")# Record consent
POST /api/v1/compliance/consent
{
"user_id": "user123",
"consent_type": "marketing",
"granted": true,
"consent_text": "I agree to receive marketing emails"
}
# Check consent
GET /api/v1/compliance/consent/{user_id}/{consent_type}
# Revoke consent
DELETE /api/v1/compliance/consent/{user_id}/{consent_type}# Create data subject request
POST /api/v1/compliance/data-subject-request
{
"user_id": "user123",
"request_type": "erasure",
"notes": "User requested account deletion"
}
# Export user data
GET /api/v1/compliance/data-export/{user_id}
# Delete/anonymize user data
DELETE /api/v1/compliance/user-data/{user_id}# Master encryption key (required for data encryption)
MASTER_ENCRYPTION_KEY=your-secure-master-key
# Rate limiting
RATE_LIMIT_PER_MINUTE=60
# Password policy
PASSWORD_MIN_LENGTH=12- Consent management
- Data subject rights (access, rectification, erasure, portability)
- Data anonymization
- Audit logging
- Data retention policies
- Security measures
- Lawful basis for processing
- Consent management
- Data subject rights
- Data portability
- Right to be forgotten
- Data breach notification (via audit logs)
- Privacy by design
- Data protection impact assessment support
- A01: Broken Access Control (rate limiting, authentication)
- A02: Cryptographic Failures (encryption at rest)
- A03: Injection (input sanitization, parameterized queries)
- A04: Insecure Design (security by design)
- A05: Security Misconfiguration (security headers)
- A06: Vulnerable Components (dependency management)
- A07: Authentication Failures (password policy, audit logging)
- A08: Software and Data Integrity (audit logging)
- A09: Security Logging Failures (comprehensive audit logging)
- A10: SSRF (input validation)
- Always log sensitive operations: Use audit logging for all data access and modifications
- Encrypt sensitive data: Use encryption for passwords, tokens, and personal data
- Implement consent checks: Verify consent before processing user data
- Regular data cleanup: Implement automated jobs to delete expired data
- Key rotation: Regularly rotate encryption keys
- Monitor audit logs: Set up alerts for suspicious activities
- Test security: Regular security testing and penetration testing
- Update dependencies: Keep security dependencies up to date
- Document data flows: Maintain documentation of data processing activities
- Train team: Ensure team understands compliance requirements
To add compliance features to existing code:
- Add audit logging to sensitive operations:
# Before
user = await create_user(db, user_data)
# After
user = await create_user(db, user_data)
await AuditLogger.log_data_access(
db, AuditAction.DATA_CREATED, user.id, "user", user.id
)- Add consent checks:
# Before
await send_marketing_email(user)
# After
if await ConsentManager.check_consent(db, user.id, ConsentType.MARKETING):
await send_marketing_email(user)- Anonymize data on deletion:
# Before
await db.delete(user)
# After
await DataSubjectRightsManager.anonymize_user_data(db, user.id)Set up monitoring for:
- Failed authentication attempts
- Suspicious activity patterns
- Data subject requests
- Consent revocations
- Data retention policy violations
- Encryption key expiration
For compliance questions or security concerns, refer to: