Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/mongocrypt-key.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool _mongocrypt_key_alt_name_from_iter(const bson_iter_t *iter_in,
bool _mongocrypt_key_parse_owned(const bson_t *bson, _mongocrypt_key_doc_t *out, mongocrypt_status_t *status) {
bson_iter_t iter = {0};
bool has_id = false, has_key_material = false, has_status = false, has_creation_date = false,
has_update_date = false, has_master_key = false;
has_update_date = false, has_master_key = false, has_key_alt_names = false, has_version = false;

BSON_ASSERT_PARAM(bson);
BSON_ASSERT_PARAM(out);
Expand All @@ -151,6 +151,10 @@ bool _mongocrypt_key_parse_owned(const bson_t *bson, _mongocrypt_key_doc_t *out,
return false;
}
if (0 == strcmp("_id", field)) {
if (has_id) {
CLIENT_ERR("duplicate field '_id' in key document");
return false;
}
has_id = true;
if (!_mongocrypt_buffer_copy_from_uuid_iter(&out->id, &iter)) {
CLIENT_ERR("invalid key, '_id' is not a UUID");
Expand All @@ -161,13 +165,22 @@ bool _mongocrypt_key_parse_owned(const bson_t *bson, _mongocrypt_key_doc_t *out,

/* keyAltNames (optional) */
if (0 == strcmp("keyAltNames", field)) {
if (has_key_alt_names) {
CLIENT_ERR("duplicate field 'keyAltNames' in key document");
return false;
}
has_key_alt_names = true;
if (!_mongocrypt_key_alt_name_from_iter(&iter, &out->key_alt_names, status)) {
return false;
}
continue;
}

if (0 == strcmp("keyMaterial", field)) {
if (has_key_material) {
CLIENT_ERR("duplicate field 'keyMaterial' in key document");
return false;
}
has_key_material = true;
if (!_mongocrypt_buffer_copy_from_binary_iter(&out->key_material, &iter)) {
CLIENT_ERR("invalid 'keyMaterial', expected binary");
Expand All @@ -193,6 +206,11 @@ bool _mongocrypt_key_parse_owned(const bson_t *bson, _mongocrypt_key_doc_t *out,
}

if (0 == strcmp("version", field)) {
if (has_version) {
CLIENT_ERR("duplicate field 'version' in key document");
return false;
}
has_version = true;
if (!BSON_ITER_HOLDS_INT(&iter)) {
CLIENT_ERR("invalid 'version', expect int");
return false;
Expand All @@ -205,12 +223,20 @@ bool _mongocrypt_key_parse_owned(const bson_t *bson, _mongocrypt_key_doc_t *out,
}

if (0 == strcmp("status", field)) {
if (has_status) {
CLIENT_ERR("duplicate field 'status' in key document");
return false;
}
/* Don't need status. Check that it's present and ignore it. */
has_status = true;
continue;
}

if (0 == strcmp("creationDate", field)) {
if (has_creation_date) {
CLIENT_ERR("duplicate field 'creationDate' in key document");
return false;
}
has_creation_date = true;

if (!BSON_ITER_HOLDS_DATE_TIME(&iter)) {
Expand All @@ -223,6 +249,10 @@ bool _mongocrypt_key_parse_owned(const bson_t *bson, _mongocrypt_key_doc_t *out,
}

if (0 == strcmp("updateDate", field)) {
if (has_update_date) {
CLIENT_ERR("duplicate field 'updateDate' in key document");
return false;
}
has_update_date = true;

if (!BSON_ITER_HOLDS_DATE_TIME(&iter)) {
Expand Down
16 changes: 16 additions & 0 deletions src/mongocrypt-marking.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ _mongocrypt_marking_parse_fle1_placeholder(const bson_t *in, _mongocrypt_marking
field = bson_iter_key(&iter);
BSON_ASSERT(field);
if (0 == strcmp("ki", field)) {
if (has_ki) {
CLIENT_ERR("duplicate field 'ki' in marking");
return false;
}
has_ki = true;
if (!_mongocrypt_buffer_from_uuid_iter(&out->u.fle1.key_id, &iter)) {
CLIENT_ERR("key id must be a UUID");
Expand All @@ -74,6 +78,10 @@ _mongocrypt_marking_parse_fle1_placeholder(const bson_t *in, _mongocrypt_marking
}

if (0 == strcmp("ka", field)) {
if (has_ka) {
CLIENT_ERR("duplicate field 'ka' in marking");
return false;
}
has_ka = true;
/* Some bson_value types are not allowed to be key alt names */
const bson_value_t *value;
Expand All @@ -92,6 +100,10 @@ _mongocrypt_marking_parse_fle1_placeholder(const bson_t *in, _mongocrypt_marking
}

if (0 == strcmp("v", field)) {
if (has_v) {
CLIENT_ERR("duplicate field 'v' in marking");
return false;
}
has_v = true;
memcpy(&out->u.fle1.v_iter, &iter, sizeof(bson_iter_t));
continue;
Expand All @@ -100,6 +112,10 @@ _mongocrypt_marking_parse_fle1_placeholder(const bson_t *in, _mongocrypt_marking
if (0 == strcmp("a", field)) {
int32_t algorithm;

if (has_a) {
CLIENT_ERR("duplicate field 'a' in marking");
return false;
}
has_a = true;
if (!BSON_ITER_HOLDS_INT32(&iter)) {
CLIENT_ERR("invalid marking, 'a' must be an int32");
Expand Down
46 changes: 46 additions & 0 deletions test/test-mongocrypt-key.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,51 @@ static void test_mongocrypt_key_parsing_duplicate_master_key(_mongocrypt_tester_
bson_destroy(&key_bson);
}

/* Regression test for MONGOCRYPT-990. Error when parsing duplicate fields. */
static void test_mongocrypt_key_parsing_duplicate_fields(_mongocrypt_tester_t *tester) {
bson_t key_bson = BSON_INITIALIZER;
mongocrypt_status_t *const status = mongocrypt_status_new();
mongocrypt_binary_t *uuid = TEST_BIN(16);

_recreate_and_reset(tester, &key_bson, status, "_id", NULL);
BSON_APPEND_BINARY(&key_bson, "_id", BSON_SUBTYPE_UUID, uuid->data, uuid->len);
BSON_APPEND_BINARY(&key_bson, "_id", BSON_SUBTYPE_UUID, uuid->data, uuid->len);
_parse_fails(&key_bson, status, "duplicate field '_id'");

_recreate_and_reset(tester, &key_bson, status, "keyMaterial", NULL);
BSON_APPEND_BINARY(&key_bson, "keyMaterial", BSON_SUBTYPE_BINARY, uuid->data, uuid->len);
BSON_APPEND_BINARY(&key_bson, "keyMaterial", BSON_SUBTYPE_BINARY, uuid->data, uuid->len);
_parse_fails(&key_bson, status, "duplicate field 'keyMaterial'");

_recreate_and_reset(tester, &key_bson, status, "keyAltNames", NULL);
bson_concat(&key_bson, TMP_BSON("{'keyAltNames': ['a']}"));
bson_concat(&key_bson, TMP_BSON("{'keyAltNames': ['b']}"));
_parse_fails(&key_bson, status, "duplicate field 'keyAltNames'");

_recreate_and_reset(tester, &key_bson, status, "version", NULL);
BSON_APPEND_INT32(&key_bson, "version", 0);
BSON_APPEND_INT32(&key_bson, "version", 0);
_parse_fails(&key_bson, status, "duplicate field 'version'");

_recreate_and_reset(tester, &key_bson, status, "status", NULL);
BSON_APPEND_INT32(&key_bson, "status", 0);
BSON_APPEND_INT32(&key_bson, "status", 0);
_parse_fails(&key_bson, status, "duplicate field 'status'");

_recreate_and_reset(tester, &key_bson, status, "creationDate", NULL);
BSON_APPEND_DATE_TIME(&key_bson, "creationDate", 123);
BSON_APPEND_DATE_TIME(&key_bson, "creationDate", 123);
_parse_fails(&key_bson, status, "duplicate field 'creationDate'");

_recreate_and_reset(tester, &key_bson, status, "updateDate", NULL);
BSON_APPEND_DATE_TIME(&key_bson, "updateDate", 123);
BSON_APPEND_DATE_TIME(&key_bson, "updateDate", 123);
_parse_fails(&key_bson, status, "duplicate field 'updateDate'");

mongocrypt_status_destroy(status);
bson_destroy(&key_bson);
}

static void test_mongocrypt_key_alt_name_from_iter(_mongocrypt_tester_t *tester) {
mongocrypt_status_t *status;
bson_iter_t iter;
Expand Down Expand Up @@ -256,5 +301,6 @@ static void test_mongocrypt_key_alt_name_from_iter(_mongocrypt_tester_t *tester)
void _mongocrypt_tester_install_key(_mongocrypt_tester_t *tester) {
INSTALL_TEST(test_mongocrypt_key_parsing);
INSTALL_TEST(test_mongocrypt_key_parsing_duplicate_master_key);
INSTALL_TEST(test_mongocrypt_key_parsing_duplicate_fields);
INSTALL_TEST(test_mongocrypt_key_alt_name_from_iter);
}
54 changes: 54 additions & 0 deletions test/test-mongocrypt-marking.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,59 @@ static void test_mongocrypt_marking_parse(_mongocrypt_tester_t *tester) {
* MinCoverInterfaceTest in the server code:
* https://github.com/mongodb/mongo/blob/a4a3eba2a0e0a839ca6213491361269b42e12761/src/mongo/crypto/fle_crypto_test.cpp#L2585
*/

static void test_mongocrypt_marking_parse_duplicate_fields(_mongocrypt_tester_t *tester) {
_mongocrypt_buffer_t marking_buf;
_mongocrypt_marking_t marking;
bson_t *marking_bson;

/* Two identical 'ka' fields. */
marking_bson = bson_new();
BSON_APPEND_INT32(marking_bson, "a", 1);
BSON_APPEND_UTF8(marking_bson, "v", "abc");
BSON_APPEND_UTF8(marking_bson, "ka", "alt1");
BSON_APPEND_UTF8(marking_bson, "ka", "alt2");
_make_marking(marking_bson, &marking_buf);
_parse_fails(&marking_buf, "duplicate field 'ka'", &marking);
_mongocrypt_buffer_cleanup(&marking_buf);
_mongocrypt_marking_cleanup(&marking);
bson_destroy(marking_bson);

/* Two identical 'ki' fields. */
marking_bson = bson_new();
BSON_APPEND_INT32(marking_bson, "a", 1);
BSON_APPEND_UTF8(marking_bson, "v", "abc");
BSON_APPEND_BINARY(marking_bson, "ki", BSON_SUBTYPE_UUID, (TEST_BIN(16))->data, 16);
BSON_APPEND_BINARY(marking_bson, "ki", BSON_SUBTYPE_UUID, (TEST_BIN(16))->data, 16);
_make_marking(marking_bson, &marking_buf);
_parse_fails(&marking_buf, "duplicate field 'ki'", &marking);
_mongocrypt_buffer_cleanup(&marking_buf);
_mongocrypt_marking_cleanup(&marking);
bson_destroy(marking_bson);

/* Two identical 'v' fields. */
marking_bson = bson_new();
BSON_APPEND_INT32(marking_bson, "a", 1);
BSON_APPEND_UTF8(marking_bson, "v", "abc");
BSON_APPEND_UTF8(marking_bson, "v", "def");
_make_marking(marking_bson, &marking_buf);
_parse_fails(&marking_buf, "duplicate field 'v'", &marking);
_mongocrypt_buffer_cleanup(&marking_buf);
_mongocrypt_marking_cleanup(&marking);
bson_destroy(marking_bson);

/* Two identical 'a' fields. */
marking_bson = bson_new();
BSON_APPEND_INT32(marking_bson, "a", 1);
BSON_APPEND_UTF8(marking_bson, "v", "abc");
BSON_APPEND_INT32(marking_bson, "a", 1);
_make_marking(marking_bson, &marking_buf);
_parse_fails(&marking_buf, "duplicate field 'a'", &marking);
_mongocrypt_buffer_cleanup(&marking_buf);
_mongocrypt_marking_cleanup(&marking);
bson_destroy(marking_bson);
}

static void test_mc_get_mincover_from_FLE2RangeFindSpec(_mongocrypt_tester_t *tester) {
typedef struct {
const char *description; // May be NULL.
Expand Down Expand Up @@ -2105,6 +2158,7 @@ static void test_ciphertext_len_steps_fle2_text_search(_mongocrypt_tester_t *tes

void _mongocrypt_tester_install_marking(_mongocrypt_tester_t *tester) {
INSTALL_TEST(test_mongocrypt_marking_parse);
INSTALL_TEST(test_mongocrypt_marking_parse_duplicate_fields);
INSTALL_TEST(test_mc_get_mincover_from_FLE2RangeFindSpec);
INSTALL_TEST(test_mc_marking_to_ciphertext_fle2_range);
INSTALL_TEST(test_mc_marking_to_ciphertext_fle2_text_search);
Expand Down