From ad3ddafb065d63806b4b7b1e9a1c4f87c650f0ff Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Wed, 4 Nov 2026 07:08:23 +0000 Subject: [PATCH 1/2] Fix Encoding::CompatibilityError on non-ASCII binary input Bundled patterns such as MONTH contain non-ASCII characters, so the expanded regexps are UTF-8. Matching them against an ASCII-8BIT string with non-ASCII bytes (the default for in_tail) raises Encoding::CompatibilityError instead of trying the next section. With multiline_grok and multiline_flush_interval, the error repeats on every flush. Retry the match with the text reinterpreted as UTF-8, and treat text that is not valid UTF-8 as not matched. Fixes #107 Signed-off-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> --- lib/fluent/plugin/parser_grok.rb | 26 +++++++--- lib/fluent/plugin/parser_multiline_grok.rb | 16 ------- test/test_grok_parser.rb | 56 ++++++++++++++++++++++ test/test_multiline_grok_parser.rb | 22 +++++++++ 4 files changed, 98 insertions(+), 22 deletions(-) diff --git a/lib/fluent/plugin/parser_grok.rb b/lib/fluent/plugin/parser_grok.rb index 7b872cc..0232412 100644 --- a/lib/fluent/plugin/parser_grok.rb +++ b/lib/fluent/plugin/parser_grok.rb @@ -64,12 +64,11 @@ def configure(conf={}) def parse(text) @grok.parsers.each do |name_or_index, parser| - parser.parse(text) do |time, record| - if time and record - record[@grok_name_key] = name_or_index if @grok_name_key - yield time, record - return - end + time, record = parse_by(parser, text) + if time and record + record[@grok_name_key] = name_or_index if @grok_name_key + yield time, record + return end end @default_parser.parse(text) do |time, record| @@ -77,6 +76,21 @@ def parse(text) yield time, record end end + + private + + def parse_by(parser, text) + parser.parse(text) do |time, record| + return time, record + end + nil + rescue Encoding::CompatibilityError + # Some bundled patterns (e.g. MONTH) contain non-ASCII characters, so their + # regexps are UTF-8 and raise on non-ASCII binary text such as in_tail lines. + utf8_text = text.dup.force_encoding(Encoding::UTF_8) + return nil unless utf8_text.valid_encoding? + parse_by(parser, utf8_text) + end end end end diff --git a/lib/fluent/plugin/parser_multiline_grok.rb b/lib/fluent/plugin/parser_multiline_grok.rb index a4e29fe..eac6eae 100644 --- a/lib/fluent/plugin/parser_multiline_grok.rb +++ b/lib/fluent/plugin/parser_multiline_grok.rb @@ -15,22 +15,6 @@ def has_firstline? def firstline?(text) @multiline_start_regexp && !!@grok.multiline_start_regexp.match(text) end - - def parse(text) - @grok.parsers.each do |name_or_index, parser| - parser.parse(text) do |time, record| - if time and record - record[@grok_name_key] = name_or_index if @grok_name_key - yield time, record - return - end - end - end - @default_parser.parse(text) do |time, record| - record[@grok_failure_key] = "No grok pattern matched" if @grok_failure_key - yield time, record - end - end end end end diff --git a/test/test_grok_parser.rb b/test/test_grok_parser.rb index e83a4d7..e3482bd 100644 --- a/test/test_grok_parser.rb +++ b/test/test_grok_parser.rb @@ -466,6 +466,62 @@ class GrokParserTest < ::Test::Unit::TestCase end end + sub_test_case "non-ASCII binary text" do + test "pattern with non-ASCII characters matches" do + d = create_driver(%[ + + pattern %{HTTPDATE:timestamp} %{GREEDYDATA:city} + + ]) + records = [] + d.instance.parse("22/Nov/2023:18:18:09 +0100 München".b) do |_time, record| + records << record + end + expected = [{ "timestamp" => "22/Nov/2023:18:18:09 +0100", "city" => "München" }] + assert_equal(expected, records) + end + + test "falls through to the next grok section" do + d = create_driver(%[ + grok_name_key grok_name + + pattern %{HTTPDATE:timestamp} %{NUMBER:response} + + + pattern %{TIMESTAMP_ISO8601:timestamp} %{GREEDYDATA:message} + + ]) + records = [] + d.instance.parse("2023-11-22 18:18:09.823+0100 Testing München".b) do |_time, record| + records << record + end + expected = [{ + "timestamp" => "2023-11-22 18:18:09.823+0100", + "message" => "Testing München".b, + "grok_name" => 1 + }] + assert_equal(expected, records) + end + + test "invalid UTF-8 text falls back to grok_failure_key" do + d = create_driver(%[ + grok_failure_key grok_failure + + pattern %{HTTPDATE:timestamp} %{GREEDYDATA:city} + + ]) + records = [] + d.instance.parse("22/Nov/2023:18:18:09 +0100 M\xFCnchen".b) do |_time, record| + records << record + end + expected = [{ + "message" => "22/Nov/2023:18:18:09 +0100 M\xFCnchen".b, + "grok_failure" => "No grok pattern matched" + }] + assert_equal(expected, records) + end + end + private def create_driver(conf) diff --git a/test/test_multiline_grok_parser.rb b/test/test_multiline_grok_parser.rb index 2907f77..165c5a4 100644 --- a/test/test_multiline_grok_parser.rb +++ b/test/test_multiline_grok_parser.rb @@ -95,6 +95,28 @@ def test_without_multiline_start_regexp end end + test "non-ASCII binary text" do + text = "2023-11-22 18:18:09.823+0100 Testing München\njavax.management.RuntimeErrorException: null".b + d = create_driver(%[ + multiline_start_regexp /\\d{4}-\\d{1,2}-\\d{1,2}/ + + pattern %{HTTPDATE:timestamp} %{NUMBER:response} + + + pattern %{TIMESTAMP_ISO8601:timestamp} %{GREEDYDATA:message} + + ]) + records = [] + d.instance.parse(text) do |_time, record| + records << record + end + expected = [{ + "timestamp" => "2023-11-22 18:18:09.823+0100", + "message" => "Testing München\njavax.management.RuntimeErrorException: null".b + }] + assert_equal(expected, records) + end + test "no_grok_patterns" do assert_raise Fluent::ConfigError do create_driver('') From 970f77ce346c2fcf25af36c791019b4617b590f4 Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:16:32 +0000 Subject: [PATCH 2/2] Retry only binary text as UTF-8 Text in another encoding, such as UTF-16LE or ISO-8859-1, was reinterpreted as UTF-8 bytes and could return a wrong match. Raise the Encoding::CompatibilityError for such text, as before. This also bounds the retry. If matching the UTF-8 text raises again, for example because of a binary null_value_pattern, the error is raised instead of recursing until SystemStackError. Signed-off-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> --- lib/fluent/plugin/parser_grok.rb | 1 + test/test_grok_parser.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/fluent/plugin/parser_grok.rb b/lib/fluent/plugin/parser_grok.rb index 0232412..7e374f4 100644 --- a/lib/fluent/plugin/parser_grok.rb +++ b/lib/fluent/plugin/parser_grok.rb @@ -85,6 +85,7 @@ def parse_by(parser, text) end nil rescue Encoding::CompatibilityError + raise unless text.encoding == Encoding::ASCII_8BIT # Some bundled patterns (e.g. MONTH) contain non-ASCII characters, so their # regexps are UTF-8 and raise on non-ASCII binary text such as in_tail lines. utf8_text = text.dup.force_encoding(Encoding::UTF_8) diff --git a/test/test_grok_parser.rb b/test/test_grok_parser.rb index e3482bd..a625116 100644 --- a/test/test_grok_parser.rb +++ b/test/test_grok_parser.rb @@ -520,6 +520,28 @@ class GrokParserTest < ::Test::Unit::TestCase }] assert_equal(expected, records) end + + test "text in another encoding is not retried as UTF-8" do + d = create_driver(%[ + + pattern %{HTTPDATE:timestamp} %{GREEDYDATA:city} + + ]) + text = "22/Nov/2023:18:18:09 +0100 München".encode("ISO-8859-1") + assert_raise(Encoding::CompatibilityError) do + d.instance.parse(text) {} + end + end + + test "an error on the UTF-8 retry is raised" do + d = create_driver({ + "grok_pattern" => "%{HTTPDATE:timestamp} %{GREEDYDATA:message}", + "null_value_pattern" => "なし".b + }) + assert_raise(Encoding::CompatibilityError) do + d.instance.parse("22/Nov/2023:18:18:09 +0100 München".b) {} + end + end end private