diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/plugin/PluginDrivenExternalCatalog.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/plugin/PluginDrivenExternalCatalog.java index 95628d1eca6bde..a51bac974fa0a6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/plugin/PluginDrivenExternalCatalog.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/plugin/PluginDrivenExternalCatalog.java @@ -433,8 +433,9 @@ private void checkEndpointsAgainstSsrf(Map candidate, Map= 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('/'); @@ -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)) { diff --git a/fe/fe-core/src/test/java/org/apache/doris/datasource/plugin/PluginDrivenExternalCatalogSsrfGateTest.java b/fe/fe-core/src/test/java/org/apache/doris/datasource/plugin/PluginDrivenExternalCatalogSsrfGateTest.java index 0e7443323886ba..5a3934972f60f9 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/datasource/plugin/PluginDrivenExternalCatalogSsrfGateTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/datasource/plugin/PluginDrivenExternalCatalogSsrfGateTest.java @@ -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; @@ -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) { @@ -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";