From beb0913a59c1372109bc0cdec238cfbda5cc3cb1 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Mon, 3 Aug 2026 14:04:07 +0800 Subject: [PATCH 1/2] Stop reading past a string_view that is not NUL terminated nostd::string_view::data() points at the first character of a view, not at a C string, so anything that reads until a NUL can run past the end of what the caller offered. HttpSslOptions did that with strncmp() and it changes an answer rather than only reading too far: a view holding "https" over a buffer that continues with a colon is read as a secure scheme, and the connection silently turns on TLS the caller did not ask for. BuildResponseLogMessage in the Elasticsearch and OTLP HTTP exporters streamed header names and values through the const char* overload, which logs whatever follows the view up to the next NUL. The same file already writes seven other views with their length. The HTTP example built a std::string the same way. The bundled curl client hands out views onto std::string, which is NUL terminated, so none of this misbehaves in tree today. It is reachable through the public HTTP client factory. --- CHANGELOG.md | 2 ++ examples/http/client.cc | 2 +- .../src/es_log_record_exporter.cc | 2 +- exporters/otlp/src/otlp_http_client.cc | 2 +- .../ext/http/client/http_client.h | 4 ++-- ext/test/http/url_parser_test.cc | 24 +++++++++++++++++++ 6 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffe609db7e..2a9a03d962 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ Increment the: [#4245](https://github.com/open-telemetry/opentelemetry-cpp/pull/4245) * [CONFIGURATION] Add the probability sampler to file configuration [#4334](https://github.com/open-telemetry/opentelemetry-cpp/pull/4334) +* [BUG] Stop reading past a `nostd::string_view` that is not NUL terminated + [#4346](https://github.com/open-telemetry/opentelemetry-cpp/pull/4346) * [OTLP/HTTP] Honor `Retry-After` response header when retrying exports, supporting both delay-seconds and HTTP-date formats per RFC 7231 ยง7.1.3. diff --git a/examples/http/client.cc b/examples/http/client.cc index b919e86430..a1e0aad80a 100644 --- a/examples/http/client.cc +++ b/examples/http/client.cc @@ -71,7 +71,7 @@ void sendRequest(const std::string &url) span->SetAttribute(semconv::http::kHttpResponseStatusCode, status_code); result.GetResponse().ForEachHeader( [&span](nostd::string_view header_name, nostd::string_view header_value) { - span->SetAttribute("http.header." + std::string(header_name.data()), header_value); + span->SetAttribute("http.header." + static_cast(header_name), header_value); return true; }); diff --git a/exporters/elasticsearch/src/es_log_record_exporter.cc b/exporters/elasticsearch/src/es_log_record_exporter.cc index a87a514f01..e8b777a8e9 100644 --- a/exporters/elasticsearch/src/es_log_record_exporter.cc +++ b/exporters/elasticsearch/src/es_log_record_exporter.cc @@ -59,7 +59,7 @@ class ResponseHandler : public http_client::EventHandler ss << "Status:" << response.GetStatusCode() << ", Header:"; response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name, opentelemetry::nostd::string_view header_value) { - ss << "\t" << header_name.data() << ": " << header_value.data() << ","; + ss << "\t" << header_name << ": " << header_value << ","; return true; }); ss << "Body:" << body; diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 1f5e458c2c..5153e7141e 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -99,7 +99,7 @@ class ResponseHandler : public http_client::EventHandler ss << "Status:" << response.GetStatusCode() << ", Header:"; response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name, opentelemetry::nostd::string_view header_value) { - ss << "\t" << header_name.data() << ": " << header_value.data() << ","; + ss << "\t" << header_name << ": " << header_value << ","; return true; }); ss << "Body:" << body; diff --git a/ext/include/opentelemetry/ext/http/client/http_client.h b/ext/include/opentelemetry/ext/http/client/http_client.h index 5637519821..b477292fed 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client.h +++ b/ext/include/opentelemetry/ext/http/client/http_client.h @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -148,7 +147,8 @@ struct HttpSslOptions ssl_cipher_suite(input_ssl_cipher_suite) { /* Use SSL if url starts with "https:" */ - if (strncmp(url.data(), "https:", 6) == 0) + const nostd::string_view https_scheme{"https:"}; + if (url.substr(0, https_scheme.size()) == https_scheme) { use_ssl = true; } diff --git a/ext/test/http/url_parser_test.cc b/ext/test/http/url_parser_test.cc index b4cd095ce0..1c18655902 100644 --- a/ext/test/http/url_parser_test.cc +++ b/ext/test/http/url_parser_test.cc @@ -8,9 +8,13 @@ #include #include +#include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/ext/http/common/url_parser.h" +#include "opentelemetry/nostd/string_view.h" namespace http_common = opentelemetry::ext::http::common; +namespace http_client = opentelemetry::ext::http::client; +namespace nostd = opentelemetry::nostd; namespace { @@ -142,4 +146,24 @@ TEST_P(UrlDecoderTests, BasicTests) EXPECT_EQ(actual, expected); } +http_client::HttpSslOptions SslOptionsFor(nostd::string_view url) +{ + return http_client::HttpSslOptions{url, false, "", "", "", "", "", "", "", "", "", ""}; +} + +// HttpSslOptions decides use_ssl from the scheme the caller passed, and the url is a string_view, +// so it can be a window onto a longer buffer. Reading one byte past it changes the answer: a view +// holding "https" over a buffer that continues with a colon reads as a secure scheme. +TEST(HttpSslOptionsTests, UsesOnlyTheGivenUrlToDecideTheScheme) +{ + const char buffer[] = "https://example.com"; + + EXPECT_TRUE(SslOptionsFor(nostd::string_view{buffer}).use_ssl); + EXPECT_FALSE(SslOptionsFor(nostd::string_view{"http://example.com"}).use_ssl); + + // The same buffer, but the caller offers only the five characters before the colon. + EXPECT_FALSE(SslOptionsFor(nostd::string_view{buffer, 5}).use_ssl) + << "read past the end of the url view"; +} + } // namespace From 59b31d0a8b6818031ee89b75bc1e679e4e17e752 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Tue, 4 Aug 2026 01:17:07 +0800 Subject: [PATCH 2/2] Build the header name string rather than casting to one @marcalff on #4346: a cast to a class type reads as a conversion where the intent is a temporary. `nostd::string_view` carries an explicit `operator std::string`, which direct initialisation reaches just as the cast did, so the two forms compile to the same thing and this one says what it is. Compiles clean in both configurations of the type: the fallback `nostd::string_view` class, and `WITH_STL=CXX17` where it is `std::string_view` and the standard library's own string_view constructor takes over. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- examples/http/client.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/http/client.cc b/examples/http/client.cc index a1e0aad80a..3b65ba5684 100644 --- a/examples/http/client.cc +++ b/examples/http/client.cc @@ -71,7 +71,7 @@ void sendRequest(const std::string &url) span->SetAttribute(semconv::http::kHttpResponseStatusCode, status_code); result.GetResponse().ForEachHeader( [&span](nostd::string_view header_name, nostd::string_view header_value) { - span->SetAttribute("http.header." + static_cast(header_name), header_value); + span->SetAttribute("http.header." + std::string(header_name), header_value); return true; });