Skip to content

Commit

Permalink
Bugfix: Crypto ReadOnlyMemory<byte> decryption times out (#1443)
Browse files Browse the repository at this point in the history
* Bugfix: Removed use of MemoryMarshal as it wasn't decrypting in-memory byte arrays properly (doesn't impact stream encryption/decryption).

Signed-off-by: Whit Waldo <[email protected]>

* Added extension method similar to how the CommunityToolkit.HighPerformance project handles the creation of MemoryStreams without an allocation. Restored the use of MemoryMarshal, but throws an exception if the data cannot be accessed now, instead of hanging as it did in a previous iteration.

Tested both paths (string and stream) from example project successfully.

Signed-off-by: Whit Waldo <[email protected]>

* Added missing using

Signed-off-by: Whit Waldo <[email protected]>

---------

Signed-off-by: Whit Waldo <[email protected]>
  • Loading branch information
WhitWaldo authored Jan 15, 2025
1 parent ef54d75 commit 676c0d7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
44 changes: 17 additions & 27 deletions src/Dapr.Client/DaprClientGrpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1670,24 +1670,18 @@ public override async Task<ReadOnlyMemory<byte>> EncryptAsync(string vaultResour
ReadOnlyMemory<byte> plaintextBytes, string keyName, EncryptionOptions encryptionOptions,
CancellationToken cancellationToken = default)
{
if (MemoryMarshal.TryGetArray(plaintextBytes, out var plaintextSegment) && plaintextSegment.Array != null)
{
var encryptionResult = await EncryptAsync(vaultResourceName, new MemoryStream(plaintextSegment.Array),
keyName, encryptionOptions,
cancellationToken);
using var memoryStream = plaintextBytes.CreateMemoryStream(true);

var bufferedResult = new ArrayBufferWriter<byte>();
var encryptionResult =
await EncryptAsync(vaultResourceName, memoryStream, keyName, encryptionOptions, cancellationToken);

await foreach (var item in encryptionResult.WithCancellation(cancellationToken))
{
bufferedResult.Write(item.Span);
}

return bufferedResult.WrittenMemory;
var bufferedResult = new ArrayBufferWriter<byte>();
await foreach (var item in encryptionResult.WithCancellation(cancellationToken))
{
bufferedResult.Write(item.Span);
}

throw new ArgumentException("The input instance doesn't have a valid underlying data store.",
nameof(plaintextBytes));
return bufferedResult.WrittenMemory;
}

/// <inheritdoc />
Expand Down Expand Up @@ -1895,22 +1889,18 @@ public override async Task<ReadOnlyMemory<byte>> DecryptAsync(string vaultResour
ReadOnlyMemory<byte> ciphertextBytes, string keyName, DecryptionOptions decryptionOptions,
CancellationToken cancellationToken = default)
{
if (MemoryMarshal.TryGetArray(ciphertextBytes, out var ciphertextSegment) && ciphertextSegment.Array != null)
{
var decryptionResult = await DecryptAsync(vaultResourceName, new MemoryStream(ciphertextSegment.Array),
keyName, decryptionOptions, cancellationToken);
using var memoryStream = ciphertextBytes.CreateMemoryStream(true);

var bufferedResult = new ArrayBufferWriter<byte>();
await foreach (var item in decryptionResult.WithCancellation(cancellationToken))
{
bufferedResult.Write(item.Span);
}

return bufferedResult.WrittenMemory;
var decryptionResult =
await DecryptAsync(vaultResourceName, memoryStream, keyName, decryptionOptions, cancellationToken);

var bufferedResult = new ArrayBufferWriter<byte>();
await foreach (var item in decryptionResult.WithCancellation(cancellationToken))
{
bufferedResult.Write(item.Span);
}

throw new ArgumentException("The input instance doesn't have a valid underlying data store",
nameof(ciphertextBytes));
return bufferedResult.WrittenMemory;
}

/// <inheritdoc />
Expand Down
36 changes: 36 additions & 0 deletions src/Dapr.Client/Extensions/ReadOnlyMemoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ------------------------------------------------------------------------
// Copyright 2025 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Dapr.Client;

internal static class ReadOnlyMemoryExtensions
{
public static MemoryStream CreateMemoryStream(this ReadOnlyMemory<byte> memory, bool isReadOnly)
{
if (memory.IsEmpty)
{
return new MemoryStream(Array.Empty<byte>(), !isReadOnly);
}

if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
{
return new MemoryStream(segment.Array!, segment.Offset, segment.Count, !isReadOnly);
}

throw new ArgumentException(nameof(memory), "Unable to create MemoryStream from provided memory value");
}
}

0 comments on commit 676c0d7

Please sign in to comment.