From 239d81e334829ea9c02f2a2845a54050ee6d80e3 Mon Sep 17 00:00:00 2001 From: shreeraj Date: Sun, 15 Nov 2026 14:06:06 +0530 Subject: [PATCH] parser_syslog: do not skip unmatched header fields in the RFC 5424 regexp parser parse_plain calls re.match(text, idx), but Regexp#match with a position only starts searching there; it does not anchor the match. When the host, ident, pid, msgid or structured data at idx did not match, the regexp engine could match further on in the text, so the record got values from the wrong place and the text in between was dropped without any warning. For example, a hostname with a non-ASCII character returned "st" as the host, and a header split by a line break returned the second line's fields as the record. Anchor the RFC 5424 regexp with \G so that a header that does not match at idx is reported as not matched. Fixes #5630 Signed-off-by: shreeraj --- lib/fluent/plugin/parser_syslog.rb | 4 +++- test/plugin/test_parser_syslog.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/fluent/plugin/parser_syslog.rb b/lib/fluent/plugin/parser_syslog.rb index 52c3b1f066..f9befbb314 100644 --- a/lib/fluent/plugin/parser_syslog.rb +++ b/lib/fluent/plugin/parser_syslog.rb @@ -44,7 +44,9 @@ class SyslogParser < Parser RFC3164_CAPTURES = RFC3164_WITHOUT_TIME_AND_PRI_REGEXP.names.freeze RFC3164_PRI_REGEXP = /\A<(?[0-9]{1,3})>/ - RFC5424_WITHOUT_TIME_AND_PRI_REGEXP = /(?[!-~]{1,255}) (?[!-~]{1,48}) (?[!-~]{1,128}) (?[!-~]{1,32}) (?(?:\-|(?:\[.*?(?.+))?\z/m + # \G anchors the match at the position given to Regexp#match, so a header + # that doesn't match there is rejected instead of matched further on. + RFC5424_WITHOUT_TIME_AND_PRI_REGEXP = /\G(?[!-~]{1,255}) (?[!-~]{1,48}) (?[!-~]{1,128}) (?[!-~]{1,32}) (?(?:\-|(?:\[.*?(?.+))?\z/m RFC5424_CAPTURES = RFC5424_WITHOUT_TIME_AND_PRI_REGEXP.names.freeze RFC5424_PRI_REGEXP = /\A<(?\d{1,3})>\d\d{0,2}\s/ diff --git a/test/plugin/test_parser_syslog.rb b/test/plugin/test_parser_syslog.rb index a96dd360ed..f2009ba740 100644 --- a/test/plugin/test_parser_syslog.rb +++ b/test/plugin/test_parser_syslog.rb @@ -871,6 +871,19 @@ def test_parse_with_rfc5424_message_includes_right_bracket(param) end end + data( + 'non-ASCII hostname' => '<14>1 2026-04-25T16:43:29Z hôst app 123 ID - msg', + 'header fields after a line break' => "<14>1 2026-04-25T16:43:29Z first line\nhost app 123 ID - second line", + ) + def test_parse_with_rfc5424_regexp_does_not_skip_unmatched_header(text) + @parser.configure( + 'message_format' => 'rfc5424', + 'with_priority' => true, + 'parser_engine' => 'regexp' + ) + assert_input_rejected(text, :rejects) + end + data( 'regexp/with message' => ['regexp', '[x@1 a="b\] c"]', 'msg'], 'string/with message' => ['string', '[x@1 a="b\] c"]', 'msg'],