Apache Solr open-source search software
SOLR-18226: Validate replication file names resolve within core config/tlog directory
Mark Robert Millere044bf20b405
diff --git a/changelog/unreleased/SOLR-18226-replication-file-name-validation.yml b/changelog/unreleased/SOLR-18226-replication-file-name-validation.ymlnew file mode 100644index 00000000000..9024d0fab68--- /dev/null+++ b/changelog/unreleased/SOLR-18226-replication-file-name-validation.yml@@ -0,0 +1,12 @@+title: >-+ Replication file fetching treats both "/" and "\" as path separators when+ validating a file name, and config/tlog file names must resolve within the+ core's config or tlog directory.+type: changed+authors:+ - name: Mark Robert Miller+links:+ - name: SOLR-18226+ url: https://issues.apache.org/jira/browse/SOLR-18226+important_notes:+ - "A `filecontent` request to `/solr/{core}/replication` with `dirType=cf` or `dirType=tlogFile` must name a file that resolves inside the core's config or tlog directory; otherwise it is rejected."diff --git a/solr/core/src/java/org/apache/solr/handler/admin/api/ReplicationAPIBase.java b/solr/core/src/java/org/apache/solr/handler/admin/api/ReplicationAPIBase.javaindex 8dbf4afa911..743aad55e88 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/api/ReplicationAPIBase.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/api/ReplicationAPIBase.java@@ -26,6 +26,7 @@ import java.lang.invoke.MethodHandles;import java.nio.ByteBuffer;import java.nio.channels.SeekableByteChannel;import java.nio.file.Files;+import java.nio.file.InvalidPathException;import java.nio.file.Path;import java.util.ArrayList;import java.util.List;@@ -56,6 +57,7 @@ import org.apache.solr.core.SolrCore;import org.apache.solr.handler.ReplicationHandler;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.SolrQueryResponse;+import org.apache.solr.util.FileUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -288,13 +290,10 @@ public abstract class ReplicationAPIBase extends JerseyResource {switch (dirType) {case CONF_FILE_SHORT:- cfileName = file;+ cfileName = fileName;break;case TLOG_FILE:- tlogFileName = file;- break;- default:- fileName = file;+ tlogFileName = fileName;break;}@@ -317,7 +316,15 @@ public abstract class ReplicationAPIBase extends JerseyResource {// Throw exception on directory traversal attemptsprotected String validateFilenameOrError(String fileName) {if (fileName != null) {- Path filePath = Path.of(fileName);+ // Treat both '/' and '\' as separators so that ".." components are detected on+ // every platform. The original fileName is returned; initFile() enforces that it+ // resolves within the intended directory.+ Path filePath;+ try {+ filePath = Path.of(FileUtils.normalizeToOsPathSeparator(fileName));+ } catch (InvalidPathException e) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid file name");+ }filePath.forEach(subpath -> {if ("..".equals(subpath.toString())) {@@ -554,8 +561,10 @@ public abstract class ReplicationAPIBase extends JerseyResource {@Overrideprotected Path initFile() {- // if it is a tlog file read from tlog directory- return Path.of(solrCore.getUpdateHandler().getUpdateLog().getTlogDir(), tlogFileName);+ return resolveWithinOrForbidden(+ Path.of(solrCore.getUpdateHandler().getUpdateLog().getTlogDir()),+ tlogFileName,+ "tlog directory");}}@@ -575,9 +584,26 @@ public abstract class ReplicationAPIBase extends JerseyResource {@Overrideprotected Path initFile() {- // if it is a conf file read from config directory- return solrCore.getResourceLoader().getConfigPath().resolve(cfileName);+ return resolveWithinOrForbidden(+ solrCore.getResourceLoader().getConfigPath(), cfileName, "core config directory");+ }+ }++ /**+ * Resolves {@code child} against {@code parent}, throwing a FORBIDDEN {@link SolrException} if+ * the result lies outside {@code parent}.+ */+ private static Path resolveWithinOrForbidden(+ Path parent, String child, String parentDescription) {+ if (child == null) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "File name required");+ }+ Path resolved = parent.resolve(child);+ if (!FileUtils.isPathAChildOfParent(parent, resolved)) {+ throw new SolrException(+ SolrException.ErrorCode.FORBIDDEN, "File path must be within the " + parentDescription);}+ return resolved.normalize();}private void reportErrorOnResponse(diff --git a/solr/core/src/java/org/apache/solr/util/FileUtils.java b/solr/core/src/java/org/apache/solr/util/FileUtils.javaindex 0f106a11f41..d720db81719 100644--- a/solr/core/src/java/org/apache/solr/util/FileUtils.java+++ b/solr/core/src/java/org/apache/solr/util/FileUtils.java@@ -72,10 +72,20 @@ public class FileUtils {}/**- * Takes a path and normalizes it with the OS default filesystems separator+ * Rewrites {@code path} so every path separator matches the current filesystem's separator.+ *+ * <p>Both {@code /} and {@code \} are replaced with {@link FileSystems#getDefault()}'s separator.+ * This is useful before iterating {@link Path} components, since on POSIX a {@code \} is+ * otherwise part of a name rather than a separator, so {@code \..\conf\x} would be a single+ * component.+ *+ * <p>Caveat: on non-Windows platforms this reinterprets any filename that legitimately contains a+ * backslash as a path with additional components. Callers that treat backslash as a valid+ * filename character on POSIX must not use this method.** @param path the path to normalize- * @return path normalized with the filesystems default separator+ * @return path normalized with the filesystem's default separator, or {@code null} if {@code+ * path} was {@code null}*/public static String normalizeToOsPathSeparator(String path) {if (path == null) return null;diff --git a/solr/core/src/test/org/apache/solr/handler/admin/api/CoreReplicationAPITest.java b/solr/core/src/test/org/apache/solr/handler/admin/api/CoreReplicationAPITest.javaindex 2e3a321bc5e..a4091f56aeb 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/api/CoreReplicationAPITest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/api/CoreReplicationAPITest.java@@ -22,14 +22,17 @@ import static org.mockito.Mockito.when;import io.opentelemetry.api.trace.Span;import java.io.IOException;+import java.nio.file.Files;import java.nio.file.Path;import java.util.ArrayList;import java.util.Arrays;import java.util.List;+import org.apache.lucene.util.Constants;import org.apache.solr.SolrTestCaseJ4;import org.apache.solr.client.api.model.FileListResponse;import org.apache.solr.client.api.model.FileMetaData;import org.apache.solr.client.api.model.IndexVersionResponse;+import org.apache.solr.common.SolrException;import org.apache.solr.core.SolrCore;import org.apache.solr.core.SolrResourceLoader;import org.apache.solr.handler.ReplicationHandler;@@ -37,6 +40,7 @@ import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.update.UpdateHandler;import org.apache.solr.update.UpdateLog;+import org.junit.Assume;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Test;@@ -98,6 +102,183 @@ public class CoreReplicationAPITest extends SolrTestCaseJ4 {assertTrue(actual instanceof ReplicationAPIBase.LocalFsConfFileStream);}+ @Test+ public void testValidateFilenameRejectsBackslashTraversal() {+ // Backslash is a separator for validation on every platform, not just Windows.+ for (String dirType : new String[] {"cf", "tlogFile"}) {+ SolrException ex =+ expectThrows(+ SolrException.class,+ "Expected FORBIDDEN for dirType=" + dirType,+ () ->+ coreReplicationAPI.doFetchFile(+ "\\..\\conf\\solrconfig.xml", dirType, null, null, false, false, 0, null));+ assertEquals(+ "Wrong error code for dirType=" + dirType,+ SolrException.ErrorCode.FORBIDDEN.code,+ ex.code());+ }+ }++ @Test+ public void testValidateFilenameRejectsTraversalVariants() {+ // Mixed separators, across both conf-file and tlog-file dirTypes.+ String[] inputs = {+ "\\..\\conf\\solrconfig.xml",+ "/..\\conf\\solrconfig.xml",+ "\\../conf/solrconfig.xml",+ "foo\\..\\..\\other\\file.txt",+ "..\\other\\file.txt",+ };+ for (String dirType : new String[] {"cf", "tlogFile"}) {+ for (String input : inputs) {+ SolrException ex =+ expectThrows(+ SolrException.class,+ "Expected FORBIDDEN for dirType=" + dirType + " input=" + input,+ () ->+ coreReplicationAPI.doFetchFile(+ input, dirType, null, null, false, false, 0, null));+ assertEquals(+ "Wrong error code for dirType=" + dirType + " input=" + input,+ SolrException.ErrorCode.FORBIDDEN.code,+ ex.code());+ }+ }+ }++ @Test+ public void testValidateFilenameRejectsAbsolutePath() {+ // On POSIX, '/abs/file.txt' is absolute; validateFilenameOrError must reject it+ // via its isAbsolute() branch. Windows treats a leading '/' as drive-relative+ // (not absolute), so on Windows the containment check in initFile carries the+ // load instead; this assertion targets the POSIX code path explicitly.+ Assume.assumeFalse(Constants.WINDOWS);+ SolrException ex =+ expectThrows(+ SolrException.class,+ () ->+ coreReplicationAPI.doFetchFile(+ "/abs/file.txt", "cf", null, null, false, false, 0, null));+ assertEquals(SolrException.ErrorCode.FORBIDDEN.code, ex.code());+ }++ @Test+ public void testValidateFilenameRejectsWindowsAbsolutePath() {+ // Symmetric to testValidateFilenameRejectsAbsolutePath: on Windows,+ // 'C:\Windows\...' IS absolute and must be rejected by+ // validateFilenameOrError's isAbsolute() branch. POSIX treats 'C:\...' as a+ // relative path with a literal ':' character, so Path.of won't mark it+ // absolute there; this assertion is therefore gated to Windows only.+ Assume.assumeTrue(Constants.WINDOWS);+ SolrException ex =+ expectThrows(+ SolrException.class,+ () ->+ coreReplicationAPI.doFetchFile(+ "C:\\Windows\\System32\\drivers\\etc\\hosts",+ "cf",+ null,+ null,+ false,+ false,+ 0,+ null));+ assertEquals(SolrException.ErrorCode.FORBIDDEN.code, ex.code());+ }++ @Test+ public void testValidateFilenameRejectsInvalidPath() {+ // A NUL character is rejected by Path.of on all platforms; ensure the+ // InvalidPathException is translated into a BAD_REQUEST SolrException+ // (malformed input, not access-denied) rather than bubbling as an+ // unhandled runtime error.+ SolrException ex =+ expectThrows(+ SolrException.class,+ () ->+ coreReplicationAPI.doFetchFile(+ "foo\u0000bar", "cf", null, null, false, false, 0, null));+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, ex.code());+ }++ @Test+ public void testValidNestedPathAccepted() throws IOException {+ // Positive test: legal nested relative paths (e.g. 'lang/en.txt') must not+ // be rejected by validateFilenameOrError OR by the isPathAChildOfParent+ // containment check in LocalFs{Conf,Tlog}FileStream.initFile(). Guards+ // against those checks over-rejecting.+ // Also exercises the normalize() applied to the resolved path by+ // resolveWithinOrForbidden; the on-disk file must still be readable.+ Path configPath = mockCore.getResourceLoader().getConfigPath();+ Files.createDirectories(configPath.resolve("lang"));+ Path cfFile = configPath.resolve("lang").resolve("en.txt");+ Files.writeString(cfFile, "hello");+ Path tlogDir = Path.of(mockCore.getUpdateHandler().getUpdateLog().getTlogDir());+ Files.createDirectories(tlogDir.resolve("nested"));+ Path tlogFile = tlogDir.resolve("nested").resolve("tlog.0");+ Files.writeString(tlogFile, "hello");++ ReplicationAPIBase.DirectoryFileStream cfStream =+ coreReplicationAPI.doFetchFile("lang/en.txt", "cf", null, null, false, false, 0, null);+ assertTrue(cfStream instanceof ReplicationAPIBase.LocalFsConfFileStream);+ assertTrue("Expected readable config file at " + cfFile, Files.isReadable(cfFile));+ assertEquals(cfFile.normalize(), configPath.resolve("lang/en.txt").normalize());++ ReplicationAPIBase.DirectoryFileStream tlogStream =+ coreReplicationAPI.doFetchFile(+ "nested/tlog.0", "tlogFile", null, null, false, false, 0, null);+ assertTrue(tlogStream instanceof ReplicationAPIBase.LocalFsTlogFileStream);+ assertTrue("Expected readable tlog file at " + tlogFile, Files.isReadable(tlogFile));+ assertEquals(tlogFile.normalize(), tlogDir.resolve("nested/tlog.0").normalize());+ }++ @Test+ public void testDoFetchFileRejectsNullFilename() {+ // A null file argument for the LocalFs{Conf,Tlog}FileStream paths+ // must surface as a clean 4xx SolrException rather than an NPE bubbling out+ // of Path.resolve(null). Guards the null-child branch of+ // resolveWithinOrForbidden for both dirType=cf and dirType=tlogFile.+ for (String dirType : new String[] {"cf", "tlogFile"}) {+ SolrException ex =+ expectThrows(+ SolrException.class,+ "Expected 4xx for dirType=" + dirType,+ () ->+ coreReplicationAPI.doFetchFile(null, dirType, null, null, false, false, 0, null));+ assertTrue(+ "Expected a 4xx error code for dirType=" + dirType + ", got " + ex.code(),+ ex.code() >= 400 && ex.code() < 500);+ }+ }++ @Test+ public void testInitFileGuardCatchesPathEscapingConfigDir() {+ // Directly exercise LocalFsConfFileStream.initFile()'s isPathAChildOfParent guard,+ // independent of validateFilenameOrError.+ SolrException ex =+ expectThrows(+ SolrException.class,+ () ->+ ((CoreReplicationAPIMock) coreReplicationAPI)+ .newUnvalidatedConfFileStream("../../other/file.txt"));+ assertEquals(SolrException.ErrorCode.FORBIDDEN.code, ex.code());+ }++ @Test+ public void testInitFileGuardCatchesPathEscapingTlogDir() {+ // Symmetric to testInitFileGuardCatchesPathEscapingConfigDir: ensures the+ // LocalFsTlogFileStream.initFile() containment check rejects paths that+ // resolve outside the tlog directory, independent of validateFilenameOrError.+ SolrException ex =+ expectThrows(+ SolrException.class,+ () ->+ ((CoreReplicationAPIMock) coreReplicationAPI)+ .newUnvalidatedTlogFileStream("../../other/file.txt"));+ assertEquals(SolrException.ErrorCode.FORBIDDEN.code, ex.code());+ }+private void setUpMocks() throws IOException {mockCore = mock(SolrCore.class);mockReplicationHandler = mock(ReplicationHandler.class);@@ -106,15 +287,15 @@ public class CoreReplicationAPITest extends SolrTestCaseJ4 {UpdateHandler mockUpdateHandler = mock(UpdateHandler.class);UpdateLog mockUpdateLog = mock(UpdateLog.class);when(mockUpdateHandler.getUpdateLog()).thenReturn(mockUpdateLog);- when(mockUpdateLog.getTlogDir()).thenReturn("ignore");+ when(mockUpdateLog.getTlogDir()).thenReturn(createTempDir("coreReplicationTlog").toString());// Mocks for LocalFsConfFileStreamSolrResourceLoader mockSolrResourceLoader = mock(SolrResourceLoader.class);- Path mockPath = mock(Path.class);+ Path configPath = createTempDir("coreReplicationConf");when(mockCore.getRequestHandler(ReplicationHandler.PATH)).thenReturn(mockReplicationHandler);when(mockCore.getUpdateHandler()).thenReturn(mockUpdateHandler);when(mockCore.getResourceLoader()).thenReturn(mockSolrResourceLoader);- when(mockSolrResourceLoader.getConfigPath()).thenReturn(mockPath);+ when(mockSolrResourceLoader.getConfigPath()).thenReturn(configPath);}private static class CoreReplicationAPIMock extends CoreReplication {@@ -129,5 +310,23 @@ public class CoreReplicationAPITest extends SolrTestCaseJ4 {filesResponse.fileList = new ArrayList<>(fileMetaData);return filesResponse;}++ ReplicationAPIBase.LocalFsConfFileStream newUnvalidatedConfFileStream(String cfname) {+ return new LocalFsConfFileStream(cfname, "cf", null, null, false, false, 0, null) {+ @Override+ protected String validateFilenameOrError(String fileName) {+ return fileName;+ }+ };+ }++ ReplicationAPIBase.LocalFsTlogFileStream newUnvalidatedTlogFileStream(String tlogname) {+ return new LocalFsTlogFileStream(tlogname, "tlogFile", null, null, false, false, 0, null) {+ @Override+ protected String validateFilenameOrError(String fileName) {+ return fileName;+ }+ };+ }}}