Skip to content

Commit 208fa27

Browse files
bhapasmergify[bot]
authored andcommitted
filebeat: Fix scenarios for short message and invalid month number in syslog messages (#48031)
* Fix message size and empty year * fix linter errors * Fix (cherry picked from commit 3cc5299)
1 parent cb50c2b commit 208fa27

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

filebeat/input/syslog/event.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var month = map[string]time.Month{
4343
"Dec": time.December,
4444
}
4545

46-
var monthIndexed = []time.Month{
46+
var monthIndexed = [...]time.Month{
4747
0,
4848
time.January,
4949
time.February,
@@ -117,7 +117,7 @@ func (s *event) SetTimeZone(b []byte) {
117117
}
118118

119119
// +00 +00:00 or +0000
120-
// Use second value directly and don't use unecessary time.Duration.
120+
// Use second value directly and don't use unnecessary time.Duration.
121121
// Time.FixedZone accepts number of seconds.
122122
var h, m int
123123
switch len(b[1:]) {
@@ -137,7 +137,10 @@ func (s *event) SetTimeZone(b []byte) {
137137

138138
// SetMonthNumeric sets the month with a number.
139139
func (s *event) SetMonthNumeric(b []byte) {
140-
s.month = monthIndexed[bytesToInt(skipLeadZero(b))]
140+
month := bytesToInt(skipLeadZero(b))
141+
if month < len(monthIndexed) {
142+
s.month = monthIndexed[month]
143+
}
141144
}
142145

143146
// SetMonth sets the month.
@@ -211,12 +214,8 @@ func (s *event) Year() int {
211214

212215
// SetMessage sets the message.
213216
func (s *event) SetMessage(b []byte) {
214-
// remove BOM
215-
if b[0] == 0xef && b[1] == 0xbb && b[2] == 0xbf {
216-
s.message = string(b[3:])
217-
} else {
218-
s.message = string(b)
219-
}
217+
// Trim BOM: https://unicode.org/faq/utf_bom#bom5
218+
s.message = string(bytes.TrimPrefix(b, []byte("\ufeff")))
220219
}
221220

222221
// Message returns the message.
@@ -262,7 +261,7 @@ func (s *event) SetHostname(b []byte) {
262261

263262
// Hostname returns the hostname.
264263
func (s *event) Hostname() string {
265-
return string(s.hostname)
264+
return s.hostname
266265
}
267266

268267
// SetProgram sets the programs as a byte slice.

filebeat/input/syslog/rfc3164_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,16 @@ func TestPriority(t *testing.T) {
796796
}
797797
}
798798

799+
func TestParserRFC3164InvalidMonthNumeric(t *testing.T) {
800+
event := newEvent()
801+
ParserRFC3164([]byte("0000-20-"), event)
802+
}
803+
804+
func TestParserRFC3164ShortMessage(t *testing.T) {
805+
event := newEvent()
806+
ParserRFC3164([]byte("Oct 1 00:00:00 \xef"), event)
807+
}
808+
799809
var e *event
800810

801811
func BenchmarkParserRFC3164r(b *testing.B) {

0 commit comments

Comments
 (0)