hello-stephen commented on code in PR #71414: URL: https://github.com/apache/doris/pull/71414#discussion_r4795482832
########## fe/fe-core/src/main/java/org/apache/doris/connector/CatalogEndpointSecurity.java: ########## @@ -0,0 +1,123 @@ +// 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.connector.spi.ConnectorSecuritySurface; + +import com.google.common.net.HostAndPort; +import okhttp3.HttpUrl; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** Standalone endpoint admission; callers supply the provider's effective, detached security surface. */ +public final class CatalogEndpointSecurity { + private CatalogEndpointSecurity() { + } + + /** + * Checks every active destination using the installed policy. This utility does not initialize a + * connector, mutate a catalog, interpret storage properties, or guard subsequent SDK requests. + */ + public static void validate(String catalogName, ConnectorSecuritySurface surface, Map<String, String> properties) + throws DdlException { + validate(catalogName, surface, properties, SecurityChecker.getInstance()); + } + + static void validate(String catalogName, ConnectorSecuritySurface surface, Map<String, String> properties, + SecurityChecker checker) throws DdlException { + if (!checker.isEnabled()) { + return; + } + Set<HostAndPort> destinations = new LinkedHashSet<>(); + for (String key : surface.ssrfEndpointKeys()) { + List<String> values = surface.ssrfEndpointValues(key, properties.get(key)); + for (String value : values) { + if (value.trim().isEmpty()) { + continue; + } + if (value.contains("${")) { + throw new DdlException("Endpoint property '" + key + "' must be literal under the SSRF policy"); + } + String[] endpoints = surface.isCommaSeparatedSsrfEndpoint(key) + ? value.split(",", -1) : new String[] {value}; + for (String endpoint : endpoints) { + try { + destinations.add(parseEndpoint(endpoint.trim(), + surface.ssrfEndpointDefaultPorts().getOrDefault(key, -1))); + } catch (IllegalArgumentException e) { + // Authorities and queries may contain credentials; do not retain the parser's cause. + throw new DdlException("Cannot validate endpoint property '" + key + "' of catalog '" + + catalogName + "' against the SSRF policy: use an explicit URI or host:port"); + } + } + } + } + // Resolve the complete declaration before probing, so malformed later values cannot be skipped. + for (HostAndPort destination : destinations) { + checker.checkEndpointForSsrf(destination.getHost(), destination.getPort()); + } + } + + private static HostAndPort parseEndpoint(String value, int defaultPort) { + try { + String host; + int port; + if (value.regionMatches(true, 0, "http://", 0, 7) + || value.regionMatches(true, 0, "https://", 0, 8)) { + HttpUrl target = HttpUrl.parse(value); + if (target == null) { + throw new IllegalArgumentException("Invalid HTTP destination"); + } + host = target.host(); + port = target.port(); + } else if (value.matches("[A-Za-z][A-Za-z0-9+.-]*://.*")) { + URI target = new URI(value); + host = target.getHost(); + port = target.getPort(); + } else if (defaultPort >= 0) { + URL raw = new URL("https://" + value); + HttpUrl target = HttpUrl.get(raw); + if (target == null) { + throw new IllegalArgumentException("Invalid destination authority"); + } + host = target.host(); + port = raw.getPort() < 0 ? defaultPort : raw.getPort(); + } else { + int slash = value.indexOf('/'); + HostAndPort target = HostAndPort.fromString(slash < 0 ? value : value.substring(0, slash)); Review Comment: [P2] Validate bare host syntax before probing With the checker enabled, a key without a default port accepts a malformed authority such as `bad host:9083` through the `HostAndPort` split. The subsequent check only requires a nonempty host and an in-range port. `SecurityChecker` treats an unresolved host as an ordinary connectivity failure, so this validator can return successfully for a malformed active endpoint. Validate the host syntax locally before adding the destination and add a negative test that asserts no probe starts. ########## fe/fe-core/src/main/java/org/apache/doris/connector/CatalogEndpointSecurity.java: ########## @@ -0,0 +1,123 @@ +// 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.connector.spi.ConnectorSecuritySurface; + +import com.google.common.net.HostAndPort; +import okhttp3.HttpUrl; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** Standalone endpoint admission; callers supply the provider's effective, detached security surface. */ +public final class CatalogEndpointSecurity { + private CatalogEndpointSecurity() { + } + + /** + * Checks every active destination using the installed policy. This utility does not initialize a + * connector, mutate a catalog, interpret storage properties, or guard subsequent SDK requests. + */ + public static void validate(String catalogName, ConnectorSecuritySurface surface, Map<String, String> properties) + throws DdlException { + validate(catalogName, surface, properties, SecurityChecker.getInstance()); + } + + static void validate(String catalogName, ConnectorSecuritySurface surface, Map<String, String> properties, + SecurityChecker checker) throws DdlException { + if (!checker.isEnabled()) { + return; + } + Set<HostAndPort> destinations = new LinkedHashSet<>(); + for (String key : surface.ssrfEndpointKeys()) { + List<String> values = surface.ssrfEndpointValues(key, properties.get(key)); + for (String value : values) { + if (value.trim().isEmpty()) { + continue; + } + if (value.contains("${")) { + throw new DdlException("Endpoint property '" + key + "' must be literal under the SSRF policy"); + } + String[] endpoints = surface.isCommaSeparatedSsrfEndpoint(key) Review Comment: [P3] Preserve commas inside projected destinations `withSsrfEndpointValues` supplies individual effective destinations. If the same key has a comma-separated property declaration, this loop splits those destinations again. A projected value such as `https://service.example/a,b` becomes two entries, and the `b` fragment causes validation to reject the whole candidate. Apply list splitting to the raw property value and cover the combination of a list declaration and a projected URL in a test. -- 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]
