Make host flows only accept canonicalized host names as input

This now throws errors when a non-lower-cased, non-puny-coded, or non-canonicalized host name is passed in as an input parameter.

The approach we'll take is to first notify registrars which hosts we'll be renaming, then
issue EPP host update commands to effect those renames as superuser, then push this code
live to production.

This fixes #38 on GitHub.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=138441130
This commit is contained in:
mcilwain 2016-11-07 14:34:30 -08:00 committed by Ben McIlwain
parent cbe76e8615
commit 9aa2f3b96e
12 changed files with 215 additions and 8 deletions

View file

@ -17,7 +17,9 @@ package google.registry.flows.host;
import static google.registry.model.EppResourceUtils.isActive;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.registry.Registries.findTldForName;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.base.Ascii;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.Iterables;
@ -29,6 +31,7 @@ import google.registry.flows.EppException.ParameterValuePolicyErrorException;
import google.registry.flows.EppException.ParameterValueRangeErrorException;
import google.registry.flows.EppException.ParameterValueSyntaxErrorException;
import google.registry.model.domain.DomainResource;
import google.registry.util.Idn;
import org.joda.time.DateTime;
/** Static utility functions for host flows. */
@ -36,14 +39,23 @@ public class HostFlowUtils {
/** Checks that a host name is valid. */
static InternetDomainName validateHostName(String name) throws EppException {
if (name == null) {
return null;
}
checkArgumentNotNull(name, "Must specify host name to validate");
if (name.length() > 253) {
throw new HostNameTooLongException();
}
String hostNameLowerCase = Ascii.toLowerCase(name);
if (!name.equals(hostNameLowerCase)) {
throw new HostNameNotLowerCaseException(hostNameLowerCase);
}
String hostNamePunyCoded = Idn.toASCII(name);
if (!name.equals(hostNamePunyCoded)) {
throw new HostNameNotPunyCodedException(hostNamePunyCoded);
}
try {
InternetDomainName hostName = InternetDomainName.from(name);
if (!name.equals(hostName.toString())) {
throw new HostNameNotNormalizedException(hostName.toString());
}
// Checks whether a hostname is deep enough. Technically a host can be just one under a
// public suffix (e.g. example.com) but we require by policy that it has to be at least one
// part beyond that (e.g. ns1.example.com). The public suffix list includes all current
@ -135,4 +147,26 @@ public class HostFlowUtils {
super("Invalid host name");
}
}
/** Host names must be in lower-case. */
static class HostNameNotLowerCaseException extends ParameterValueSyntaxErrorException {
public HostNameNotLowerCaseException(String expectedHostName) {
super(String.format("Host names must be in lower-case; expected %s", expectedHostName));
}
}
/** Host names must be puny-coded. */
static class HostNameNotPunyCodedException extends ParameterValueSyntaxErrorException {
public HostNameNotPunyCodedException(String expectedHostName) {
super(String.format("Host names must be puny-coded; expected %s", expectedHostName));
}
}
/** Host names must be in normalized format. */
static class HostNameNotNormalizedException extends ParameterValueSyntaxErrorException {
public HostNameNotNormalizedException(String expectedHostName) {
super(
String.format("Host names must be in normalized format; expected %s", expectedHostName));
}
}
}