Skip to content

Commit 5552f9f

Browse files
fix(NODE-6300): make expressionMode required in C++ in makeExplicitEncryptionContext (#37)
1 parent 38f1be6 commit 5552f9f

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

Diff for: addon/mongocrypt.cc

+20-9
Original file line numberDiff line numberDiff line change
@@ -589,13 +589,29 @@ Value MongoCrypt::MakeEncryptionContext(const CallbackInfo& info) {
589589
}
590590

591591
Value MongoCrypt::MakeExplicitEncryptionContext(const CallbackInfo& info) {
592-
std::unique_ptr<mongocrypt_ctx_t, MongoCryptContextDeleter> context(
593-
mongocrypt_ctx_new(_mongo_crypt.get()));
594-
595592
Uint8Array valueBuffer = Uint8ArrayFromValue(info[0], "value");
596593

597594
Object options = info.Length() > 1 ? info[1].ToObject() : Object::New(info.Env());
598595

596+
if (!options.Get("expressionMode").IsBoolean()) {
597+
throw TypeError::New(Env(), "option `expressionMode` is required.");
598+
}
599+
600+
bool expression_mode = options.Get("expressionMode").ToBoolean();
601+
ExplicitEncryptionContextInitFunction context_init_function =
602+
expression_mode ? mongocrypt_ctx_explicit_encrypt_expression_init
603+
: mongocrypt_ctx_explicit_encrypt_init;
604+
605+
return MakeExplicitEncryptionContextInternal(context_init_function, valueBuffer, options);
606+
}
607+
608+
Value MongoCrypt::MakeExplicitEncryptionContextInternal(
609+
ExplicitEncryptionContextInitFunction context_init_function,
610+
const Uint8Array& valueBuffer,
611+
const Object& options) {
612+
std::unique_ptr<mongocrypt_ctx_t, MongoCryptContextDeleter> context(
613+
mongocrypt_ctx_new(_mongo_crypt.get()));
614+
599615
if (!options.Get("keyId").IsUndefined()) {
600616
Uint8Array keyId = Uint8ArrayFromValue(options["keyId"], "keyId");
601617

@@ -658,12 +674,7 @@ Value MongoCrypt::MakeExplicitEncryptionContext(const CallbackInfo& info) {
658674
std::unique_ptr<mongocrypt_binary_t, MongoCryptBinaryDeleter> binaryValue(
659675
Uint8ArrayToBinary(valueBuffer));
660676

661-
const bool isExpressionMode = options.Get("expressionMode").ToBoolean();
662-
663-
const bool status =
664-
isExpressionMode
665-
? mongocrypt_ctx_explicit_encrypt_expression_init(context.get(), binaryValue.get())
666-
: mongocrypt_ctx_explicit_encrypt_init(context.get(), binaryValue.get());
677+
const bool status = context_init_function(context.get(), binaryValue.get());
667678

668679
if (!status) {
669680
throw TypeError::New(Env(), errorStringFromStatus(context.get()));

Diff for: addon/mongocrypt.h

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ namespace opensslcrypto {
5656
std::unique_ptr<CryptoHooks> createOpenSSLCryptoHooks();
5757
}
5858

59+
typedef bool (*ExplicitEncryptionContextInitFunction)(mongocrypt_ctx_t*, mongocrypt_binary_t*);
60+
5961
class MongoCrypt : public Napi::ObjectWrap<MongoCrypt> {
6062
public:
6163
static Napi::Function Init(Napi::Env env);
@@ -67,6 +69,9 @@ class MongoCrypt : public Napi::ObjectWrap<MongoCrypt> {
6769
Napi::Value MakeExplicitDecryptionContext(const Napi::CallbackInfo& info);
6870
Napi::Value MakeDataKeyContext(const Napi::CallbackInfo& info);
6971
Napi::Value MakeRewrapManyDataKeyContext(const Napi::CallbackInfo& info);
72+
Napi::Value MakeExplicitEncryptionContextInternal(ExplicitEncryptionContextInitFunction init_fn,
73+
const Napi::Uint8Array& value,
74+
const Napi::Object& options);
7075

7176
Napi::Value Status(const Napi::CallbackInfo& info);
7277
Napi::Value CryptSharedLibVersionInfo(const Napi::CallbackInfo& info);

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"check:clang-format": "clang-format --style=file:.clang-format --dry-run --Werror addon/*",
2424
"test": "mocha test",
2525
"prepare": "tsc",
26+
"rebuild": "node-gyp rebuild",
2627
"prebuild": "prebuild --runtime napi --strip --verbose --all"
2728
},
2829
"author": {

Diff for: test/bindings.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,20 @@ describe('MongoCryptConstructor', () => {
387387
).to.be.instanceOf(MongoCryptContextCtor);
388388
});
389389
});
390+
391+
describe('options.expressionMode', function () {
392+
it('throws if `expressionMode` is not defined', function () {
393+
expect(() =>
394+
mc.makeExplicitEncryptionContext(value, {
395+
// minimum required arguments from libmongocrypt
396+
keyId: keyId.buffer,
397+
algorithm: 'Unindexed'
398+
})
399+
)
400+
.to.throw(/option `expressionMode` is required./)
401+
.to.be.instanceOf(TypeError);
402+
});
403+
});
390404
});
391405
});
392406

Diff for: test/crypto.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ function createEncryptedDocument(mongoCrypt: MongoCrypt) {
3838

3939
const ctx = mongoCrypt.makeExplicitEncryptionContext(BSON.serialize({ v }), {
4040
keyId: keyId.buffer,
41-
algorithm
41+
algorithm,
42+
expressionMode: false
4243
});
4344

4445
const getState = () => ctx.state;

0 commit comments

Comments
 (0)