commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1649251 - in /commons/proper/validator/trunk: RELEASE-NOTES.txt src/changes/changes.xml src/main/java/org/apache/commons/validator/routines/DomainValidator.java src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
Date Sat, 03 Jan 2015 21:27:33 GMT
Author: sebb
Date: Sat Jan  3 21:27:33 2015
New Revision: 1649251

URL: http://svn.apache.org/r1649251
Log:
VALIDATOR-297 - Punycode url is not valid. Top-level domain regex matching was wrong;
                e.g. it did not allow embedded "-" as per RFC2396

Modified:
    commons/proper/validator/trunk/RELEASE-NOTES.txt
    commons/proper/validator/trunk/src/changes/changes.xml
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java

Modified: commons/proper/validator/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/RELEASE-NOTES.txt?rev=1649251&r1=1649250&r2=1649251&view=diff
==============================================================================
--- commons/proper/validator/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/validator/trunk/RELEASE-NOTES.txt Sat Jan  3 21:27:33 2015
@@ -69,6 +69,8 @@ BUGS FROM PREVIOUS RELEASE
  * [VALIDATOR-348] - Update TLD list to latest version (Version 2014123000)
  * [VALIDATOR-309] - UrlValidator does not validate uppercase URL schemes
                      Thanks to Rashid Rashidov
+ * [VALIDATOR-297] - Punycode url is not valid. Top-level domain regex matching was wrong;
+                     e.g. it did not allow embedded "-" as per RFC2396
 
 IMPROVEMENTS OVER PREVIOUS RELEASE
 ===================================

Modified: commons/proper/validator/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/changes/changes.xml?rev=1649251&r1=1649250&r2=1649251&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/changes/changes.xml (original)
+++ commons/proper/validator/trunk/src/changes/changes.xml Sat Jan  3 21:27:33 2015
@@ -43,6 +43,10 @@ The <action> type attribute can be add,u
   <body>
 
   <release version="1.4.1" date="tba" description="1.4 Maintenance release">
+    <action issue="VALIDATOR-297" dev="sebb" type="fix">
+      Punycode url is not valid
+      Top-level domain regex matching was wrong; did not allow embedded "-" as per RFC2396
+    </action>
     <action issue="VALIDATOR-334" dev="sebb" type="update">
       UrlValidator: isValidAuthority() returning true when supplied authority validator fails
     </action>

Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java?rev=1649251&r1=1649250&r2=1649251&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java
(original)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java
Sat Jan  3 21:27:33 2015
@@ -65,8 +65,13 @@ public class DomainValidator implements
     private static final long serialVersionUID = -4407125112880174009L;
 
     // Regular expression strings for hostnames (derived from RFC2396 and RFC 1123)
+
+    // RFC2396: domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum
     private static final String DOMAIN_LABEL_REGEX = "\\p{Alnum}(?>[\\p{Alnum}-]*\\p{Alnum})*";
-    private static final String TOP_LABEL_REGEX = "\\p{Alpha}{2,}";
+
+    // RFC2396 toplabel = alpha | alpha *( alphanum | "-" ) alphanum
+    private static final String TOP_LABEL_REGEX = "\\p{Alpha}|(?:\\p{Alpha}(?:[\\p{Alnum}-])*\\p{Alnum})";
+
     private static final String DOMAIN_NAME_REGEX =
             "^(?:" + DOMAIN_LABEL_REGEX + "\\.)+" + "(" + TOP_LABEL_REGEX + ")$";
 
@@ -137,6 +142,12 @@ public class DomainValidator implements
         return allowLocal && hostnameRegex.isValid(domain);
     }
 
+    // package protected for unit test access 
+    final boolean isValidDomainSyntax(String domain) {
+        String[] groups = domainRegex.match(domain);
+        return (groups != null && groups.length > 0);
+    }
+
     /**
      * Returns true if the specified <code>String</code> matches any
      * IANA-defined top-level domain. Leading dots are ignored if present.

Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java?rev=1649251&r1=1649250&r2=1649251&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
(original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/DomainValidatorTest.java
Sat Jan  3 21:27:33 2015
@@ -134,6 +134,35 @@ public class DomainValidatorTest extends
        assertTrue("b\u00fccher.ch in IDN should validate", validator.isValid("www.xn--bcher-kva.ch"));
     }
 
+    // RFC2396: domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum
+    public void testRFC2396domainlabel() {
+        assertTrue("a.ch should validate", validator.isValid("a.ch"));
+        assertTrue("9.ch should validate", validator.isValid("9.ch"));
+        assertTrue("az.ch should validate", validator.isValid("az.ch"));
+        assertTrue("09.ch should validate", validator.isValid("09.ch"));
+        assertTrue("9-1.ch should validate", validator.isValid("9-1.ch"));
+        assertFalse("91-.ch should not validate", validator.isValid("91-.ch"));
+        assertFalse("-.ch should not validate", validator.isValid("-.ch"));
+    }
+
+    // RFC2396 toplabel = alpha | alpha *( alphanum | "-" ) alphanum
+    public void testRFC2396toplabel() {
+        // These tests use non-existent TLDs so currently need to use a package protected
method
+        assertTrue("a.c (alpha) should validate", validator.isValidDomainSyntax("a.c"));
+        assertTrue("a.cc (alpha alpha) should validate", validator.isValidDomainSyntax("a.cc"));
+        assertTrue("a.c9 (alpha alphanum) should validate", validator.isValidDomainSyntax("a.c9"));
+        assertTrue("a.c-9 (alpha - alphanum) should validate", validator.isValidDomainSyntax("a.c-9"));
+        assertTrue("a.c-z (alpha - alpha) should validate", validator.isValidDomainSyntax("a.c-z"));
+
+        assertFalse("a.c- (alpha -) should fail", validator.isValidDomainSyntax("a.c-"));
+        assertFalse("a.- (-) should fail", validator.isValidDomainSyntax("a.-"));
+        assertFalse("a.-9 (- alphanum) should fail", validator.isValidDomainSyntax("a.-9"));
+    }
+
+    public void testValidator297() {
+        assertTrue("xn--d1abbgf6aiiy.xn--p1ai should validate", validator.isValid("xn--d1abbgf6aiiy.xn--p1ai"));
// This uses a valid TLD
+     }
+
     // Check array is sorted and is lower-case
     public void test_INFRASTRUCTURE_TLDS_sortedAndLowerCase() throws Exception {
         final boolean sorted = isSortedLowerCase("INFRASTRUCTURE_TLDS");



Mime
View raw message