Skip to content

fix: truncate ingest strings on UTF-8 boundary - #907

Merged
unjica merged 1 commit into
developfrom
cursor/fix-truncate-utf8-boundary-a8e4
Oct 30, 2026
Merged

unjica merged 1 commit into
developfrom
cursor/fix-truncate-utf8-boundary-a8e4

Conversation

@unjica

@unjica unjica commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

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 maxBytes to 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.ts had this pattern. No similar issues found in packages/telemetry-core or elsewhere.

Refs #906

How to test

pnpm lint && pnpm test && pnpm build

All tests pass, including new comprehensive UTF-8 boundary tests covering:

  • 1-, 2-, 3-, and 4-byte characters (a, č, 日, 🚀)
  • Prefix lengths 0-3 around the 16 KiB boundary
  • Performance: each test completes in <10ms (vs infinite loop before)
  • Validation: output byte length ≤ limit, no U+FFFD introduced
  • Ingest-level tests through scrubIngestErrorFields and scrubIngestEventFields

Code review (Bugbot)

Paused: GitHub bugbot-review is currently a no-op. /review-bugbot is optional.

  • For notification, alert, billing, ingest, or auth changes: considered running /review-bugbot in Cursor (optional while the gate is paused)
  • If Bugbot comments appear anyway: addressed or replied (resolve thread + update .cursor/BUGBOT.md if a finding was a false positive)

Checklist

  • Tests pass (pnpm test)
  • Lint passes (pnpm lint)
  • Build succeeds (pnpm build)
  • CHANGELOG.md updated under [Unreleased] (if user-facing)
  • Documentation updated (if needed)
  • New code follows the existing style
  • No breaking changes (or called out clearly above)

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.

Open in Web Open in Cursor 

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
@unjica
unjica merged commit d88452d into develop Oct 30, 2026
7 checks passed
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.

1 participant