Skip to content

Commit 676c0d7

Browse files
authored
Bugfix: Crypto ReadOnlyMemory<byte> decryption times out (#1443)
* 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]>
1 parent ef54d75 commit 676c0d7

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

src/Dapr.Client/DaprClientGrpc.cs

+17-27
Original file line numberDiff line numberDiff line change
@@ -1670,24 +1670,18 @@ public override async Task<ReadOnlyMemory<byte>> EncryptAsync(string vaultResour
16701670
ReadOnlyMemory<byte> plaintextBytes, string keyName, EncryptionOptions encryptionOptions,
16711671
CancellationToken cancellationToken = default)
16721672
{
1673-
if (MemoryMarshal.TryGetArray(plaintextBytes, out var plaintextSegment) && plaintextSegment.Array != null)
1674-
{
1675-
var encryptionResult = await EncryptAsync(vaultResourceName, new MemoryStream(plaintextSegment.Array),
1676-
keyName, encryptionOptions,
1677-
cancellationToken);
1673+
using var memoryStream = plaintextBytes.CreateMemoryStream(true);
16781674

1679-
var bufferedResult = new ArrayBufferWriter<byte>();
1675+
var encryptionResult =
1676+
await EncryptAsync(vaultResourceName, memoryStream, keyName, encryptionOptions, cancellationToken);
16801677

1681-
await foreach (var item in encryptionResult.WithCancellation(cancellationToken))
1682-
{
1683-
bufferedResult.Write(item.Span);
1684-
}
1685-
1686-
return bufferedResult.WrittenMemory;
1678+
var bufferedResult = new ArrayBufferWriter<byte>();
1679+
await foreach (var item in encryptionResult.WithCancellation(cancellationToken))
1680+
{
1681+
bufferedResult.Write(item.Span);
16871682
}
16881683

1689-
throw new ArgumentException("The input instance doesn't have a valid underlying data store.",
1690-
nameof(plaintextBytes));
1684+
return bufferedResult.WrittenMemory;
16911685
}
16921686

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

1903-
var bufferedResult = new ArrayBufferWriter<byte>();
1904-
await foreach (var item in decryptionResult.WithCancellation(cancellationToken))
1905-
{
1906-
bufferedResult.Write(item.Span);
1907-
}
1908-
1909-
return bufferedResult.WrittenMemory;
1894+
var decryptionResult =
1895+
await DecryptAsync(vaultResourceName, memoryStream, keyName, decryptionOptions, cancellationToken);
1896+
1897+
var bufferedResult = new ArrayBufferWriter<byte>();
1898+
await foreach (var item in decryptionResult.WithCancellation(cancellationToken))
1899+
{
1900+
bufferedResult.Write(item.Span);
19101901
}
19111902

1912-
throw new ArgumentException("The input instance doesn't have a valid underlying data store",
1913-
nameof(ciphertextBytes));
1903+
return bufferedResult.WrittenMemory;
19141904
}
19151905

19161906
/// <inheritdoc />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// ------------------------------------------------------------------------
2+
// Copyright 2025 The Dapr Authors
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ------------------------------------------------------------------------
13+
14+
using System;
15+
using System.IO;
16+
using System.Runtime.InteropServices;
17+
18+
namespace Dapr.Client;
19+
20+
internal static class ReadOnlyMemoryExtensions
21+
{
22+
public static MemoryStream CreateMemoryStream(this ReadOnlyMemory<byte> memory, bool isReadOnly)
23+
{
24+
if (memory.IsEmpty)
25+
{
26+
return new MemoryStream(Array.Empty<byte>(), !isReadOnly);
27+
}
28+
29+
if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
30+
{
31+
return new MemoryStream(segment.Array!, segment.Offset, segment.Count, !isReadOnly);
32+
}
33+
34+
throw new ArgumentException(nameof(memory), "Unable to create MemoryStream from provided memory value");
35+
}
36+
}

0 commit comments

Comments
 (0)