Skip to content

Commit 24a73ac

Browse files
author
Joe Hanvy
committed
Added support for uppercase date fields in CEF.
Renamed peek shiftItem, and added an actual peak (which doesn't mutate the array).
1 parent c51d69c commit 24a73ac

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

parser.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const RXS = {
66
"pri" : /^<\d+>/,
77
"prinmr" : /^\d+ /,
88
"prival" : /<(\d+)>/,
9-
"month" : /^[A-Za-z][a-z]{2} /,
10-
"day" : /^\d{1,2} /,
9+
"month" : /^[A-Za-z]{3} /,
10+
"day" : /^\d{1,2}/,
1111
"time" : /^\d+:\d+:\d+ /,
1212
"ts" : /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\S+ /,
1313
"invalid" : /[^a-zA-Z0-9\.\$\-_#%\/\[\]\(\)]/,
@@ -24,7 +24,12 @@ const DOPS = {
2424
generateTimestamp: true
2525
}
2626

27-
function peek(arr) {
27+
/**
28+
* Removes the first non whitespace item from the array and returns the item
29+
* @param {string[]} arr the array to shift the item from
30+
* @returns the first non whitespace item of the array
31+
*/
32+
function shiftItem(arr) {
2833
do {
2934
var item = arr.shift();
3035
if(item===undefined) return item;
@@ -34,6 +39,21 @@ function peek(arr) {
3439
return item;
3540
}
3641

42+
/**
43+
* Gets the first non whitespace item from the array without mutating the array
44+
* @param {string[]} arr the array to peek for the first item
45+
* @returns the first non whitespace item of the array
46+
*/
47+
function peekItem(arr) {
48+
for (const item of arr) {
49+
let trimmedItem = item.trim();
50+
if (trimmedItem) {
51+
return trimmedItem;
52+
}
53+
}
54+
return undefined;
55+
}
56+
3757
function assign(entry,item) {
3858
if(!entry.host) entry.host = item.trim();
3959
else if(!entry.appName) entry.appName = item.trim();
@@ -78,33 +98,34 @@ function parse(line,opts) {
7898
// Date search
7999
var endparse = false;
80100
while(line.length && !endparse) {
81-
var item = peek(items)+" ";
101+
var item = shiftItem(items)+" ";
102+
var nextItem = peekItem(items);
82103

83104
// RFC RFC5424
84105
if(item.match(RXS.prinmr)) {
85106
entry.version = parseInt(item);
86107
entry.type = "RFC5424";
87-
item = peek(items)+" ";
108+
item = shiftItem(items)+" ";
88109
if(item.match(RXS.ts)) {
89110
entry.ts = new Date(Date.parse(item.match(RXS.ts)[0].trim()));
90111
}
91112
}
92113
// BSD
93-
else if(item.match(RXS.month)) {
114+
else if(item.match(RXS.month) && nextItem && nextItem.match(RXS.day)) {
94115
entry.type = "BSD";
95116
const month = item.trim();
96-
const day = peek(items);
97-
let time = peek(items);
117+
const day = shiftItem(items);
118+
let time = shiftItem(items);
98119
let year = new Date().getYear() + 1900
99120
let timezone = "";
100121
// Check if the time is actually a year field and it is in the form "MMM dd yyyy HH:mm:ss"
101122
if (time.length === 4 && !Number.isNaN(+time)) {
102123
year = +time;
103-
time = peek(items);
124+
time = shiftItem(items);
104125
}
105126
// Check if we have a timezone
106127
if (isValidTimeZone(items[0].trim())) {
107-
timezone = peek(items);
128+
timezone = shiftItem(items);
108129
}
109130

110131
entry.ts = new Date(Date.parse(`${year} ${month} ${day} ${time} ${timezone}`.trim()));
@@ -130,7 +151,7 @@ function parse(line,opts) {
130151
}
131152

132153
while(line.length && !endparse) {
133-
var item = peek(items);
154+
var item = shiftItem(items);
134155
if(!item) {
135156
endparse = true;
136157
}

parser.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ test.each([
3131
fields: [],
3232
header: "<34>Oct 11 22:14:15 mymachine su: ",
3333
},
34+
{
35+
originalMessage:
36+
"<34>OCT 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8",
37+
pri: "<34>",
38+
prival: 34,
39+
facilityval: 4,
40+
levelval: 2,
41+
facility: "auth",
42+
level: "crit",
43+
type: "BSD",
44+
ts: dateAsCurrentYear("2019-10-11T21:14:15.000Z"),
45+
host: "mymachine",
46+
appName: "su",
47+
message: "'su root' failed for lonvick on /dev/pts/8",
48+
chain: [],
49+
fields: [],
50+
header: "<34>OCT 11 22:14:15 mymachine su: ",
51+
},
3452
{
3553
originalMessage:
3654
"<34>Oct 11 22:14:15.123 UTC mymachine su: 'su root' failed for lonvick on /dev/pts/8",

0 commit comments

Comments
 (0)