From 6a6ff19af5115a89064086f60fe4dc6be1194a62 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 9 Apr 2024 17:42:20 -0600 Subject: [PATCH 1/5] veqryn/marshal-json: convert json into strings --- v2/metadata.go | 6 ++++++ v2/metadata_test.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/v2/metadata.go b/v2/metadata.go index f812411..b1b778f 100644 --- a/v2/metadata.go +++ b/v2/metadata.go @@ -2,6 +2,7 @@ package bugsnag import ( "encoding" + "encoding/json" "fmt" "reflect" "strings" @@ -114,6 +115,11 @@ 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) + } } switch t.Kind() { diff --git a/v2/metadata_test.go b/v2/metadata_test.go index d67d56e..383a13c 100644 --- a/v2/metadata_test.go +++ b/v2/metadata_test.go @@ -1,6 +1,7 @@ package bugsnag import ( + "encoding/json" stderrors "errors" "reflect" "testing" @@ -215,6 +216,7 @@ 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(`{"hello": "world"}`), "array": []hash{{ "creditcard": "1234567812345678", "broken": broken, @@ -240,6 +242,7 @@ func TestMetaDataSanitize(t *testing.T) { "time": "2023-12-05T23:59:59.123456789Z", "duration": "29h19m27.462s", "text": "marshalled text", + "json": `{"hello": "world"}`, "array": []interface{}{map[string]interface{}{ "creditcard": "[FILTERED]", "broken": map[string]interface{}{ From d749f5e120a7f4b5f569c3ef7d0294eaa6edc224 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 9 Apr 2024 22:05:48 -0600 Subject: [PATCH 2/5] veqryn/marshal-json: also convert byte slices to strings --- v2/metadata.go | 5 ++++- v2/metadata_test.go | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/v2/metadata.go b/v2/metadata.go index b1b778f..68335b2 100644 --- a/v2/metadata.go +++ b/v2/metadata.go @@ -99,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() @@ -120,6 +120,9 @@ func (s sanitizer) Sanitize(data interface{}) interface{} { if b, err := dataT.MarshalJSON(); err == nil { return string(b) } + + case []byte: + return string(dataT) } switch t.Kind() { diff --git a/v2/metadata_test.go b/v2/metadata_test.go index 383a13c..51bd1f6 100644 --- a/v2/metadata_test.go +++ b/v2/metadata_test.go @@ -216,7 +216,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(`{"hello": "world"}`), + "json": json.RawMessage(`{"json_property": "json_value"}`), + "bytes": []byte(`lots of bytes`), "array": []hash{{ "creditcard": "1234567812345678", "broken": broken, @@ -242,7 +243,8 @@ func TestMetaDataSanitize(t *testing.T) { "time": "2023-12-05T23:59:59.123456789Z", "duration": "29h19m27.462s", "text": "marshalled text", - "json": `{"hello": "world"}`, + "json": `{"json_property": "json_value"}`, + "bytes": "lots of bytes", "array": []interface{}{map[string]interface{}{ "creditcard": "[FILTERED]", "broken": map[string]interface{}{ From b1ce8cb9ae351babd3f47050b36ca1d1cb446169 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 11 Apr 2024 23:09:58 -0600 Subject: [PATCH 3/5] veqryn/marshal-json: add nil tests --- v2/metadata_test.go | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/v2/metadata_test.go b/v2/metadata_test.go index 51bd1f6..e5d412c 100644 --- a/v2/metadata_test.go +++ b/v2/metadata_test.go @@ -120,6 +120,9 @@ func TestMetadataAddNil(t *testing.T) { var nilMap map[string]interface{} md.AddStruct("nilmap", nilMap) + var nilSlice []interface{} + md.AddStruct("nilSlice", nilSlice) + var nilError _testError md.AddStruct("error", nilError) @@ -138,6 +141,18 @@ func TestMetadataAddNil(t *testing.T) { var marshalFullPtr = &_textMarshaller{} md.AddStruct("marshalFullPtr", marshalFullPtr) + var nullJsonMarshaller json.RawMessage + md.AddStruct("nullJsonMarshaller", nullJsonMarshaller) + + var fullJsonMarshaller = &json.RawMessage{} + md.AddStruct("fullJsonMarshaller", fullJsonMarshaller) + + var nilBytes []byte + md.AddStruct("nilBytes", nilBytes) + + var emptyBytes = []byte{} + md.AddStruct("emptyBytes", emptyBytes) + if !reflect.DeepEqual(md, MetaData{ "map": { "data": map[string]interface{}{ @@ -146,12 +161,17 @@ func TestMetadataAddNil(t *testing.T) { }, "nilmap": map[string]interface{}{}, "Extra data": { - "error": "errorstr", - "errorNilPtr": "", - "timeUnset": "0001-01-01T00:00:00Z", - "durationUnset": "0s", - "marshalFullPtr": "marshalled text", - "marshalNilPtr": "", + "nilSlice": []interface{}{}, + "error": "errorstr", + "errorNilPtr": "", + "timeUnset": "0001-01-01T00:00:00Z", + "durationUnset": "0s", + "marshalFullPtr": "marshalled text", + "marshalNilPtr": "", + "nullJsonMarshaller": "null", + "fullJsonMarshaller": "", + "nilBytes": "", + "emptyBytes": "", }, }) { t.Errorf("metadata.AddStruct didn't work: %#v", md) From f6c598484b0f54722b3c5adb9142c638edac7e64 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 11 Apr 2024 23:19:11 -0600 Subject: [PATCH 4/5] veqryn/marshal-json: add nested metadata tests --- v2/metadata_test.go | 69 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/v2/metadata_test.go b/v2/metadata_test.go index e5d412c..1c205a8 100644 --- a/v2/metadata_test.go +++ b/v2/metadata_test.go @@ -113,9 +113,6 @@ 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) @@ -144,34 +141,74 @@ func TestMetadataAddNil(t *testing.T) { var nullJsonMarshaller json.RawMessage md.AddStruct("nullJsonMarshaller", nullJsonMarshaller) - var fullJsonMarshaller = &json.RawMessage{} - md.AddStruct("fullJsonMarshaller", fullJsonMarshaller) + 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": "", }, + "nilmap": map[string]interface{}{}, + "nilSlice": []interface{}{}, + "error": "errorstr", + "errorNilPtr": "", + "timeUnset": "0001-01-01T00:00:00Z", + "durationUnset": "0s", + "marshalFullPtr": "marshalled text", + "marshalNilPtr": "", + "nullJsonMarshaller": "null", + "nullJsonMarshallerPtr": "", + "emptyJsonMarshaller": "", + "nilBytes": "", + "nilBytesPtr": "", + "emptyBytes": "", }, "nilmap": map[string]interface{}{}, "Extra data": { - "nilSlice": []interface{}{}, - "error": "errorstr", - "errorNilPtr": "", - "timeUnset": "0001-01-01T00:00:00Z", - "durationUnset": "0s", - "marshalFullPtr": "marshalled text", - "marshalNilPtr": "", - "nullJsonMarshaller": "null", - "fullJsonMarshaller": "", - "nilBytes": "", - "emptyBytes": "", + "nilSlice": []interface{}{}, + "error": "errorstr", + "errorNilPtr": "", + "timeUnset": "0001-01-01T00:00:00Z", + "durationUnset": "0s", + "marshalFullPtr": "marshalled text", + "marshalNilPtr": "", + "nullJsonMarshaller": "null", + "nullJsonMarshallerPtr": "", + "emptyJsonMarshaller": "", + "nilBytes": "", + "nilBytesPtr": "", + "emptyBytes": "", }, }) { t.Errorf("metadata.AddStruct didn't work: %#v", md) From c659c100e7d5311b8415c45efc4cd2ae8a6f929d Mon Sep 17 00:00:00 2001 From: Daria Bialobrzeska Date: Fri, 12 Apr 2024 13:13:18 +0200 Subject: [PATCH 5/5] Update version to 2.4.0 --- CHANGELOG.md | 8 ++++++++ v2/bugsnag.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3d0bf3..400e7fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/v2/bugsnag.go b/v2/bugsnag.go index 458f72a..631d2e9 100644 --- a/v2/bugsnag.go +++ b/v2/bugsnag.go @@ -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