From cfe68f2616755eee9c2e98fb1f3bcca609ae4f9a Mon Sep 17 00:00:00 2001 From: Rajyavardhan Date: Wed, 2 Dec 2026 18:15:38 +0530 Subject: [PATCH] parser_multiline: match valid UTF-8 binary input with a UTF-8 regexp in_tail passes lines to the parser as ASCII-8BIT strings when `encoding` is not set. When format_firstline was a UTF-8 regexp, such as one with \p{Alpha}, MultilineParser#firstline? raised Encoding::CompatibilityError at the first non-ASCII line. The error escaped from in_tail's parse_multilines, so in_tail read the same lines again on every notification and never moved past them. When format1 was a UTF-8 regexp, MultilineRegexpParser#parse raised the same error and in_tail dropped the whole multiline event. Apply the retry added in #5710 in MultilineRegexpParser#parse and MultilineParser#firstline? too. The check and the UTF-8 copy move into RegexpParser.utf8_text_for_retry, which runs only after the error, so matching without the error costs the same as before. A binary string with valid UTF-8 bytes is matched again as UTF-8. Other incompatible input still raises Encoding::CompatibilityError. Fixes #5714 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Rajyavardhan --- lib/fluent/plugin/parser_multiline.rb | 14 ++++++++- lib/fluent/plugin/parser_regexp.rb | 15 +++++++--- test/plugin/test_in_tail.rb | 41 +++++++++++++++++++++++++++ test/plugin/test_parser_multiline.rb | 29 +++++++++++++++++++ 4 files changed, 94 insertions(+), 5 deletions(-) diff --git a/lib/fluent/plugin/parser_multiline.rb b/lib/fluent/plugin/parser_multiline.rb index 935bfcb499..7ed2b00933 100644 --- a/lib/fluent/plugin/parser_multiline.rb +++ b/lib/fluent/plugin/parser_multiline.rb @@ -31,7 +31,14 @@ class MultilineParser < Parser class MultilineRegexpParser < Fluent::Plugin::RegexpParser def parse(text) - m = @expression.match(text) + m = begin + @expression.match(text) + rescue Encoding::CompatibilityError + utf8_text = RegexpParser.utf8_text_for_retry(@expression, text) + raise unless utf8_text + + @expression.match(utf8_text) + end unless m yield nil, nil return m @@ -105,6 +112,11 @@ def has_firstline? def firstline?(text) @firstline_regex.match?(text) + rescue Encoding::CompatibilityError + utf8_text = RegexpParser.utf8_text_for_retry(@firstline_regex, text) + raise unless utf8_text + + @firstline_regex.match?(utf8_text) end private diff --git a/lib/fluent/plugin/parser_regexp.rb b/lib/fluent/plugin/parser_regexp.rb index 557357c755..cde1d5c9ab 100644 --- a/lib/fluent/plugin/parser_regexp.rb +++ b/lib/fluent/plugin/parser_regexp.rb @@ -46,14 +46,21 @@ def configure(conf) end end + # Returns a UTF-8 copy of text when regexp is UTF-8 and text is a + # binary string with valid UTF-8 bytes. Returns nil otherwise. + def self.utf8_text_for_retry(regexp, text) + return nil unless text.encoding == Encoding::ASCII_8BIT && regexp.encoding == Encoding::UTF_8 + + utf8_text = text.dup.force_encoding(Encoding::UTF_8) + utf8_text if utf8_text.valid_encoding? + end + def parse(text) m = begin @expression.match(text) rescue Encoding::CompatibilityError - raise unless text.encoding == Encoding::ASCII_8BIT && @expression.encoding == Encoding::UTF_8 - - utf8_text = text.dup.force_encoding(Encoding::UTF_8) - raise unless utf8_text.valid_encoding? + utf8_text = RegexpParser.utf8_text_for_retry(@expression, text) + raise unless utf8_text @expression.match(utf8_text) end diff --git a/test/plugin/test_in_tail.rb b/test/plugin/test_in_tail.rb index 2f4dac2e30..30d6c04252 100644 --- a/test/plugin/test_in_tail.rb +++ b/test/plugin/test_in_tail.rb @@ -2417,6 +2417,47 @@ def test_multiline_without_firstline(data) assert_equal({"var1" => "foo 1", "var2" => "bar 1", "var3" => "baz 1"}, events[0][2]) assert_equal({"var1" => "foo 2", "var2" => "bar 2", "var3" => "baz 2"}, events[1][2]) end + + data( + 'utf8 format_firstline' => [ + '/^\\p{Alpha}+:/', '/^(?\\w+):(?.*)/', + # The event with the non-ASCII line does not match format1 and is dropped. + [{ "key" => "a", "value" => "1" }, { "key" => "c", "value" => "3" }, { "key" => "d", "value" => "4" }] + ], + 'utf8 format1' => [ + '/^\\w+:/', '/^(?\\p{Alpha}+):(?.*)/', + [{ "key" => "a", "value" => "1\ncaf\u00e9:2\n continued" }, { "key" => "c", "value" => "3" }, { "key" => "d", "value" => "4" }] + ], + 'utf8 format_firstline and format1' => [ + '/^\\p{Alpha}+:/', '/^(?\\p{Alpha}+):(?.*)/', + [{ "key" => "a", "value" => "1" }, { "key" => "caf\u00e9", "value" => "2\n continued" }, { "key" => "c", "value" => "3" }, { "key" => "d", "value" => "4" }] + ]) + def test_multiline_utf8_regexp_with_non_ascii_line(data) + format_firstline, format1, expected = data + Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") { |f| + f.write "a:1\ncaf\xC3\xA9:2\n continued\nc:3\n".b + } + + config = config_element("", "", { + "read_from_head" => "true", + "multiline_flush_interval" => "1s", + }, [ + config_element("parse", "", { + "@type" => "multiline", + "format_firstline" => format_firstline, + "format1" => format1, + }) + ]) + d = create_driver(config) + d.run(expect_records: expected.size, timeout: 10) do + Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "ab") { |f| + f.write "d:4\n" + } + end + + assert_false(d.broken?) + assert_equal(expected, d.events.map { |e| e[2] }) + end end sub_test_case "path" do diff --git a/test/plugin/test_parser_multiline.rb b/test/plugin/test_parser_multiline.rb index 9cbda2e7b3..2839b551aa 100644 --- a/test/plugin/test_parser_multiline.rb +++ b/test/plugin/test_parser_multiline.rb @@ -98,6 +98,35 @@ def test_parse_with_keep_time_key } end + def test_firstline_utf8_regexp_with_binary_text + parser = create_parser('format_firstline' => '/^\p{Alpha}+:/', 'format1' => '/^(?\w+):(?.*)/') + assert(parser.instance.firstline?("caf\u00e9:2".b)) + assert(parser.instance.firstline?("a:1".b)) + assert_false(parser.instance.firstline?(" continued".b)) + end + + def test_firstline_utf8_regexp_with_text_not_utf8 + parser = create_parser('format_firstline' => '/^\p{Alpha}+:/', 'format1' => '/^(?\w+):(?.*)/') + assert_raise(Encoding::CompatibilityError) do + parser.instance.firstline?("\xff:1".b) + end + end + + def test_parse_utf8_regexp_with_binary_text + parser = create_parser('format_firstline' => '/^\w+:/', 'format1' => '/^(?\p{Alpha}+):(?.*)/') + r = [] + parser.instance.parse("caf\u00e9:2\n continued".b) { |_, record| r << record } + assert_equal [{ 'key' => "caf\u00e9", 'value' => "2\n continued" }], r + assert_equal Encoding::UTF_8, r[0]['key'].encoding + end + + def test_parse_utf8_regexp_with_text_not_utf8 + parser = create_parser('format1' => '/^(?\p{Alpha}+):(?.*)/') + assert_raise(Encoding::CompatibilityError) do + parser.instance.parse("\xff:1".b) { |_, _| } + end + end + def test_parse_unmatched_lines parser = Fluent::Test::Driver::Parser.new(Fluent::Plugin::MultilineParser).configure( 'format1' => '/^message (?\d)/',