Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSHARP-5065: Deserialize wrappedBytes directly instead of using RawBsonDocument in ExplicitEncryptionLibMongoCryptController.cs #1321

Merged
merged 2 commits into from
May 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public Guid CreateDataKey(
{
var wrappedKeyBytes = ProcessStates(context, _keyVaultNamespace.DatabaseNamespace.DatabaseName, cancellationToken);

var wrappedKeyDocument = new RawBsonDocument(wrappedKeyBytes);
var wrappedKeyDocument = BsonSerializer.Deserialize<BsonDocument>(wrappedKeyBytes);
var keyId = UnwrapKeyId(wrappedKeyDocument);

_keyVaultCollection.Value.InsertOne(wrappedKeyDocument, cancellationToken: cancellationToken);
Expand Down Expand Up @@ -132,7 +132,7 @@ public async Task<Guid> CreateDataKeyAsync(
{
var wrappedKeyBytes = await ProcessStatesAsync(context, _keyVaultNamespace.DatabaseNamespace.DatabaseName, cancellationToken).ConfigureAwait(false);

var wrappedKeyDocument = new RawBsonDocument(wrappedKeyBytes);
var wrappedKeyDocument = BsonSerializer.Deserialize<BsonDocument>(wrappedKeyBytes);
var keyId = UnwrapKeyId(wrappedKeyDocument);

await _keyVaultCollection.Value.InsertOneAsync(wrappedKeyDocument, cancellationToken: cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -556,7 +556,7 @@ private BsonValue RenderFilter(FilterDefinition<BsonDocument> filter)
return filter.Render(serializer, registry);
}

private Guid UnwrapKeyId(RawBsonDocument wrappedKeyDocument)
private Guid UnwrapKeyId(BsonDocument wrappedKeyDocument)
{
var keyId = wrappedKeyDocument["_id"].AsBsonBinaryData;
if (keyId.SubType != BsonBinarySubType.UuidStandard)
Expand All @@ -568,8 +568,8 @@ private Guid UnwrapKeyId(RawBsonDocument wrappedKeyDocument)

private BsonValue UnwrapValue(byte[] encryptedWrappedBytes)
{
var rawDocument = new RawBsonDocument(encryptedWrappedBytes);
return rawDocument["v"];
var bsonDocument = BsonSerializer.Deserialize<BsonDocument>(encryptedWrappedBytes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks safe to me. I don't recall what was the motivation to use RawBsonDocument as in most cases the v value will being read completely but consumer.
Maybe @rstam has some ideas.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why a RawBsonDocument is being used here. If the goal is to deserialize the key bytes we should just do that directly.

Not sure that is the goal though.

return bsonDocument["v"];
}
}
}