Don't validate RDAP nameserver names using validateDomainName

The nameserver may be external, in which case its TLD will not appear in our
list of valid TLDs, and the search will be rejected erroneously.

Tests for letter case canonicalizations also added at reviewer's suggestion.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171985702
This commit is contained in:
mountford 2017-10-12 11:21:57 -07:00 committed by jianglai
parent 9d1eb0d429
commit 326cf698e0
15 changed files with 113 additions and 91 deletions

View file

@ -169,7 +169,7 @@ public class DomainFlowUtils {
*
* @see #validateDomainNameWithIdnTables(InternetDomainName)
*/
static InternetDomainName validateDomainName(String name)
public static InternetDomainName validateDomainName(String name)
throws EppException {
if (!ALLOWED_CHARS.matchesAllOf(name)) {
throw new BadDomainNameCharacterException();

View file

@ -39,7 +39,7 @@ import org.joda.time.DateTime;
public class HostFlowUtils {
/** Checks that a host name is valid. */
static InternetDomainName validateHostName(String name) throws EppException {
public static InternetDomainName validateHostName(String name) throws EppException {
checkArgumentNotNull(name, "Must specify host name to validate");
if (name.length() > 253) {
throw new HostNameTooLongException();

View file

@ -9,6 +9,7 @@ java_library(
srcs = glob(["*.java"]),
deps = [
"//java/google/registry/config",
"//java/google/registry/flows",
"//java/google/registry/model",
"//java/google/registry/request",
"//java/google/registry/request/auth",

View file

@ -19,8 +19,6 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registry.Registries.findTldForName;
import static google.registry.model.registry.Registries.getTlds;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
@ -28,7 +26,6 @@ import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.InternetDomainName;
import com.google.common.net.MediaType;
import com.google.re2j.Pattern;
import com.googlecode.objectify.Key;
@ -39,8 +36,6 @@ import google.registry.model.registrar.Registrar;
import google.registry.rdap.RdapSearchResults.IncompletenessWarningType;
import google.registry.request.Action;
import google.registry.request.HttpException;
import google.registry.request.HttpException.BadRequestException;
import google.registry.request.HttpException.NotFoundException;
import google.registry.request.HttpException.UnprocessableEntityException;
import google.registry.request.Parameter;
import google.registry.request.RequestMethod;
@ -247,18 +242,6 @@ public abstract class RdapActionBase implements Runnable {
&& (!registrarParam.isPresent() || registrarParam.get().equals(registrar.getClientId()));
}
void validateDomainName(String name) {
try {
Optional<InternetDomainName> tld = findTldForName(InternetDomainName.from(name));
if (!tld.isPresent() || !getTlds().contains(tld.get().toString())) {
throw new NotFoundException(name + " not found");
}
} catch (IllegalArgumentException e) {
throw new BadRequestException(
name + " is not a valid " + getHumanReadableObjectTypeName());
}
}
String canonicalizeName(String name) {
name = canonicalizeDomainName(name);
if (name.endsWith(".")) {

View file

@ -14,14 +14,17 @@
package google.registry.rdap;
import static google.registry.flows.domain.DomainFlowUtils.validateDomainName;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD;
import com.google.common.collect.ImmutableMap;
import google.registry.flows.EppException;
import google.registry.model.domain.DomainResource;
import google.registry.rdap.RdapJsonFormatter.OutputDataType;
import google.registry.request.Action;
import google.registry.request.HttpException.BadRequestException;
import google.registry.request.HttpException.NotFoundException;
import google.registry.request.auth.Auth;
import google.registry.util.Clock;
@ -57,7 +60,14 @@ public class RdapDomainAction extends RdapActionBase {
String pathSearchString, boolean isHeadRequest, String linkBase) {
DateTime now = clock.nowUtc();
pathSearchString = canonicalizeName(pathSearchString);
validateDomainName(pathSearchString);
try {
validateDomainName(pathSearchString);
} catch (EppException e) {
throw new BadRequestException(
String.format(
"%s is not a valid %s: %s",
pathSearchString, getHumanReadableObjectTypeName(), e.getMessage()));
}
// The query string is not used; the RDAP syntax is /rdap/domain/mydomain.com.
DomainResource domainResource = loadByForeignKey(DomainResource.class, pathSearchString, now);
if (domainResource == null) {

View file

@ -14,15 +14,18 @@
package google.registry.rdap;
import static google.registry.flows.host.HostFlowUtils.validateHostName;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.collect.ImmutableMap;
import google.registry.flows.EppException;
import google.registry.model.host.HostResource;
import google.registry.rdap.RdapJsonFormatter.OutputDataType;
import google.registry.request.Action;
import google.registry.request.HttpException.BadRequestException;
import google.registry.request.HttpException.NotFoundException;
import google.registry.request.auth.Auth;
import google.registry.util.Clock;
@ -59,7 +62,14 @@ public class RdapNameserverAction extends RdapActionBase {
DateTime now = clock.nowUtc();
pathSearchString = canonicalizeName(pathSearchString);
// The RDAP syntax is /rdap/nameserver/ns1.mydomain.com.
validateDomainName(pathSearchString);
try {
validateHostName(pathSearchString);
} catch (EppException e) {
throw new BadRequestException(
String.format(
"%s is not a valid %s: %s",
pathSearchString, getHumanReadableObjectTypeName(), e.getMessage()));
}
// If there are no undeleted nameservers with the given name, the foreign key should point to
// the most recently deleted one.
HostResource hostResource =