Skip to content
Merged
Show file tree
Hide file tree
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 @@ -433,8 +433,9 @@ private void checkEndpointsAgainstSsrf(Map<String, String> candidate, Map<String

/**
* Parse one endpoint — {@code scheme://host:port} (e.g. {@code thrift://host:9083}) or a bare
* {@code host:port} — into host and port for the SSRF probe. Returns null when no concrete host:port
* can be extracted (nothing to probe); {@link HostAndPort} handles IPv6 literals in brackets natively.
* {@code host:port} — into host and port for the SSRF probe. An http or https URL that names no port is
* probed at its scheme's default port. Returns null when no concrete host:port can be extracted (nothing to
* probe); {@link HostAndPort} handles IPv6 literals in brackets natively.
*/
static HostAndPort parseEndpoint(String value) {
if (value == null || value.isEmpty()) {
Expand All @@ -445,10 +446,11 @@ static HostAndPort parseEndpoint(String value) {
// Match the HMS client's URI semantics: query, fragment and userinfo do not name
// the socket destination. HostAndPort.fromParts also strips IPv6 brackets.
URI uri = new URI(value);
if (uri.getHost() == null || uri.getPort() < 0) {
int port = uri.getPort() >= 0 ? uri.getPort() : defaultWebPort(uri.getScheme());
if (uri.getHost() == null || port < 0) {
return null;
}
return HostAndPort.fromParts(uri.getHost(), uri.getPort());
return HostAndPort.fromParts(uri.getHost(), port);
}
String authority = value;
int slashIdx = authority.indexOf('/');
Expand All @@ -465,6 +467,21 @@ static HostAndPort parseEndpoint(String value) {
}
}

/**
* The port an http or https client connects to when the URI names none, 80 or 443 (RFC 9110 section 4.2), so a
* REST endpoint such as {@code https://host/ns} is probed where it connects; -1 for any other scheme, whose
* default, if it has one, is its client's.
*/
private static int defaultWebPort(String scheme) {
if ("http".equalsIgnoreCase(scheme)) {
return 80;
}
if ("https".equalsIgnoreCase(scheme)) {
return 443;
}
return -1;
}

private void checkHiveParquetTimeZone(CatalogProperty property) throws DdlException {
String catalogType = getType();
if ("hms".equalsIgnoreCase(catalogType) || "hudi".equalsIgnoreCase(catalogType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;

Expand Down Expand Up @@ -124,6 +125,21 @@ public void parseEndpointUsesUriDestination(String endpoint) {
Assertions.assertEquals(9083, parsed.getPort());
}

/**
* An http or https client connects to its scheme's default port when the URI names none, as a REST catalog
* URI such as https://host/ns usually does, so that is the destination to probe.
*/
@ParameterizedTest
@CsvSource({"http://host1/ns, host1, 80", "http://host1, host1, 80", "HTTP://host1/, host1, 80",
"https://host1/v1, host1, 443", "https://user@host1/v1?ignored#ignored, host1, 443",
"http://[::1]/ns, ::1, 80", "https://host1:8443/v1, host1, 8443"})
public void parseEndpointUsesTheDefaultPortOfAWebScheme(String endpoint, String host, int port) {
HostAndPort parsed = PluginDrivenExternalCatalog.parseEndpoint(endpoint);
Assertions.assertNotNull(parsed, endpoint);
Assertions.assertEquals(host, parsed.getHost());
Assertions.assertEquals(port, parsed.getPort());
}

@ParameterizedTest
@ValueSource(strings = {"thrift://[::1]:9083", "thrift://user@[::1]:9083?ignored#ignored", "[::1]:9083"})
public void parseEndpointPreservesIpv6Destination(String endpoint) {
Expand Down Expand Up @@ -170,6 +186,19 @@ public void alterRejectsUriDestinationWithSuffixes(String endpoint) {
Assertions.assertEquals(Collections.singletonList("10.0.0.1:9083"), checker.probed);
}

/** A web endpoint that names no port must not slip past the gate: its default port is what is probed. */
@ParameterizedTest
@CsvSource({"http://10.0.0.1/ns, 10.0.0.1:80", "https://10.0.0.1, 10.0.0.1:443"})
public void alterRejectsAnIntranetWebEndpointWithoutAPort(String endpoint, String probed) {
checker.intranetHost = "10.0.0.1";
TestCatalog catalog = new TestCatalog(hms("thrift://hms.public.example.com:9083"));

Assertions.assertThrows(DdlException.class,
() -> catalog.validatePropertiesBeforeUpdate(catalog.getProperties(),
Collections.singletonMap("uri", endpoint)));
Assertions.assertTrue(checker.probed.contains(probed), checker.probed.toString());
}

@Test
public void alterChecksEveryEndpointInAnHaUriList() {
checker.intranetHost = "192.168.0.7";
Expand Down
Loading