Repository navigation
fix: truncate ingest strings on UTF-8 boundary - #907
Merged
Merged
Conversation
Replace infinite loop with bounded byte-boundary backtrack in truncateString. The previous implementation re-encoded after each truncation, causing an infinite loop when the cut landed mid-character (U+FFFD re-encodes as 3 bytes, dropping one byte yields U+FFFD again). New implementation walks backward from maxBytes to find the last valid UTF-8 start byte, ensuring O(1) complexity and no U+FFFD introduction. Add comprehensive tests for 1-, 2-, 3-, and 4-byte characters with various prefix lengths around 16 KiB boundary, plus ingest-level tests through scrubIngestErrorFields and scrubIngestEventFields. No similar patterns found in packages/telemetry-core or elsewhere in the repo. Refs #906
This was referenced Oct 30, 2026
Merged
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.
Base branch: open feature and fix PRs against
develop.Summary
Fixes infinite loop in
truncateString(added in PR #897) that froze the API when the 16 KiB byte cut landed inside a multi-byte UTF-8 character.Bug: The previous implementation re-encoded after each truncation. When the cut landed mid-character, it produced U+FFFD (3 bytes). Dropping one byte and decoding again yielded U+FFFD, creating an infinite synchronous loop that froze the entire API process.
Fix: Replace the loop with a bounded byte-boundary backtrack that walks backward from
maxBytesto find the last valid UTF-8 start byte. This is O(1) and never introduces U+FFFD.Scope: Only
apps/api/src/lib/ingest-pii-scrub.tshad this pattern. No similar issues found inpackages/telemetry-coreor elsewhere.Refs #906
How to test
All tests pass, including new comprehensive UTF-8 boundary tests covering:
scrubIngestErrorFieldsandscrubIngestEventFieldsCode review (Bugbot)
/review-bugbotin Cursor (optional while the gate is paused).cursor/BUGBOT.mdif a finding was a false positive)Checklist
pnpm test)pnpm lint)pnpm build)[Unreleased](if user-facing)Security / privacy
This is a production stability fix for ingest PII scrubbing. The new implementation maintains the same truncation guarantee (≤16 KiB) while ensuring valid UTF-8 output without hanging.