Skip to content

fix: scrub invalid UTF-8 in CloudFront log lines - #2

Open
cesarokuti wants to merge 3 commits into
kdgm:developfrom
cesarokuti:master
Open

fix: scrub invalid UTF-8 in CloudFront log lines#2
cesarokuti wants to merge 3 commits into
kdgm:developfrom
cesarokuti:master

Conversation

@cesarokuti

@cesarokuti cesarokuti commented Aug 7, 2026

Copy link
Copy Markdown

Summary

  • Scrub invalid UTF-8 in process_line with String#scrub before CGI.unescape / .split("\t").
  • Re-scrub string field values so emitted records stay valid UTF-8 for JSON outputs (e.g. Loki).
  • Preserve valid Unicode (percent-decoded multi-byte sequences) and keep the existing %09 → space handling.
  • Bump version to 0.2.3 and document the change in CHANGELOG.md.

Why

Some CloudFront access log lines contain raw invalid UTF-8 bytes. On current Ruby this raises ArgumentError: invalid byte sequence in UTF-8 during parse (often surfaced as Fluentd S3 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 fail JSON.generate (JSON::GeneratorError / Encoding::UndefinedConversionError), so logs still never reach Loki.

Test plan

  • bundle exec rake test on Ruby 2.7.8 — 17 tests / 27 assertions, including new invalid-UTF-8 and Unicode-preservation cases
  • Stock parser raises on invalid UTF-8 (Ruby 2.7.8 and Fluentd v1.19.2 / Ruby 3.4.8)
  • Patched plugin parses invalid lines, keeps João/çafé, scrubs 0xFF/0x80, and %09 regression still passes
  • Scrubbed record JSON.generate succeeds; ASCII-8BIT-only path fails JSON as expected
  • Push scrubbed JSON to Loki on local k3d (cf-utf8) → HTTP 204 and query returns the line

Notes for reviewers

Suggested base branch: develop (repo default).

cesarokuti and others added 3 commits August 7, 2026 21:33
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants