Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions lib/fluent/plugin/parser_grok.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,34 @@ 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|
record[@grok_failure_key] = "No grok pattern matched" if @grok_failure_key
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MUST: Please add a guard in this rescue so that only binary (ASCII-8BIT) text is retried as UTF-8. The first parse attempt can stay as it is.

The retry currently runs for any encoding. Text tagged with another encoding, such as UTF-16LE or ISO-8859-1, is then reinterpreted as UTF-8 bytes. A wrong value can come back as a successful match. On this commit:

  • %{NUMBER:value:integer} with "42".encode("UTF-16LE") returns {"value" => 4}.
  • %{HTTPDATE:timestamp} %{GREEDYDATA:city} with "22/Nov/2023:18:18:09 +0100 München".encode("ISO-8859-1") returns "city" => "München".

On master, both raise Encoding::CompatibilityError. in_tail passes such text when its encoding is set to something other than UTF-8. With the change below, these cases raise as on master.

Suggested change
rescue Encoding::CompatibilityError
rescue Encoding::CompatibilityError
raise unless text.encoding == Encoding::ASCII_8BIT

The guard also bounds the retry. If parsing the retried UTF-8 text raises Encoding::CompatibilityError again, that error is raised instead of starting another retry. Today it recurses until SystemStackError. For example, a Hash config with "grok_pattern" => "%{HTTPDATE:timestamp} %{GREEDYDATA:message}" and "null_value_pattern" => "なし".b does this for "22/Nov/2023:18:18:09 +0100 München".b.

With this change, the four new tests still pass. Could you add two tests that expect Encoding::CompatibilityError: one for the ISO-8859-1 example above, and one for the null_value_pattern example?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done in 970f77c. The rescue now re-raises unless the text is ASCII-8BIT. The two new tests (ISO-8859-1 text, and the binary null_value_pattern) expect Encoding::CompatibilityError. On the previous commit, they fail with no error and with SystemStackError. All 52 tests pass, and every commit is signed off.

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)
return nil unless utf8_text.valid_encoding?
parse_by(parser, utf8_text)
end
end
end
end
16 changes: 0 additions & 16 deletions lib/fluent/plugin/parser_multiline_grok.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
78 changes: 78 additions & 0 deletions test/test_grok_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,84 @@ 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(%[
<grok>
pattern %{HTTPDATE:timestamp} %{GREEDYDATA:city}
</grok>
])
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
<grok>
pattern %{HTTPDATE:timestamp} %{NUMBER:response}
</grok>
<grok>
pattern %{TIMESTAMP_ISO8601:timestamp} %{GREEDYDATA:message}
</grok>
])
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
<grok>
pattern %{HTTPDATE:timestamp} %{GREEDYDATA:city}
</grok>
])
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

test "text in another encoding is not retried as UTF-8" do
d = create_driver(%[
<grok>
pattern %{HTTPDATE:timestamp} %{GREEDYDATA:city}
</grok>
])
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

def create_driver(conf)
Expand Down
22 changes: 22 additions & 0 deletions test/test_multiline_grok_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}/
<grok>
pattern %{HTTPDATE:timestamp} %{NUMBER:response}
</grok>
<grok>
pattern %{TIMESTAMP_ISO8601:timestamp} %{GREEDYDATA:message}
</grok>
])
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('')
Expand Down
Loading