Skip to content

Commit d060b94

Browse files
committed
PHPC-2151: Validate masterKey option in createDataKey() and rewrapManyDataKey()
1 parent da5e12c commit d060b94

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/MongoDB/ClientEncryption.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,15 @@ static PHP_METHOD(MongoDB_Driver_ClientEncryption, rewrapManyDataKey)
407407
}
408408

409409
if (options && php_array_existsc(options, "masterKey")) {
410-
masterkey = bson_new();
410+
zval* zmasterkey = php_array_fetchc(options, "masterKey");
411411

412-
php_phongo_zval_to_bson(php_array_fetchc(options, "masterKey"), PHONGO_BSON_NONE, masterkey, NULL);
412+
if (Z_TYPE_P(zmasterkey) != IS_OBJECT && Z_TYPE_P(zmasterkey) != IS_ARRAY) {
413+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"masterKey\" option to be array or object, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(zmasterkey));
414+
goto cleanup;
415+
}
416+
417+
masterkey = bson_new();
418+
php_phongo_zval_to_bson(zmasterkey, PHONGO_BSON_NONE, masterkey, NULL);
413419

414420
if (EG(exception)) {
415421
goto cleanup;
@@ -752,9 +758,16 @@ static mongoc_client_encryption_datakey_opts_t* phongo_clientencryption_datakey_
752758
}
753759

754760
if (php_array_existsc(options, "masterKey")) {
755-
bson_t masterkey = BSON_INITIALIZER;
761+
zval* zmasterkey = php_array_fetchc(options, "masterKey");
762+
bson_t masterkey = BSON_INITIALIZER;
763+
764+
if (Z_TYPE_P(zmasterkey) != IS_OBJECT && Z_TYPE_P(zmasterkey) != IS_ARRAY) {
765+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"masterKey\" option to be array or object, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(zmasterkey));
766+
goto cleanup;
767+
}
768+
769+
php_phongo_zval_to_bson(zmasterkey, PHONGO_BSON_NONE, &masterkey, NULL);
756770

757-
php_phongo_zval_to_bson(php_array_fetchc(options, "masterKey"), PHONGO_BSON_NONE, &masterkey, NULL);
758771
if (EG(exception)) {
759772
bson_destroy(&masterkey);
760773
goto cleanup;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
MongoDB\Driver\ClientEncryption::createDataKey() masterKey option invalid type
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongocrypt(); ?>
6+
<?php skip_if_not_live(); ?>
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = create_test_manager();
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+
echo throws(function () use ($clientEncryption) {
19+
$clientEncryption->createDataKey('local', ['masterKey' => 'not-array-or-object']);
20+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
21+
22+
?>
23+
===DONE===
24+
<?php exit(0); ?>
25+
--EXPECT--
26+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
27+
Expected "masterKey" option to be array or object, string given
28+
===DONE===
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
MongoDB\Driver\ClientEncryption::rewrapManyDataKey() masterKey option invalid type
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongocrypt(); ?>
6+
<?php skip_if_not_live(); ?>
7+
<?php skip_if_not_clean(CSFLE_KEY_VAULT_DATABASE_NAME, CSFLE_KEY_VAULT_COLLECTION_NAME);
8+
--FILE--
9+
<?php
10+
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
$manager = create_test_manager();
14+
15+
$clientEncryption = $manager->createClientEncryption([
16+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
17+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
18+
]);
19+
20+
echo throws(function () use ($clientEncryption) {
21+
$clientEncryption->rewrapManyDataKey([], ['masterKey' => 'not-array-or-object']);
22+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECT--
28+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
29+
Expected "masterKey" option to be array or object, string given
30+
===DONE===

0 commit comments

Comments
 (0)