You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2020/06/20 19:13:54 UTC

[commons-validator] branch master updated: VALIDATOR-464 - fix file: validation

This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-validator.git


The following commit(s) were added to refs/heads/master by this push:
     new 81d60ae  VALIDATOR-464 - fix file: validation
81d60ae is described below

commit 81d60ae98cfd5ae0bff1ccecb7654bdbc6bc2692
Author: Sebb <se...@apache.org>
AuthorDate: Sat Jun 20 20:13:44 2020 +0100

    VALIDATOR-464 - fix file: validation
---
 src/changes/changes.xml                            |  3 ++
 .../commons/validator/routines/UrlValidator.java   | 43 ++++++++++++++++++----
 .../validator/routines/UrlValidatorTest.java       | 14 +++++--
 3 files changed, 49 insertions(+), 11 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 85a852d..a007282 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -140,6 +140,9 @@ http://commons.apache.org/validator/dependencies.html
     <action issue="VALIDATOR-467" type="fix" dev="sebb" due-to="Ivan Larionov">
     URL validator fails if path starts with double slash and has underscores
     </action>
+    <action issue="VALIDATOR-464" type="fix" dev="sebb" due-to="Sebastian Choina">
+    UrlValidator says "file://bad ^ domain.com/label/test" is valid
+    </action>
   </release>
 
   <release version="1.6" date="2017-02-21" description="
diff --git a/src/main/java/org/apache/commons/validator/routines/UrlValidator.java b/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
index 8860a14..62abd9f 100644
--- a/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
+++ b/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
@@ -312,14 +312,11 @@ public class UrlValidator implements Serializable {
         }
 
         String authority = urlMatcher.group(PARSE_URL_AUTHORITY);
-        if ("file".equals(scheme)) {// Special case - file: allows an empty authority
-            if (authority != null) {
-                if (authority.contains(":")) { // but cannot allow trailing :
-                    return false;
-                }
-            }
-            // drop through to continue validation
-        } else { // not file:
+        if ("file".equals(scheme) && (authority == null || "".equals(authority))) {// Special case - file: allows an empty authority
+            return true; // this is a local file - nothing more to do here
+        } else if ("file".equals(scheme) && authority != null && authority.contains(":")) {
+            return false;
+        } else {
             // Validate the authority
             if (!isValidAuthority(authority)) {
                 return false;
@@ -542,4 +539,34 @@ public class UrlValidator implements Serializable {
     Matcher matchURL(String value) {
         return URL_PATTERN.matcher(value);
     }
+
+    public static void main(String[] args) {
+        UrlValidator val = new UrlValidator(new String[] { "file", "http", "https" }, UrlValidator.ALLOW_LOCAL_URLS);
+        for(String arg: args) {
+           Matcher m = val.matchURL(arg);
+           if (m.matches()) {
+              System.out.printf("%s has %d parts%n",arg,m.groupCount());
+              for(int i=1;i <m.groupCount(); i++) {
+                 String grp = m.group(i);
+                 if (grp != null) {
+                    System.out.printf("%d: %s%n",i, grp);
+                 }
+              }
+              String authority = m.group(4);
+              String path = m.group(5);
+              String query = m.group(7);
+              String frag = m.group(9);
+              System.out.printf("auth: %s %s%n", authority,val.isValidAuthority(authority));
+              System.out.printf("path: %s %s%n", path,val.isValidPath(path));
+              System.out.printf("query: %s %s%n", query,val.isValidQuery(query));
+              System.out.printf("frag: %s %s%n", frag,val.isValidFragment(frag));
+              System.out.printf("valid: %s %s%n", arg,val.isValid(arg));
+              System.out.println();
+           } else {
+              System.out.printf("%s is not valid%n",arg);
+           }
+  
+        }
+     }   
+  
 }
diff --git a/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
index 7a40e3c..b34a030 100644
--- a/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
+++ b/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
@@ -35,7 +35,7 @@ public class UrlValidatorTest extends TestCase {
    }
 
    @Override
-protected void setUp() {
+   protected void setUp() {
       for (int index = 0; index < testPartsIndex.length - 1; index++) {
          testPartsIndex[index] = 0;
       }
@@ -254,8 +254,7 @@ protected void setUp() {
         assertTrue("file:///c:/ should now be allowed",
                  validator.isValid("file:///C:/some.file"));
 
-        // Currently, we don't support the c:\ form
-        assertFalse("file:///c:\\ shouldn't be allowed",
+        assertTrue("file:///c:\\ should be allowed",
               validator.isValid("file:///C:\\some.file"));
 
         assertTrue("file:///etc/ should now be allowed",
@@ -331,6 +330,15 @@ protected void setUp() {
         assertFalse(urlValidator.isValid("http://example.rocks:100000/"));
     }
 
+    public void testValidator464() {
+        String[] schemes = {"file"};
+        UrlValidator urlValidator = new UrlValidator(schemes);
+        String fileOK = "file:///bad ^ domain.com/label/test";
+        String fileNAK = "file://bad ^ domain.com/label/test";
+        assertTrue(fileOK, urlValidator.isValid(fileOK));
+        assertFalse(fileNAK, urlValidator.isValid(fileNAK));
+    }
+
     static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) {
       boolean carry = true;  //add 1 to lowest order part.
       boolean maxIndex = true;