Fix base64 deserialization when JSON encoder escapes forward slashes (#1342)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
This commit is contained in:
Copilot 2026-02-23 14:20:25 -05:00 • committed by GitHub
commit 774a1dba35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 710 additions and 48 deletions

View file

@ -1,6 +1,7 @@
using System.Buffers;
using System.Buffers.Text;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;
@ -28,7 +29,7 @@ namespace ModelContextProtocol.Protocol;
public sealed class BlobResourceContents : ResourceContents
{
private ReadOnlyMemory<byte>? _decodedData;
private ReadOnlyMemory<byte> _blob;
private ReadOnlyMemory<byte>? _blob;
/// <summary>
/// Creates an <see cref="BlobResourceContents"/> from raw data.
@ -40,15 +41,20 @@ public sealed class BlobResourceContents : ResourceContents
/// <exception cref="InvalidOperationException"></exception>
public static BlobResourceContents FromBytes(ReadOnlyMemory<byte> bytes, string uri, string? mimeType = null)
{
ReadOnlyMemory<byte> blob = EncodingUtilities.EncodeToBase64Utf8(bytes);
return new()
{
_decodedData = bytes,
Blob = blob,
MimeType = mimeType,
Uri = uri
};
return new(bytes, uri, mimeType);
}
/// <summary>Initializes a new instance of the <see cref="BlobResourceContents"/> class.</summary>
public BlobResourceContents()
{
}
[SetsRequiredMembers]
private BlobResourceContents(ReadOnlyMemory<byte> decodedData, string uri, string? mimeType)
{
_decodedData = decodedData;
Uri = uri;
MimeType = mimeType;
}
/// <summary>
@ -60,7 +66,16 @@ public sealed class BlobResourceContents : ResourceContents
[JsonPropertyName("blob")]
public required ReadOnlyMemory<byte> Blob
{
get => _blob;
get
{
if (_blob is null)
{
Debug.Assert(_decodedData is not null);
_blob = EncodingUtilities.EncodeToBase64Utf8(_decodedData!.Value);
}
return _blob.Value;
}
set
{
_blob = value;

View file

@ -92,6 +92,7 @@ public abstract class ContentBlock
string? name = null;
string? title = null;
ReadOnlyMemory<byte>? data = null;
ReadOnlyMemory<byte>? decodedData = null;
string? mimeType = null;
string? uri = null;
string? description = null;
@ -137,7 +138,14 @@ public abstract class ContentBlock
break;
case "data":
data = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray();
if (!reader.ValueIsEscaped)
{
data = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray();
}
else
{
decodedData = reader.GetBytesFromBase64();
}
break;
case "mimeType":
@ -230,17 +238,23 @@ public abstract class ContentBlock
Text = text ?? throw new JsonException("Text contents must be provided for 'text' type."),
},
"image" => new ImageContentBlock
{
Data = data ?? throw new JsonException("Image data must be provided for 'image' type."),
MimeType = mimeType ?? throw new JsonException("MIME type must be provided for 'image' type."),
},
"image" => decodedData is not null ?
ImageContentBlock.FromBytes(decodedData.Value,
mimeType ?? throw new JsonException("MIME type must be provided for 'image' type.")) :
new ImageContentBlock
{
Data = data ?? throw new JsonException("Image data must be provided for 'image' type."),
MimeType = mimeType ?? throw new JsonException("MIME type must be provided for 'image' type."),
},
"audio" => new AudioContentBlock
{
Data = data ?? throw new JsonException("Audio data must be provided for 'audio' type."),
MimeType = mimeType ?? throw new JsonException("MIME type must be provided for 'audio' type."),
},
"audio" => decodedData is not null ?
AudioContentBlock.FromBytes(decodedData.Value,
mimeType ?? throw new JsonException("MIME type must be provided for 'audio' type.")) :
new AudioContentBlock
{
Data = data ?? throw new JsonException("Audio data must be provided for 'audio' type."),
MimeType = mimeType ?? throw new JsonException("MIME type must be provided for 'audio' type."),
},
"resource" => new EmbeddedResourceBlock
{
@ -414,7 +428,7 @@ public sealed class TextContentBlock : ContentBlock
public sealed class ImageContentBlock : ContentBlock
{
private ReadOnlyMemory<byte>? _decodedData;
private ReadOnlyMemory<byte> _data;
private ReadOnlyMemory<byte>? _data;
/// <summary>
/// Creates an <see cref="ImageContentBlock"/> from decoded image bytes.
@ -423,7 +437,7 @@ public sealed class ImageContentBlock : ContentBlock
/// <param name="mimeType">The MIME type of the image.</param>
/// <returns>A new <see cref="ImageContentBlock"/> instance.</returns>
/// <remarks>
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// This method stores the provided bytes as <see cref="DecodedData"/> and lazily encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
@ -431,14 +445,19 @@ public sealed class ImageContentBlock : ContentBlock
{
Throw.IfNullOrWhiteSpace(mimeType);
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
return new()
{
_decodedData = bytes,
Data = data,
MimeType = mimeType
};
return new(bytes, mimeType);
}
/// <summary>Initializes a new instance of the <see cref="ImageContentBlock"/> class.</summary>
public ImageContentBlock()
{
}
[SetsRequiredMembers]
private ImageContentBlock(ReadOnlyMemory<byte> decodedData, string mimeType)
{
_decodedData = decodedData;
MimeType = mimeType;
}
/// <inheritdoc/>
@ -453,7 +472,16 @@ public sealed class ImageContentBlock : ContentBlock
[JsonPropertyName("data")]
public required ReadOnlyMemory<byte> Data
{
get => _data;
get
{
if (_data is null)
{
Debug.Assert(_decodedData is not null);
_data = EncodingUtilities.EncodeToBase64Utf8(_decodedData!.Value);
}
return _data.Value;
}
set
{
_data = value;
@ -494,7 +522,14 @@ public sealed class ImageContentBlock : ContentBlock
public required string MimeType { get; set; }
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"MimeType = {MimeType}, Length = {DebuggerDisplayHelper.GetBase64LengthDisplay(Data)}";
private string DebuggerDisplay
{
get
{
string lengthDisplay = _decodedData is not null ? $"{_decodedData.Value.Length} bytes" : DebuggerDisplayHelper.GetBase64LengthDisplay(Data);
return $"MimeType = {MimeType}, Length = {lengthDisplay}";
}
}
}
/// <summary>Represents audio provided to or from an LLM.</summary>
@ -502,7 +537,7 @@ public sealed class ImageContentBlock : ContentBlock
public sealed class AudioContentBlock : ContentBlock
{
private ReadOnlyMemory<byte>? _decodedData;
private ReadOnlyMemory<byte> _data;
private ReadOnlyMemory<byte>? _data;
/// <summary>
/// Creates an <see cref="AudioContentBlock"/> from decoded audio bytes.
@ -511,7 +546,7 @@ public sealed class AudioContentBlock : ContentBlock
/// <param name="mimeType">The MIME type of the audio.</param>
/// <returns>A new <see cref="AudioContentBlock"/> instance.</returns>
/// <remarks>
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// This method stores the provided bytes as <see cref="DecodedData"/> and lazily encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
@ -519,14 +554,19 @@ public sealed class AudioContentBlock : ContentBlock
{
Throw.IfNullOrWhiteSpace(mimeType);
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
return new()
{
_decodedData = bytes,
Data = data,
MimeType = mimeType
};
return new(bytes, mimeType);
}
/// <summary>Initializes a new instance of the <see cref="AudioContentBlock"/> class.</summary>
public AudioContentBlock()
{
}
[SetsRequiredMembers]
private AudioContentBlock(ReadOnlyMemory<byte> decodedData, string mimeType)
{
_decodedData = decodedData;
MimeType = mimeType;
}
/// <inheritdoc/>
@ -541,7 +581,16 @@ public sealed class AudioContentBlock : ContentBlock
[JsonPropertyName("data")]
public required ReadOnlyMemory<byte> Data
{
get => _data;
get
{
if (_data is null)
{
Debug.Assert(_decodedData is not null);
_data = EncodingUtilities.EncodeToBase64Utf8(_decodedData!.Value);
}
return _data.Value;
}
set
{
_data = value;
@ -582,7 +631,14 @@ public sealed class AudioContentBlock : ContentBlock
public required string MimeType { get; set; }
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"MimeType = {MimeType}, Length = {DebuggerDisplayHelper.GetBase64LengthDisplay(Data)}";
private string DebuggerDisplay
{
get
{
string lengthDisplay = _decodedData is not null ? $"{_decodedData.Value.Length} bytes" : DebuggerDisplayHelper.GetBase64LengthDisplay(Data);
return $"MimeType = {MimeType}, Length = {lengthDisplay}";
}
}
}
/// <summary>Represents the contents of a resource, embedded into a prompt or tool call result.</summary>

View file

@ -80,6 +80,7 @@ public abstract class ResourceContents
string? uri = null;
string? mimeType = null;
ReadOnlyMemory<byte>? blob = null;
ReadOnlyMemory<byte>? decodedBlob = null;
string? text = null;
JsonObject? meta = null;
@ -105,7 +106,14 @@ public abstract class ResourceContents
break;
case "blob":
blob = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray();
if (!reader.ValueIsEscaped)
{
blob = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray();
}
else
{
decodedBlob = reader.GetBytesFromBase64();
}
break;
case "text":
@ -122,6 +130,13 @@ public abstract class ResourceContents
}
}
if (decodedBlob is not null)
{
var blobResource = BlobResourceContents.FromBytes(decodedBlob.Value, uri ?? string.Empty, mimeType);
blobResource.Meta = meta;
return blobResource;
}
if (blob is not null)
{
return new BlobResourceContents