Skip to content

Commit 020c78a

Browse files
authored
MONGOCRYPT-828 improve checks for text required options (#1045)
Check text options and contention are set for text algorithm
1 parent 09898b7 commit 020c78a

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/mongocrypt-ctx-encrypt.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,11 +1403,20 @@ static bool _fle2_finalize_explicit(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *
14031403
_mongocrypt_buffer_copy_to(&ctx->opts.key_id, &marking.u.fle2.index_key_id);
14041404
}
14051405

1406-
if (ctx->opts.contention_factor.set) {
1406+
// Handle contention factor:
1407+
switch (ctx->opts.index_type.value) {
1408+
case MONGOCRYPT_INDEX_TYPE_NONE:
1409+
default:
1410+
BSON_ASSERT(!ctx->opts.contention_factor.set); // Checked earlier in explicit_encrypt_init.
1411+
break;
1412+
1413+
case MONGOCRYPT_INDEX_TYPE_EQUALITY:
1414+
case MONGOCRYPT_INDEX_TYPE_RANGEPREVIEW_DEPRECATED:
1415+
case MONGOCRYPT_INDEX_TYPE_RANGE:
1416+
case MONGOCRYPT_INDEX_TYPE_TEXTPREVIEW:
1417+
// All QE indexed algorithms require contention factor.
1418+
BSON_ASSERT(ctx->opts.contention_factor.set); // Checked earlier in explicit_encrypt_init.
14071419
marking.u.fle2.maxContentionFactor = ctx->opts.contention_factor.value;
1408-
} else if (ctx->opts.index_type.value == MONGOCRYPT_INDEX_TYPE_EQUALITY) {
1409-
_mongocrypt_ctx_fail_w_msg(ctx, "contention factor required for indexed algorithm");
1410-
goto fail;
14111420
}
14121421

14131422
/* Convert marking to ciphertext. */
@@ -1906,6 +1915,7 @@ static bool explicit_encrypt_init(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *ms
19061915
return _mongocrypt_ctx_fail_w_msg(ctx, "contention factor is required for indexed algorithm");
19071916
}
19081917

1918+
// Check required options for range algorithm are set:
19091919
if (ctx->opts.index_type.set
19101920
&& (ctx->opts.index_type.value == MONGOCRYPT_INDEX_TYPE_RANGE
19111921
|| ctx->opts.index_type.value == MONGOCRYPT_INDEX_TYPE_RANGEPREVIEW_DEPRECATED)) {
@@ -1918,6 +1928,17 @@ static bool explicit_encrypt_init(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *ms
19181928
}
19191929
}
19201930

1931+
// Check required options for text algorithm are set:
1932+
if (ctx->opts.index_type.set && (ctx->opts.index_type.value == MONGOCRYPT_INDEX_TYPE_TEXTPREVIEW)) {
1933+
if (!ctx->opts.contention_factor.set) {
1934+
return _mongocrypt_ctx_fail_w_msg(ctx, "contention factor is required for textPreview algorithm");
1935+
}
1936+
1937+
if (!ctx->opts.textopts.set) {
1938+
return _mongocrypt_ctx_fail_w_msg(ctx, "text opts are required for textPreview algorithm");
1939+
}
1940+
}
1941+
19211942
if (ctx->opts.rangeopts.set && !mc_validate_sparsity(ctx->opts.rangeopts.value.sparsity, ctx->status)) {
19221943
return _mongocrypt_ctx_fail(ctx);
19231944
}

test/test-mongocrypt-ctx-setopt.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,26 @@ static void _test_setopt_for_explicit_encrypt(_mongocrypt_tester_t *tester) {
10281028
ASSERT_EX_ENCRYPT_INIT_FAILS(bson, "suffixPreview query type requires textPreview index type");
10291029
}
10301030

1031+
/* It is an error to set a text algorithm without setting text options */
1032+
{
1033+
REFRESH;
1034+
/* Set key ID to get past the 'either key id or key alt name required' error */
1035+
ASSERT_KEY_ID_OK(uuid);
1036+
ASSERT_OK(mongocrypt_ctx_setopt_algorithm(ctx, MONGOCRYPT_ALGORITHM_TEXTPREVIEW_STR, -1), ctx);
1037+
ASSERT_OK(mongocrypt_ctx_setopt_contention_factor(ctx, 0), ctx);
1038+
ASSERT_EX_ENCRYPT_INIT_FAILS(bson, "text opts are required for textPreview algorithm");
1039+
}
1040+
1041+
/* It is an error to set a text algorithm without setting contention */
1042+
{
1043+
REFRESH;
1044+
/* Set key ID to get past the 'either key id or key alt name required' error */
1045+
ASSERT_KEY_ID_OK(uuid);
1046+
ASSERT_OK(mongocrypt_ctx_setopt_algorithm(ctx, MONGOCRYPT_ALGORITHM_TEXTPREVIEW_STR, -1), ctx);
1047+
ASSERT_OK(mongocrypt_ctx_setopt_algorithm_text(ctx, textopts), ctx);
1048+
ASSERT_EX_ENCRYPT_INIT_FAILS(bson, "contention factor is required for textPreview algorithm");
1049+
}
1050+
10311051
mongocrypt_ctx_destroy(ctx);
10321052
mongocrypt_destroy(crypt);
10331053
}

0 commit comments

Comments
 (0)