Skip to content

Commit

Permalink
Merge pull request #228 from bugsnag/next
Browse files Browse the repository at this point in the history
## 2.4.0 (2024-04-15)

### Enhancements

* Sanitize for metadata should also handler json and []byte
  [#226](#226)
  [Chris Duncan](https://github.com/veqryn)
  • Loading branch information
DariaKunoichi committed Apr 15, 2024
2 parents c17eb96 + 245bce0 commit 4ae0bc0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 2.4.0 (2024-04-15)

### Enhancements

* Sanitize for metadata should also handler json and []byte
[#226](https://github.com/bugsnag/bugsnag-go/pull/226)
[Chris Duncan](https://github.com/veqryn)

## 2.3.1 (2024-03-18)

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion v2/bugsnag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// Version defines the version of this Bugsnag notifier
const Version = "2.3.1"
const Version = "2.4.0"

var panicHandlerOnce sync.Once
var sessionTrackerOnce sync.Once
Expand Down
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

0 comments on commit 4ae0bc0

Please sign in to comment.