Skip to content

Commit beb0913

Browse files
committed
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.
1 parent 84c3d1d commit beb0913

6 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Increment the:
2525
[#4245](https://github.com/open-telemetry/opentelemetry-cpp/pull/4245)
2626
* [CONFIGURATION] Add the probability sampler to file configuration
2727
[#4334](https://github.com/open-telemetry/opentelemetry-cpp/pull/4334)
28+
* [BUG] Stop reading past a `nostd::string_view` that is not NUL terminated
29+
[#4346](https://github.com/open-telemetry/opentelemetry-cpp/pull/4346)
2830

2931
* [OTLP/HTTP] Honor `Retry-After` response header when retrying exports,
3032
supporting both delay-seconds and HTTP-date formats per RFC 7231 §7.1.3.

‎examples/http/client.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void sendRequest(const std::string &url)
7171
span->SetAttribute(semconv::http::kHttpResponseStatusCode, status_code);
7272
result.GetResponse().ForEachHeader(
7373
[&span](nostd::string_view header_name, nostd::string_view header_value) {
74-
span->SetAttribute("http.header." + std::string(header_name.data()), header_value);
74+
span->SetAttribute("http.header." + static_cast<std::string>(header_name), header_value);
7575
return true;
7676
});
7777

‎exporters/elasticsearch/src/es_log_record_exporter.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ResponseHandler : public http_client::EventHandler
5959
ss << "Status:" << response.GetStatusCode() << ", Header:";
6060
response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name,
6161
opentelemetry::nostd::string_view header_value) {
62-
ss << "\t" << header_name.data() << ": " << header_value.data() << ",";
62+
ss << "\t" << header_name << ": " << header_value << ",";
6363
return true;
6464
});
6565
ss << "Body:" << body;

‎exporters/otlp/src/otlp_http_client.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class ResponseHandler : public http_client::EventHandler
9999
ss << "Status:" << response.GetStatusCode() << ", Header:";
100100
response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name,
101101
opentelemetry::nostd::string_view header_value) {
102-
ss << "\t" << header_name.data() << ": " << header_value.data() << ",";
102+
ss << "\t" << header_name << ": " << header_value << ",";
103103
return true;
104104
});
105105
ss << "Body:" << body;

‎ext/include/opentelemetry/ext/http/client/http_client.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include <chrono>
77
#include <cstdint>
8-
#include <cstring>
98
#include <map>
109
#include <memory>
1110
#include <string>
@@ -148,7 +147,8 @@ struct HttpSslOptions
148147
ssl_cipher_suite(input_ssl_cipher_suite)
149148
{
150149
/* Use SSL if url starts with "https:" */
151-
if (strncmp(url.data(), "https:", 6) == 0)
150+
const nostd::string_view https_scheme{"https:"};
151+
if (url.substr(0, https_scheme.size()) == https_scheme)
152152
{
153153
use_ssl = true;
154154
}

‎ext/test/http/url_parser_test.cc‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
#include <tuple>
99
#include <variant>
1010

11+
#include "opentelemetry/ext/http/client/http_client.h"
1112
#include "opentelemetry/ext/http/common/url_parser.h"
13+
#include "opentelemetry/nostd/string_view.h"
1214

1315
namespace http_common = opentelemetry::ext::http::common;
16+
namespace http_client = opentelemetry::ext::http::client;
17+
namespace nostd = opentelemetry::nostd;
1418

1519
namespace
1620
{
@@ -142,4 +146,24 @@ TEST_P(UrlDecoderTests, BasicTests)
142146
EXPECT_EQ(actual, expected);
143147
}
144148

149+
http_client::HttpSslOptions SslOptionsFor(nostd::string_view url)
150+
{
151+
return http_client::HttpSslOptions{url, false, "", "", "", "", "", "", "", "", "", ""};
152+
}
153+
154+
// HttpSslOptions decides use_ssl from the scheme the caller passed, and the url is a string_view,
155+
// so it can be a window onto a longer buffer. Reading one byte past it changes the answer: a view
156+
// holding "https" over a buffer that continues with a colon reads as a secure scheme.
157+
TEST(HttpSslOptionsTests, UsesOnlyTheGivenUrlToDecideTheScheme)
158+
{
159+
const char buffer[] = "https://example.com";
160+
161+
EXPECT_TRUE(SslOptionsFor(nostd::string_view{buffer}).use_ssl);
162+
EXPECT_FALSE(SslOptionsFor(nostd::string_view{"http://example.com"}).use_ssl);
163+
164+
// The same buffer, but the caller offers only the five characters before the colon.
165+
EXPECT_FALSE(SslOptionsFor(nostd::string_view{buffer, 5}).use_ssl)
166+
<< "read past the end of the url view";
167+
}
168+
145169
} // namespace

0 commit comments

Comments
 (0)