Skip to content

Commit 892bb6b

Browse files
committed
prohibit db_index and unique=True on encrypted fields
1 parent f37c647 commit 892bb6b

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

django_mongodb_backend/fields/encryption.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
class EncryptedFieldMixin:
88
encrypted = True
99

10-
def __init__(self, *args, queries=None, **kwargs):
11-
if kwargs.get("null", False):
12-
raise ValueError("'null=True' is not supported for encrypted fields.")
10+
def __init__(self, *args, queries=None, db_index=False, null=False, unique=False, **kwargs):
11+
if db_index:
12+
raise ValueError("'db_index=True' is not supported on encrypted fields.")
13+
if null:
14+
raise ValueError("'null=True' is not supported on encrypted fields.")
15+
if unique:
16+
raise ValueError("'unique=True' is not supported on encrypted fields.")
1317
self.queries = queries
1418
super().__init__(*args, **kwargs)
1519

docs/ref/models/encrypted-fields.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ Queryable Encryption.
8181
| :class:`~django.db.models.SlugField` | :ref:`Queryable Encryption does not support TTL Indexes or Unique Indexes <manual:qe-reference-encryption-limits>` |
8282
+--------------------------------------+--------------------------------------------------------------------------------------------------------------------+
8383

84+
Limitations
85+
===========
86+
87+
MongoDB imposes some restrictions on encrypted fields:
88+
89+
* They cannot be indexed.
90+
* They cannot be part of a unique constraint.
91+
* They cannot be null.
92+
8493
``EncryptedFieldMixin``
8594
=======================
8695

tests/encryption_/test_fields.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from bson import ObjectId
66

7-
from django_mongodb_backend.fields import EncryptedCharField
7+
from django_mongodb_backend.fields import EncryptedCharField, EncryptedIntegerField
88

99
from .models import (
1010
Actor,
@@ -188,11 +188,20 @@ def test_time(self):
188188

189189

190190
class FieldMixinTests(EncryptionTestCase):
191-
def test_null_true_raises_error(self):
192-
with self.assertRaisesMessage(
193-
ValueError, "'null=True' is not supported for encrypted fields."
194-
):
195-
EncryptedCharField(max_length=50, null=True)
191+
def test_db_index(self):
192+
msg = "'db_index=True' is not supported on encrypted fields."
193+
with self.assertRaisesMessage(ValueError, msg):
194+
EncryptedIntegerField(db_index=True)
195+
196+
def test_null(self):
197+
msg = "'null=True' is not supported on encrypted fields."
198+
with self.assertRaisesMessage(ValueError, msg):
199+
EncryptedIntegerField(null=True)
200+
201+
def test_unique(self):
202+
msg = "'unique=True' is not supported on encrypted fields."
203+
with self.assertRaisesMessage(ValueError, msg):
204+
EncryptedIntegerField(unique=True)
196205

197206
def test_deconstruct_preserves_queries_and_rewrites_path(self):
198207
field = EncryptedCharField(max_length=50, queries={"field": "value"})

0 commit comments

Comments
 (0)