diff --git a/CHANGELOG.md b/CHANGELOG.md index f0cf628784..d32921f859 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,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..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." + std::string(header_name.data()), header_value); + span->SetAttribute("http.header." + std::string(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 f8b6ec7b11..af819c8eb7 100644 --- a/exporters/elasticsearch/src/es_log_record_exporter.cc +++ b/exporters/elasticsearch/src/es_log_record_exporter.cc @@ -61,7 +61,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