Skip to content

fix(node/buffer): fix base64 decoding for strings with hyphens#32298

Merged
bartlomieju merged 1 commit intodenoland:mainfrom
bartlomieju:fix/buffer-base64-hyphen
Feb 26, 2026
Merged

fix(node/buffer): fix base64 decoding for strings with hyphens#32298
bartlomieju merged 1 commit intodenoland:mainfrom
bartlomieju:fix/buffer-base64-hyphen

Conversation

@bartlomieju
Copy link
Copy Markdown
Member

Summary

  • Fix Buffer.from(str, "base64") throwing on strings containing - characters (e.g. "base64-encoded-bytes-from-browser")
  • Two bugs in base64ToBytes / base64clean:
    1. Base64url character replacements (-+, _/) were applied after base64clean, so the padding calculation used the wrong string length
    2. base64clean appended === when length % 4 == 1, which is invalid base64 padding — now drops the trailing character to match Node.js behavior

Test plan

  • Added regression test in tests/unit_node/buffer_test.ts verifying:
    • Buffer.from("base64-encoded-bytes-from-browser", "base64") produces correct output matching Node.js
    • Buffer.from("not-valid-base64!!!", "base64") doesn't throw
    • Buffer.from("A", "base64") returns empty buffer (single char too short)
  • Verified output matches Node.js exactly for all test cases

Closes #24908

🤖 Generated with Claude Code

Fixes base64 decoding for strings containing `-` and `_` characters
(base64url chars). Two bugs: (1) base64url replacements happened after
base64clean, causing wrong padding calculation, and (2) base64clean
appended `===` for length%4==1 which is invalid base64 — now drops the
trailing char like Node does.

Closes denoland#24908

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bartlomieju bartlomieju merged commit 7a2fdf5 into denoland:main Feb 26, 2026
110 checks passed
@bartlomieju bartlomieju deleted the fix/buffer-base64-hyphen branch February 26, 2026 08:26
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.

Bug: Buffer fails to encode ascii string to base64

2 participants