Repository navigation
fix(logs): Sanitize lone surrogates in LogBatcher to prevent Relay drops (#8144) - #8162
Open
ansh-rohilla wants to merge 1 commit into
Open
ansh-rohilla wants to merge 1 commit into
ansh-rohilla wants to merge 1 commit into
Conversation
…ops (getsentry#8144) When file names, environment variables, or arguments containing non-UTF-8 bytes are decoded with surrogateescape (e.g. via os.fsdecode), strings contain lone surrogates. When LogBatcher serializes logs with json.dumps(..., ensure_ascii=True), these lone surrogates are output as hex escapes (e.g. \udcff). Relay rejects lone surrogates in hex escapes with 'lone leading surrogate in hex escape', causing Relay to drop the entire log batch container. Sanitize strings in LogBatcher._to_transport_format by replacing lone surrogates with the Unicode replacement character (U+FFFD) across the log body, attribute keys, and string attribute values (including string arrays). Add regression tests verifying lone surrogates are replaced and JSON payloads remain valid UTF-8. Fixes getsentry#8144
This branch has not been deployed
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
When file names, environment variables, or arguments containing non-UTF-8 bytes are decoded with
surrogateescape(such as viaos.fsdecode), strings contain lone surrogates. WhenLogBatcherserializes logs withjson.dumps(..., ensure_ascii=True), these lone surrogates are output as hex escapes (e.g.\udcff).Relay's JSON parser strictly rejects lone surrogates in JSON hex escapes with
lone leading surrogate in hex escape, causing Relay to drop the entire log batch container and all logs flushed with it.Solution
In
LogBatcher._to_transport_format:\uFFFD(Unicode replacement character) usings.encode("utf-16", "surrogatepass").decode("utf-16", "replace")whens.encode("utf-8")fails.body, attribute keys, and string attribute values (including string arrays and coerced values).tests/test_logs.pyensuring lone surrogates are sanitized and serialized JSON envelopes are valid UTF-8.Fixes #8144