Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.17] Make hostname resolution for loopback address more robust. (#89788) #89898

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,22 @@ public void testGetOauth2TokenInExchangeForKerberosTickets() throws PrivilegedAc
@SuppressForbidden(reason = "SPNEGO relies on hostnames and we need to ensure host isn't a IP address")
protected HttpHost buildHttpHost(String host, int port) {
try {
InetAddress inetAddress = InetAddress.getByName(host);
return super.buildHttpHost(inetAddress.getCanonicalHostName(), port);
final InetAddress address = InetAddress.getByName(host);
final String hostname = address.getCanonicalHostName();
// InetAddress#getCanonicalHostName depends on the system configuration (e.g. /etc/hosts) to return the FQDN.
// In case InetAddress cannot resolve the FQDN it will return the textual representation of the IP address.
if (hostname.equals(address.getHostAddress())) {
if (address.isLoopbackAddress()) {
// Fall-back and return "localhost" for loopback address if it's not resolved.
// This is safe because InetAddress implements a reverse fall-back to loopback address
// in case the resolution of "localhost" hostname fails.
return super.buildHttpHost("localhost", port);
} else {
throw new IllegalStateException("failed to resolve [" + host + "] to FQDN");
}
} else {
return super.buildHttpHost(hostname, port);
}
} catch (UnknownHostException e) {
assumeNoException("failed to resolve host [" + host + "]", e);
}
Expand Down