17
17
package org .apache .solr .core ;
18
18
19
19
import static org .apache .solr .core .FileSystemConfigSetService .METADATA_FILE ;
20
+ import static org .hamcrest .Matchers .hasItem ;
20
21
22
+ import java .io .File ;
21
23
import java .io .IOException ;
22
24
import java .nio .charset .StandardCharsets ;
23
25
import java .nio .file .Files ;
@@ -49,13 +51,43 @@ public static void afterClass() throws Exception {
49
51
fileSystemConfigSetService = null ;
50
52
}
51
53
54
+ @ Test
55
+ public void testIgnoresFileUploadsOutsideOfConfigSetDirectory () throws IOException {
56
+ final var initialNumConfigs = fileSystemConfigSetService .listConfigs ().size ();
57
+ final String configName = "fileEscapeTestConfig" ;
58
+ final var specificConfigSetBase = configSetBase .resolve (configName );
59
+
60
+ fileSystemConfigSetService .uploadConfig (configName , configset ("cloud-minimal" ));
61
+ assertEquals (fileSystemConfigSetService .listConfigs ().size (), initialNumConfigs + 1 );
62
+ assertTrue (fileSystemConfigSetService .checkConfigExists (configName ));
63
+
64
+ // This succeeds, as the file is an allowed type and the path doesn't attempt to escape the
65
+ // configset's root
66
+ byte [] testdata = "test data" .getBytes (StandardCharsets .UTF_8 );
67
+ fileSystemConfigSetService .uploadFileToConfig (configName , "validPath" , testdata , true );
68
+ final var knownFiles = fileSystemConfigSetService .getAllConfigFiles (configName );
69
+ assertThat (knownFiles , hasItem ("validPath" ));
70
+ assertTrue (Files .exists (specificConfigSetBase .resolve ("validPath" )));
71
+
72
+ // Each of these will fail "quietly" as ConfigSetService opts to log warnings but otherwise not
73
+ // surface validation errors to enable bulk uploading
74
+ final var invalidFilePaths =
75
+ List .of (
76
+ ".." + File .separator + "escapePath" ,
77
+ "foo" + File .separator + ".." + File .separator + ".." + File .separator + "bar" );
78
+ for (String invalidFilePath : invalidFilePaths ) {
79
+ fileSystemConfigSetService .uploadFileToConfig (configName , invalidFilePath , testdata , true );
80
+ assertFalse (Files .exists (specificConfigSetBase .resolve (invalidFilePath )));
81
+ }
82
+ }
83
+
52
84
@ Test
53
85
public void testUploadAndDeleteConfig () throws IOException {
86
+ final var initialNumConfigs = fileSystemConfigSetService .listConfigs ().size ();
54
87
String configName = "testconfig" ;
55
88
56
89
fileSystemConfigSetService .uploadConfig (configName , configset ("cloud-minimal" ));
57
-
58
- assertEquals (fileSystemConfigSetService .listConfigs ().size (), 1 );
90
+ assertEquals (fileSystemConfigSetService .listConfigs ().size (), initialNumConfigs + 1 );
59
91
assertTrue (fileSystemConfigSetService .checkConfigExists (configName ));
60
92
61
93
byte [] testdata = "test data" .getBytes (StandardCharsets .UTF_8 );
@@ -79,7 +111,7 @@ public void testUploadAndDeleteConfig() throws IOException {
79
111
assertEquals ("[schema.xml, solrconfig.xml]" , allConfigFiles .toString ());
80
112
81
113
fileSystemConfigSetService .copyConfig (configName , "copytestconfig" );
82
- assertEquals (fileSystemConfigSetService .listConfigs ().size (), 2 );
114
+ assertEquals (fileSystemConfigSetService .listConfigs ().size (), initialNumConfigs + 2 );
83
115
84
116
allConfigFiles = fileSystemConfigSetService .getAllConfigFiles ("copytestconfig" );
85
117
assertEquals ("[schema.xml, solrconfig.xml]" , allConfigFiles .toString ());
0 commit comments