Repository navigation
Fix Encoding::CompatibilityError on non-ASCII binary input - #126
Conversation
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 <grok> 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 fluent#107 Signed-off-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com>
Watson1978
left a comment
There was a problem hiding this comment.
Thanks for the fix and the tests.
I checked f5fe675. The four new tests fail on master with Encoding::CompatibilityError and pass with this change. I also ran the config from #107 (in_tail with multiline_flush_interval) on fluentd 1.19.3. All four records are now emitted, and the flush error no longer repeats.
For input that parses without an error on master, I found no change in the records, including the encoding of the values. Some text in other encodings, such as UTF-16LE, used to raise. Now it can return a wrong value. I'd like this fixed before merging. Please see the inline comment, which limits the retry to binary text.
| return time, record | ||
| end | ||
| nil | ||
| rescue Encoding::CompatibilityError |
There was a problem hiding this comment.
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.
| 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?
There was a problem hiding this comment.
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.
|
Also, the DCO check is failing because commit f5fe675 has no
|
f5fe675 to
ad3ddaf
Compare
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>
Watson1978
left a comment
There was a problem hiding this comment.
Thanks for the update. I checked 970f77c.
The guard and both requested tests are in. The tests fail on f5fe675 and pass on 970f77c. The ISO-8859-1 and UTF-16LE examples from my last review raise Encoding::CompatibilityError again, as on master. The binary null_value_pattern case raises the same error instead of SystemStackError.
Binary input that parsed on f5fe675 still gives the same records. I also re-ran the config from #107 on fluentd 1.19.3. All four records are emitted, and the flush error no longer repeats. DCO and CI pass.
LGTM.
Fixes #107
Problem
The bundled
MONTHpattern contains a non-ASCII character (ä), so every expanded regexp that uses it (for exampleHTTPDATE,SYSLOGTIMESTAMP,COMBINEDAPACHELOG) is a UTF-8 regexp.in_tailpasses lines as ASCII-8BIT by default. When a line contains non-ASCII bytes,Regexp#matchraisesEncoding::CompatibilityError. Because of that:<grok>section instead of trying the next one or falling back togrok_failure_key.in_taildrops the line withinvalid line found.multiline_grokandmultiline_flush_interval, the error escapes the flush timer and repeats on every flush, as reported inmultiline_grokcrashes whenmultiline_flush_intervalis used. #107.Changes
GrokParser#parsematches each section through a new privateparse_byhelper. When the match raisesEncoding::CompatibilityError, it retries with the text reinterpreted as UTF-8. If the text is not valid UTF-8, the section counts as not matched, so the next section or thegrok_failure_keyfallback applies.MultilineGrokParser#parsewas an exact copy ofGrokParser#parse, so it is removed and the subclass inherits the fix.Input that matched before is unchanged. Only input that used to raise now goes through the retry.
Tests
test/test_grok_parser.rb: aMONTH-based pattern matches non-ASCII binary text, a non-matchingMONTH-based section falls through to the next section, and invalid UTF-8 falls back togrok_failure_key.test/test_multiline_grok_parser.rb: the multiline record frommultiline_grokcrashes whenmultiline_flush_intervalis used. #107 parses.bundle exec rake test: 50 tests, 0 failures, 0 errors. Without the change inlib/, the 4 new tests fail withEncoding::CompatibilityError.The configuration from #107 also runs with
fluentd1.19.4. It emits the multiline record on flush and stops repeating the error.