Skip to content

Commit 8135f12

Browse files
Copilotstephentoub
andcommitted
Add mimeType validation to ImageContentBlock.FromBytes and AudioContentBlock.FromBytes
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 5c0b287 commit 8135f12

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/ModelContextProtocol.Core/Protocol/ContentBlock.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,12 @@ public sealed class ImageContentBlock : ContentBlock
391391
/// <remarks>
392392
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
393393
/// </remarks>
394-
/// <exception cref="InvalidOperationException"></exception>
394+
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
395+
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
395396
public static ImageContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
396397
{
398+
Throw.IfNullOrWhiteSpace(mimeType);
399+
397400
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
398401

399402
return new()
@@ -476,9 +479,12 @@ public sealed class AudioContentBlock : ContentBlock
476479
/// <remarks>
477480
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
478481
/// </remarks>
479-
/// <exception cref="InvalidOperationException"></exception>
482+
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
483+
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
480484
public static AudioContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
481485
{
486+
Throw.IfNullOrWhiteSpace(mimeType);
487+
482488
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
483489

484490
return new()

tests/ModelContextProtocol.Tests/Protocol/ContentBlockTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,22 @@ public void ToolUseContentBlock_SerializationRoundTrip()
225225
Assert.Equal("Paris", result.Input.GetProperty("city").GetString());
226226
Assert.Equal("metric", result.Input.GetProperty("units").GetString());
227227
}
228+
229+
[Theory]
230+
[InlineData(null)]
231+
[InlineData("")]
232+
[InlineData(" ")]
233+
public void ImageContentBlock_FromBytes_ThrowsForNullOrWhiteSpaceMimeType(string? mimeType)
234+
{
235+
Assert.ThrowsAny<ArgumentException>(() => ImageContentBlock.FromBytes((byte[])[1, 2, 3], mimeType!));
236+
}
237+
238+
[Theory]
239+
[InlineData(null)]
240+
[InlineData("")]
241+
[InlineData(" ")]
242+
public void AudioContentBlock_FromBytes_ThrowsForNullOrWhiteSpaceMimeType(string? mimeType)
243+
{
244+
Assert.ThrowsAny<ArgumentException>(() => AudioContentBlock.FromBytes((byte[])[1, 2, 3], mimeType!));
245+
}
228246
}

0 commit comments

Comments
 (0)