Merged
fix(http): strictly validate MCP and A2A content lengths #384
fix(http): strictly validate MCP and A2A content lengths #384
Merged
Yiyang ShaoAdd on 16 days ago
Total: 13 files changed+674-90
MA2A/cpp-sdk/src/shared/http_common.cpp+51-31File content is under review, please try again later
MA2A/cpp-sdk/src/shared/http_common.h+7-2File content is under review, please try again later
MA2A/cpp-sdk/tests/ut/shared/test_http_common.cpp+118-4File content is under review, please try again later
MMCP/cpp-sdk/src/client/http_client.cpp+28-11File content is under review, please try again later
MMCP/cpp-sdk/src/client/http_client.h+0-1File content is under review, please try again later
@@ -6,6 +6,8 @@
6 6 
7#include <algorithm>7#include <algorithm>
8#include <regex>8#include <regex>
9+#include <charconv>
10+#include <cctype>
9#include <sstream>11#include <sstream>
10#include <stdexcept>12#include <stdexcept>
11 13 
@@ -86,63 +88,77 @@ void TrimInPlace(std::string& value)
86 }88 }
87}89}
88 90 
89-bool ParseHeadersAndBody(const std::string& buffer, std::size_t headerEnd,91+namespace {
90- std::unordered_map<std::string, std::string>& headers, std::string& body,92+ 
91- std::size_t& consumedBytes)93+bool ParseHeaderFields(const std::string& headerPart,
94+ std::unordered_map<std::string, std::string>& headers, std::size_t& contentLength)
92{95{
93- consumedBytes = 0;
94- 
95- if (headerEnd == std::string::npos || headerEnd + HTTP_HEADER_BODY_SEPARATOR_LENGTH > buffer.size()) {
96- return false;
97- }
98- 
99- const std::size_t bodyStart = headerEnd + HTTP_HEADER_BODY_SEPARATOR_LENGTH;
100- 
101- // Parse headers from the header part.
102- std::string headerPart = buffer.substr(0, headerEnd);
103 std::istringstream headerStream(headerPart);96 std::istringstream headerStream(headerPart);
104- 97+ bool hasContentLength = false;
105 std::string headerLine;98 std::string headerLine;
106 while (std::getline(headerStream, headerLine)) {99 while (std::getline(headerStream, headerLine)) {
107 if (!headerLine.empty() && headerLine.back() == '\r') {100 if (!headerLine.empty() && headerLine.back() == '\r') {
108 headerLine.pop_back();101 headerLine.pop_back();
109 }102 }
110- if (headerLine.empty()) {103+ const std::size_t colonPosition = headerLine.find(':');
111- continue;
112- }
113- std::size_t colonPosition = headerLine.find(':');
114 if (colonPosition == std::string::npos) {104 if (colonPosition == std::string::npos) {
115 continue;105 continue;
116 }106 }
117- 
118 std::string key = headerLine.substr(0, colonPosition);107 std::string key = headerLine.substr(0, colonPosition);
119 std::string value = headerLine.substr(colonPosition + 1);108 std::string value = headerLine.substr(colonPosition + 1);
120- 
121 TrimInPlace(key);109 TrimInPlace(key);
122 TrimInPlace(value);110 TrimInPlace(value);
123- 111+ std::string normalizedKey = key;
124- // Convert header name to lowercase for consistent processing112+ std::transform(normalizedKey.begin(), normalizedKey.end(), normalizedKey.begin(),
125- std::transform(key.begin(), key.end(), key.begin(), ::tolower);113+ [](unsigned char character) { return static_cast<char>(std::tolower(character)); });
126- 114+ if (normalizedKey == "content-length") {
127- headers[key] = value;115+ if (hasContentLength) {
128- }116+ return false;
129- 117+ }
130- std::size_t contentLength = 0;118+ hasContentLength = true;
131- auto contentLengthIterator = headers.find(CONTENT_LENGTH_HEADER);119+ const auto result = std::from_chars(value.data(), value.data() + value.size(), contentLength);
132- if (contentLengthIterator != headers.end()) {120+ if (result.ec != std::errc() || result.ptr != value.data() + value.size()) {
133- try {121+ return false;
134- contentLength = static_cast<std::size_t>(std::stoul(contentLengthIterator->second));122+ }
135- } catch (...) {123+ } else if (normalizedKey == "transfer-encoding") {
136- contentLength = 0;124+ // Transfer-coded bodies are not decoded by this helper.
125+ return false;
137 }126 }
127+ headers[normalizedKey] = value;
138 }128 }
129+ return true;
130+}
139 131 
132+} // namespace
133+ 
134+bool ParseHeadersAndBody(const std::string& buffer, std::size_t headerEnd,
135+ std::unordered_map<std::string, std::string>& headers, std::string& body,
136+ std::size_t& consumedBytes, HttpParseStatus* status)
137+{
138+ consumedBytes = 0;
139+ if (status != nullptr) {
140+ *status = HttpParseStatus::INCOMPLETE;
141+ }
142+ if (headerEnd == std::string::npos || headerEnd > buffer.size() ||
143+ buffer.size() - headerEnd < HTTP_HEADER_BODY_SEPARATOR_LENGTH) {
144+ return false;
145+ }
146+ const std::size_t bodyStart = headerEnd + HTTP_HEADER_BODY_SEPARATOR_LENGTH;
147+ std::size_t contentLength = 0;
148+ if (!ParseHeaderFields(buffer.substr(0, headerEnd), headers, contentLength)) {
149+ if (status != nullptr) {
150+ *status = HttpParseStatus::INVALID;
151+ }
152+ return false;
153+ }
140 if (buffer.size() - bodyStart < contentLength) {154 if (buffer.size() - bodyStart < contentLength) {
141 return false;155 return false;
142 }156 }
143- 
144 body = buffer.substr(bodyStart, contentLength);157 body = buffer.substr(bodyStart, contentLength);
145 consumedBytes = bodyStart + contentLength;158 consumedBytes = bodyStart + contentLength;
159+ if (status != nullptr) {
160+ *status = HttpParseStatus::COMPLETE;
161+ }
146 return true;162 return true;
147}163}
148 164 
@@ -153,16 +153,21 @@ enum class HttpServerStartResult {
153// Trim ASCII spaces and tabs from both ends of a string in place.153// Trim ASCII spaces and tabs from both ends of a string in place.
154void TrimInPlace(std::string& value);154void TrimInPlace(std::string& value);
155 155 
156+enum class HttpParseStatus { COMPLETE, INCOMPLETE, INVALID };
157+ 
156// Parse HTTP headers and body given a full buffer and the position of "\r\n\r\n".158// Parse HTTP headers and body given a full buffer and the position of "\r\n\r\n".
157// - buffer: entire HTTP message buffer.159// - buffer: entire HTTP message buffer.
158// - headerEnd: index of the '\r' in "\r\n\r\n" (returned by find("\r\n\r\n")).160// - headerEnd: index of the '\r' in "\r\n\r\n" (returned by find("\r\n\r\n")).
159// - headers: target map to fill with parsed header key/value pairs.161// - headers: target map to fill with parsed header key/value pairs.
160// - body: target string to fill with body content.162// - body: target string to fill with body content.
161// - consumedBytes: on success, total bytes consumed (headers + separator + body).163// - consumedBytes: on success, total bytes consumed (headers + separator + body).
162-// Returns true if a complete message (including body) is present; false if incomplete.164+// Returns true for a complete message; false for incomplete or invalid input.
165+// status optionally distinguishes these cases. On failure, consumedBytes stays zero.
166+// Only a single Content-Length is accepted. Transfer-Encoding is unsupported
167+// and always yields INVALID.
163bool ParseHeadersAndBody(const std::string& buffer, std::size_t headerEnd,168bool ParseHeadersAndBody(const std::string& buffer, std::size_t headerEnd,
164 std::unordered_map<std::string, std::string>& headers, std::string& body,169 std::unordered_map<std::string, std::string>& headers, std::string& body,
165- std::size_t& consumedBytes);170+ std::size_t& consumedBytes, HttpParseStatus* status = nullptr);
166 171 
167 172 
168std::string getContentType(const HttpResponse& response);173std::string getContentType(const HttpResponse& response);
MMCP/cpp-sdk/tests/ut/http/CMakeLists.txt+18-0File content is under review, please try again later
AMCP/cpp-sdk/tests/ut/http/fakes/event/event_system.h+52-0File content is under review, please try again later
AMCP/cpp-sdk/tests/ut/http/fakes/mcp_log.h+13-0File content is under review, please try again later
AMCP/cpp-sdk/tests/ut/http/fakes/net/tcp_socket.h+80-0File content is under review, please try again later
AMCP/cpp-sdk/tests/ut/http/http_client_callbacks_test.cpp+131-0File content is under review, please try again later
@@ -6,6 +6,7 @@
6 6 
7#include <gtest/gtest.h>7#include <gtest/gtest.h>
8#include <string>8#include <string>
9+#include <limits>
9#include <unordered_map>10#include <unordered_map>
10 11 
11static constexpr int HEADER_SIZE = 2;12static constexpr int HEADER_SIZE = 2;
@@ -167,11 +168,9 @@ TEST(HttpCommonTest, ParseHeadersAndBody_InvalidContentLength)
167 std::string body;168 std::string body;
168 std::size_t consumedBytes = 0;169 std::size_t consumedBytes = 0;
169 170 
170- EXPECT_TRUE(ParseHeadersAndBody(buffer, headerEnd, headers, body, consumedBytes));171+ EXPECT_FALSE(ParseHeadersAndBody(buffer, headerEnd, headers, body, consumedBytes));
171- EXPECT_EQ(headers.size(), 1);
172- EXPECT_EQ(headers[CONTENT_LENGTH_HEADER], "abc");
173 EXPECT_EQ(body, "");172 EXPECT_EQ(body, "");
174- EXPECT_EQ(consumedBytes, headerEnd + HTTP_HEADER_BODY_SEPARATOR_LENGTH);173+ EXPECT_EQ(consumedBytes, 0);
175}174}
176 175 
177TEST(HttpCommonTest, ParseHeadersAndBody_MultipleHeaders)176TEST(HttpCommonTest, ParseHeadersAndBody_MultipleHeaders)
@@ -388,4 +387,119 @@ TEST(HttpCommonTest, IsValidStreamableHttpEndpoint_InvalidUrls)
388 EXPECT_EQ(errorMsg, "port out of range");387 EXPECT_EQ(errorMsg, "port out of range");
389}388}
390 389 
390+ 
391+TEST(HttpLengthValidationTest, RejectsMalformedDecimalLengths)
392+{
393+ for (const auto* value : {"abc", "3junk", "+3", "-0", "-1", "1.0", "3 0", "", " \t",
394+ "184467440737095516160000"}) {
395+ SCOPED_TRACE(value);
396+ std::string buffer = std::string("Content-Length: ") + value + "\r\n\r\nabcdef";
397+ std::unordered_map<std::string, std::string> headers;
398+ std::string body = "unchanged";
399+ std::size_t consumed = 42;
400+ HttpParseStatus status = HttpParseStatus::COMPLETE;
401+ EXPECT_FALSE(ParseHeadersAndBody(buffer, buffer.find("\r\n\r\n"), headers, body, consumed, &status));
402+ EXPECT_EQ(status, HttpParseStatus::INVALID);
403+ EXPECT_EQ(consumed, 0);
404+ EXPECT_EQ(body, "unchanged");
405+ }
406+}
407+ 
408+TEST(HttpLengthValidationTest, RejectsAmbiguousFraming)
409+{
410+ for (const auto* fields : {
411+ "Content-Length: 3\r\nContent-Length: 3",
412+ "Content-Length: 2\r\ncontent-length: 3",
413+ "content-length: 3\r\nCONTENT-LENGTH: 2",
414+ "Content-Length: 3, 3",
415+ "Content-Length: 3\r\nTransfer-Encoding: chunked",
416+ "transfer-encoding: chunked\r\nCONTENT-LENGTH: 3"}) {
417+ SCOPED_TRACE(fields);
418+ std::string buffer = std::string(fields) + "\r\n\r\nabc";
419+ std::unordered_map<std::string, std::string> headers;
420+ std::string body;
421+ std::size_t consumed = 0;
422+ HttpParseStatus status;
423+ EXPECT_FALSE(ParseHeadersAndBody(buffer, buffer.find("\r\n\r\n"), headers, body, consumed, &status));
424+ EXPECT_EQ(status, HttpParseStatus::INVALID);
425+ EXPECT_EQ(consumed, 0);
426+ }
427+}
428+ 
429+TEST(HttpLengthValidationTest, AcceptsDecimalWithOptionalWhitespace)
430+{
431+ for (const auto* value : {"3", "003", "\t3 \t"}) {
432+ SCOPED_TRACE(value);
433+ std::string buffer = std::string("cOnTeNt-LeNgTh: ") + value + "\r\n\r\nabcNEXT";
434+ const auto headerEnd = buffer.find("\r\n\r\n");
435+ std::unordered_map<std::string, std::string> headers;
436+ std::string body;
437+ std::size_t consumed = 0;
438+ HttpParseStatus status;
439+ EXPECT_TRUE(ParseHeadersAndBody(buffer, headerEnd, headers, body, consumed, &status));
440+ EXPECT_EQ(status, HttpParseStatus::COMPLETE);
441+ EXPECT_EQ(body, "abc");
442+ EXPECT_EQ(consumed, headerEnd + HTTP_HEADER_BODY_SEPARATOR_LENGTH + 3);
443+ }
444+}
445+ 
446+TEST(HttpLengthValidationTest, IncompleteBodyCanBeRetried)
447+{
448+ std::string buffer = "Content-Length: 3\r\n\r\na";
449+ const auto headerEnd = buffer.find("\r\n\r\n");
450+ std::unordered_map<std::string, std::string> headers;
451+ std::string body;
452+ std::size_t consumed = 0;
453+ HttpParseStatus status;
454+ EXPECT_FALSE(ParseHeadersAndBody(buffer, headerEnd, headers, body, consumed, &status));
455+ EXPECT_EQ(status, HttpParseStatus::INCOMPLETE);
456+ EXPECT_EQ(consumed, 0);
457+ buffer += "bc";
458+ EXPECT_TRUE(ParseHeadersAndBody(buffer, headerEnd, headers, body, consumed, &status));
459+ EXPECT_EQ(status, HttpParseStatus::COMPLETE);
460+ EXPECT_EQ(body, "abc");
461+}
462+ 
463+TEST(HttpLengthValidationTest, ChecksSizeLimitsWithoutOverflow)
464+{
465+ const std::string maximum = std::to_string(std::numeric_limits<std::size_t>::max());
466+ for (const auto& value : {maximum, maximum + "0"}) {
467+ std::string buffer = "Content-Length: " + value + "\r\n\r\n";
468+ std::unordered_map<std::string, std::string> headers;
469+ std::string body;
470+ std::size_t consumed = 0;
471+ HttpParseStatus status;
472+ EXPECT_FALSE(ParseHeadersAndBody(buffer, buffer.find("\r\n\r\n"), headers, body, consumed, &status));
473+ EXPECT_EQ(status, value == maximum ? HttpParseStatus::INCOMPLETE : HttpParseStatus::INVALID);
474+ EXPECT_EQ(consumed, 0);
475+ }
476+}
477+ 
478+TEST(HttpLengthValidationTest, HeaderBoundsCannotWrapAround)
479+{
480+ std::unordered_map<std::string, std::string> headers;
481+ std::string body;
482+ std::size_t consumed = 42;
483+ HttpParseStatus status;
484+ EXPECT_FALSE(ParseHeadersAndBody("short", std::numeric_limits<std::size_t>::max() - 1,
485+ headers, body, consumed, &status));
486+ EXPECT_EQ(consumed, 0);
487+}
488+ 
489+TEST(HttpLengthValidationTest, RejectsUnsupportedTransferEncodingWithoutLength)
490+{
491+ for (const auto* encoding : {"chunked", "gzip", "identity", ""}) {
492+ SCOPED_TRACE(encoding);
493+ std::string buffer = std::string("Transfer-Encoding: ") + encoding + "\r\n\r\n3\r\nabc\r\n0\r\n\r\n";
494+ std::unordered_map<std::string, std::string> headers;
495+ std::string body = "unchanged";
496+ std::size_t consumed = 42;
497+ HttpParseStatus status;
498+ EXPECT_FALSE(ParseHeadersAndBody(buffer, buffer.find("\r\n\r\n"), headers, body, consumed, &status));
499+ EXPECT_EQ(status, HttpParseStatus::INVALID);
500+ EXPECT_EQ(consumed, 0);
501+ EXPECT_EQ(body, "unchanged");
502+ }
503+}
504+ 
391} // namespace Mcp::Http505} // namespace Mcp::Http