Author: jukka
Date: Fri Feb 17 13:41:46 2012
New Revision: 1245445

URL: http://svn.apache.org/viewvc?rev=1245445&view=rev
Log:
TIKA-866: Invalid configuration file causes OutOfMemoryException

Add an explicit check against composite parsers in <parser> elements. Include a 
test case.

Added:
    tika/trunk/tika-core/src/test/java/org/apache/tika/config/
    
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java   
(with props)
    tika/trunk/tika-core/src/test/resources/org/apache/tika/config/
    
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-invalid.xml
   (with props)
    
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-valid.xml
   (with props)
Modified:
    tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java

Modified: 
tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java?rev=1245445&r1=1245444&r2=1245445&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java 
(original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java 
Fri Feb 17 13:41:46 2012
@@ -40,6 +40,7 @@ import org.apache.tika.mime.MediaTypeReg
 import org.apache.tika.mime.MimeTypeException;
 import org.apache.tika.mime.MimeTypes;
 import org.apache.tika.mime.MimeTypesFactory;
+import org.apache.tika.parser.AutoDetectParser;
 import org.apache.tika.parser.CompositeParser;
 import org.apache.tika.parser.DefaultParser;
 import org.apache.tika.parser.Parser;
@@ -285,6 +286,13 @@ public class TikaConfig {
 
             try {
                 Class<?> parserClass = Class.forName(name, true, loader);
+                // https://issues.apache.org/jira/browse/TIKA-866
+                if (DefaultParser.class.isAssignableFrom(parserClass)
+                        || 
AutoDetectParser.class.isAssignableFrom(parserClass)) {
+                    throw new TikaException(
+                            "Composite parsers not supported in <parser>"
+                            + " configuration elements: " + name);
+                }
                 Object instance = parserClass.newInstance();
                 if (!(instance instanceof Parser)) {
                     throw new TikaException(

Added: 
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java?rev=1245445&view=auto
==============================================================================
--- 
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java 
(added)
+++ 
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java 
Fri Feb 17 13:41:46 2012
@@ -0,0 +1,65 @@
+/*
+ * 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.tika.config;
+
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.parser.DefaultParser;
+
+public class TikaConfigTest extends TestCase {
+
+    /**
+     * Make sure that a configuration file can't reference to composite
+     * parser classes like {@link DefaultParser} in the &lt;parser&gt;
+     * configuration elements.
+     *
+     * @see <a 
href="https://issues.apache.org/jira/browse/TIKA-866";>TIKA-866</a>
+     */
+    public void testInvalidParser() throws Exception {
+        InputStream xml = TikaConfigTest.class.getResourceAsStream(
+                "TIKA-866-invalid.xml");
+        try {
+            new TikaConfig(xml);
+            fail("Composite parser class was allowed in <parser>");
+        } catch (TikaException expected) {
+            // OK
+        } finally {
+            xml.close();
+        }
+    }
+
+    /**
+     * Make sure that a valid configuration file without mimetypes or
+     * detector entries can be loaded without problems.
+     *
+     * @see <a 
href="https://issues.apache.org/jira/browse/TIKA-866";>TIKA-866</a>
+     */
+    public void testValidParser() throws Exception {
+        InputStream xml = TikaConfigTest.class.getResourceAsStream(
+                "TIKA-866-valid.xml");
+        try {
+            new TikaConfig(xml);
+            // OK
+        } finally {
+            xml.close();
+        }
+    }
+
+}
\ No newline at end of file

Propchange: 
tika/trunk/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-invalid.xml
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-invalid.xml?rev=1245445&view=auto
==============================================================================
--- 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-invalid.xml
 (added)
+++ 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-invalid.xml
 Fri Feb 17 13:41:46 2012
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<properties>
+  <parsers>
+    <parser class="org.apache.tika.parser.DefaultParser"/>
+  </parsers>
+</properties>

Propchange: 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-invalid.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-valid.xml
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-valid.xml?rev=1245445&view=auto
==============================================================================
--- 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-valid.xml
 (added)
+++ 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-valid.xml
 Fri Feb 17 13:41:46 2012
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<properties>
+  <parsers>
+    <parser class="org.apache.tika.parser.EmptyParser"/>
+  </parsers>
+</properties>

Propchange: 
tika/trunk/tika-core/src/test/resources/org/apache/tika/config/TIKA-866-valid.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to