Skip to content

fix(logs): Sanitize lone surrogates in LogBatcher to prevent Relay drops (#8144) - #8162

Open
ansh-rohilla wants to merge 1 commit into
getsentry:masterfrom
ansh-rohilla:fix/log-lone-surrogates-8144
Open

ansh-rohilla wants to merge 1 commit into
getsentry:masterfrom
ansh-rohilla:fix/log-lone-surrogates-8144

Conversation

@ansh-rohilla

Copy link
Copy Markdown

Problem

When file names, environment variables, or arguments containing non-UTF-8 bytes are decoded with surrogateescape (such as 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'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:

  • Replace lone surrogates with \uFFFD (Unicode replacement character) using s.encode("utf-16", "surrogatepass").decode("utf-16", "replace") when s.encode("utf-8") fails.
  • Sanitize the log body, attribute keys, and string attribute values (including string arrays and coerced values).
  • Add regression tests in tests/test_logs.py ensuring lone surrogates are sanitized and serialized JSON envelopes are valid UTF-8.

Fixes #8144

…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
@ansh-rohilla
ansh-rohilla requested a review from a team as a code owner October 30, 2026 04:55

This branch has not been deployed

No deployments
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.

Logs with a lone surrogate make Relay drop the whole log batch

1 participant