From 848e204ef9bdaf989e7e48025d4ea6ee21e7678e Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 13 Sep 2014 06:36:54 -0400
Subject: [PATCH] Fix validation of email addresses with IDNA domains (#1490067)

---
 CHANGELOG                             |    1 +
 tests/Framework/Utils.php             |    1 +
 program/lib/Roundcube/rcube_utils.php |    5 +++--
 3 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 5e02d93..c046100 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -60,6 +60,7 @@
 - Fix handling of email addresses with quoted domain part (#1490040)
 - Fix comm_path update on task switch (#1490041)
 - Fix error in MSSQL update script 2013061000.sql (#1490061)
+- Fix validation of email addresses with IDNA domains (#1490067)
 
 RELEASE 1.0.2
 -------------
diff --git a/program/lib/Roundcube/rcube_utils.php b/program/lib/Roundcube/rcube_utils.php
index 330322f..a51247e 100644
--- a/program/lib/Roundcube/rcube_utils.php
+++ b/program/lib/Roundcube/rcube_utils.php
@@ -103,13 +103,14 @@
             }
 
             foreach ($domain_array as $part) {
-                if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $part)) {
+                if (!preg_match('/^((xn--)?([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $part)) {
                     return false;
                 }
             }
 
             // last domain part
-            if (preg_match('/[^a-zA-Z]/', array_pop($domain_array))) {
+            $last_part = array_pop($domain_array);
+            if (strpos($last_part, 'xn--') !== 0 && preg_match('/[^a-zA-Z]/', $last_part)) {
                 return false;
             }
 
diff --git a/tests/Framework/Utils.php b/tests/Framework/Utils.php
index 560a8bd..b32974d 100644
--- a/tests/Framework/Utils.php
+++ b/tests/Framework/Utils.php
@@ -29,6 +29,7 @@
             array('email@domain.name', '.name is valid Top Level Domain name'),
             array('email@domain.co.jp', 'Dot in Top Level Domain name also considered valid (use co.jp as example here)'),
             array('firstname-lastname@domain.com', 'Dash in address field is valid'),
+            array('test@xn--e1aaa0cbbbcacac.xn--p1ai', 'IDNA domain'),
         );
     }
 

--
Gitblit v1.9.1