Skip to content

Commit cc3910e

Browse files
committed
Fix McpHeaderEncoder.DecodeValue throwing on the degenerate base64 wrapper
1 parent 514cf68 commit cc3910e

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/ModelContextProtocol.Core/Protocol/McpHeaderEncoder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ public static class McpHeaderEncoder
126126

127127
// Check for Base64 wrapper. The spec requires the sentinel markers to be
128128
// case-sensitive and exactly lowercase per SEP-2243.
129-
if (headerValue.StartsWith(Base64Prefix, StringComparison.Ordinal) &&
129+
if (headerValue.Length >= Base64Prefix.Length + Base64Suffix.Length &&
130+
headerValue.StartsWith(Base64Prefix, StringComparison.Ordinal) &&
130131
headerValue.EndsWith(Base64Suffix, StringComparison.Ordinal))
131132
{
132133
var base64Content = headerValue.Substring(

tests/ModelContextProtocol.Tests/Client/McpHeaderEncoderTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ public void DecodeValue_ValidBase64_Decodes()
104104
Assert.Equal("Hello", result);
105105
}
106106

107+
[Fact]
108+
public void DecodeValue_DegenerateWrapper_ReturnsLiteralValue()
109+
{
110+
// "=?base64?=" matches both the prefix "=?base64?" and the suffix "?=" because they
111+
// overlap on the shared '?', but it is too short to contain any base64 content. It must be
112+
// returned as-is rather than throwing when the wrapper is stripped.
113+
var result = McpHeaderEncoder.DecodeValue("=?base64?=");
114+
Assert.Equal("=?base64?=", result);
115+
}
116+
107117
[Fact]
108118
public void DecodeValue_CaseSensitivePrefix_ReturnsLiteralValue()
109119
{

0 commit comments

Comments
 (0)