fix: scrub invalid UTF-8 in CloudFront log lines - #2
Open
cesarokuti wants to merge 3 commits into
Open
Conversation
CloudFront logs can contain raw invalid UTF-8 that breaks CGI.unescape/split and JSON emit. Scrub with String#scrub while preserving valid Unicode. Co-authored-by: Cursor <cursoragent@cursor.com>
CGI.unescape of values like %E9 yields high bytes that break UTF-8 String#split. Parse as ASCII-8BIT and fall back to ISO-8859-1 so records stay valid UTF-8 for Loki. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
getulioruiz
approved these changes
Aug 10, 2026
lugomas
approved these changes
Aug 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
process_linewithString#scrubbeforeCGI.unescape/.split("\t").%09→ space handling.0.2.3and document the change inCHANGELOG.md.Why
Some CloudFront access log lines contain raw invalid UTF-8 bytes. On current Ruby this raises
ArgumentError: invalid byte sequence in UTF-8during parse (often surfaced as FluentdS3 GET client error), so the file never emits records.Using only
force_encoding("ASCII-8BIT")avoids the parse error but leaves binary strings that later failJSON.generate(JSON::GeneratorError/Encoding::UndefinedConversionError), so logs still never reach Loki.Test plan
bundle exec rake teston Ruby 2.7.8 — 17 tests / 27 assertions, including new invalid-UTF-8 and Unicode-preservation casesv1.19.2/ Ruby 3.4.8)João/çafé, scrubs0xFF/0x80, and%09regression still passesJSON.generatesucceeds; ASCII-8BIT-only path fails JSON as expectedcf-utf8) → HTTP 204 and query returns the lineNotes for reviewers
Suggested base branch:
develop(repo default).