hello-stephen commented on code in PR #71205:
URL: https://github.com/apache/doris/pull/71205#discussion_r4751479021


##########
fe/fe-core/src/main/java/org/apache/doris/connector/CatalogEndpointSecurity.java:
##########
@@ -0,0 +1,223 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.connector;
+
+import org.apache.doris.cloud.security.SecurityChecker;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.util.DatasourcePrintableMap;
+import org.apache.doris.common.util.DebugPointUtil;
+import org.apache.doris.common.util.DebugPointUtil.DebugPoint;
+import org.apache.doris.connector.spi.ConnectorSecuritySurface;
+import org.apache.doris.filesystem.properties.StorageEndpointProperties;
+
+import com.google.common.net.HostAndPort;
+import com.google.common.net.InetAddresses;
+
+import java.net.IDN;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+/** Shared endpoint admission for root and indirectly created catalog 
connectors. */
+public final class CatalogEndpointSecurity {
+    private static final String REJECT_HOST_POINT = 
"CatalogEndpointSecurity.reject_host";
+
+    private CatalogEndpointSecurity() {
+    }
+
+    public static boolean isEnabled() {
+        return SecurityChecker.getInstance().isEnabled() || 
DebugPointUtil.isEnable(REJECT_HOST_POINT);
+    }
+
+    public static void validate(String catalogName, ConnectorSecuritySurface 
surface, Map<String, String> properties)
+            throws DdlException {
+        SecurityChecker checker = SecurityChecker.getInstance();
+        DebugPoint rejection = DebugPointUtil.getDebugPoint(REJECT_HOST_POINT);
+        if (!checker.isEnabled() && rejection == null) {
+            return;
+        }
+        Map<String, Integer> endpoints = new LinkedHashMap<>(
+                StorageEndpointProperties.endpoints(properties, 
DatasourcePrintableMap::isSensitiveKey));
+        for (String key : surface.ssrfEndpointKeys(properties)) {
+            int defaultPort = surface.ssrfEndpointDefaultPort(key);
+            endpoints.put(key, defaultPort < 0 ? endpoints.getOrDefault(key, 
-1) : defaultPort);
+        }
+        // Driver downloads can connect during pre-validation, before a 
connector's connectivity test.
+        String flavor = surface.jdbcDriverUrlFlavorKey();
+        String flavorValue = flavor == null ? "jdbc" : 
properties.getOrDefault(flavor, "").trim();
+        if ("jdbc".equalsIgnoreCase(flavorValue)) {
+            for (String key : surface.jdbcDriverUrlKeys()) {
+                String value = properties.getOrDefault(key, 
"").trim().toLowerCase(Locale.ROOT);
+                if (value.startsWith("http://";) || 
value.startsWith("https://";)) {
+                    endpoints.put(key, -1);
+                }
+            }
+        }
+        // Validate every candidate on ALTER: a flavor/authentication change 
can activate a stored address
+        // without modifying its key. Replays never enter this statement-time 
path.
+        Map<String, List<HostAndPort>> parsedEndpoints = new LinkedHashMap<>();
+        for (Map.Entry<String, Integer> entry : endpoints.entrySet()) {
+            String key = entry.getKey();
+            String value = properties.get(key);
+            if (value == null || value.trim().isEmpty()) {
+                continue;
+            }
+            if (value.contains("${")) {
+                throw new DdlException("Endpoint property '" + key + "' must 
be literal under the SSRF policy; "
+                        + "a placeholder can change its destination after 
validation");
+            }
+            List<HostAndPort> destinations;
+            try {
+                destinations = new ArrayList<>();
+                for (String endpoint : surface.ssrfEndpointValues(key, value)) 
{
+                    destinations.addAll(parseEndpoints(endpoint.trim(), 
entry.getValue()));
+                }
+            } catch (IllegalArgumentException e) {
+                // Do not echo URLs: their authority or query can carry 
credentials.
+                throw new DdlException("Cannot validate endpoint property '" + 
key + "' of catalog '"
+                        + catalogName + "' against the SSRF policy: use an 
explicit network URI or host:port");
+            }
+            parsedEndpoints.put(key, destinations);
+        }
+        for (Map.Entry<String, List<HostAndPort>> entry : 
parsedEndpoints.entrySet()) {
+            String key = entry.getKey();
+            for (HostAndPort destination : entry.getValue()) {
+                // A fail-only seam lets SQL regression tests exercise routing 
without the optional SDK.
+                // It cannot bypass a real policy rejection or allow a 
connection.
+                if (rejection != null && 
destination.getHost().equals(rejection.param("host", "127.0.0.1"))) {
+                    throw new DdlException("Endpoint property '" + key + "' is 
rejected by the SSRF policy");
+                }
+                checker.checkEndpointForSsrf(destination.getHost(), 
destination.getPort());
+            }
+        }
+    }
+
+    /** Parses URI lists, multi-host authorities and scheme-less endpoints 
without splitting URL path commas. */
+    public static List<HostAndPort> parseEndpoints(String value, int 
defaultPort) {
+        List<HostAndPort> result = new ArrayList<>();
+        String normalized = value.trim();
+        // JDBC-specific parameters and Oracle descriptors are not ordinary 
URI authorities.
+        if (normalized.startsWith("jdbc:sqlserver://")) {
+            return JdbcEndpointParser.sqlServer(normalized);
+        }
+        if (normalized.startsWith("jdbc:oracle:thin:")) {
+            return JdbcEndpointParser.oracle(normalized);
+        }
+        if (normalized.startsWith("jdbc:")) {
+            normalized = normalized.substring(5);
+        }
+        for (String endpoint : 
normalized.split(",(?=\\s*[A-Za-z][A-Za-z0-9+.-]*://)", -1)) {
+            parseSingle(endpoint.trim(), defaultPort, result);
+        }
+        return result;
+    }
+
+    private static void parseSingle(String endpoint, int defaultPort, 
List<HostAndPort> result) {
+        if (endpoint.isEmpty()) {
+            throw new IllegalArgumentException("Empty endpoint");
+        }
+        if (endpoint.regionMatches(true, 0, "file:", 0, 5)) {
+            try {
+                URI local = new URI(endpoint);
+                String authority = local.getRawAuthority();
+                if (local.isOpaque() || (authority != null && 
!authority.isEmpty()
+                        && !"localhost".equalsIgnoreCase(authority))) {
+                    throw new IllegalArgumentException("Remote or opaque file 
URI");
+                }
+                return;
+            } catch (URISyntaxException e) {
+                throw new IllegalArgumentException("Invalid local file URI");
+            }
+        }
+        if (!endpoint.contains("://")) {
+            String authority = endpoint.split("/", 2)[0];
+            for (String host : authority.split(",", -1)) {
+                result.add(parseAuthority(host.trim(), defaultPort));
+            }
+            return;
+        }
+        URI uri;
+        try {
+            uri = new URI(endpoint);
+        } catch (URISyntaxException e) {
+            throw new IllegalArgumentException("Invalid endpoint URI");
+        }
+        String scheme = uri.getScheme().toLowerCase(Locale.ROOT);
+        String authority = uri.getRawAuthority();

Review Comment:
   [P2] Resolve PostgreSQL query destination overrides before probing
   
   `jdbc:postgresql://allowed.example.com:5432/db?PGHOST=127.0.0.1&PGPORT=1544` 
is checked only against `allowed.example.com:5432` here. In [pgJDBC 
42.7.13](https://github.com/pgjdbc/pgjdbc/blob/REL42.7.13/pgjdbc/src/main/java/org/postgresql/Driver.java#L558-L588),
 the version pinned in `fe/pom.xml`, query properties overwrite the 
authority-derived host/port, so its effective destination is `127.0.0.1:1544`. 
Doris's URL normalizer preserves those overrides. With `test_connection=false`, 
admission can therefore accept a destination the configured policy would 
reject; the later JDBC sanitizer remains separate. Resolve these overrides with 
driver precedence, or reject unsupported destination-changing parameters, and 
add enabled-policy tests for host/port overrides and multiple hosts.



##########
fe/fe-filesystem/fe-filesystem-api/src/main/java/org/apache/doris/filesystem/properties/StorageEndpointProperties.java:
##########
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.filesystem.properties;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.function.Predicate;
+
+/** Network endpoint aliases consumed by filesystem providers and their 
authentication clients. */
+public final class StorageEndpointProperties {
+    private StorageEndpointProperties() {
+    }
+
+    /** Shared aliases can reach different SDKs; require an explicit scheme or 
port instead of guessing it. */
+    public static Map<String, Integer> endpoints(Map<String, String> 
properties, Predicate<String> opaqueCredential) {
+        Map<String, Integer> endpoints = new LinkedHashMap<>();
+        for (String key : new String[] {"s3.endpoint", "AWS_ENDPOINT", 
"endpoint", "ENDPOINT", "aws.endpoint",
+                "glue.endpoint", "aws.glue.endpoint", "oss.endpoint", 
"OSS_ENDPOINT", "dlf.endpoint",
+                "dlf.catalog.endpoint", "fs.oss.endpoint", "cos.endpoint", 
"COS_ENDPOINT", "obs.endpoint",
+                "OBS_ENDPOINT", "azure.endpoint", "AZURE_ENDPOINT", 
"azure.oauth2_server_uri",
+                "azure.oauth2_account_host", "gs.endpoint", "minio.endpoint", 
"ozone.endpoint", "fs.s3a.endpoint"}) {
+            if (properties.containsKey(key)) {
+                endpoints.put(key, -1);
+            }
+        }
+        // ABFS OAuth always uses HTTPS; this value is a hostname also used in 
Hadoop property keys.
+        if (properties.containsKey("azure.oauth2_account_host")) {
+            endpoints.put("azure.oauth2_account_host", 443);
+        }
+        // Hadoop passes through new client options. Keep URI-valued token and 
service endpoints covered
+        // even when a dependency adds an option that Doris does not bind 
itself yet.
+        properties.forEach((key, value) -> {
+            String normalized = value == null ? "" : 
value.trim().toLowerCase(Locale.ROOT);
+            String lowerKey = key.toLowerCase(Locale.ROOT);
+            boolean endpointName = lowerKey.endsWith(".url") || 
lowerKey.endsWith(".uri")
+                    || lowerKey.endsWith(".endpoint");
+            if (((key.startsWith("fs.") || key.startsWith("dfs.") || 
key.startsWith("hadoop."))
+                    // URL-shaped passwords stay opaque; explicit aliases and 
endpoint names still win.
+                    && (endpointName || (!opaqueCredential.test(key)
+                    && (normalized.startsWith("http://";) || 
normalized.startsWith("https://";)))))
+                    || ((key.equals("warehouse") || key.endsWith(".warehouse"))
+                    && (normalized.startsWith("http://";) || 
normalized.startsWith("https://";)))) {

Review Comment:
   [P2] Check HDFS warehouse and default-filesystem endpoints
   
   With the checker enabled, a Paimon HMS catalog with an allowed metastore can 
still pass CREATE/ALTER admission with 
`warehouse=hdfs://127.0.0.1:8020/warehouse`, or with `warehouse=/warehouse` and 
`fs.defaultFS=hdfs://127.0.0.1:8020`: neither value selects an endpoint here. 
`PaimonConnector.createHmsCatalog` resolves those values and calls 
`FileIO.checkOrMkdirs` from FE, so these are real literal destinations omitted 
from the statement-time policy. Later runtime hooks remain independent. Include 
HDFS warehouse/default-filesystem authorities in admission, preserve logical HA 
nameservice handling, and add rejecting-policy CREATE/ALTER cases.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to