Skip to content

Commit

Permalink
Allow hyphens anywhere in URL hostnames (#69)
Browse files Browse the repository at this point in the history
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 validator/validator#720 Thanks @KatieMFritz
  • Loading branch information
sideshowbarker authored and smola committed Dec 17, 2018
1 parent 1e647f6 commit 242ae66
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/io/mola/galimatias/URLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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) {
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 242ae66

Please sign in to comment.