toBase64 has broken Unicode fallback — will throw on non-ASCII content #15

Closed
opened 2026-05-09 05:19:31 +00:00 by Mark · 0 comments
Owner

Severity: Low (edge case)

File: src/lib/git.ts lines 17-24

Issue: The toBase64 function:

function toBase64(str: string): string {
  try {
    return btoa(unescape(encodeURIComponent(str)));
  } catch {
    return btoa(str);
  }
}

The fallback btoa(str) will throw a InvalidCharacterError if str contains non-ASCII characters, because btoa only works with Latin-1 characters. The fallback is essentially useless — it would throw for the same reason the try block failed (non-ASCII characters).

This affects pushing files containing Unicode content to GitHub or Forgejo via the API.

Fix: The proper pattern is already in the try block (encodeURIComponent + unescape). The catch should not exist, or should use a TextEncoder approach.

**Severity:** Low (edge case) **File:** `src/lib/git.ts` lines 17-24 **Issue:** The `toBase64` function: ```typescript function toBase64(str: string): string { try { return btoa(unescape(encodeURIComponent(str))); } catch { return btoa(str); } } ``` The fallback `btoa(str)` will throw a `InvalidCharacterError` if `str` contains non-ASCII characters, because `btoa` only works with Latin-1 characters. The fallback is essentially useless — it would throw for the same reason the try block failed (non-ASCII characters). This affects pushing files containing Unicode content to GitHub or Forgejo via the API. **Fix:** The proper pattern is already in the try block (encodeURIComponent + unescape). The catch should not exist, or should use a TextEncoder approach.
Mark 2026-05-20 08:38:41 +00:00
Sign in to join this conversation.
No description provided.