From c03af7184afedc58573138c2abcb4ef0a5e6af06 Mon Sep 17 00:00:00 2001 From: om7057 Date: Wed, 5 Aug 2026 23:57:24 +0530 Subject: [PATCH 1/5] [SDK] Percent-decode values from OTEL_RESOURCE_ATTRIBUTES The resource spec was updated to require decoding OTEL_RESOURCE_ATTRIBUTES values per the W3C Baggage value grammar, but OTELResourceDetector::Detect() stored the raw substring after '=' verbatim. A value like "key=hello%20world" therefore became the literal string "hello%20world" instead of "hello world". Adds a local percent-decode helper scoped to resource_detector.cc; a malformed escape sequence is left in the value as-is rather than dropping the attribute, consistent with this parser never validating attribute syntax elsewhere. Fixes #1536 --- CHANGELOG.md | 5 +++ sdk/src/resource/resource_detector.cc | 43 ++++++++++++++++++++++++- sdk/test/resource/resource_test.cc | 45 +++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f584c3b765..640f506cb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,11 @@ 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) * [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..988fddd96d 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,46 @@ namespace resource constexpr const char *kOtelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES"; constexpr const char *kOtelServiceName = "OTEL_SERVICE_NAME"; +namespace +{ +bool IsHexDigit(unsigned char c) noexcept +{ + return std::isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); +} + +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() && + IsHexDigit(static_cast(value[i + 1])) && + IsHexDigit(static_cast(value[i + 2]))) + { + decoded.push_back(static_cast((HexDigitValue(static_cast(value[i + 1])) + << 4) | + HexDigitValue(static_cast(value[i + 2])))); + i += 2; + } + else + { + decoded.push_back(value[i]); + } + } + return decoded; +} +} // namespace + Resource ResourceDetector::Create(const ResourceAttributes &attributes, const std::string &schema_url) { @@ -54,7 +95,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"}}; From 6d8a38ad3145068d8b4ab9901ac0a8fc5b32ebcc Mon Sep 17 00:00:00 2001 From: om7057 Date: Thu, 6 Aug 2026 00:05:37 +0530 Subject: [PATCH 2/5] Fix clang-format violations in resource_detector.cc --- sdk/src/resource/resource_detector.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 988fddd96d..4ec85db6ed 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -35,9 +35,10 @@ unsigned char HexDigitValue(unsigned char c) noexcept } // 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. +// 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; @@ -48,9 +49,9 @@ std::string PercentDecode(const std::string &value) IsHexDigit(static_cast(value[i + 1])) && IsHexDigit(static_cast(value[i + 2]))) { - decoded.push_back(static_cast((HexDigitValue(static_cast(value[i + 1])) - << 4) | - HexDigitValue(static_cast(value[i + 2])))); + decoded.push_back( + static_cast((HexDigitValue(static_cast(value[i + 1])) << 4) | + HexDigitValue(static_cast(value[i + 2])))); i += 2; } else From bbe407657df7f7a84a2a681aa8381aec7bc3e5af Mon Sep 17 00:00:00 2001 From: om7057 Date: Thu, 6 Aug 2026 08:04:29 +0530 Subject: [PATCH 3/5] Address review: evaluate value[i+1]/value[i+2] once, cast once Per marcalff's review comment, avoids re-evaluating and re-casting the same byte twice when checking and decoding a percent-escape. --- sdk/src/resource/resource_detector.cc | 28 ++++++++++++--------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 4ec85db6ed..cc402ecc5e 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -24,11 +24,6 @@ constexpr const char *kOtelServiceName = "OTEL_SERVICE_NAME"; namespace { -bool IsHexDigit(unsigned char c) noexcept -{ - return std::isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); -} - unsigned char HexDigitValue(unsigned char c) noexcept { return static_cast(std::isdigit(c) ? c - '0' : std::toupper(c) - 'A' + 10); @@ -45,19 +40,20 @@ std::string PercentDecode(const std::string &value) decoded.reserve(value.size()); for (std::size_t i = 0; i < value.size(); ++i) { - if (value[i] == '%' && i + 2 < value.size() && - IsHexDigit(static_cast(value[i + 1])) && - IsHexDigit(static_cast(value[i + 2]))) + if (value[i] == '%' && i + 2 < value.size()) { - decoded.push_back( - static_cast((HexDigitValue(static_cast(value[i + 1])) << 4) | - HexDigitValue(static_cast(value[i + 2])))); - i += 2; - } - else - { - decoded.push_back(value[i]); + 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; } From fab8b4111d7718e6760f3fc6b6a5bd2a36bbe8eb Mon Sep 17 00:00:00 2001 From: om7057 Date: Thu, 6 Aug 2026 21:52:13 +0530 Subject: [PATCH 4/5] Retrigger CI (previous run had cancelled/stuck Windows jobs) From 5ca08fbf3ce16466cddcc640a2027cc561751477 Mon Sep 17 00:00:00 2001 From: om7057 Date: Fri, 7 Aug 2026 08:53:02 +0530 Subject: [PATCH 5/5] Retrigger CI (previous run hit a GitHub Actions infra outage - Service Unavailable resolving actions)