Make WordPress Core

Opened 13 years ago

Last modified 7 weeks ago

#25108 new defect (bug)

is_email() function validates email with domain that just has 1 character after dot

Reported by: hchouhan Owned by:
Priority: normal Milestone:
Component: Formatting Version: 3.6
Severity: normal Keywords: has-patch has-unit-tests is-email
Cc: Focuses:

Description

is_email() function validates an email address that has domain name extension of just 1 character. I am not sure if there are any domains with just 1 character after.

To Test I used below code:

<?php if ( is_email( 'email@domain.c' ) ) {
      echo 'email address is valid.';
} ?>

Attachments (2)

25108.diff​ (816 bytes ) - added by DrewAPicture 13 years ago.
25108.2.diff​ (1.3 KB ) - added by DrewAPicture 13 years ago.
+ unit-test

Download all attachments as: .zip

Change History (9)

#1 @rmccue
13 years ago

​This answer on Stack Overflow seems to indicate that in practice it's not valid, but it is valid in theory at least.

#2 @SergeyBiryukov
13 years ago

  • Component Validation → Mail

Related: #17491

#3 @DrewAPicture
13 years ago

  • Keywords has-patch added

25108.diff​ should work if we choose to "fix" this case.

#4 @nacin
13 years ago

  • Keywords needs-unit-tests added

In 25108.diff​, we can probably wait until after the loop is finished, then check $sub (which will hold the final piece) or do end( $subs ).

Needs unit tests.

I think it's more than safe to exclude a one-letter TLD, yes.

#5 @DrewAPicture
13 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

25108.2.diff​ uses @nacin's suggested changes and adds (my first!) unit test.

#6 @miqrogroove
11 years ago

  • Component Mail → Formatting
  • Keywords is-email added

This ticket was mentioned in ​PR #13714 on ​WordPress/wordpress-develop by ​skikken.


7 weeks ago
#7

The ticket is from 2013 and the behaviour it reports is still present on trunk: is_email( 'email@domain.c' ) returns the address as valid.

The direction was settled by a committer at the time. In comment #4 it was agreed that a one-letter TLD is safe to exclude, and the suggestion was to check the final segment after the loop finishes, using end( $subs ). Two patches were attached and both included tests, but the change was never committed. This pull request implements that agreed approach.

What the current state on trunk is

The domain handling in is_email() splits the domain into segments, requires at least two of them, and then validates each segment for hyphen limits and character set. Nothing checks the length of the final segment, so a single-character TLD passes every existing test.

A one-character address such as a@b.c is rejected today, but only incidentally, by the older minimum-length check (strlen( $email ) < 6). That is why the bug needs a longer local part to surface, as in the reporter's example.

What this changes

  • After the segment loop, the final segment is checked and rejected when it is shorter than two characters.
  • The rejection goes through the existing is_email filter with a last_sub_too_few_chars context, reusing the context string from the original attached patch, and that context is added to the list documented on the filter.
  • The older strlen( $email ) < 6 guard is untouched and still runs first.

Tests

  • email@domain.c (the reporter's exact example) and a second case are added to the invalid data provider.
  • A dedicated test pins the boundary, asserting the one-character segment is rejected while a two-character segment such as domain.co is still accepted, so the rule cannot drift.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Sonnet
Used for: test cases, code review

Note: See TracTickets for help on using tickets.