fix(isByteLength): handle unpaired surrogates instead of throwing#2742
fix(isByteLength): handle unpaired surrogates instead of throwing#2742sarathfrancis90 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:
|
|
Flagging that this overlaps with #2822, which fixes the same This one has been open since early June and is green, but I have no stake in which lands. Happy to close it if you prefer the #2822 approach, or to rebase mine. I just did not want two PRs sitting on the same function without anyone saying so. |
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.