diff --git a/CHANGELOG.md b/CHANGELOG.md index 688ebc3c14..59562d7854 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,10 +15,15 @@ Increment the: ## [Unreleased] +* [SDK] `OTELResourceDetector` now percent-decodes values parsed from the + `OTEL_RESOURCE_ATTRIBUTES` environment variable, per the W3C Baggage value + grammar the resource spec defers to. A malformed escape sequence is left + in the value as-is rather than dropping the attribute. + [#1536](https://github.com/open-telemetry/opentelemetry-cpp/issues/1536) + * [BUG] Report one outcome per request when a curl session is cancelled after the response arrives ([#4363](https://github.com/open-telemetry/opentelemetry-cpp/pull/4363)) - * [CODE HEALTH] Enable clang-tidy `modernize-deprecated-headers` and replace deprecated C headers (`stdint.h`, `stddef.h`, `stdlib.h`, `string.h`, `stdio.h`, `ctype.h`, `limits.h`, `assert.h`) with their C++ equivalents diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 5da994b727..cc402ecc5e 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -8,6 +8,7 @@ #include "opentelemetry/semconv/service_attributes.h" #include "opentelemetry/version.h" +#include #include #include #include @@ -21,6 +22,43 @@ namespace resource constexpr const char *kOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES"; constexpr const char *kOtelServiceName = "OTEL_SERVICE_NAME"; +namespace +{ +unsigned char HexDigitValue(unsigned char c) noexcept +{ + return static_cast(std::isdigit(c) ? c - '0' : std::toupper(c) - 'A' + 10); +} + +// Percent-decodes a value from OTEL_RESOURCE_ATTRIBUTES, per the W3C Baggage value grammar +// the resource spec defers to +// (https://github.com/open-telemetry/opentelemetry-specification/pull/2670). A malformed escape +// sequence is left in the output as-is rather than dropping the attribute, since this parser has +// never validated attribute syntax. +std::string PercentDecode(const std::string &value) +{ + std::string decoded; + decoded.reserve(value.size()); + for (std::size_t i = 0; i < value.size(); ++i) + { + if (value[i] == '%' && i + 2 < value.size()) + { + unsigned char high = static_cast(value[i + 1]); + unsigned char low = static_cast(value[i + 2]); + if (std::isxdigit(high) && std::isxdigit(low)) + { + unsigned char unescaped_value = + static_cast((HexDigitValue(high) << 4) | HexDigitValue(low)); + decoded.push_back(static_cast(unescaped_value)); + i += 2; + continue; + } + } + decoded.push_back(value[i]); + } + return decoded; +} +} // namespace + Resource ResourceDetector::Create(const ResourceAttributes &attributes, const std::string &schema_url) { @@ -54,7 +92,7 @@ Resource OTELResourceDetector::Detect() noexcept { std::string key = token.substr(0, pos); std::string value = token.substr(pos + 1); - attributes[key] = value; + attributes[key] = PercentDecode(value); } } } diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 792ce96050..c3467a43cb 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -238,6 +238,51 @@ TEST(ResourceTest, OtelResourceDetector) unsetenv("OTEL_RESOURCE_ATTRIBUTES"); } +TEST(ResourceTest, OtelResourceDetectorPercentDecodesValues) +{ + std::map expected_attributes = { + {"key1", "hello world"}, {"key2", "a,b"}, {"key3", "100%"}}; + + setenv("OTEL_RESOURCE_ATTRIBUTES", "key1=hello%20world,key2=a%2Cb,key3=100%25", 1); + + OTELResourceDetector detector; + auto resource = detector.Detect(); + auto received_attributes = resource.GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + { + EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get(e.second)); + } + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); + + unsetenv("OTEL_RESOURCE_ATTRIBUTES"); +} + +TEST(ResourceTest, OtelResourceDetectorMalformedEscapeLeftAsIs) +{ + std::map expected_attributes = {{"key", "100%"}, {"bad", "50%z"}}; + + setenv("OTEL_RESOURCE_ATTRIBUTES", "key=100%,bad=50%z", 1); + + OTELResourceDetector detector; + auto resource = detector.Detect(); + auto received_attributes = resource.GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + { + EXPECT_EQ(expected_attributes.find(e.first)->second, nostd::get(e.second)); + } + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); + + unsetenv("OTEL_RESOURCE_ATTRIBUTES"); +} + TEST(ResourceTest, OtelResourceDetectorServiceNameOverride) { std::map expected_attributes = {{"service.name", "new_name"}};