Pii Leakage in Django with Mongodb
Pii Leakage in Django with Mongodb — how this specific combination creates or exposes the vulnerability
Django does not include a built-in ODM for MongoDB, so developers typically integrate MongoDB via third-party libraries such as djongo or mongoengine. This integration introduces a PII leakage risk when object-document mappers expose raw MongoDB query results that include fields such as email, phone, national ID, or location coordinates without appropriate filtering or transformation. Because MongoDB is schema-less, it is easy for collections to contain unexpected sensitive fields, and without strict schema enforcement or field-level permissions, a query that appears safe in Django models can return PII through nested or polymorphic documents.
Another vector is logging or error handling. If MongoDB connection strings, database names, or full query responses are included in Django logs or error messages (for example, via unhandled pymongo exceptions), PII can be exposed in log aggregation systems or browser consoles. A common pattern is to serialize MongoDB documents directly into JSON responses using json.dumps() or Django REST Framework’s JSONRenderer without redaction, inadvertently exposing fields like ssn or credit_card. Additionally, if indexes or aggregation pipelines are constructed dynamically using string interpolation, developers might inadvertently include user-supplied values that return sensitive fields, increasing the likelihood of PII leakage through verbose or misconfigured queries.
Compliance mappings such as GDPR and CCPA treat email addresses, IP addresses, and biometric data as personal data, and MongoDB collections often store these as top-level or nested fields. Without runtime inspection or schema validation, Django applications using MongoDB may fail to enforce data minimization, retaining more PII than necessary. middleBrick’s LLM/AI Security checks and Data Exposure scans detect these patterns by correlating OpenAPI specifications with runtime responses, identifying endpoints that return fields commonly associated with PII. The scanner highlights gaps such as missing field-level encryption, lack of tokenization, and absence of consent-driven data selection, which are especially critical when MongoDB is used as the primary store in Django-based services.
Mongodb-Specific Remediation in Django — concrete code fixes
To reduce PII leakage when using MongoDB with Django, apply strict field selection, schema validation, and response transformation. Prefer explicit projection in queries to return only required fields, and use library-specific tools to control document serialization. Below are concrete examples using mongoengine, a common ODM for MongoDB in Django-like projects.
Example 1: Query with field projection
Use .only() or .exclude() to limit returned fields and avoid exposing PII inadvertently.
import mongoengine
class UserProfile(mongoengine.Document):
email = mongoengine.EmailField()
phone = mongoengine.StringField()
ssn = mongoengine.StringField()
metadata = mongoengine.DictField()
# Only fetch non-sensitive fields
safe_users = UserProfile.objects.only('email', 'metadata')
for user in safe_users:
print(user.email, user.metadata) # phone and ssn are not loaded
# Exclude sensitive fields explicitly
incomplete = UserProfile.objects.exclude('ssn', 'phone')
for user in incomplete:
print(user.email) # ssn and phone are omitted
Example 2: Controlled serialization for APIs
Avoid serializing entire MongoDB documents to JSON. Instead, build dictionaries with only allowed fields before rendering.
from django.http import JsonResponse
def user_profile_view(request, user_id):
raw = UserProfile.objects.get(id=user_id)
# Explicitly construct safe payload
safe_payload = {
'email': raw.email,
'username': raw.metadata.get('username'),
}
return JsonResponse(safe_payload)
Example 3: Schema validation with mongoengine
Define strict field rules and use mongoengine’s validation to prevent unexpected PII from being stored or returned.
from mongoengine import StringField, EmailField, ValidationError
class SecureProfile(mongoengine.Document):
email = EmailField(required=True)
token = StringField(regex=r'^[A-Z0-9-_]+\.[A-Z0-9-_]+\.[A-Z0-9-_]+$', max_length=255)
def clean(self):
# Enforce that no SSN field is present
if hasattr(self, 'ssn'):
raise ValidationError('Storing SSN is not allowed')
meta = {
'strict': True,
}
# Attempting to save a document with unexpected fields will raise errors
try:
SecureProfile(email='[email protected]', token='abc.def.ghi').save()
except ValidationError as e:
print(e)
Example 4: Middleware-based field redaction
Use Django middleware to scrub sensitive fields from MongoDB responses before they reach logs or templates.
class MongoPiiScrubberMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
if response and hasattr(response, 'content'):
# Example: remove PII from JSON response body
import json
try:
data = json.loads(response.content)
if isinstance(data, dict):
data.pop('ssn', None)
data.pop('phone', None)
response.content = json.dumps(data).encode('utf-8')
except (ValueError, TypeError):
pass
return response
Operational practices
- Use role-based access control at the MongoDB level to restrict which fields users can query.
- Enable TLS for all MongoDB connections to protect data in transit.
- Audit logs should exclude or hash PII before storage to avoid secondary leakage.
Related CWEs: dataExposure
| CWE ID | Name | Severity |
|---|---|---|
| CWE-200 | Exposure of Sensitive Information | HIGH |
| CWE-209 | Error Information Disclosure | MEDIUM |
| CWE-213 | Exposure of Sensitive Information Due to Incompatible Policies | HIGH |
| CWE-215 | Insertion of Sensitive Information Into Debugging Code | MEDIUM |
| CWE-312 | Cleartext Storage of Sensitive Information | HIGH |
| CWE-359 | Exposure of Private Personal Information (PII) | HIGH |
| CWE-522 | Insufficiently Protected Credentials | CRITICAL |
| CWE-532 | Insertion of Sensitive Information into Log File | MEDIUM |
| CWE-538 | Insertion of Sensitive Information into Externally-Accessible File | HIGH |
| CWE-540 | Inclusion of Sensitive Information in Source Code | HIGH |
Frequently Asked Questions
Can PII leakage occur if I use Django REST Framework with MongoDB?
.all() queries are used without explicit .values() or field selection, MongoDB documents can expose PII. Control serialization explicitly and limit fields in viewsets.