diff --git a/lib/fluent/plugin/in_syslog.rb b/lib/fluent/plugin/in_syslog.rb index 1b94f4ded1..734bc0196c 100644 --- a/lib/fluent/plugin/in_syslog.rb +++ b/lib/fluent/plugin/in_syslog.rb @@ -28,6 +28,7 @@ class SyslogInput < Input DEFAULT_PARSER = 'syslog' SYSLOG_REGEXP = /\A\<([0-9]+)\>(.*)/ + PRI_RANGE = 0..191 FACILITY_MAP = { 0 => 'kern', @@ -322,6 +323,14 @@ def message_handler(data, sock) end pri ||= record.delete('pri') + # RFC 5424 limits PRI to 0..191. A larger value has no facility. + unless PRI_RANGE.cover?(pri) + if @emit_unmatched_lines + emit_unmatched(data, sock) + end + log.warn "invalid syslog priority", data: data + return + end facility = FACILITY_MAP[pri >> 3] severity = SEVERITY_MAP[pri & 0b111] diff --git a/test/plugin/test_in_syslog.rb b/test/plugin/test_in_syslog.rb index 2a0a9409dd..bb93abbb24 100755 --- a/test/plugin/test_in_syslog.rb +++ b/test/plugin/test_in_syslog.rb @@ -181,8 +181,8 @@ def test_space_after_input_owned_priority_preserves_input_priority_syntax(parser ].join("\n")) messages = [ - '<1234>Apr 25 16:43:29 host app: message', - '<1234> Apr 25 16:43:29 host app: message', + '<0134>Apr 25 16:43:29 host app: message', + '<0134> Apr 25 16:43:29 host app: message', ' Apr 25 16:43:29 host app: message', ] d.run(expect_emits: 3) do @@ -218,6 +218,26 @@ def test_priority_on_a_later_line_is_unmatched_when_input_parses_priority assert_equal(message, d.events[0][2]['unmatched_line']) end + data('with_priority' => [], + 'without_priority' => ['', ' @type syslog', ' with_priority false', '']) + def test_priority_out_of_range_is_unmatched(parse_conf) + d = create_driver(([ipv4_config, 'emit_unmatched_lines true'] + parse_conf).join("\n")) + + messages = [ + '<192>Apr 25 16:43:29 host app: msg', + '<1234>Apr 25 16:43:29 host app: msg', + '<191>Apr 25 16:43:29 host app: msg', + ] + d.run(expect_emits: 3) do + u = UDPSocket.new + u.connect('127.0.0.1', @port) + messages.each { |message| u.send(message, 0) } + end + + assert_equal(['syslog.unmatched', 'syslog.unmatched', 'syslog.local7.debug'], d.events.map { |tag, _time, _record| tag }) + assert_equal(messages[0, 2], d.events[0, 2].map { |_tag, _time, record| record['unmatched_line'] }) + end + def test_msg_size d = create_driver tests = create_test_case