Fix charset decoding for text bodies - #754
Open
deepakganesh78 wants to merge 1 commit into
Open
Conversation
Use Node's WHATWG TextDecoder for charsets it supports so text bodies follow Encoding Standard mappings, while keeping iconv-lite as the fallback for legacy encodings such as generic utf-16 and utf-32. Add regression coverage for windows-1252 C1 controls and invalid UTF-16 surrogate pairs. Fixes expressjs#680 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
bjohansebas
suggested changes
Aug 2, 2026
bjohansebas
left a comment
Member
There was a problem hiding this comment.
I actually recommend upgrading to the latest version of raw-body instead of using iconv-lite. It also lets you override the TextDecoder, as raw-body now does, in case anyone needs support for UTF-7 or other custom encodings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #680
Reproduction
A
text/plain; charset=windows-1252request containing bytes80 81 82 83 8d 9e 9fdecoded to€�‚ƒ�žŸ, replacing C1 control mappings at0x81and0x8d. Atext/plain; charset=utf-16lerequest containing two unpaired high surrogates decoded to a non-well-formed JavaScript string instead of replacement characters.Root cause
raw-body/iconv-litedecoding is used for request character sets. For encodings covered by the WHATWG Encoding Standard, that can differ from the platformTextDecodermappings and UTF-16 replacement behavior available in supported Node.js versions.Fix
Read parser bodies as bytes, then decode with Node's WHATWG
TextDecoderwhen it supports the charset. Keepiconv-liteas the fallback for legacy charsets thatTextDecoderdoes not support, and keep genericutf-16on the existing iconv-lite path to preserve current BOM-based behavior.Compatibility notes
This intentionally changes decoded text for WHATWG-supported charset labels to match
TextDecoder; unsupported labels and existingutf-16/utf-32behavior continue throughiconv-lite.Validation
npx mocha --reporter spec --check-leaks test\text.js --grep "windows-1252|utf-16"-> 0 passing, 2 failing.npm test-> 271 passing.npm run lint-> passed.