Skip to content

Commit a4feb0f

Browse files
authored
Merge pull request #97 from satta/nullfix
fix parsing of JSON null values
2 parents a2562a0 + b7766d8 commit a4feb0f

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to FEVER will be documented in this file.
44

5+
## [1.3.3] - 2022-01-25
6+
7+
### Changed
8+
- Fixed handling of JSON `null` values (#97)
9+
510
## [1.3.2] - 2021-12-09
611

712
### Added
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"timestamp":"2017-03-06T06:54:10.839668+0000","flow_id":null,"in_iface":"enp2s0f1","event_type":"fileinfo","vlan":null,"src_ip":null,"src_port":null,"dest_ip":null,"dest_port":null,"http":{"hostname":"api.icndb.com","url":null,"state":"CLOSED","md5":null}}

util/util.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package util
44
// Copyright (c) 2017, 2018, 2020, DCSO GmbH
55

66
import (
7+
"bytes"
78
"crypto/tls"
89
"crypto/x509"
910
"encoding/json"
@@ -73,6 +74,11 @@ func ParseJSON(json []byte) (e types.Entry, parseerr error) {
7374
parseerr = err
7475
return
7576
}
77+
// skip null fields; these will not be handled by the low-level
78+
// jsonparser.Parse* () functions
79+
if bytes.Equal(value, []byte("null")) {
80+
return
81+
}
7682
switch idx {
7783
case 0:
7884
e.EventType, err = jsonparser.ParseString(value)

util/util_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
"github.com/DCSO/fever/types"
1313
)
1414

15+
var nullEntry = types.Entry{
16+
Timestamp: "2017-03-06T06:54:10.839668+0000",
17+
EventType: "fileinfo",
18+
JSONLine: `{"timestamp":"2017-03-06T06:54:10.839668+0000","flow_id":null,"in_iface":"enp2s0f1","event_type":"fileinfo","vlan":null,"src_ip":null,"src_port":null,"dest_ip":null,"dest_port":null,"http":{"hostname":"api.icndb.com","url":null,"state":"CLOSED","md5":null}}`,
19+
Iface: "enp2s0f1",
20+
HTTPHost: "api.icndb.com",
21+
}
22+
1523
var entries = []types.Entry{
1624
types.Entry{
1725
SrcIP: "10.0.0.10",
@@ -127,6 +135,31 @@ func TestJSONParseEVEempty(t *testing.T) {
127135
}
128136
}
129137

138+
func TestJSONParseEVEwithnull(t *testing.T) {
139+
f, err := os.Open("testdata/jsonparse_eve_nulls.json")
140+
if err != nil {
141+
t.Fatalf(err.Error())
142+
}
143+
scanner := bufio.NewScanner(f)
144+
i := 0
145+
var entry types.Entry
146+
for scanner.Scan() {
147+
json := scanner.Bytes()
148+
e, err := ParseJSON(json)
149+
if err != nil {
150+
t.Fatalf(err.Error())
151+
}
152+
entry = e
153+
i++
154+
}
155+
if i != 1 {
156+
t.Fatalf("should parse only one entry, got %d", i)
157+
}
158+
if !reflect.DeepEqual(nullEntry, entry) {
159+
t.Fatalf("entry %d parsed from JSON does not match expected value", i)
160+
}
161+
}
162+
130163
func TestGetSensorID(t *testing.T) {
131164
sid, err := GetSensorID()
132165
if err != nil {

0 commit comments

Comments
 (0)