Skip to content

Fix false UTF-7 detection of SHA-1 git hashes#324

Merged
dan-blanchard merged 1 commit intochardet:mainfrom
rembish:main
Mar 4, 2026
Merged

Fix false UTF-7 detection of SHA-1 git hashes#324
dan-blanchard merged 1 commit intochardet:mainfrom
rembish:main

Conversation

@rembish
Copy link
Copy Markdown
Contributor

@rembish rembish commented Mar 4, 2026

_is_valid_utf7_b64 skipped all content checks when base64 length was a multiple of 8 (padding_bits == 0). A 40-char hex SHA-1 hash meets that condition exactly, causing pure-ASCII requirements files with VCS pins to be misdetected as UTF-7 (fixes #323).

Fix: decode the base64 and reject lone surrogates — the reported hash decodes to 0xDDC6, an unpaired low surrogate, which is invalid UTF-16BE and can never appear in real UTF-7.

@dan-blanchard dan-blanchard self-requested a review March 4, 2026 13:11
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 4, 2026

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

Copy link
Copy Markdown
Member

@dan-blanchard dan-blanchard left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! If you could provide a more realistic example for that one test that is trying to trigger it via a VCS file (which I think is where you said the issue originally occurred), I'd appreciate. Otherwise this looks pretty good.

@rembish rembish force-pushed the main branch 2 times, most recently from b11a886 to 5e7cea8 Compare March 4, 2026 14:27
_is_valid_utf7_b64 skipped all content checks when base64 length
was a multiple of 8 (padding_bits == 0). A 40-char hex SHA-1 hash
meets that condition exactly, causing pure-ASCII requirements files
with VCS pins to be misdetected as UTF-7 (fixes chardet#323).

Fix: decode the base64 and reject lone surrogates — the reported
hash decodes to 0xDDC6, an unpaired low surrogate, which is invalid
UTF-16BE and can never appear in real UTF-7.
@dan-blanchard dan-blanchard merged commit 172aeb2 into chardet:main Mar 4, 2026
13 checks passed
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.

Wrong UTF7 detection breaks tox

2 participants