mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
Return proper RDAP error messages when invalid IP addresses are specified
We were relying on Dagger to validate the IP address, but that resulted in 500 errors when the IP address was not valid, which is undesirable. Instead, accept the parameters as strings, then convert them to IP addresses and throw a proper error when conversion fails. Also fixes an improperly specified test. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=173172516
This commit is contained in:
parent
52fd9d8c4e
commit
4267fa7e48
6 changed files with 39 additions and 38 deletions
|
@ -20,9 +20,7 @@ import static com.google.common.base.Strings.nullToEmpty;
|
|||
|
||||
import com.google.common.base.Ascii;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.net.InetAddresses;
|
||||
import google.registry.request.HttpException.BadRequestException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Optional;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -215,26 +213,6 @@ public final class RequestParameters {
|
|||
return datesBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first request parameter associated with {@code name} parsed as an optional
|
||||
* {@link InetAddress} (which might be IPv6).
|
||||
*
|
||||
* @throws BadRequestException if request parameter named {@code name} is present but could not
|
||||
* be parsed as an {@link InetAddress}
|
||||
*/
|
||||
public static Optional<InetAddress> extractOptionalInetAddressParameter(
|
||||
HttpServletRequest req, String name) {
|
||||
Optional<String> paramVal = extractOptionalParameter(req, name);
|
||||
if (!paramVal.isPresent()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
try {
|
||||
return Optional.of(InetAddresses.forString(paramVal.get()));
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new BadRequestException("Not an IPv4 or IPv6 address: " + name);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean equalsFalse(@Nullable String value) {
|
||||
return nullToEmpty(value).equalsIgnoreCase("false");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue