HostValidator { HostValidationResult validateHost(final String host) { if (blockedAddresses.isEmpty()) { return HostValidationResult.valid(); } else if (allowedHostnames.contains(host)) { return HostValidationResult.valid(); } else { try { final InetAddress[] inetAddresses = resolver.resolve(host); for (final InetAddress requestAddress : inetAddresses) { if (requestAddress.isLoopbackAddress()) { return HostValidationResult.blocked(host, "the hostname resolved to a loopback address."); } else if (requestAddress.isSiteLocalAddress()) { return HostValidationResult.blocked(host, "the hostname resolved to a site local address."); } else if (requestAddress.isMulticastAddress()) { return HostValidationResult.blocked(host, "the hostname resolved to a multicast address."); } else if (requestAddress.isAnyLocalAddress()) { return HostValidationResult.blocked(host, "the hostname resolved to a wildcard address."); } else if (blockedAddresses.contains(requestAddress)) { return HostValidationResult.blocked(host); } } return HostValidationResult.valid(); } catch (UnknownHostException e) { final String reason = String.format("The configured host '%s' is invalid: %s", host, e.getMessage()); return HostValidationResult.invalid(host, reason); } } } HostValidator(final ConnectivityConfig connectivityConfig, final LoggingAdapter loggingAdapter);   HostValidator(final ConnectivityConfig connectivityConfig, final LoggingAdapter loggingAdapter,
            final AddressResolver resolver);   }
@Test public void testAllowedBlockedHosts() { final HostValidator underTest = getHostValidatorWithAllowlist("0.0.0.0", "8.8.8.8", "[::1]", "192.168.0.1", "224.0.1.1"); assertValid(underTest.validateHost("0.0.0.0")); assertValid(underTest.validateHost("8.8.8.8")); assertValid(underTest.validateHost("[::1]")); assertValid(underTest.validateHost("192.168.0.1")); assertValid(underTest.validateHost("224.0.1.1")); }
@Test public void expectValidationFailsForInvalidHost() { final HostValidator underTest = getHostValidatorWithAllowlist(); final HostValidationResult validationResult = underTest.validateHost("ditto"); assertThat(validationResult).extracting(HostValidationResult::isValid).isEqualTo(false); final ConnectionConfigurationInvalidException exception = validationResult.toException(DITTO_HEADERS); assertThat(exception.getDittoHeaders()).isEqualTo(DITTO_HEADERS); assertThat(exception.getMessage()).contains("The configured host 'ditto' is invalid"); }
@Test public void expectConfiguredAllowedAndBlockedHostIsAllowed() { final String eclipseOrg = "eclipse.org"; final HostValidator underTest = getHostValidatorWithCustomResolver(HostValidatorTest::resolveHost, eclipseOrg); final HostValidationResult validationResult = underTest.validateHost(eclipseOrg); assertThat(validationResult.isValid()).isTrue(); }
@Test public void expectConfiguredBlockedHostIsBlocked() { final HostValidator underTest = getHostValidatorWithCustomResolver(HostValidatorTest::resolveHost); final HostValidationResult validationResult = underTest.validateHost("eclipse.org"); assertThat(validationResult.isValid()).isFalse(); assertThat(validationResult.toException(DITTO_HEADERS).getDittoHeaders()).isEqualTo(DITTO_HEADERS); }
@Test public void expectBlockedHostIsBlocked() { final String theHost = "eclipse.org"; when(connectionConfig.getBlockedHostnames()).thenReturn(List.of("dummy.org")); final HostValidator.AddressResolver resolveToLoopback = host -> resolveHost(theHost, InetAddress.getLoopbackAddress().getAddress()); final HostValidator underTest = getHostValidatorWithCustomResolver(resolveToLoopback); final HostValidationResult validationResult = underTest.validateHost(theHost); assertThat(validationResult.isValid()).isFalse(); }