From: Nick Burch Date: Mon, 21 Jul 2008 19:35:47 +0000 (+0000) Subject: Fix bug #45437 - Detect encrypted word documents, and throw an EncryptedDocumentExcep... X-Git-Tag: REL_3_2_FINAL~229 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=eaf60c3114377f2ce9346c3bb26a569bec2bcdc8;p=poi.git Fix bug #45437 - Detect encrypted word documents, and throw an EncryptedDocumentException instead of a OOM git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@678539 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 021e946e7d..30c53bb213 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 45437 - Detect encrypted word documents, and throw an EncryptedDocumentException instead of a OOM 45404 - New class, hssf.usermodel.HSSFDataFormatter, for formatting numbers and dates in the same way that Excel does 45414 - Don't add too many UncalcedRecords to sheets with charts in them 45398 - Support detecting date formats containing "am/pm" as date times diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 0384580066..b81e839ef4 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 45437 - Detect encrypted word documents, and throw an EncryptedDocumentException instead of a OOM 45404 - New class, hssf.usermodel.HSSFDataFormatter, for formatting numbers and dates in the same way that Excel does 45414 - Don't add too many UncalcedRecords to sheets with charts in them 45398 - Support detecting date formats containing "am/pm" as date times diff --git a/src/java/org/apache/poi/EncryptedDocumentException.java b/src/java/org/apache/poi/EncryptedDocumentException.java new file mode 100644 index 0000000000..4922d1c81d --- /dev/null +++ b/src/java/org/apache/poi/EncryptedDocumentException.java @@ -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); + } +} diff --git a/src/scratchpad/src/org/apache/poi/hslf/exceptions/EncryptedPowerPointFileException.java b/src/scratchpad/src/org/apache/poi/hslf/exceptions/EncryptedPowerPointFileException.java index 77f93a10f6..08eabd223b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/exceptions/EncryptedPowerPointFileException.java +++ b/src/scratchpad/src/org/apache/poi/hslf/exceptions/EncryptedPowerPointFileException.java @@ -1,4 +1,3 @@ - /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -15,19 +14,15 @@ 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); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java index a43357f021..c97d6a8bf0 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java @@ -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 index 0000000000..0d6c169063 Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hwpf/data/PasswordProtected.doc differ diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java index 23681486f3..764b3239dc 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java @@ -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 + } + } }