]> source.dussan.org Git - poi.git/commitdiff
Fix bug #45437 - Detect encrypted word documents, and throw an EncryptedDocumentExcep...
authorNick Burch <nick@apache.org>
Mon, 21 Jul 2008 19:35:47 +0000 (19:35 +0000)
committerNick Burch <nick@apache.org>
Mon, 21 Jul 2008 19:35:47 +0000 (19:35 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@678539 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/EncryptedDocumentException.java [new file with mode: 0644]
src/scratchpad/src/org/apache/poi/hslf/exceptions/EncryptedPowerPointFileException.java
src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
src/scratchpad/testcases/org/apache/poi/hwpf/data/PasswordProtected.doc [new file with mode: 0644]
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java

index 021e946e7d97e5a53f6accbbae635196aba214c4..30c53bb2132359f1a94994dec5c92cff1db40705 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">45437 - Detect encrypted word documents, and throw an EncryptedDocumentException instead of a OOM</action>
            <action dev="POI-DEVELOPERS" type="add">45404 - New class, hssf.usermodel.HSSFDataFormatter, for formatting numbers and dates in the same way that Excel does</action>
            <action dev="POI-DEVELOPERS" type="fix">45414 - Don't add too many UncalcedRecords to sheets with charts in them</action>
            <action dev="POI-DEVELOPERS" type="fix">45398 - Support detecting date formats containing "am/pm" as date times</action>
index 03845800663770b11eadc97e1afdab86092370c2..b81e839ef469a4c7fe1477342917c753bc36e4b7 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">45437 - Detect encrypted word documents, and throw an EncryptedDocumentException instead of a OOM</action>
            <action dev="POI-DEVELOPERS" type="add">45404 - New class, hssf.usermodel.HSSFDataFormatter, for formatting numbers and dates in the same way that Excel does</action>
            <action dev="POI-DEVELOPERS" type="fix">45414 - Don't add too many UncalcedRecords to sheets with charts in them</action>
            <action dev="POI-DEVELOPERS" type="fix">45398 - Support detecting date formats containing "am/pm" as date times</action>
diff --git a/src/java/org/apache/poi/EncryptedDocumentException.java b/src/java/org/apache/poi/EncryptedDocumentException.java
new file mode 100644 (file)
index 0000000..4922d1c
--- /dev/null
@@ -0,0 +1,24 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi;
+
+public class EncryptedDocumentException extends IllegalStateException
+{
+       public EncryptedDocumentException(String s) {
+               super(s);
+       }
+}
index 77f93a10f6eba215c613b519ab8e9541b7d38b37..08eabd223b6056bfeb2a30d4174c96ab31b5ef8b 100644 (file)
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
-
-
 package org.apache.poi.hslf.exceptions;
 
+import org.apache.poi.EncryptedDocumentException;
+
 /**
  * This exception is thrown when we try to open a PowerPoint file, and
  *  discover that it is encrypted
- *
- * @author Nick Burch
  */
-
-public class EncryptedPowerPointFileException extends IllegalStateException
+public class EncryptedPowerPointFileException extends EncryptedDocumentException
 {
        public EncryptedPowerPointFileException(String s) {
                super(s);
index a43357f02146ea1950eb4b9914e04f2b6bcaf3d4..c97d6a8bf07a52ac45b839e99d6d6130013d34b3 100644 (file)
@@ -28,6 +28,7 @@ import java.io.ByteArrayInputStream;
 
 import java.util.Iterator;
 
+import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.POIDocument;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@@ -174,9 +175,13 @@ public class HWPFDocument extends POIDocument
     
     directory.createDocumentInputStream("WordDocument").read(_mainStream);
 
-    // use the fib to determine the name of the table stream.
+    // Create our FIB, and check for the doc being encrypted
     _fib = new FileInformationBlock(_mainStream);
+    if(_fib.isFEncrypted()) {
+       throw new EncryptedDocumentException("Cannot process encrypted word files!");
+    }
 
+    // use the fib to determine the name of the table stream.
     String name = "0Table";
     if (_fib.isFWhichTblStm())
     {
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/data/PasswordProtected.doc b/src/scratchpad/testcases/org/apache/poi/hwpf/data/PasswordProtected.doc
new file mode 100644 (file)
index 0000000..0d6c169
Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hwpf/data/PasswordProtected.doc differ
index 23681486f32b9945fa184a74237662faa02dfc99..764b3239dcd46c0759cf418c18f462c550402188 100644 (file)
@@ -22,6 +22,7 @@ import java.io.FileOutputStream;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.hwpf.HWPFDocument;
 import org.apache.poi.hwpf.model.StyleSheet;
 
@@ -138,4 +139,18 @@ public class TestProblems extends TestCase {
                
                assertEquals(newLength, totalLength - deletedLength);
        }
+       
+       /**
+        * With an encrypted file, we should give a suitable
+        *  exception, and not OOM
+        */
+       public void testEncryptedFile() throws Exception {
+               try {
+                       new HWPFDocument(new FileInputStream(
+                       new File(dirname, "PasswordProtected.doc")));
+                       fail();
+               } catch(EncryptedDocumentException e) {
+                       // Good
+               }
+       }
 }