Skip to content

Commit 72c7772

Browse files
committed
PYTHON-1899 Implement encryption corpus test
Validate type to decrypt is Binary subtype 6. Skip tests for deprecated symbol and dbPointer types.
1 parent 59e3bcb commit 72c7772

File tree

7 files changed

+9263
-20
lines changed

7 files changed

+9263
-20
lines changed

pymongo/encryption.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
MongoCryptCallback = object
3131

3232
from bson import _bson_to_dict, _dict_to_bson, decode, encode
33-
from bson.binary import STANDARD
33+
from bson.binary import STANDARD, Binary
3434
from bson.codec_options import CodecOptions
3535
from bson.raw_bson import (DEFAULT_RAW_BSON_OPTIONS,
3636
RawBSONDocument,
@@ -401,7 +401,7 @@ def encrypt(self, value, algorithm, key_id=None, key_alt_name=None):
401401
The encrypted value, a :class:`~bson.binary.Binary` with subtype 6.
402402
"""
403403
# TODO: Add a required codec_options argument for encoding?
404-
doc = encode({'v': value})
404+
doc = encode({'v': value}, codec_options=_DATA_KEY_OPTS)
405405
if isinstance(key_id, uuid.UUID):
406406
raw_key_id = key_id.bytes
407407
else:
@@ -420,10 +420,13 @@ def decrypt(self, value):
420420
:Returns:
421421
The decrypted BSON value.
422422
"""
423+
if not (isinstance(value, Binary) and value.subtype == 6):
424+
raise TypeError(
425+
'value to decrypt must be a bson.binary.Binary with subtype 6')
423426
doc = encode({'v': value})
424427
decrypted_doc = self._encryption.decrypt(doc)
425428
# TODO: Add a required codec_options argument for decoding?
426-
return decode(decrypted_doc)['v']
429+
return decode(decrypted_doc, codec_options=_DATA_KEY_OPTS)['v']
427430

428431
def close(self):
429432
"""Release resources."""

0 commit comments

Comments
 (0)