Skip to content
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

Sanitize should also handler json and []byte #226

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion v2/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bugsnag

import (
"encoding"
"encoding/json"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -98,7 +99,7 @@ func (s sanitizer) Sanitize(data interface{}) interface{} {
}
}

// Handle certain well known interfaces and types
// Handle certain well known interfaces and types, in preferred order
switch dataT := data.(type) {
case error:
return dataT.Error()
Expand All @@ -114,6 +115,14 @@ func (s sanitizer) Sanitize(data interface{}) interface{} {
if b, err := dataT.MarshalText(); err == nil {
return string(b)
}

case json.Marshaler:
if b, err := dataT.MarshalJSON(); err == nil {
return string(b)
}

case []byte:
return string(dataT)
}

switch t.Kind() {
Expand Down
80 changes: 71 additions & 9 deletions v2/metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bugsnag

import (
"encoding/json"
stderrors "errors"
"reflect"
"testing"
Expand Down Expand Up @@ -112,13 +113,13 @@ func TestMetadataAddPointer(t *testing.T) {

func TestMetadataAddNil(t *testing.T) {
md := MetaData{}
md.AddStruct("map", map[string]interface{}{
"data": _testStruct{Name: nil},
})

var nilMap map[string]interface{}
md.AddStruct("nilmap", nilMap)

var nilSlice []interface{}
md.AddStruct("nilSlice", nilSlice)

var nilError _testError
md.AddStruct("error", nilError)

Expand All @@ -137,20 +138,77 @@ func TestMetadataAddNil(t *testing.T) {
var marshalFullPtr = &_textMarshaller{}
md.AddStruct("marshalFullPtr", marshalFullPtr)

var nullJsonMarshaller json.RawMessage
md.AddStruct("nullJsonMarshaller", nullJsonMarshaller)

var nullJsonMarshallerPtr *json.RawMessage
md.AddStruct("nullJsonMarshallerPtr", nullJsonMarshallerPtr)

var emptyJsonMarshaller = &json.RawMessage{}
md.AddStruct("emptyJsonMarshaller", emptyJsonMarshaller)

var nilBytes []byte
md.AddStruct("nilBytes", nilBytes)

var nilBytesPtr *[]byte
md.AddStruct("nilBytesPtr", nilBytesPtr)

var emptyBytes = []byte{}
md.AddStruct("emptyBytes", emptyBytes)

md.AddStruct("map", map[string]interface{}{
"data": _testStruct{Name: nil},
"nilmap": nilMap,
"nilSlice": nilSlice,
"error": nilError,
"errorNilPtr": nilErrorPtr,
"timeUnset": timeVar,
"durationUnset": duration,
"marshalNilPtr": marshalNilPtr,
"marshalFullPtr": marshalFullPtr,
"nullJsonMarshaller": nullJsonMarshaller,
"nullJsonMarshallerPtr": nullJsonMarshallerPtr,
"emptyJsonMarshaller": emptyJsonMarshaller,
"nilBytes": nilBytes,
"nilBytesPtr": nilBytesPtr,
"emptyBytes": emptyBytes,
})

if !reflect.DeepEqual(md, MetaData{
"map": {
"data": map[string]interface{}{
"Name": "<nil>",
},
"nilmap": map[string]interface{}{},
"nilSlice": []interface{}{},
"error": "errorstr",
"errorNilPtr": "<nil>",
"timeUnset": "0001-01-01T00:00:00Z",
"durationUnset": "0s",
"marshalFullPtr": "marshalled text",
"marshalNilPtr": "<nil>",
"nullJsonMarshaller": "null",
"nullJsonMarshallerPtr": "<nil>",
"emptyJsonMarshaller": "",
"nilBytes": "",
"nilBytesPtr": "<nil>",
"emptyBytes": "",
},
"nilmap": map[string]interface{}{},
"Extra data": {
"error": "errorstr",
"errorNilPtr": "<nil>",
"timeUnset": "0001-01-01T00:00:00Z",
"durationUnset": "0s",
"marshalFullPtr": "marshalled text",
"marshalNilPtr": "<nil>",
"nilSlice": []interface{}{},
"error": "errorstr",
"errorNilPtr": "<nil>",
"timeUnset": "0001-01-01T00:00:00Z",
"durationUnset": "0s",
"marshalFullPtr": "marshalled text",
"marshalNilPtr": "<nil>",
"nullJsonMarshaller": "null",
"nullJsonMarshallerPtr": "<nil>",
"emptyJsonMarshaller": "",
"nilBytes": "",
"nilBytesPtr": "<nil>",
"emptyBytes": "",
},
}) {
t.Errorf("metadata.AddStruct didn't work: %#v", md)
Expand Down Expand Up @@ -215,6 +273,8 @@ func TestMetaDataSanitize(t *testing.T) {
"time": time.Date(2023, 12, 5, 23, 59, 59, 123456789, time.UTC),
"duration": 105567462 * time.Millisecond,
"text": _textMarshaller{},
"json": json.RawMessage(`{"json_property": "json_value"}`),
"bytes": []byte(`lots of bytes`),
"array": []hash{{
"creditcard": "1234567812345678",
"broken": broken,
Expand All @@ -240,6 +300,8 @@ func TestMetaDataSanitize(t *testing.T) {
"time": "2023-12-05T23:59:59.123456789Z",
"duration": "29h19m27.462s",
"text": "marshalled text",
"json": `{"json_property": "json_value"}`,
"bytes": "lots of bytes",
"array": []interface{}{map[string]interface{}{
"creditcard": "[FILTERED]",
"broken": map[string]interface{}{
Expand Down