Skip to content

Commit 8a0bbe8

Browse files
authored
TIKA-4490 (#2349) -- fixes for problems found via ossfuzz integration
1 parent 3c0dab1 commit 8a0bbe8

5 files changed

Lines changed: 123 additions & 0 deletions

File tree

tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-mail-module/src/main/java/org/apache/tika/parser/mail/MailContentHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public void body(BodyDescriptor body, InputStream is) throws MimeException, IOEx
128128
if (!extractAllAlternatives && alternativePartBuffer.size() > 0) {
129129
UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get();
130130
IOUtils.copy(is, bos);
131+
byte[] bytes = bos.toByteArray();
132+
if (bytes.length == 0) {
133+
return;
134+
}
131135
alternativePartBuffer.peek().children.add(new BodyContents(submd, bos.toByteArray()));
132136
} else if (!extractAllAlternatives && parts.size() < 2) {
133137
//if you're at the first level of embedding
@@ -137,6 +141,9 @@ public void body(BodyDescriptor body, InputStream is) throws MimeException, IOEx
137141
UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get();
138142
IOUtils.copy(is, bos);
139143
final byte[] bytes = bos.toByteArray();
144+
if (bytes.length == 0) {
145+
return;
146+
}
140147
if (detectInlineTextOrHtml(submd, bytes)) {
141148
handleInlineBodyPart(new BodyContents(submd, bytes));
142149
} else {

tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/detect/microsoft/ooxml/OPCPackageDetector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ public static MediaType detectOfficeOpenXML(OPCPackage pkg) {
198198

199199
// Get the type of the core document part
200200
PackagePart corePart = pkg.getPart(core.getRelationship(0));
201+
if (corePart == null) {
202+
return null;
203+
}
204+
201205
String coreType = corePart.getContentType();
202206

203207
if (coreType.contains(".xps")) {

tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/ExcelExtractor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ private void internalProcessRecord(Record record)
508508
break;
509509

510510
case LabelSSTRecord.sid: // Ref. a string in the shared string table
511+
if (sstRecord == null) {
512+
throw new TikaException("sstRecord should have been initialized before a ref to the shared string table");
513+
}
511514
LabelSSTRecord sst = (LabelSSTRecord) record;
512515
UnicodeString unicode = sstRecord.getString(sst.getSSTIndex());
513516
String cellString = null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.tika.ossfuzz;
18+
19+
import org.junit.jupiter.api.Disabled;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.apache.tika.parser.mail.RFC822Parser;
23+
24+
public class OssFuzzReplicator {
25+
26+
@Test
27+
@Disabled("turn this on for debugging ossfuzz findings")
28+
public void testOne() throws Throwable {
29+
byte[] bytes = new byte[0];//specify bytes here...probably from a path
30+
ParserFuzzer.parseOne(new RFC822Parser(), bytes);
31+
}
32+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.tika.ossfuzz;
18+
19+
import java.io.InputStream;
20+
21+
import org.xml.sax.ContentHandler;
22+
23+
import org.apache.tika.io.TikaInputStream;
24+
import org.apache.tika.metadata.Metadata;
25+
import org.apache.tika.parser.ParseContext;
26+
import org.apache.tika.parser.Parser;
27+
import org.apache.tika.parser.RecursiveParserWrapper;
28+
import org.apache.tika.sax.BasicContentHandlerFactory;
29+
import org.apache.tika.sax.RecursiveParserWrapperHandler;
30+
31+
import org.apache.tika.sax.ToTextContentHandler;
32+
33+
34+
class ParserFuzzer {
35+
36+
public static void parseOne(Parser parser, byte[] bytes, ParseContext parseContext) throws Throwable {
37+
parseBytes(parser, bytes, parseContext);
38+
parseFile(parser, bytes, parseContext);
39+
}
40+
41+
42+
public static void parseOne(Parser parser, byte[] bytes) throws Throwable {
43+
parseBytes(parser, bytes, new ParseContext());
44+
parseFile(parser, bytes, new ParseContext());
45+
}
46+
47+
public static void parseRMetaFile(Parser parser, byte[] bytes) throws Throwable {
48+
RecursiveParserWrapper wrapper = new RecursiveParserWrapper(parser);
49+
RecursiveParserWrapperHandler rpwh = new RecursiveParserWrapperHandler(
50+
new BasicContentHandlerFactory(BasicContentHandlerFactory.HANDLER_TYPE.XML, -1));
51+
try (TikaInputStream tis = TikaInputStream.get(bytes)) {
52+
tis.getPath();
53+
wrapper.parse(tis, rpwh, new Metadata(), new ParseContext());
54+
}
55+
}
56+
57+
public static void parseBytes(Parser parser, byte[] bytes, ParseContext parseContext) throws Throwable {
58+
ContentHandler handler = new ToTextContentHandler();
59+
//make sure that other parsers cannot be invoked
60+
parseContext.set(Parser.class, parser);
61+
//try first with bytes
62+
try (InputStream is = TikaInputStream.get(bytes)) {
63+
parser.parse(is, handler, new Metadata(), parseContext);
64+
}
65+
}
66+
67+
public static void parseFile(Parser parser, byte[] bytes, ParseContext parseContext) throws Throwable {
68+
ContentHandler handler = new ToTextContentHandler();
69+
//make sure that other parsers cannot be invoked
70+
parseContext.set(Parser.class, parser);
71+
try (TikaInputStream tis = TikaInputStream.get(bytes)) {
72+
//force writing to tmp file
73+
tis.getPath();
74+
parser.parse(tis, handler, new Metadata(), parseContext);
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)