pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1852826 - in /pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight: PreflightContext.java utils/ContextHelper.java
Date Sun, 03 Feb 2019 12:10:45 GMT
Author: tilman
Date: Sun Feb  3 12:10:45 2019
New Revision: 1852826

URL: http://svn.apache.org/viewvc?rev=1852826&view=rev
Log:
PDFBOX-4450: catch more recursions, avoid multiple checking of certain COS elements that isn't
caught in recursion check

Modified:
    pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightContext.java
    pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/ContextHelper.java

Modified: pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightContext.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightContext.java?rev=1852826&r1=1852825&r2=1852826&view=diff
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightContext.java
(original)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/PreflightContext.java
Sun Feb  3 12:10:45 2019
@@ -23,11 +23,14 @@ package org.apache.pdfbox.preflight;
 
 import java.io.Closeable;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.pdfparser.XrefTrailerResolver;
+import org.apache.pdfbox.pdmodel.common.COSObjectable;
 import org.apache.pdfbox.preflight.ValidationResult.ValidationError;
 import org.apache.pdfbox.preflight.font.container.FontContainer;
 import org.apache.pdfbox.preflight.graphic.ICCProfileWrapper;
@@ -76,6 +79,8 @@ public class PreflightContext implements
 
     private PreflightPath validationPath = new PreflightPath();
 
+    private final Set<COSObjectable> processedSet = new HashSet<>();
+
     private Integer currentPageNumber = null;
     
     private long fileLen;
@@ -287,4 +292,25 @@ public class PreflightContext implements
     {
         return fileLen;
     }
+
+    /**
+     * Add the argument to the set of processed elements,
+     *
+     * @param cos
+     */
+    public void addToProcessedSet(COSObjectable cos)
+    {
+        processedSet.add(cos);
+    }
+
+    /**
+     * Tell if the argument is in the set of processed elements.
+     *
+     * @param cos
+     * @return true if in the set, false if not.
+     */
+    public boolean isInProcessedSet(COSObjectable cos)
+    {
+        return processedSet.contains(cos);
+    }
 }

Modified: pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/ContextHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/ContextHelper.java?rev=1852826&r1=1852825&r2=1852826&view=diff
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/ContextHelper.java
(original)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/ContextHelper.java
Sun Feb  3 12:10:45 2019
@@ -21,7 +21,10 @@
 
 package org.apache.pdfbox.preflight.utils;
 
+import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.common.COSObjectable;
+import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
 import org.apache.pdfbox.preflight.PreflightConfiguration;
 import static org.apache.pdfbox.preflight.PreflightConstants.ERROR_PDF_PROCESSING_MISSING;
 import static org.apache.pdfbox.preflight.PreflightConstants.ERROR_PDF_PROCESSING;
@@ -77,7 +80,22 @@ public final class ContextHelper
         {
             return;
         }
-        
+        // avoid checking certain elements twice. This can't be generalized,
+        // because some are checked by several processes.
+        if (element instanceof COSObjectable && 
+                // PDFBOX-4450 catch undetected recursions
+                (PreflightConfiguration.GRAPHIC_PROCESS.equals(processName) || 
+                 PreflightConfiguration.FONT_PROCESS.equals(processName))) // for speed
+        {
+            // don't check PDObjects, only their COSObject
+            COSBase cos = ((COSObjectable) element).getCOSObject();
+            if (context.isInProcessedSet(cos))
+            {
+                return;
+            }
+            context.addToProcessedSet(cos);
+        }
+
         boolean needPop = validationPath.pushObject(element);
         PreflightConfiguration config = context.getConfig();
         ValidationProcess process = config.getInstanceOfProcess(processName);
@@ -91,17 +109,18 @@ public final class ContextHelper
     // detect recursion that would lead to stack overflow
     private static boolean hasRecursion(PreflightContext context, Object element, PreflightPath
validationPath)
     {
-        if (element instanceof PDResources)
+        if (element instanceof PDResources || element instanceof PDFormXObject)
         {
             for (int i = 0; i < validationPath.size(); ++i)
             {
                 Object obj = validationPath.getPathElement(i, Object.class);
-                if (obj instanceof PDResources)
+                if (obj instanceof COSObjectable)
                 {
-                    PDResources pdRes = (PDResources) obj;
-                    if (pdRes.getCOSObject() == ((PDResources) element).getCOSObject())
+                    COSObjectable cos = (COSObjectable) obj;
+                    if (cos.getCOSObject() == ((COSObjectable) element).getCOSObject())
                     {
-                        context.addValidationError(new ValidationError(ERROR_PDF_PROCESSING,
"Resources recursion"));
+                        context.addValidationError(new ValidationError(ERROR_PDF_PROCESSING,

+                                element.getClass().getSimpleName() + " recursion"));
                         return true;
                     }
                 }               



Mime
View raw message