From 3ee410e8b54790b3fd22b42ce61f1947a8177fe9 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 20 Mar 2022 06:52:43 +0000 Subject: [PATCH] Bug 65854: Use revert() instead of close() when OPCPackage is opened read-only Also add tests, however it is not possible to verify that no logging takes place, so you have to use debugging to verify it. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899071 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/openxml4j/opc/OPCPackage.java | 24 ++++++++++++------- .../TestOPCCompliancePackageModel.java | 22 +++++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java index ac9568afba..1035459f82 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -211,7 +211,9 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { // pack.originalPackagePath = file.getAbsolutePath(); return pack; } catch (InvalidFormatException | RuntimeException e) { - IOUtils.closeQuietly(pack); + // use revert() to free resources when the packgae is opened read-only + pack.revert(); + throw e; } } @@ -272,14 +274,14 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { */ public static OPCPackage open(File file, PackageAccess access) throws InvalidFormatException { - if (file == null) { - throw new IllegalArgumentException("'file' must be given"); - } - if (file.exists() && file.isDirectory()) { - throw new IllegalArgumentException("file must not be a directory"); - } + if (file == null) { + throw new IllegalArgumentException("'file' must be given"); + } + if (file.exists() && file.isDirectory()) { + throw new IllegalArgumentException("file must not be a directory"); + } - OPCPackage pack = new ZipPackage(file, access); + OPCPackage pack = new ZipPackage(file, access); try { if (pack.partList == null && access != PackageAccess.WRITE) { pack.getParts(); @@ -287,7 +289,11 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { pack.originalPackagePath = file.getAbsolutePath(); return pack; } catch (InvalidFormatException | RuntimeException e) { - IOUtils.closeQuietly(pack); + if (access == PackageAccess.READ) { + pack.revert(); + } else { + IOUtils.closeQuietly(pack); + } throw e; } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java index 6e03152cb7..48d01fa575 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java +++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java @@ -30,10 +30,12 @@ import org.apache.poi.openxml4j.exceptions.InvalidOperationException; import org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException; import org.apache.poi.openxml4j.opc.ContentTypes; import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; import org.apache.poi.openxml4j.opc.PackagingURIHelper; import org.apache.poi.openxml4j.opc.TargetMode; +import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource; import org.apache.poi.util.TempFile; import org.junit.jupiter.api.Test; @@ -88,6 +90,26 @@ class TestOPCCompliancePackageModel { ); } + @Test + void testInvalidformatExceptionZipSource() throws IOException { + try (AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource( + POIDataSamples.getOpenXML4JInstance().openResourceAsStream("OPCCompliance_DerivedPartNameFAIL.docx"))) { + assertThrows(InvalidFormatException.class, () -> + OPCPackage.open(source), + "Should fail for invalid file" + ); + } + } + + @Test + void testInvalidformatExceptionFile() { + assertThrows(InvalidFormatException.class, + () -> OPCPackage.open(POIDataSamples.getOpenXML4JInstance(). + getFile("OPCCompliance_DerivedPartNameFAIL.docx"), PackageAccess.READ), + "Should fail for invalid file" + ); + } + /** * Rule M1.12 : Packages shall not contain equivalent part names and package * implementers shall neither create nor recognize packages with equivalent