Repository navigation
fix: do not crash on UTF-8 BOM documents after jschardet 4 - #31
Merged
Merged
Conversation
jschardet 4 reports chardet labels such as UTF-8-SIG that iconv-lite does not recognize. Decoding then threw on common UTF-8-with-BOM input when no charset was declared in headers or a meta tag.
|
Important Review skippedBot user detected. To trigger a single review, invoke the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
A missing iconv codec was decoded as the target encoding, including an explicit Content-Type charset. UTF-8-SIG still maps to utf-8. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
After the jschardet 4 bump,
detect()reports chardet labels such asUTF-8-SIGfor UTF-8 files that start with a BOM. iconv-lite does not recognize that name, soiconv.decodethrewEncoding not recognized: 'UTF-8-SIG'whenever charset could not be read from the Content-Type header or a<meta>tag.That is a common real-world input: Windows Notepad and many editors save UTF-8 with a BOM, and servers often send
Content-Type: text/htmlwith no charset.Fix: map
utf-8-sigtoutf-8. Any other name iconv-lite does not implement is passed through, soiconv.decodethrows instead of decoding those bytes as the target encoding.Validation: existing fixtures still pass; added a test for BOM input without a charset hint, and a test that an unsupported
charset=label throws.Note
Low Risk
Narrow encoding-alias fix in decode path; behavior for unsupported charsets remains fail-fast via iconv.
Overview
Fixes crashes when decoding UTF-8 with BOM HTML that has no
charsetin the Content-Type or document meta. jschardet 4 can reportUTF-8-SIG, which iconv-lite does not accept, soiconv.decodeused to throw on a common Windows/editor output pattern.The change adds a small
toIconvNamehelper and anutf-8-sig→utf-8alias applied to both Content-Type–parsed and inferred encodings before decode. Other labels are still passed through unchanged, so unsupported declared charsets continue to fail at decode time rather than being silently mis-decoded.Tests cover BOM-only detection (correct text, no leading BOM in output) and that an unknown
charset=value throws with the encoding name in the error.Reviewed by Cursor Bugbot for commit 15978f4. Bugbot is set up for automated code reviews on this repo. Configure here.