hello-stephen commented on code in PR #71414: URL: https://github.com/apache/doris/pull/71414#discussion_r4796517804
########## fe/fe-core/src/main/java/org/apache/doris/connector/CatalogEndpointSecurity.java: ########## @@ -0,0 +1,137 @@ +// 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.Locale; +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)); + // Projections already name individual destinations; list syntax belongs to the raw property only. + boolean splitRawValues = surface.isCommaSeparatedSsrfEndpoint(key) + && !surface.hasSsrfEndpointValueOverride(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 = splitRawValues + ? 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)); + // HostAndPort only splits the port. Validate locally before a failed DNS lookup can be ignored. + URI authority = new URI("endpoint://" + target).parseServerAuthority(); + if (authority.getRawUserInfo() != null || authority.getRawQuery() != null + || authority.getRawFragment() != null) { + throw new IllegalArgumentException("Bare destinations cannot embed URI delimiters"); + } + host = authority.getHost(); + port = authority.getPort(); + } + if (host == null || host.isEmpty() || port < 1 || port > 65535) { + throw new IllegalArgumentException("Destination requires a host and a valid port"); + } + // DNS and address hex case must not repeat probes; IPv6 scope identifiers may be case-sensitive. + int scope = host.indexOf('%'); + host = scope < 0 ? host.toLowerCase(Locale.ROOT) Review Comment: [P3] Deduplicate equivalent IPv6 literal spellings Two Thrift URIs using `[2001:db8::1]:9083` and `[2001:0db8:0:0:0:0:0:1]:9083` name the same socket destination. `URI.getHost()` preserves their different spellings, so lowercasing leaves two `HostAndPort` entries and can repeat the five-second connect wait. Canonicalize IPv6 address text without a DNS lookup and preserve the scope identifier. ########## fe/fe-core/src/main/java/org/apache/doris/connector/CatalogEndpointSecurity.java: ########## @@ -0,0 +1,137 @@ +// 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.Locale; +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)); + // Projections already name individual destinations; list syntax belongs to the raw property only. + boolean splitRawValues = surface.isCommaSeparatedSsrfEndpoint(key) + && !surface.hasSsrfEndpointValueOverride(key); + for (String value : values) { + if (value.trim().isEmpty()) { Review Comment: [P3] Reject blank members of endpoint projections A projection containing one valid endpoint and one whitespace-only entry succeeds after checking only the valid entry. The SPI defines each projected member as a destination and uses an empty projection to mark the property dormant. Keep allowing blank optional raw properties, and reject blank projected members before starting any probes. -- 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]
