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
This commit is contained in:
Nick Burch 2008-07-21 19:35:47 +00:00
parent d4655fe1bb
commit 9500a5f31e
7 changed files with 50 additions and 9 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! --> <!-- Don't forget to update status.xml too! -->
<release version="3.1.1-alpha1" date="2008-??-??"> <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="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">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> <action dev="POI-DEVELOPERS" type="fix">45398 - Support detecting date formats containing "am/pm" as date times</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! --> <!-- Don't forget to update changes.xml too! -->
<changes> <changes>
<release version="3.1.1-alpha1" date="2008-??-??"> <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="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">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> <action dev="POI-DEVELOPERS" type="fix">45398 - Support detecting date formats containing "am/pm" as date times</action>

View File

@ -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);
}
}

View File

@ -1,4 +1,3 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
@ -15,19 +14,15 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hslf.exceptions; package org.apache.poi.hslf.exceptions;
import org.apache.poi.EncryptedDocumentException;
/** /**
* This exception is thrown when we try to open a PowerPoint file, and * This exception is thrown when we try to open a PowerPoint file, and
* discover that it is encrypted * discover that it is encrypted
*
* @author Nick Burch
*/ */
public class EncryptedPowerPointFileException extends EncryptedDocumentException
public class EncryptedPowerPointFileException extends IllegalStateException
{ {
public EncryptedPowerPointFileException(String s) { public EncryptedPowerPointFileException(String s) {
super(s); super(s);

View File

@ -28,6 +28,7 @@ import java.io.ByteArrayInputStream;
import java.util.Iterator; import java.util.Iterator;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIDocument; import org.apache.poi.POIDocument;
import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@ -174,9 +175,13 @@ public class HWPFDocument extends POIDocument
directory.createDocumentInputStream("WordDocument").read(_mainStream); 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); _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"; String name = "0Table";
if (_fib.isFWhichTblStm()) if (_fib.isFWhichTblStm())
{ {

View File

@ -22,6 +22,7 @@ import java.io.FileOutputStream;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.StyleSheet; import org.apache.poi.hwpf.model.StyleSheet;
@ -138,4 +139,18 @@ public class TestProblems extends TestCase {
assertEquals(newLength, totalLength - deletedLength); 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
}
}
} }