Apache Solr open-source search software
SOLR-8642: SOLR allows creation of collections with invalid names
Erick Ericksonf77feb718a4c
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 93a844792ad..8ad69f733ed 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -353,6 +353,8 @@ New Featuressystem property. NOTE: this is an expert option and can result in more often needing to do full index replicationfor recovery, the sweet spot for using this is very high volume, leader-only indexing. (Tim Potter, Erick Erickson)+* SOLR-8642: SOLR allows creation of collections with invalid names+ (Jason Gerlowski via Erick Erickson)Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.javaindex bca7e6c3f24..b007dbf90f1 100644--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java@@ -31,7 +31,6 @@ import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Future;-import java.util.regex.Pattern;import com.google.common.collect.ImmutableMap;import com.google.common.collect.Maps;@@ -64,6 +63,7 @@ import org.apache.solr.security.PKIAuthenticationPlugin;import org.apache.solr.security.SecurityPluginHolder;import org.apache.solr.update.UpdateShardHandler;import org.apache.solr.util.DefaultSolrThreadFactory;+import org.apache.solr.util.SolrIdentifierValidator;import org.apache.zookeeper.KeeperException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -656,19 +656,6 @@ public class CoreContainer {return coresLocator;}- // Insure that the core name won't cause problems later on.- final static Pattern corePattern = Pattern.compile("^[\\._A-Za-z0-9]*$");--- public void validateCoreName(String name) {- if (name == null || !corePattern.matcher(name).matches()) {- throw new IllegalArgumentException("Invalid core name: '" + name +- "' Names must consist entirely of periods, underscores and alphanumerics");- }- }---protected SolrCore registerCore(String name, SolrCore core, boolean registerInZk) {if( core == null ) {throw new RuntimeException( "Can not register a null core." );@@ -817,7 +804,7 @@ public class CoreContainer {SolrCore core = null;try {MDCLoggingContext.setCore(core);- validateCoreName(dcore.getName());+ SolrIdentifierValidator.validateCoreName(dcore.getName());if (zkSys.getZkController() != null) {zkSys.getZkController().preRegister(dcore);}@@ -1020,7 +1007,7 @@ public class CoreContainer {}public void rename(String name, String toName) {- validateCoreName(toName);+ SolrIdentifierValidator.validateCoreName(toName);try (SolrCore core = getCore(name)) {if (core != null) {registerCore(toName, core, true);diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.javaindex ac14a2224bf..c04386dec29 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java@@ -131,6 +131,7 @@ import org.apache.solr.handler.RequestHandlerBase;import org.apache.solr.handler.component.ShardHandler;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.SolrQueryResponse;+import org.apache.solr.util.SolrIdentifierValidator;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.slf4j.Logger;@@ -356,7 +357,9 @@ public class CollectionsHandler extends RequestHandlerBase {addMapObject(props, RULE);addMapObject(props, SNITCH);verifyRuleParams(h.coreContainer, props);- if (SYSTEM_COLL.equals(props.get(NAME))) {+ final String collectionName = (String) props.get(NAME);+ SolrIdentifierValidator.validateCollectionName(collectionName);+ if (SYSTEM_COLL.equals(collectionName)) {//We must always create a .system collection with only a single shardprops.put(NUM_SLICES, 1);props.remove(SHARDS_PROP);@@ -426,6 +429,8 @@ public class CollectionsHandler extends RequestHandlerBase {@OverrideMap<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler)throws Exception {+ final String aliasName = req.getParams().get(NAME);+ SolrIdentifierValidator.validateCollectionName(aliasName);return req.getParams().required().getAll(null, NAME, "collections");}},diff --git a/solr/core/src/java/org/apache/solr/util/SolrIdentifierValidator.java b/solr/core/src/java/org/apache/solr/util/SolrIdentifierValidator.javanew file mode 100644index 00000000000..dd6133d17e2--- /dev/null+++ b/solr/core/src/java/org/apache/solr/util/SolrIdentifierValidator.java@@ -0,0 +1,47 @@+/*+ * 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.util;++import java.lang.invoke.MethodHandles;+import java.util.regex.Pattern;++import org.apache.solr.common.SolrException;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++/**+ * Ensures that provided identifiers align with Solr's recommendations/requirements for choosing+ * collection, core, etc identifiers.+ *+ * Identifiers are allowed to contain underscores, periods, and alphanumeric characters.+ */+public class SolrIdentifierValidator {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());+ final static Pattern identifierPattern = Pattern.compile("^[\\._A-Za-z0-9]*$");++ public static void validateCollectionName(String collectionName) throws SolrException {+ validateCoreName(collectionName);+ }++ public static void validateCoreName(String name) throws SolrException {+ if (name == null || !identifierPattern.matcher(name).matches()) {+ log.info("Validation failed on the invalid identifier [{}]. Throwing SolrException to indicate a BAD REQUEST.", name);+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ "Invalid name: '" + name + "' Identifiers must consist entirely of periods, underscores and alphanumerics");+ }+ }+}diff --git a/solr/core/src/test/org/apache/solr/cloud/TestCollectionAPI.java b/solr/core/src/test/org/apache/solr/cloud/TestCollectionAPI.javaindex 18aa33b5fb5..b19c1c117b8 100644--- a/solr/core/src/test/org/apache/solr/cloud/TestCollectionAPI.java+++ b/solr/core/src/test/org/apache/solr/cloud/TestCollectionAPI.java@@ -28,6 +28,7 @@ import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrRequest;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException;import org.apache.solr.client.solrj.request.CollectionAdminRequest;import org.apache.solr.client.solrj.request.QueryRequest;import org.apache.solr.client.solrj.response.QueryResponse;@@ -78,6 +79,8 @@ public class TestCollectionAPI extends ReplicaPropertiesBase {replicaPropTest();clusterStatusZNodeVersion();testClusterStateMigration();+ testCollectionCreationNameValidation();+ testAliasCreationNameValidation();}private void clusterStatusWithCollectionAndShard() throws IOException, SolrServerException {@@ -627,6 +630,47 @@ public class TestCollectionAPI extends ReplicaPropertiesBase {assertEquals(10, response.getResults().getNumFound());}}++ private void testCollectionCreationNameValidation() throws Exception {+ try (CloudSolrClient client = createCloudClient(null)) {+ ModifiableSolrParams params = new ModifiableSolrParams();+ params.set("action", CollectionParams.CollectionAction.CREATE.toString());+ params.set("name", "invalid@name#with$weird%characters");+ SolrRequest request = new QueryRequest(params);+ request.setPath("/admin/collections");++ try {+ client.request(request);+ fail();+ } catch (RemoteSolrException e) {+ final String errorMessage = e.getMessage();+ assertTrue(errorMessage.contains("Invalid name"));+ assertTrue(errorMessage.contains("invalid@name#with$weird%characters"));+ assertTrue(errorMessage.contains("Identifiers must consist entirely of"));+ }+ }+ }++ private void testAliasCreationNameValidation() throws Exception{+ try (CloudSolrClient client = createCloudClient(null)) {+ ModifiableSolrParams params = new ModifiableSolrParams();+ params.set("action", CollectionParams.CollectionAction.CREATEALIAS.toString());+ params.set("name", "invalid@name#with$weird%characters");+ params.set("collections", COLLECTION_NAME);+ SolrRequest request = new QueryRequest(params);+ request.setPath("/admin/collections");++ try {+ client.request(request);+ fail();+ } catch (RemoteSolrException e) {+ final String errorMessage = e.getMessage();+ assertTrue(errorMessage.contains("Invalid name"));+ assertTrue(errorMessage.contains("invalid@name#with$weird%characters"));+ assertTrue(errorMessage.contains("Identifiers must consist entirely of"));+ }+ }+ }// Expects the map will have keys, but blank values.private Map<String, String> getProps(CloudSolrClient client, String collectionName, String replicaName, String... props)diff --git a/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.javaindex bbdd1a3b429..215932559b8 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.java@@ -139,7 +139,7 @@ public class CoreAdminHandlerTest extends SolrTestCaseJ4 {resp);} catch (SolrException se) {- assertTrue("Expected error message for bad core name.", se.toString().contains("Invalid core name"));+ assertTrue("Expected error message for bad core name.", se.toString().contains("Invalid name"));}CoreDescriptor cd = cores.getCoreDescriptor("ugly$core=name");assertNull("Should NOT have added this core!", cd);@@ -227,8 +227,8 @@ public class CoreAdminHandlerTest extends SolrTestCaseJ4 {CoreAdminParams.CORE, "rename_me",CoreAdminParams.OTHER, "bad$name"),resp);- } catch (IllegalArgumentException iae) { // why the heck does create return a SolrException (admittedly wrapping an IAE)- assertTrue("Expected error message for bad core name.", iae.getMessage().contains("Invalid core name"));+ } catch (SolrException e) { // why the heck does create return a SolrException (admittedly wrapping an IAE)+ assertTrue("Expected error message for bad core name.", e.getMessage().contains("Invalid name"));}cd = cores.getCoreDescriptor("bad$name");