Skip to content

Stop percent-encoding the header environment variables in otlplog exporters #6392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Drop support for [Go 1.22]. (#6381)

### Fixes

- Stop percent encoding header environment variables in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc` and `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#6392)

<!-- Released section -->
<!-- Don't change this section unless doing release -->

Expand Down
33 changes: 30 additions & 3 deletions exporters/otlp/otlplog/otlploggrpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -442,13 +443,15 @@ func convHeaders(s string) (map[string]string, error) {
continue
}

escKey, e := url.PathUnescape(rawKey)
if e != nil {
key := strings.TrimSpace(rawKey)

// Validate the key.
if !isValidHeaderKey(key) {
err = errors.Join(err, fmt.Errorf("invalid header key: %s", rawKey))
continue
}
key := strings.TrimSpace(escKey)

// Only decode the value.
escVal, e := url.PathUnescape(rawVal)
if e != nil {
err = errors.Join(err, fmt.Errorf("invalid header value: %s", rawVal))
Expand All @@ -458,6 +461,11 @@ func convHeaders(s string) (map[string]string, error) {

out[key] = val
}
// If at least one valid header exists, reset err to nil
if len(out) > 0 {
global.Warn("Some headers were invalid but at least one valid header exists: %v", err)
err = nil
}
return out, err
}

Expand Down Expand Up @@ -651,3 +659,22 @@ func fallback[T any](val T) resolver[T] {
return s
}
}

func isValidHeaderKey(key string) bool {
if key == "" {
return false
}
for _, c := range key {
if !isTokenChar(c) {
return false
}
}
return true
}

