Skip to content

Commit dd3be31

Browse files
committed
SOLR-11971: Don't allow referal to external resources in DataImportHandler's dataConfig request parameter
1 parent 51e712c commit dd3be31

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

solr/CHANGES.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ Apache UIMA 2.3.1
2929
Apache ZooKeeper 3.4.10
3030
Jetty 9.3.14.v20161028
3131

32+
Bug Fixes
33+
----------------------
3234

33-
(No Changes)
34-
35+
* SOLR-11971: Don't allow referal to external resources in DataImportHandler's dataConfig request parameter.
36+
(麦 香浓郁, Uwe Schindler)
3537

3638
================== 6.6.2 ==================
3739

solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DataImporter.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.solr.handler.dataimport;
1818

19+
import org.apache.solr.common.EmptyEntityResolver;
1920
import org.apache.solr.common.SolrException;
2021
import org.apache.solr.core.SolrCore;
2122
import org.apache.solr.schema.IndexSchema;
@@ -178,11 +179,11 @@ public IndexSchema getSchema() {
178179
/**
179180
* Used by tests
180181
*/
181-
public void loadAndInit(String configStr) {
182+
void loadAndInit(String configStr) {
182183
config = loadDataConfig(new InputSource(new StringReader(configStr)));
183184
}
184185

185-
public void loadAndInit(InputSource configFile) {
186+
void loadAndInit(InputSource configFile) {
186187
config = loadDataConfig(configFile);
187188
}
188189

@@ -191,8 +192,10 @@ public DIHConfiguration loadDataConfig(InputSource configFile) {
191192
DIHConfiguration dihcfg = null;
192193
try {
193194
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
195+
dbf.setValidating(false);
194196

195-
// only enable xinclude, if a a SolrCore and SystemId is present (makes no sense otherwise)
197+
// only enable xinclude, if XML is coming from safe source (local file)
198+
// and a a SolrCore and SystemId is present (makes no sense otherwise):
196199
if (core != null && configFile.getSystemId() != null) {
197200
try {
198201
dbf.setXIncludeAware(true);
@@ -203,8 +206,14 @@ public DIHConfiguration loadDataConfig(InputSource configFile) {
203206
}
204207

205208
DocumentBuilder builder = dbf.newDocumentBuilder();
206-
if (core != null)
209+
// only enable xinclude / external entities, if XML is coming from
210+
// safe source (local file) and a a SolrCore and SystemId is present:
211+
if (core != null && configFile.getSystemId() != null) {
207212
builder.setEntityResolver(new SystemIdResolver(core.getResourceLoader()));
213+
} else {
214+
// Don't allow external entities without having a system ID:
215+
builder.setEntityResolver(EmptyEntityResolver.SAX_INSTANCE);
216+
}
208217
builder.setErrorHandler(XMLLOG);
209218
Document document;
210219
try {

solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestErrorHandling.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ public void testTransformerErrorContinue() throws Exception {
8989
assertQ(req("*:*"), "//*[@numFound='3']");
9090
}
9191

92+
public void testExternalEntity() throws Exception {
93+
StringDataSource.xml = wellformedXml;
94+
// This should not fail as external entities are replaced by an empty string during parsing:
95+
runFullImport(dataConfigWithEntity);
96+
assertQ(req("*:*"), "//*[@numFound='3']");
97+
}
98+
9299
public static class StringDataSource extends DataSource<Reader> {
93100
public static String xml = "";
94101

@@ -157,6 +164,19 @@ public Object transformRow(Map<String, Object> row, Context context) {
157164
" </document>\n" +
158165
"</dataConfig>";
159166

167+
private String dataConfigWithEntity = "<!DOCTYPE dataConfig [\n" +
168+
" <!ENTITY internalTerm \"node\">\n" +
169+
" <!ENTITY externalTerm SYSTEM \"foo://bar.xyz/external\">\n" +
170+
"]><dataConfig>\n" +
171+
" <dataSource name=\"str\" type=\"TestErrorHandling$StringDataSource\" />" +
172+
" <document>\n" +
173+
" <entity name=\"&internalTerm;\" dataSource=\"str\" processor=\"XPathEntityProcessor\" url=\"test\" forEach=\"/root/node\" onError=\"skip\">\n" +
174+
" <field column=\"id\" xpath=\"/root/node/id\">&externalTerm;</field>\n" +
175+
" <field column=\"desc\" xpath=\"/root/node/desc\" />\n" +
176+
" </entity>\n" +
177+
" </document>\n" +
178+
"</dataConfig>";
179+
160180
private String malformedXml = "<root>\n" +
161181
" <node>\n" +
162182
" <id>1</id>\n" +

0 commit comments

Comments
 (0)