Skip to content

Transcode ASCII and Latin-1 like Node in node:buffer transcode() - #8687

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-19-audit-node-transcode
Nov 20, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-19-audit-node-transcode

Conversation

@littledivy

Copy link
Copy Markdown
Member

transcode() opens ICU converters named us-ascii and iso8859-1, but Chromium's ICU data makes both aliases of windows-1252. So every pair with ASCII or Latin-1 on either side, apart from widening into UTF-16LE, gave different bytes from Node:

transcode(Buffer.from([0x80]), 'latin1', 'utf8');  // Node: U+0080, workerd: U+20AC
transcode(Buffer.from('€'), 'utf8', 'latin1');     // Node: '?', workerd: 0x80
transcode(Buffer.from([0xe9]), 'ascii', 'utf8');   // Node: U+FFFD, workerd: 'é'

ascii to ascii, ascii to latin1 and latin1 to ascii also passed bytes 0x80-0xFF through unchanged, where Node gives ?. The C++ implementation used the same converter names and had the same problem.

In this patch these pairs no longer use ICU. A small safe-Rust converter decodes Latin-1 bytes to U+0000-U+00FF. Non-ASCII bytes, ill-formed UTF-8 (one per maximal subpart) and unpaired surrogates decode to U+FFFD. Encoding into ASCII or Latin-1 writes ? for anything the target can't represent, and drops a default-ignorable code point it can't represent, as ICU's substitute callback does. ICU is left with the UTF-8 and UTF-16LE identity pairs, so the second commit removes the substitute setup that nothing uses any more.

I compared workerd with Node 26 in two runs: 2240 hand-picked transcode() calls covering every encoding pair and alias, and 4000 seeded random calls built from raw bytes and code points at the edges of every default-ignorable range, plus surrogates, C1 controls, noncharacters and U+10FFFF. Every call that succeeds in both now gives the same bytes. Before, 190 of the hand-picked calls didn't.

This changes the output for existing Workers that transcode ASCII or Latin-1 text with non-ASCII bytes. We accept that without a compat flag: the old output is wrong for code written for Node, and it doesn't match workerd's own buffer.toString('latin1').

transcode() opened ICU converters named "us-ascii" and "iso8859-1", but
Chromium's ICU data makes both aliases of windows-1252. So every pair with
ASCII or Latin-1 on either side, apart from widening into UTF-16LE, gave
different bytes from Node:

  transcode(Buffer.from([0x80]), 'latin1', 'utf8')  // Node: U+0080, workerd: U+20AC
  transcode(Buffer.from('€'), 'utf8', 'latin1') // Node: '?', workerd: 0x80
  transcode(Buffer.from([0xe9]), 'ascii', 'utf8')   // Node: U+FFFD, workerd: U+00E9

ascii->ascii, ascii->latin1 and latin1->ascii passed bytes 0x80-0xFF
through, where Node gives '?'. This was also true of the C++
implementation, which used the same names.

In this patch these pairs no longer use ICU. A small safe-Rust converter
decodes Latin-1 bytes to U+0000-U+00FF, and non-ASCII bytes, ill-formed
UTF-8 (per maximal subpart) and unpaired surrogates to U+FFFD. It encodes
into ASCII or Latin-1 with '?' for anything the target can't represent,
and drops an unrepresentable default-ignorable code point, as ICU's
substitute callback does. ASCII and Latin-1 to UTF-8 are now sized
exactly. A differential run of 2240 transcode() calls against Node 26
now gives the same bytes for every call that succeeds in both.

This changes the output of existing Workers that transcode ASCII or
Latin-1 text containing non-ASCII bytes. We accept that without a compat
flag: the old output is wrong for code written against Node, and doesn't
agree with workerd's own Buffer#toString('latin1').
ICU now converts only the UTF-8 and UTF-16LE identity pairs in
transcode(). Their targets can represent every code point, so the '?'
substitute set on the target converter is never used. In this patch
Converter::set_subst_chars, Converter::min_char_size and the
SetSubstituteCharsFailed error go away.
@littledivy
littledivy requested review from a team as code owners November 19, 2026 18:47

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found. The converter sizes and bounds its output correctly, unsafe ICU code is removed, and the commit message explicitly accepts the compatibility change.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The new code is safe Rust, and its destination sizing covers every write. Removing the raw-pointer ICU wrappers reduces the unsafe surface.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

The converter and its tests are correct. dest_len bounds every write transcode_into makes, and the reworked Transcoder::new match handles invalid encodings correctly. The first commit message records the decision to change behaviour without a compatibility flag.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: c5e85c34

@guybedford guybedford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I checked this against Node on a set of edge cases (CESU surrogates, overlong and truncated UTF-8, lone surrogates at EOF, BOMs, U+E0001 vs U+E1000, U+00AD into ASCII vs Latin-1), and they all match.

Optional follow-ons, not blocking:

  • Latin-1 to UTF-8 could use simdutf's convert_latin1_to_utf8 / utf8_length_from_latin1, alongside the existing convert_latin1_to_utf16. An all-ASCII memcpy fast path for ascii->ascii/utf8 would be a similar win.
  • The generic decode/encode loop in single_byte could also cover the UTF-8 and UTF-16LE identity pairs, which would let the crate drop Converter and the remaining ucnv_* FFI.

@littledivy
littledivy merged commit d62c4b3 into main Nov 20, 2026
36 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-19-audit-node-transcode branch November 20, 2026 04:21
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.

2 participants