fix(isByteLength): handle unpaired surrogates instead of throwing#2742
Open
sarathfrancis90 wants to merge 1 commit into
Open
fix(isByteLength): handle unpaired surrogates instead of throwing#2742sarathfrancis90 wants to merge 1 commit into
sarathfrancis90 wants to merge 1 commit into
Conversation
isByteLength() computes the byte count with encodeURI(), which throws a URIError on a string containing an unpaired surrogate (e.g. '\uD835'). isEmail() runs the local part through isByteLength(), so it threw too, while every other validator just returns false on such input. Replace unpaired surrogates with U+FFFD (3 UTF-8 bytes, the same substitution a UTF-8 encoder makes) before encoding, leaving valid surrogate pairs untouched so the byte count is unchanged for them.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2742 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2587 2595 +8
Branches 656 659 +3
=========================================
+ Hits 2587 2595 +8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
isByteLengththrows aURIErrorinstead of returning a boolean when the input contains an unpaired surrogate:encodeURI()rejects unpaired surrogates, so the byte-count line blows up. Every other validator just returnsfalseon this kind of input, so the throw is both surprising and a small hazard when validating untrusted strings.The fix replaces unpaired surrogates with
U+FFFDbefore encoding — that's the same substitution a UTF-8 encoder makes (3 bytes), so the byte count stays correct and valid surrogate pairs are left exactly as they were. I added a regression test for bothisByteLengthandisEmail; the full suite passes.