From 0a1fb41a0e7b248679e0f6784e02faa94f119c95 Mon Sep 17 00:00:00 2001 From: Alex O'Ree Date: Sun, 23 Aug 2026 13:35:45 -0400 Subject: [PATCH] JSPWIKI-1295 see jira for details --- .../wiki/search/tika/TikaSearchProvider.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/jspwiki-tika-searchprovider/src/main/java/org/apache/wiki/search/tika/TikaSearchProvider.java b/jspwiki-tika-searchprovider/src/main/java/org/apache/wiki/search/tika/TikaSearchProvider.java index 43f86af4f6..ac11d29158 100644 --- a/jspwiki-tika-searchprovider/src/main/java/org/apache/wiki/search/tika/TikaSearchProvider.java +++ b/jspwiki-tika-searchprovider/src/main/java/org/apache/wiki/search/tika/TikaSearchProvider.java @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.tika.exception.TikaException; +import org.apache.tika.exception.WriteLimitReachedException; import org.apache.tika.metadata.ClimateForcast; import org.apache.tika.metadata.CreativeCommons; import org.apache.tika.metadata.Database; @@ -57,6 +58,13 @@ public class TikaSearchProvider extends LuceneSearchProvider { final AutoDetectParser parser; final Set< String > textualMetadataFields; + /** + * Maximum number of characters extracted from a single attachment for indexing. Bounds the memory used at index + * time regardless of attachment content: a small compressed attachment (e.g. a zip bomb, which expands to + * gigabytes of text) must not be able to grow into an unbounded in-memory string and exhaust the heap. + */ + static final int MAX_EXTRACTED_CHARS = 1_000_000; + public TikaSearchProvider() { parser = new AutoDetectParser(); @@ -99,10 +107,17 @@ protected String getAttachmentContent( final Attachment att ) { final Metadata metadata = new Metadata(); metadata.set( TikaCoreProperties.RESOURCE_NAME_KEY, att.getFileName() ); - final ContentHandler handler = new BodyContentHandler(-1 ); - // -1 disables the character size limit; otherwise only the first 100.000 characters are indexed - - parser.parse( attStream, handler, metadata ); + final ContentHandler handler = new BodyContentHandler( MAX_EXTRACTED_CHARS ); + try { + parser.parse( attStream, handler, metadata ); + } catch( final SAXException e ) { + if( !WriteLimitReachedException.isWriteLimitReached( e ) ) { + throw e; + } + // keep what was extracted so far, so oversized attachments still get (truncated) indexing + LOG.warn( "Attachment {} reached the maximum of {} extracted characters, indexing truncated content", + att.getFileName(), MAX_EXTRACTED_CHARS ); + } out.append( handler ); final String[] names = metadata.names();