Skip to content

Handle malformed Unicode in generated avatars - #113

Merged
brionmario merged 1 commit into
thunder-id:mainfrom
ThumulaPerera:fix-avatar-malformed-unicode
Oct 16, 2026
Merged

brionmario merged 1 commit into
thunder-id:mainfrom
ThumulaPerera:fix-avatar-malformed-unicode

Conversation

@ThumulaPerera

Copy link
Copy Markdown
Member

Purpose

generateAvatarDataUri didn't handle some valid inputs correctly:

  • Emoji cut in half. Text-avatar initials were cut with String.prototype.slice, which counts UTF-16 code units. When the cut fell inside an emoji or another character outside the Basic Multilingual Plane, it left a lone surrogate. For example, content: 'B😈' with lead to a lone surrogate causing rendering issue, and content: '😈B' lost its second letter.
  • Invalid SVG. Initials were inserted into the SVG without XML escaping. Content containing & or < produced invalid SVG, which renders as a broken image.

Approach

All changes are in packages/browser/src/utils/generateAvatarDataUri.ts:

  • Initials are cut by code point (Array.from), so a character is never split.
  • Initials are XML-escaped before they're inserted into the <text> element.
  • A new toSvgDataUri helper replaces any remaining lone surrogate with U+FFFD before encoding. All three generators (gradient, anonymous animal, anonymous entity) use it, so none of them can throw on caller-supplied content or bg.
  • The surrogate check uses Array.from rather than a /\p{Surrogate}/u regex, so the package doesn't need any newer JavaScript features than it does today.

Related Issues

  • N/A

Related PRs

  • N/A

Checklist

  • Followed the contribution guidelines.
  • Manual test round performed and verified.
  • Documentation provided. (Add links if there are any)
  • Tests provided. (Add links if there are any)
    • Unit Tests
    • Integration Tests
  • Breaking changes. (Fill if applicable)
    • Breaking changes section filled.
    • breaking change label added.
  • Cross-SDK parity. Exactly one of parity/prs-raised or parity/prs-not-needed added.
    • If parity/prs-raised, the port links are posted as a reply on the parity check's comment.

Security checks

  • Followed secure coding standards.
  • Confirmed that this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

🔀 Cross-SDK feature parity, no ports needed

This pull request is labelled parity/prs-not-needed: nothing here has to ship in the other ThunderID SDKs. If that changes, swap the label for parity/prs-raised.

@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration
  • Configuration used: Repository: thunder-id/javascript-sdks/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5e0db683-94c6-4dc7-be09-287eecd39f7e
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ThumulaPerera ThumulaPerera added the parity/prs-not-needed Nothing here reaches another SDK label Oct 7, 2026
@ThumulaPerera
ThumulaPerera force-pushed the fix-avatar-malformed-unicode branch from ed2c663 to 974c486 Compare October 16, 2026 08:56
@brionmario
brionmario merged commit 224c04f into thunder-id:main Oct 16, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parity/prs-not-needed Nothing here reaches another SDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants