Skip to content

Commit aec81e3

Browse files
tsgruflin
authored andcommitted
Fix JSON panic (elastic#4042)
There can be a panic in the JSON decoding code if the input JSON line contains "null" as a string, because that unmarshals without errors but results in a nil map.
1 parent 35f375c commit aec81e3

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

CHANGELOG.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff]
6060
- Allow `-` in Apache access log byte count. {pull}3863[3863]
6161
- Downgrade Elasticsearch per batch item failure log to debug level. {issue}3953[3953]
6262
- Allow log lines without a program name in the Syslog fileset. {pull}3944[3944]
63+
- Fix panic in JSON decoding code if the input line is "null". {pull}4042[4042]
6364

6465
*Heartbeat*
6566
- Add default ports in HTTP monitor. {pull}3924[3924]

filebeat/harvester/reader/json.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (r *JSON) decodeJSON(text []byte) ([]byte, common.MapStr) {
3030
var jsonFields map[string]interface{}
3131

3232
err := unmarshal(text, &jsonFields)
33-
if err != nil {
33+
if err != nil || jsonFields == nil {
3434
logp.Err("Error decoding JSON: %v", err)
3535
if r.cfg.AddErrorKey {
3636
jsonFields = common.MapStr{JsonErrorKey: fmt.Sprintf("Error decoding JSON: %v", err)}

filebeat/harvester/reader/json_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ func TestDecodeJSON(t *testing.T) {
116116
ExpectedText: `{"message": "test", "value": "`,
117117
ExpectedMap: nil,
118118
},
119+
{
120+
// in case the JSON is "null", we should just not panic
121+
Text: `null`,
122+
Config: JSONConfig{MessageKey: "value", AddErrorKey: true},
123+
ExpectedText: `null`,
124+
ExpectedMap: common.MapStr{"json_error": "Error decoding JSON: <nil>"},
125+
},
119126
{
120127
// Add key error helps debugging this
121128
Text: `{"message": "test", "value": "`,

0 commit comments

Comments
 (0)