From 242ae66e42af3c8bc0dcf1ecbf43521df607b6ec Mon Sep 17 00:00:00 2001 From: "Michael[tm] Smith" Date: Tue, 18 Dec 2018 01:26:01 +0900 Subject: [PATCH] Allow hyphens anywhere in URL hostnames (#69) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change brings galimatias’s ToASCII and ToUnicode behavior into conformance with the current URL spec, which requires the ToASCII and ToUnicode algorithms to be performed with the CheckHyphens flag set to false — which means that hyphens are allowed anywhere in the URL hostname, including leading and trailing hyphens. Fixes https://github.com/validator/validator/issues/720 Thanks @KatieMFritz --- src/main/java/io/mola/galimatias/URLUtils.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/mola/galimatias/URLUtils.java b/src/main/java/io/mola/galimatias/URLUtils.java index e9859de..ecb99b7 100644 --- a/src/main/java/io/mola/galimatias/URLUtils.java +++ b/src/main/java/io/mola/galimatias/URLUtils.java @@ -124,7 +124,7 @@ static String domainToASCII(final String domain, final ErrorHandler errorHandler final IDNA.Info idnaInfo = new IDNA.Info(); final StringBuilder idnaOutput = new StringBuilder(); idna.nameToASCII(domain, idnaOutput, idnaInfo); - processIdnaInfo(errorHandler, idnaInfo); + processIdnaInfo(errorHandler, idnaInfo, false); return idnaOutput.toString(); } @@ -145,11 +145,13 @@ static String domainToUnicode(final String asciiDomain, final ErrorHandler error final IDNA.Info unicodeIdnaInfo = new IDNA.Info(); final StringBuilder unicodeIdnaOutput = new StringBuilder(); idna.nameToUnicode(asciiDomain, unicodeIdnaOutput, unicodeIdnaInfo); - processIdnaInfo(errorHandler, unicodeIdnaInfo); + processIdnaInfo(errorHandler, unicodeIdnaInfo, false); return unicodeIdnaOutput.toString(); } - private static void processIdnaInfo(final ErrorHandler errorHandler, final IDNA.Info idnaInfo) throws GalimatiasParseException { + private static void processIdnaInfo(final ErrorHandler errorHandler, + final IDNA.Info idnaInfo, final boolean checkHyphens) + throws GalimatiasParseException { for (IDNA.Error error : idnaInfo.getErrors()) { String msg; switch (error) { @@ -175,6 +177,9 @@ private static void processIdnaInfo(final ErrorHandler errorHandler, final IDNA. msg = "A non-final domain name label (or the whole domain name) is empty."; break; case HYPHEN_3_4: + if (!checkHyphens) { + return; + } msg = "A label contains hyphen-minus ('-') in the third and fourth positions."; break; case INVALID_ACE_LABEL: @@ -190,12 +195,18 @@ private static void processIdnaInfo(final ErrorHandler errorHandler, final IDNA. msg = "A label starts with a combining mark."; break; case LEADING_HYPHEN: + if (!checkHyphens) { + return; + } msg = "A label starts with a hyphen-minus ('-')."; break; case PUNYCODE: msg = "A label starts with \"xn--\" but does not contain valid Punycode."; break; case TRAILING_HYPHEN: + if (!checkHyphens) { + return; + } msg = "A label ends with a hyphen-minus ('-')."; break; default: