Skip to content

Commit 539481f

Browse files
Copilotstephentoub
andauthored
Validate mimeType in ImageContentBlock.FromBytes and AudioContentBlock.FromBytes (#1321)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 3727dc4 commit 539481f

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
@@ -425,9 +425,12 @@ public sealed class ImageContentBlock : ContentBlock
425425
/// <remarks>
426426
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
427427
/// </remarks>
428-
/// <exception cref="InvalidOperationException"></exception>
428+
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
429+
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
429430
public static ImageContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
430431
{
432+
Throw.IfNullOrWhiteSpace(mimeType);
433+
431434
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
432435

433436
return new()
@@ -510,9 +513,12 @@ public sealed class AudioContentBlock : ContentBlock
510513
/// <remarks>
511514
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
512515
/// </remarks>
513-
/// <exception cref="InvalidOperationException"></exception>
516+
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
517+
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
514518
public static AudioContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
515519
{
520+
Throw.IfNullOrWhiteSpace(mimeType);
521+
516522
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
517523

518524
return new()

tests/ModelContextProtocol.Tests/Protocol/ContentBlockTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,22 @@ public void ToolUseContentBlock_SerializationRoundTrip()
272272
Assert.Equal("Paris", result.Input.GetProperty("city").GetString());
273273
Assert.Equal("metric", result.Input.GetProperty("units").GetString());
274274
}
275+
276+
[Theory]
277+
[InlineData(null)]
278+
[InlineData("")]
279+
[InlineData(" ")]
280+
public void ImageContentBlock_FromBytes_ThrowsForNullOrWhiteSpaceMimeType(string? mimeType)
281+
{
282+
Assert.ThrowsAny<ArgumentException>(() => ImageContentBlock.FromBytes((byte[])[1, 2, 3], mimeType!));
283+
}
284+
285+
[Theory]
286+
[InlineData(null)]
287+
[InlineData("")]
288+
[InlineData(" ")]
289+
public void AudioContentBlock_FromBytes_ThrowsForNullOrWhiteSpaceMimeType(string? mimeType)
290+
{
291+
Assert.ThrowsAny<ArgumentException>(() => AudioContentBlock.FromBytes((byte[])[1, 2, 3], mimeType!));
292+
}
275293
}

0 commit comments

Comments
 (0)