Skip to content

Commit 5b2b17d

Browse files
authored
* TIKA-4490 -- move the check to the parser level
1 parent 8a0bbe8 commit 5b2b17d

3 files changed

Lines changed: 13 additions & 8 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: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@ 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-
}
135131
alternativePartBuffer.peek().children.add(new BodyContents(submd, bos.toByteArray()));
136132
} else if (!extractAllAlternatives && parts.size() < 2) {
137133
//if you're at the first level of embedding
@@ -141,9 +137,6 @@ public void body(BodyDescriptor body, InputStream is) throws MimeException, IOEx
141137
UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get();
142138
IOUtils.copy(is, bos);
143139
final byte[] bytes = bos.toByteArray();
144-
if (bytes.length == 0) {
145-
return;
146-
}
147140
if (detectInlineTextOrHtml(submd, bytes)) {
148141
handleInlineBodyPart(new BodyContents(submd, bytes));
149142
} else {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.tika.config.Field;
3232
import org.apache.tika.detect.Detector;
3333
import org.apache.tika.exception.TikaException;
34+
import org.apache.tika.exception.ZeroByteFileException;
3435
import org.apache.tika.extractor.EmbeddedDocumentUtil;
3536
import org.apache.tika.io.TikaInputStream;
3637
import org.apache.tika.metadata.Metadata;
@@ -95,6 +96,7 @@ public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
9596
parser.setNoRecurse();
9697
xhtml.startDocument();
9798
TikaInputStream tstream = TikaInputStream.get(stream);
99+
checkForZeroByte(tstream);//avoid stackoverflow
98100
try {
99101
parser.parse(tstream);
100102
} catch (IOException e) {
@@ -114,6 +116,17 @@ public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
114116
xhtml.endDocument();
115117
}
116118

119+
private void checkForZeroByte(TikaInputStream tstream) throws IOException, ZeroByteFileException {
120+
tstream.mark(1);
121+
try {
122+
if (tstream.read() < 0) {
123+
throw new ZeroByteFileException("rfc822 parser found zero bytes");
124+
}
125+
} finally {
126+
tstream.reset();
127+
}
128+
}
129+
117130
/**
118131
* Until version 1.17, Tika handled all body parts as embedded objects (see TIKA-2478).
119132
* In 1.17, we modified the parser to select only the best alternative body

tika-parsers/tika-parsers-standard/tika-parsers-standard-package/src/test/java/org/apache/tika/ossfuzz/ParserFuzzer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.apache.tika.parser.RecursiveParserWrapper;
2828
import org.apache.tika.sax.BasicContentHandlerFactory;
2929
import org.apache.tika.sax.RecursiveParserWrapperHandler;
30-
3130
import org.apache.tika.sax.ToTextContentHandler;
3231

3332

0 commit comments

Comments
 (0)