From 78b4e241af098394de69f12ebddeea3afe07e660 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 15 Oct 2025 16:52:26 +0200 Subject: [PATCH] Bug 41025: Check for a dot in domain name, allow localhost Content-Type: text/plain; charset=utf-8 If Email::Address has been patched, this can be removed. Test plan: Run t/Koha/Email.t Signed-off-by: Marcel de Rooy --- Koha/Email.pm | 9 ++++++++- t/Koha/Email.t | 13 ++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Koha/Email.pm b/Koha/Email.pm index ccb03ee070..7e7add1f2a 100644 --- a/Koha/Email.pm +++ b/Koha/Email.pm @@ -245,8 +245,15 @@ Return true is the email address passed in parameter is valid following RFC 2822 sub is_valid { my ( $class, $email ) = @_; - my @addrs = Email::Address->parse($email); + my @addrs = _temporary_host_test( Email::Address->parse($email) ); return @addrs ? 1 : 0; } +sub _temporary_host_test { + + # regex looks for a dot in the domain, other case should be caught by E::A + # exception for localhost (see bug 28017 and Email.t) + return grep { $_->host =~ /\.|^localhost$/ } @_; +} + 1; diff --git a/t/Koha/Email.t b/t/Koha/Email.t index 0b73edeabd..c3cc27d998 100755 --- a/t/Koha/Email.t +++ b/t/Koha/Email.t @@ -269,18 +269,21 @@ subtest 'send_or_die() tests' => sub { }; subtest 'is_valid' => sub { - plan tests => 8; + plan tests => 11; is( Koha::Email->is_valid('Fróm '), 1 ); is( Koha::Email->is_valid('from@example.com'), 1 ); is( Koha::Email->is_valid(''), 1 ); - is( Koha::Email->is_valid('root@localhost'), 1 ); # See bug 28017 + is( Koha::Email->is_valid('root@localhost'), 1, 'localhost allowed as exception' ); # See bug 28017 + is( Koha::Email->is_valid('root@example.com.'), 1, 'trailing period got accepted, dont ask me why' ); is( Koha::Email->is_valid(''), 0 ) ; # "In accordance with RFC 822 and its descendants, this module demands that email addresses be ASCII only" - isnt( Koha::Email->is_valid('@example.com'), 1 ); - isnt( Koha::Email->is_valid('example.com'), 1 ); - isnt( Koha::Email->is_valid('from'), 1 ); + isnt( Koha::Email->is_valid('@example.com'), 1 ); + isnt( Koha::Email->is_valid('example.com'), 1 ); + isnt( Koha::Email->is_valid('from'), 1 ); + isnt( Koha::Email->is_valid('root@example'), 1, 'example is not a TLD' ); + isnt( Koha::Email->is_valid('root@.example.com'), 1, 'period in front of domain' ); }; subtest 'new_from_string() tests' => sub { -- 2.39.5