Skip to content

Commit f0f2f15

Browse files
committed
Handle BSON encoding exceptions in encrypt/decrypt
Note that it is not possible to write a test for decrypt() since Binary instances always encode to BSON.
1 parent ae719cc commit f0f2f15

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/MongoDB/ClientEncryption.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,10 @@ static void phongo_clientencryption_encrypt(php_phongo_clientencryption_t* clien
10121012

10131013
php_phongo_zval_to_bson_value(zvalue, PHONGO_BSON_NONE, &value);
10141014

1015+
if (EG(exception)) {
1016+
goto cleanup;
1017+
}
1018+
10151019
opts = phongo_clientencryption_encrypt_opts_from_zval(options);
10161020

10171021
if (!opts) {
@@ -1085,6 +1089,10 @@ static void phongo_clientencryption_decrypt(php_phongo_clientencryption_t* clien
10851089

10861090
php_phongo_zval_to_bson_value(zciphertext, PHONGO_BSON_NONE, &ciphertext);
10871091

1092+
if (EG(exception)) {
1093+
goto cleanup;
1094+
}
1095+
10881096
if (!mongoc_client_encryption_decrypt(clientencryption->client_encryption, &ciphertext, &value, &error)) {
10891097
phongo_throw_exception_from_bson_error_t(&error);
10901098
goto cleanup;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
MongoDB\Driver\ClientEncryption::encrypt() BSON encoding errors
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongocrypt(); ?>
6+
--FILE--
7+
<?php
8+
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = create_test_manager();
12+
13+
$clientEncryption = $manager->createClientEncryption([
14+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
15+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
16+
]);
17+
18+
class SerializableError implements MongoDB\BSON\Serializable {
19+
#[\ReturnTypeWillChange]
20+
public function bsonSerialize()
21+
{
22+
throw new RuntimeException('bsonSerialize() error');
23+
}
24+
}
25+
26+
echo throws(function() use ($clientEncryption) {
27+
$clientEncryption->encrypt(new SerializableError());
28+
}, RuntimeException::class), "\n";
29+
30+
echo throws(function() use ($clientEncryption) {
31+
$clientEncryption->encrypt('top-secret', ['keyId' => new SerializableError()]);
32+
}, RuntimeException::class), "\n";
33+
34+
?>
35+
===DONE===
36+
<?php exit(0); ?>
37+
--EXPECTF--
38+
OK: Got RuntimeException
39+
bsonSerialize() error
40+
OK: Got RuntimeException
41+
bsonSerialize() error
42+
===DONE===

0 commit comments

Comments
 (0)