func isTokenChar(c rune) bool {
return c <= unicode.MaxASCII && (unicode.IsLetter(c) ||
unicode.IsDigit(c) ||
c == '!' || c == '#' || c == '$' || c == '%' || c == '&' || c == '\'' || c == '*' ||
c == '+' || c == '-' || c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~')
}
121 changes: 116 additions & 5 deletions exporters/otlp/otlplog/otlploggrpc/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func TestNewConfig(t *testing.T) {
require.NoError(t, err, "testing TLS config")

headers := map[string]string{"a": "A"}
percentDecodedHeaders := map[string]string{"user%2Did": "42", "user%20name": "alice smith"}
validHeaders := map[string]string{"valid-key": "value"}
rc := retry.Config{}

dialOptions := []grpc.DialOption{grpc.WithUserAgent("test-agent")}
Expand Down Expand Up @@ -338,7 +340,7 @@ func TestNewConfig(t *testing.T) {
name: "InvalidEnvironmentVariables",
envars: map[string]string{
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "%invalid",
"OTEL_EXPORTER_OTLP_LOGS_HEADERS": "a,%ZZ=valid,key=%ZZ",
"OTEL_EXPORTER_OTLP_LOGS_HEADERS": "invalid key=value",
"OTEL_EXPORTER_OTLP_LOGS_COMPRESSION": "xz",
"OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": "100 seconds",
"OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE": "invalid_cert",
Expand All @@ -355,10 +357,7 @@ func TestNewConfig(t *testing.T) {
`failed to load TLS:`,
`certificate not added`,
`tls: failed to find any PEM data in certificate input`,
`invalid OTEL_EXPORTER_OTLP_LOGS_HEADERS value a,%ZZ=valid,key=%ZZ:`,
`invalid header: a`,
`invalid header key: %ZZ`,
`invalid header value: %ZZ`,
`invalid OTEL_EXPORTER_OTLP_LOGS_HEADERS value invalid key=value: invalid header key: invalid key`,
`invalid OTEL_EXPORTER_OTLP_LOGS_COMPRESSION value xz: unknown compression: xz`,
`invalid OTEL_EXPORTER_OTLP_LOGS_TIMEOUT value 100 seconds: strconv.Atoi: parsing "100 seconds": invalid syntax`,
},
Expand Down Expand Up @@ -440,6 +439,48 @@ func TestNewConfig(t *testing.T) {
timeout: newSetting(defaultTimeout),
},
},
{
name: "with percent-encoded headers",
envars: map[string]string{
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "https://env.endpoint:8080/prefix",
"OTEL_EXPORTER_OTLP_LOGS_HEADERS": "user%2Did=42,user%20name=alice%20smith",
"OTEL_EXPORTER_OTLP_LOGS_COMPRESSION": "gzip",
"OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": "15000",
"OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE": "cert_path",
"OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE": "cert_path",
"OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY": "key_path",
},
want: config{
endpoint: newSetting("env.endpoint:8080"),
insecure: newSetting(false),
tlsCfg: newSetting(tlsCfg),
headers: newSetting(percentDecodedHeaders),
compression: newSetting(GzipCompression),
timeout: newSetting(15 * time.Second),
retryCfg: newSetting(defaultRetryCfg),
},
},
{
name: "with invalid header key",
envars: map[string]string{
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "https://env.endpoint:8080/prefix",
"OTEL_EXPORTER_OTLP_LOGS_HEADERS": "valid-key=value,invalid key=value",
"OTEL_EXPORTER_OTLP_LOGS_COMPRESSION": "gzip",
"OTEL_EXPORTER_OTLP_LOGS_TIMEOUT": "15000",
"OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE": "cert_path",
"OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE": "cert_path",
"OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY": "key_path",
},
want: config{
endpoint: newSetting("env.endpoint:8080"),
insecure: newSetting(false),
tlsCfg: newSetting(tlsCfg),
headers: newSetting(validHeaders),
compression: newSetting(GzipCompression),
timeout: newSetting(15 * time.Second),
retryCfg: newSetting(defaultRetryCfg),
},
},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -494,3 +535,73 @@ func assertTLSConfig(t *testing.T, want, got setting[*tls.Config]) {
}
assert.Equal(t, want.Value.Certificates, got.Value.Certificates, "Certificates")
}

func TestConvHeaders(t *testing.T) {
tests := []struct {
name string
value string
want map[string]string
}{
{
name: "simple test",
value: "userId=alice",
want: map[string]string{"userId": "alice"},
},
{
name: "simple test with spaces",
value: " userId = alice ",
want: map[string]string{"userId": "alice"},
},
{
name: "simple header conforms to RFC 3986 spec",
value: " userId = alice+test ",
want: map[string]string{"userId": "alice+test"},
},
{
name: "multiple headers encoded",
value: "userId=alice,serverNode=DF%3A28,isProduction=false",
want: map[string]string{
"userId": "alice",
"serverNode": "DF:28",
"isProduction": "false",
},
},
{
name: "multiple headers encoded per RFC 3986 spec",
value: "userId=alice+test,serverNode=DF%3A28,isProduction=false,namespace=localhost/test",
want: map[string]string{
"userId": "alice+test",
"serverNode": "DF:28",
"isProduction": "false",
"namespace": "localhost/test",
},
},
{
name: "invalid headers format",
value: "userId:alice",
want: map[string]string{},
},
{
name: "invalid key",
value: "%XX=missing,userId=alice",
want: map[string]string{
"%XX": "missing",
"userId": "alice",
},
},
{
name: "invalid value",
value: "missing=%XX,userId=alice",
want: map[string]string{
"userId": "alice",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
keyValues, _ := convHeaders(tt.value)
assert.Equal(t, tt.want, keyValues)
})
}
}
33 changes: 30 additions & 3 deletions exporters/otlp/otlplog/otlploghttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/retry"
Expand Down Expand Up @@ -544,13 +545,15 @@ func convHeaders(s string) (map[string]string, error) {
continue
}

escKey, e := url.PathUnescape(rawKey)
if e != nil {
key := strings.TrimSpace(rawKey)

// Validate the key.
if !isValidHeaderKey(key) {
err = errors.Join(err, fmt.Errorf("invalid header key: %s", rawKey))
continue
}
key := strings.TrimSpace(escKey)

// Only decode the value.
escVal, e := url.PathUnescape(rawVal)
if e != nil {
err = errors.Join(err, fmt.Errorf("invalid header value: %s", rawVal))
Expand All @@ -560,6 +563,11 @@ func convHeaders(s string) (map[string]string, error) {

out[key] = val
}
// If at least one valid header exists, reset err to nil
if len(out) > 0 {
global.Warn("Some headers were invalid but at least one valid header exists: %v", err)
err = nil
}
return out, err
}

Expand Down Expand Up @@ -600,3 +608,22 @@ func fallback[T any](val T) resolver[T] {
return s
}
}

func isValidHeaderKey(key string) bool {
if key == "" {
return false
}
for _, c := range key {
if !isTokenChar(c) {
return false
}
}
return true
}

func isTokenChar(c rune) bool {
return c <= unicode.MaxASCII && (unicode.IsLetter(c) ||
unicode.IsDigit(c) ||
c == '!' || c == '#' || c == '$' || c == '%' || c == '&' || c == '\'' || c == '*' ||
c == '+' || c == '-' || c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~')
}
Loading
Loading