Repository navigation
Conversation
The email format accepted values containing a non-breaking space (U+00A0) or other non-ASCII whitespace because the underlying validator's regex only excludes ASCII whitespace. As a result an email with a leading U+00A0 was treated as valid, while the same address with a regular leading space was correctly rejected. EmailFormat now rejects any value containing a non-ASCII whitespace character before delegating to the validator. Character.isWhitespace deliberately excludes U+00A0, so Character.isSpaceChar is also checked. All-ASCII emails (including quoted local parts with spaces such as "joe bloggs"@example.com) are unaffected. Adds EmailFormatTest, following the existing per-format test convention. Closes networknt#1164
|
Code Review — PR #1267: Reject non-ASCII whitespace in email format Overview Fixes issue #1164: {"format":"email"} accepts addresses containing non-ASCII whitespace (e.g. a leading U+00A0 NON-BREAKING SPACE), while the same address with a regular ASCII space is correctly rejected. The root cause is that the underlying EmailValidator regex only excludes ASCII whitespace. The PR adds a pre-check in EmailFormat.matches() that rejects any value containing a non-ASCII whitespace code point before delegating, and adds a new EmailFormatTest. 85 additions, 2 files, no deletions. Correctness ✅
Conventions 👍
Suggestions
Broader observation (non-blocking) The true root cause is that the bundled EmailValidator is permissive about non-ASCII input generally, not just whitespace. The email format is ASCII-only per RFC 5321, so one could argue any non-ASCII character should be rejected. The PR's narrow, whitespace-only scope is the right call for a targeted bugfix — it fixes #1164 without risking behavioral changes to real-world emails — but it's worth a sentence in the code/issue noting the fix is deliberately narrow. Performance / Security
Verdict Approve — correct, minimal, well-documented, and follows project test conventions. The only requested changes are optional polish: add the explicit quoted-local-part positive test and a second whitespace character, align the license header, and consider extracting the helper ahead of the idn-email follow-up. |
…tests (#1268) * Reject non-ASCII whitespace in idn-email and strengthen email format tests Follow-up to #1267 applying the review suggestions: - Extract the non-ASCII-whitespace check to Strings.containsNonAsciiWhitespace and reuse it from both EmailFormat and IdnEmailFormat. - idn-email shares EmailFormat's delegation and had the same bug (a leading U+00A0 was accepted); it now rejects non-ASCII whitespace too. Adds a license header to IdnEmailFormat (it had none) and a new IdnEmailFormatTest that also verifies a non-ASCII letter is still accepted. - Strengthen EmailFormatTest: assert a quoted local part with an ASCII space ("joe bloggs"@example.com) stays valid, and generalize the negative case over U+00A0, U+2003 and U+3000. The change stays deliberately narrow (whitespace only); non-ASCII letters are still accepted in idn-email. Zero-width format characters (e.g. U+200B, U+FEFF) are out of scope and unchanged. * Generalize idn-email whitespace test over U+2003 and U+3000 --------- Co-authored-by: el-psy-kongroo-d <[email protected]>
Problem
{ "format": "email" }accepts values containing a non-breaking space (U+00A0)or other non-ASCII whitespace. For example, an email with a leading U+00A0 is
treated as valid, even though the same address with a regular leading space is
correctly rejected.
The underlying
EmailValidatorregex only excludes ASCII whitespace, sonon-ASCII whitespace slips through.
Fix
EmailFormatnow rejects any value containing a non-ASCII whitespace characterbefore delegating to the validator.
Character.isWhitespacedeliberatelyexcludes U+00A0, so
Character.isSpaceCharis also checked.The change only affects values that contain non-ASCII whitespace; all-ASCII
emails — including quoted local parts with spaces such as
"joe bloggs"@example.com— are unchanged.Following the existing per-format test convention (
TimeFormatTest,UriFormatTest, …), this also addsEmailFormatTest.Note on idn-email
IdnEmailFormat(idn-email) shares the same delegation and therefore the samebehavior, but is intentionally left out of scope here since this issue concerns
email. Happy to address it in a follow-up if desired.Testing
EmailFormatTestcovers a plainly valid email and the reportednon-breaking space case.
Suite email cases (e.g. the valid quoted local part
"joe bloggs"@example.com).Closes #1164