# HG changeset patch # User xuelei # Date 1376959359 25200 # Node ID c4414bc886022fe1820495333f33f48c422a77c0 # Parent 57876ed3c4262b46972efd4e2bc8ecbf27021373 8020842: IDN do not throw IAE when hostname ends with a trailing dot Reviewed-by: weijun, michaelm diff -r 57876ed3c426 -r c4414bc88602 jdk/src/share/classes/java/net/IDN.java --- a/jdk/src/share/classes/java/net/IDN.java Mon Aug 19 17:17:17 2013 -0400 +++ b/jdk/src/share/classes/java/net/IDN.java Mon Aug 19 17:42:39 2013 -0700 @@ -113,11 +113,18 @@ int p = 0, q = 0; StringBuffer out = new StringBuffer(); + if (isRootLabel(input)) { + return "."; + } + while (p < input.length()) { q = searchDots(input, p); out.append(toASCIIInternal(input.substring(p, q), flag)); + if (q != (input.length())) { + // has more labels, or keep the trailing dot as at present + out.append('.'); + } p = q + 1; - if (p < input.length()) out.append('.'); } return out.toString(); @@ -167,11 +174,18 @@ int p = 0, q = 0; StringBuffer out = new StringBuffer(); + if (isRootLabel(input)) { + return "."; + } + while (p < input.length()) { q = searchDots(input, p); out.append(toUnicodeInternal(input.substring(p, q), flag)); + if (q != (input.length())) { + // has more labels, or keep the trailing dot as at present + out.append('.'); + } p = q + 1; - if (p < input.length()) out.append('.'); } return out.toString(); @@ -263,6 +277,13 @@ dest = new StringBuffer(label); } + // step 8, move forward to check the smallest number of the code points + // the length must be inside 1..63 + if (dest.length() == 0) { + throw new IllegalArgumentException( + "Empty label is not a legal name"); + } + // step 3 // Verify the absence of non-LDH ASCII code points // 0..0x2c, 0x2e..0x2f, 0x3a..0x40, 0x5b..0x60, 0x7b..0x7f @@ -311,7 +332,7 @@ // step 8 // the length must be inside 1..63 - if(dest.length() > MAX_LABEL_LENGTH){ + if (dest.length() > MAX_LABEL_LENGTH) { throw new IllegalArgumentException("The label in the input is too long"); } @@ -409,8 +430,7 @@ private static int searchDots(String s, int start) { int i; for (i = start; i < s.length(); i++) { - char c = s.charAt(i); - if (c == '.' || c == '\u3002' || c == '\uFF0E' || c == '\uFF61') { + if (isLabelSeparator(s.charAt(i))) { break; } } @@ -418,6 +438,19 @@ return i; } + // + // to check if a string is a root label, ".". + // + private static boolean isRootLabel(String s) { + return (s.length() == 1 && isLabelSeparator(s.charAt(0))); + } + + // + // to check if a character is a label separator, i.e. a dot character. + // + private static boolean isLabelSeparator(char c) { + return (c == '.' || c == '\u3002' || c == '\uFF0E' || c == '\uFF61'); + } // // to check if a string only contains US-ASCII code point diff -r 57876ed3c426 -r c4414bc88602 jdk/test/java/net/IDN/IllegalArg.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/net/IDN/IllegalArg.java Mon Aug 19 17:42:39 2013 -0700 @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8020842 + * @summary IDN do not throw IAE when hostname ends with a trailing dot + */ + +import java.net.*; + +public class IllegalArg { + + public static void main(String[] args) throws Exception { + String[] illegalNames = { + "com..net", + "com..", + ".com", + ".com." + }; + + String[] legalNames = { + "example.com", + "com\u3002", + "com.", + "." + }; + + for (String name : illegalNames) { + try { + IDN.toASCII(name, IDN.USE_STD3_ASCII_RULES); + throw new Exception( + "Expected to get IllegalArgumentException for " + name); + } catch (IllegalArgumentException iae) { + // That's the right behavior. + } + + try { + IDN.toASCII(name); + throw new Exception( + "Expected to get IllegalArgumentException for " + name); + } catch (IllegalArgumentException iae) { + // That's the right behavior. + } + } + + for (String name : legalNames) { + System.out.println("Convering " + name); + System.out.println(IDN.toASCII(name, IDN.USE_STD3_ASCII_RULES)); + } + } +} diff -r 57876ed3c426 -r c4414bc88602 jdk/test/sun/security/ssl/javax/net/ssl/ServerName/IllegalSNIName.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/sun/security/ssl/javax/net/ssl/ServerName/IllegalSNIName.java Mon Aug 19 17:42:39 2013 -0700 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8020842 + * @summary SNIHostName does not throw IAE when hostname ends + * with a trailing dot + */ + +import javax.net.ssl.SNIHostName; + +public class IllegalSNIName { + + public static void main(String[] args) throws Exception { + String[] illegalNames = { + "example\u3003\u3002com", + "example..com", + "com\u3002", + "com.", + "." + }; + + for (String name : illegalNames) { + try { + SNIHostName hostname = new SNIHostName(name); + throw new Exception( + "Expected to get IllegalArgumentException for " + name); + } catch (IllegalArgumentException iae) { + // That's the right behavior. + } + } + } +}