Skip to content

Commit

Permalink
Use url.PathUnescape rather than url.QueryUnescape when parsing OTLP …
Browse files Browse the repository at this point in the history
…headers and resource attributes env vars (#4698)
  • Loading branch information
mikeblum committed Nov 8, 2023
1 parent 0c5ebd5 commit 192db26
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixed

- Use url.PathUnescape rather than url.QueryUnescape when parsing OTLP headers and resource attributes env vars (#4698)
- Fix improper parsing of characters such us `+`, `\` by `Parse` in `go.opentelemetry.io/otel/baggage` as they were rendered as a whitespace. (#4667)
- In `go.opentelemetry.op/otel/exporters/prometheus`, the exporter no longer `Collect`s metrics after `Shutdown` is invoked. (#4648)
- Fix documentation for `WithCompressor` in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc`. (#4695)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ func stringToHeader(value string) map[string]string {
global.Error(errors.New("missing '="), "parse headers", "input", header)
continue
}
name, err := url.QueryUnescape(n)
name, err := url.PathUnescape(n)
if err != nil {
global.Error(err, "escape header key", "key", n)
continue
}
trimmedName := strings.TrimSpace(name)
value, err := url.QueryUnescape(v)
value, err := url.PathUnescape(v)
if err != nil {
global.Error(err, "escape header value", "value", v)
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ func stringToHeader(value string) map[string]string {
global.Error(errors.New("missing '="), "parse headers", "input", header)
continue
}
name, err := url.QueryUnescape(n)
name, err := url.PathUnescape(n)
if err != nil {
global.Error(err, "escape header key", "key", n)
continue
}
trimmedName := strings.TrimSpace(name)
value, err := url.QueryUnescape(v)
value, err := url.PathUnescape(v)
if err != nil {
global.Error(err, "escape header value", "value", v)
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ func stringToHeader(value string) map[string]string {
global.Error(errors.New("missing '="), "parse headers", "input", header)
continue
}
name, err := url.QueryUnescape(n)
name, err := url.PathUnescape(n)
if err != nil {
global.Error(err, "escape header key", "key", n)
continue
}
trimmedName := strings.TrimSpace(name)
value, err := url.QueryUnescape(v)
value, err := url.PathUnescape(v)
if err != nil {
global.Error(err, "escape header value", "value", v)
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ func TestStringToHeader(t *testing.T) {
value: " userId = alice ",
want: map[string]string{"userId": "alice"},
},
{
name: "encodes headers per RFC 1738 spec",
value: " userId = alice+test ",
want: map[string]string{"userId": "alice+test"},
},
{
name: "multiples headers encoded",
value: "userId=alice,serverNode=DF%3A28,isProduction=false",
Expand All @@ -435,6 +440,16 @@ func TestStringToHeader(t *testing.T) {
"isProduction": "false",
},
},
{
name: "multiples headers encoded per RFC 1738 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ func stringToHeader(value string) map[string]string {
global.Error(errors.New("missing '="), "parse headers", "input", header)
continue
}
name, err := url.QueryUnescape(n)
name, err := url.PathUnescape(n)
if err != nil {
global.Error(err, "escape header key", "key", n)
continue
}
trimmedName := strings.TrimSpace(name)
value, err := url.QueryUnescape(v)
value, err := url.PathUnescape(v)
if err != nil {
global.Error(err, "escape header value", "value", v)
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ func TestStringToHeader(t *testing.T) {
value: " userId = alice ",
want: map[string]string{"userId": "alice"},
},
{
name: "encodes headers per RFC 1738 spec",
value: " userId = alice+test ",
want: map[string]string{"userId": "alice+test"},
},
{
name: "multiples headers encoded",
value: "userId=alice,serverNode=DF%3A28,isProduction=false",
Expand All @@ -435,6 +440,16 @@ func TestStringToHeader(t *testing.T) {
"isProduction": "false",
},
},
{
name: "multiples headers encoded per RFC 1738 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",
Expand Down
4 changes: 2 additions & 2 deletions internal/shared/otlp/envconfig/envconfig.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ func stringToHeader(value string) map[string]string {
global.Error(errors.New("missing '="), "parse headers", "input", header)
continue
}
name, err := url.QueryUnescape(n)
name, err := url.PathUnescape(n)
if err != nil {
global.Error(err, "escape header key", "key", n)
continue
}
trimmedName := strings.TrimSpace(name)
value, err := url.QueryUnescape(v)
value, err := url.PathUnescape(v)
if err != nil {
global.Error(err, "escape header value", "value", v)
continue
Expand Down
2 changes: 1 addition & 1 deletion sdk/resource/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func constructOTResources(s string) (*Resource, error) {
continue
}
key := strings.TrimSpace(k)
val, err := url.QueryUnescape(strings.TrimSpace(v))
val, err := url.PathUnescape(strings.TrimSpace(v))
if err != nil {
// Retain original value if decoding fails, otherwise it will be
// an empty string.
Expand Down

0 comments on commit 192db26

Please sign in to comment.