dsmiley commented on a change in pull request #47: URL: https://github.com/apache/solr/pull/47#discussion_r617717842
########## File path: solr/solr-ref-guide/src/major-changes-in-solr-8.adoc ########## @@ -283,9 +283,9 @@ The login screen's purpose is cosmetic only - Admin UI-triggered Solr requests w *Distributed Requests* -* The `shards` parameter, used to manually select the shards and replicas that receive distributed requests, now checks nodes against a whitelist of acceptable values for security reasons. +* The `shards` parameter, used to manually select the shards and replicas that receive distributed requests, now checks nodes against an allow-list of acceptable values for security reasons. + -In SolrCloud mode this whitelist is automatically configured to contain all live nodes. In standalone mode the whitelist is empty by default. Upgrading users who use the `shards` parameter in standalone mode can correct this value by setting the `shardsWhitelist` property in any `shardHandler` configurations in their `solrconfig.xml` file. +In SolrCloud mode this allow-list is automatically configured to contain all live nodes. In standalone mode the allow-list is empty by default. Upgrading users who use the `shards` parameter in standalone mode can correct this value by setting the `allowUrls` property in their `solrconfig.xml` file. Review comment: You are going back in time and changing upgrade notes for Solr 8 where there was a `shardsWhitelist` and saying it is `allowUrls` when this isn't the case for 8.x. I think the terminology change is okay but don't change the particular setting name or that it goes on shardHandler which is true for 8.x. ########## File path: solr/core/src/java/org/apache/solr/security/AllowListUrlChecker.java ########## @@ -0,0 +1,215 @@ +/* + * 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.solr.security; + +import com.google.common.annotations.VisibleForTesting; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.cloud.ClusterState; +import org.apache.solr.core.NodeConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Validates URLs based on an allow list or a {@link ClusterState} in SolrCloud. + */ +public class AllowListUrlChecker { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + /** + * {@link org.apache.solr.core.SolrXmlConfig} property to configure the allowed URLs. + */ + public static final String URL_ALLOW_LIST = "allowUrls"; + + /** + * System property to disable URL checking and {@link #ALLOW_ALL} instead. + */ + public static final String DISABLE_URL_ALLOW_LIST = "solr.disable." + URL_ALLOW_LIST; + + /** + * Clue given in URL-forbidden exceptions messages. + */ + public static final String SET_SOLR_DISABLE_URL_ALLOW_LIST_CLUE = "Set -D" + DISABLE_URL_ALLOW_LIST + "=true to disable URL allow-list checks."; + + /** + * Singleton checker which allows all URLs. {@link #isEnabled()} returns false. + */ + public static final AllowListUrlChecker ALLOW_ALL; + + static { + try { + ALLOW_ALL = new AllowListUrlChecker(Collections.emptyList()) { + @Override + public void checkAllowList(List<String> urls, ClusterState clusterState) { + // Allow. + } + + @Override + public boolean isEnabled() { + return false; + } + + @Override + public String toString() { + return getClass().getSimpleName() + " [allow all]"; + } + }; + } catch (MalformedURLException e) { + // Never thrown. + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); + } + } + + /** + * Regex pattern to match any protocol, e.g. http:// https:// s3://. + * After a match, regex group 1 contains the protocol and group 2 the rest. + */ + private static final Pattern PROTOCOL_PATTERN = Pattern.compile("(\\w+)(://.*)"); + + /** + * Allow list of hosts. Elements in the list will be host:port (no protocol or context). + */ + private final Set<String> hostAllowList; + + /** + * @param urlAllowList List of allowed URLs. URLs must be well-formed, missing protocol is tolerated. + * An empty list means there is no explicit allow-list of URLs, in this case no + * URL is allowed unless a {@link ClusterState} is provided in + * {@link #checkAllowList(List, ClusterState)}. + * @throws MalformedURLException If an URL is invalid. + */ + public AllowListUrlChecker(List<String> urlAllowList) throws MalformedURLException { + hostAllowList = parseHostPorts(urlAllowList); + } + + /** + * Creates a URL checker based on the {@link NodeConfig} property to configure the allowed URLs. + */ + public static AllowListUrlChecker create(NodeConfig config) { + if (Boolean.getBoolean(DISABLE_URL_ALLOW_LIST)) { + return AllowListUrlChecker.ALLOW_ALL; + } else if (System.getProperty("solr.disable.shardsWhitelist") != null) { + log.warn("Property 'solr.disable.shardsWhitelist' is deprecated, please use '{}' instead.", DISABLE_URL_ALLOW_LIST); + } + try { + return new AllowListUrlChecker(config.getAllowUrls()); + } catch (MalformedURLException e) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, + "Invalid URL syntax in '" + URL_ALLOW_LIST + "' configuration: " + config.getAllowUrls(), e); + } + } + + /** + * @see #checkAllowList(List, ClusterState) + */ + public void checkAllowList(List<String> urls) throws MalformedURLException { + checkAllowList(urls, null); + } + + /** + * Checks that the given URLs are present in the configured allow-list or in the provided {@link ClusterState} + * (in case of cloud mode). + * + * @param urls The list of urls to check. + * @param clusterState The up to date {@link ClusterState}, can be null in case of non-cloud mode. + * @throws MalformedURLException If an URL is invalid. + * @throws SolrException If an URL is not present in the allow-list or in the provided {@link ClusterState}. + */ + public void checkAllowList(List<String> urls, @Nullable ClusterState clusterState) throws MalformedURLException { + Set<String> clusterHostAllowList = clusterState == null ? Collections.emptySet() : clusterState.getHostAllowList(); + for (String url : urls) { + String hostPort = parseHostPort(url); + if (!clusterHostAllowList.contains(hostPort) && !hostAllowList.contains(hostPort)) { + throw new SolrException(SolrException.ErrorCode.FORBIDDEN, "URL " + url + + " is neither a live node of the cluster nor in the configured '" + + URL_ALLOW_LIST + "' " + hostAllowList); + } + } + } + + /** + * Whether this checker has been created with a non-empty allow-list of URLs. + */ + public boolean hasExplicitAllowList() { + return !hostAllowList.isEmpty(); Review comment: Woah; 4-space indent. Please fix this and change your IDE settings. Use "Google Java Format" IntelliJ plugin, which may help and is practically required on the Lucene side any way. ########## File path: solr/core/src/test/org/apache/solr/cloud/UnloadDistributedZkTest.java ########## @@ -65,7 +67,11 @@ protected String getSolrXml() { @Test public void test() throws Exception { - jettys.forEach(j -> j.getCoreContainer().getAllowPaths().add(Path.of("_ALL_"))); // Allow non-standard core instance path + jettys.forEach(j -> { + Set<Path> allowPath = j.getCoreContainer().getAllowPaths(); + allowPath.clear(); + allowPath.addAll(SolrPaths.ALL_PATHS); // Allow non-standard core instance path Review comment: Do you think this Set should be mutable? Hmm. Not a blocker to me but worth contemplating. -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org