Scrub invalid UTF-8 in FailureFormatter#to_s#403
Merged
Conversation
82e79e0 to
546dc6b
Compare
`String#encode!(Encoding::UTF_8, invalid: :replace, ...)` is a no-op when the source encoding is already UTF-8. When `body` interpolation produces a UTF-8-tagged string with invalid byte sequences (e.g. binary protobuf bodies surfaced through WebMock failure messages), the existing fix lets those bytes through and `JSON.dump` later crashes in `BuildStatusReporter#write_failure_file` with `Encoding::UndefinedConversionError`, taking down the entire report step. Switch to `force_encoding(Encoding::UTF_8) + scrub!` which handles both the original ASCII-8BIT-source case and the new UTF-8-tagged-with-invalid- bytes case. Adds a regression test for the second case alongside the existing one.
546dc6b to
ac53686
Compare
dersam
approved these changes
Apr 30, 2026
This was referenced Apr 30, 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.
Problem
Minitest::Queue::FailureFormatter#to_susesString#encode!(Encoding::UTF_8, invalid: :replace, undef: :replace)to sanitize test output before it gets JSON-serialized inBuildStatusReporter#write_failure_file. Per the Ruby docs,encode!is a no-op when the source and destination encodings are the same. When test output contains a UTF-8-tagged string with invalid byte sequences — common when WebMock surfaces a protobuf request body in an error message — the call silently leaves the bad bytes intact.JSON.dumpthen crashes withEncoding::UndefinedConversionError, and the entireminitest-queue reportstep exits before writing any failure file. Builds with many failures lose every single failure record, breaking flake-triage workflows.Fix
Add
scrub!afterencode!to catch the caseencode!misses.encode!still handles transcoding from non-UTF-8 sources (ASCII-8BIT, ISO-8859-1, etc.), andscrub!cleans any remaining invalid byte sequences in UTF-8-tagged strings. Both use the same default replacement character (U+FFFD).Reference
Original ASCII-8BIT fix: ed4ae26 — this PR closes the remaining gap.
Closes https://github.com/shop/issues/issues/39402
co-authored by AI