Skip to content

config: fix panic on nil value in headers name/value pair #6425

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 10 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Allow marshaling types in `go.opentelemetry.io/contrib/config`. (#6347)
- Removed the redundant handling of panic from the `HTML` function in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#6373)
- The `code.function` attribute emitted by `go.opentelemetry.io/contrib/bridges/otelslog` now stores just the function name instead the package path-qualified function name. The `code.namespace` attribute now stores the package path. (#6415)
- Return an error for `nil` values when unmarshaling `NameStringValuePair` in `go.opentelemetry.io/contrib/config`. (#6425)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
49 changes: 49 additions & 0 deletions config/testdata/invalid_nil_value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"file_format": "0.3",
"disabled": false,
"logger_provider": {
"processors": [
{
"batch": {
"schedule_delay": 5000,
"export_timeout": 30000,
"max_queue_size": 2048,
"max_export_batch_size": 512,
"exporter": {
"otlp": {
"protocol": "http/protobuf",
"endpoint": "http://localhost:4318/v1/logs",
"certificate": "/app/cert.pem",
"client_key": "/app/cert.pem",
"client_certificate": "/app/cert.pem",
"headers": [
{
"name": "api-key",
"value": "1234"
},
{
"name": "nil-value"
}
],
"headers_list": "api-key=1234",
"compression": "gzip",
"timeout": 10000,
"insecure": false
}
}
}
},
{
"simple": {
"exporter": {
"console": {}
}
}
}
],
"limits": {
"attribute_value_length_limit": 4096,
"attribute_count_limit": 128
}
}
}
13 changes: 13 additions & 0 deletions config/testdata/invalid_nil_value.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
file_format: "0.3"
disabled: false
logger_provider:
processors:
- batch:
exporter:
otlp:
protocol: http/protobuf
endpoint: http://localhost:4318/v1/logs
headers:
- name: api-key
value: "1234"
- name: nil-value
4 changes: 2 additions & 2 deletions config/v0.3.0/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@
return err
}
if _, ok := raw["name"]; raw != nil && !ok {
return errors.New("field name in NameStringValuePair: required")
return errors.New("json: cannot unmarshal field name in NameStringValuePair required")

Check warning on line 116 in config/v0.3.0/config_json.go

View check run for this annotation

Codecov / codecov/patch

config/v0.3.0/config_json.go#L116

Added line #L116 was not covered by tests
}
if _, ok := raw["value"]; raw != nil && !ok {
return errors.New("field value in NameStringValuePair: required")
return errors.New("json: cannot unmarshal field value in NameStringValuePair required")
}
type Plain NameStringValuePair
var plain Plain
Expand Down
12 changes: 12 additions & 0 deletions config/v0.3.0/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ func TestParseYAML(t *testing.T) {
wantErr: errors.New(`yaml: unmarshal errors:
line 2: cannot unmarshal !!str ` + "`notabool`" + ` into bool`),
},
{
name: "invalid nil value",
input: "invalid_nil_value.yaml",
wantErr: errors.New(`yaml: cannot unmarshal field value in NameStringValuePair required`),
},
{
name: "valid v0.2 config",
input: "v0.2.yaml",
Expand All @@ -429,6 +434,7 @@ func TestParseYAML(t *testing.T) {

got, err := ParseYAML(b)
if tt.wantErr != nil {
require.Error(t, err)
require.Equal(t, tt.wantErr.Error(), err.Error())
} else {
require.NoError(t, err)
Expand Down Expand Up @@ -459,6 +465,11 @@ func TestSerializeJSON(t *testing.T) {
input: "invalid_bool.json",
wantErr: errors.New(`json: cannot unmarshal string into Go struct field Plain.disabled of type bool`),
},
{
name: "invalid nil value",
input: "invalid_nil_value.json",
wantErr: errors.New(`json: cannot unmarshal field value in NameStringValuePair required`),
},
{
name: "valid v0.2 config",
input: "v0.2.json",
Expand All @@ -480,6 +491,7 @@ func TestSerializeJSON(t *testing.T) {
err = json.Unmarshal(b, &got)

if tt.wantErr != nil {
require.Error(t, err)
require.Equal(t, tt.wantErr.Error(), err.Error())
} else {
require.NoError(t, err)
Expand Down
22 changes: 22 additions & 0 deletions config/v0.3.0/config_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package config // import "go.opentelemetry.io/contrib/config/v0.3.0"

import (
"errors"
"fmt"
"reflect"
)
Expand All @@ -30,6 +31,27 @@
return nil
}

// UnmarshalYAML implements json.Unmarshaler.
func (j *NameStringValuePair) UnmarshalYAML(unmarshal func(interface{}) error) error {
var raw map[string]interface{}
if err := unmarshal(&raw); err != nil {
return err
}

Check warning on line 39 in config/v0.3.0/config_yaml.go

View check run for this annotation

Codecov / codecov/patch

config/v0.3.0/config_yaml.go#L38-L39

Added lines #L38 - L39 were not covered by tests
if _, ok := raw["name"]; raw != nil && !ok {
return errors.New("yaml: cannot unmarshal field name in NameStringValuePair required")
}

Check warning on line 42 in config/v0.3.0/config_yaml.go

View check run for this annotation

Codecov / codecov/patch

config/v0.3.0/config_yaml.go#L41-L42

Added lines #L41 - L42 were not covered by tests
if _, ok := raw["value"]; raw != nil && !ok {
return errors.New("yaml: cannot unmarshal field value in NameStringValuePair required")
}
type Plain NameStringValuePair
var plain Plain
if err := unmarshal(&plain); err != nil {
return err
}

Check warning on line 50 in config/v0.3.0/config_yaml.go

View check run for this annotation

Codecov / codecov/patch

config/v0.3.0/config_yaml.go#L49-L50

Added lines #L49 - L50 were not covered by tests
*j = NameStringValuePair(plain)
return nil
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (j *LanguageSpecificInstrumentation) UnmarshalYAML(unmarshal func(interface{}) error) error {
var raw map[string]interface{}
Expand Down
Loading