8027687: The constructors of URLPermission class do not behave as described in javad
authormichaelm
Mon, 04 Nov 2013 17:47:59 +0000
changeset 21608 73c4bf75786c
parent 21607 c91d2094ba1d
child 21609 f23c375e4baf
8027687: The constructors of URLPermission class do not behave as described in javad Reviewed-by: chegar, mduigou
jdk/src/share/classes/java/net/HostPortrange.java
jdk/src/share/classes/java/net/URLPermission.java
jdk/test/java/net/URLPermission/URLPermissionTest.java
--- a/jdk/src/share/classes/java/net/HostPortrange.java	Mon Nov 04 08:05:02 2013 -0800
+++ b/jdk/src/share/classes/java/net/HostPortrange.java	Mon Nov 04 17:47:59 2013 +0000
@@ -114,7 +114,7 @@
                 if (hoststr.equals("*")) {
                     hoststr = "";
                 } else if (hoststr.startsWith("*.")) {
-                    hoststr = hoststr.substring(1).toLowerCase(); // leave the '.' ?
+                    hoststr = toLowerCase(hoststr.substring(1));
                 } else {
                     throw new IllegalArgumentException("invalid host wildcard specification");
                 }
@@ -147,7 +147,7 @@
                         hoststr = sb.toString();
                     } else {
                         // regular domain name
-                        hoststr = hoststr.toLowerCase();
+                        hoststr = toLowerCase(hoststr);
                     }
                 }
             }
@@ -161,6 +161,38 @@
         }
     }
 
+    static final int CASE_DIFF = 'A' - 'a';
+
+    /**
+     * Convert to lower case, and check that all chars are ascii
+     * alphanumeric, '-' or '.' only.
+     */
+    static String toLowerCase(String s) {
+        int len = s.length();
+        StringBuilder sb = null;
+
+        for (int i=0; i<len; i++) {
+            char c = s.charAt(i);
+            if ((c >= 'a' && c <= 'z') || (c == '.')) {
+                if (sb != null)
+                    sb.append(c);
+            } else if ((c >= '0' && c <= '9') || (c == '-')) {
+                if (sb != null)
+                    sb.append(c);
+            } else if (c >= 'A' && c <= 'Z') {
+                if (sb == null) {
+                    sb = new StringBuilder(len);
+                    sb.append(s, 0, i);
+                }
+                sb.append((char)(c - CASE_DIFF));
+            } else {
+                throw new IllegalArgumentException("Invalid characters in hostname");
+            }
+        }
+        return sb == null ? s : sb.toString();
+    }
+
+
     public boolean literal() {
         return literal;
     }
--- a/jdk/src/share/classes/java/net/URLPermission.java	Mon Nov 04 08:05:02 2013 -0800
+++ b/jdk/src/share/classes/java/net/URLPermission.java	Mon Nov 04 17:47:59 2013 +0000
@@ -426,7 +426,10 @@
         this.ssp = url.substring(delim + 1);
 
         if (!ssp.startsWith("//")) {
-            this.authority = new Authority(scheme, ssp.toLowerCase());
+            if (!ssp.equals("*")) {
+                throw new IllegalArgumentException("invalid URL string");
+            }
+            this.authority = new Authority(scheme, "*");
             return;
         }
         String authpath = ssp.substring(2);
--- a/jdk/test/java/net/URLPermission/URLPermissionTest.java	Mon Nov 04 08:05:02 2013 -0800
+++ b/jdk/test/java/net/URLPermission/URLPermissionTest.java	Mon Nov 04 17:47:59 2013 +0000
@@ -186,6 +186,14 @@
         imtest("http:*", "https://www.foo.com/a/b/c", false),
         imtest("http:*", "http://www.foo.com/a/b/c", true),
         imtest("http:*", "http://foo/bar", true),
+        imtest("http://WWW.foO.cOM/a/b/*", "http://wwW.foo.com/a/b/c", true),
+        imtest("http://wWw.fOo.cOm/a/b/*", "http://Www.foo.com/a/b/*", true),
+        imtest("http://www.FOO.com/", "http://www.foo.COM/", true),
+        imtest("http://66ww-w.F-O012O.com/", "http://66ww-w.f-o012o.COM/",true),
+        imtest("http://xn--ire-9la.com/", "http://xn--ire-9la.COM/", true),
+        imtest("http://x/", "http://X/", true),
+        imtest("http://x/", "http://x/", true),
+        imtest("http://X/", "http://X/", true),
         imtest("http://foo/bar", "https://foo/bar", false)
     };
 
@@ -194,9 +202,12 @@
     static Test[] exceptionTests = {
         extest("http://1.2.3.4.5/a/b/c"),
         extest("http://www.*.com"),
-        //extest("http://www.foo.com:1-X"),
         extest("http://[foo.com]:99"),
         extest("http://[fec0::X]:99"),
+        extest("http:\\www.foo.com"),
+        extest("http://w_09ww.foo.com"),
+        extest("http://w&09ww.foo.com/p"),
+        extest("http://www+foo.com"),
         extest("http:")
     };