]> source.dussan.org Git - poi.git/commitdiff
BUG-60284 -- throw EncryptedDocumentException for password protected OldExcel files
authorTim Allison <tallison@apache.org>
Thu, 20 Oct 2016 15:13:16 +0000 (15:13 +0000)
committerTim Allison <tallison@apache.org>
Thu, 20 Oct 2016 15:13:16 +0000 (15:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1765829 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java
test-data/spreadsheet/60284.xls [new file with mode: 0644]

index 605e328a043ceba07cc65ffe9559d2a6bdf247c2..c62a3c226fac07875703f06c37de2a60a3b164bc 100644 (file)
@@ -28,6 +28,7 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 
+import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.hssf.OldExcelFormatException;
 import org.apache.poi.hssf.record.BOFRecord;
 import org.apache.poi.hssf.record.CodepageRecord;
@@ -57,6 +58,9 @@ import org.apache.poi.util.IOUtils;
  * </p>
  */
 public class OldExcelExtractor implements Closeable {
+
+    private final static int FILE_PASS_RECORD_SID = 0x2f;
+
     private RecordInputStream ris;
 
     // sometimes we hold the stream here and thus need to ensure it is closed at some point
@@ -232,7 +236,9 @@ public class OldExcelExtractor implements Closeable {
             ris.nextRecord();
 
             switch (sid) {
-                // Biff 5+ only, no sheet names in older formats
+                case  FILE_PASS_RECORD_SID:
+                    throw new EncryptedDocumentException("Encryption not supported for Old Excel files");
+
                 case OldSheetRecord.sid:
                     OldSheetRecord shr = new OldSheetRecord(ris);
                     shr.setCodePage(codepage);
index 4de860d98eb9216b36198f509d87c318c9485d55..73222ed01a304471422614426003960361d12364 100644 (file)
@@ -32,6 +32,7 @@ import java.io.InputStream;
 import java.io.PrintStream;
 
 import org.apache.poi.EmptyFileException;
+import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
@@ -345,10 +346,25 @@ public final class TestOldExcelExtractor {
                 out.close();
             }
             String string = new String(out.toByteArray(), "UTF-8");
-            assertTrue("Had: " + string, 
+            assertTrue("Had: " + string,
                     string.contains("Table C-13--Lemons"));
         } finally {
             System.setOut(save);
         }
     }
+
+    @Test
+    public void testEncryptionException() throws Exception {
+        //test file derives from Common Crawl
+        File file = HSSFTestDataSamples.getSampleFile("60284.xls");
+        OldExcelExtractor ex = new OldExcelExtractor(file);
+        assertEquals(5, ex.getBiffVersion());
+        assertEquals(5, ex.getFileType());
+        try {
+            ex.getText();
+            fail();
+        } catch (EncryptedDocumentException e) {
+            assertTrue("correct exception thrown", true);
+        }
+    }
 }
diff --git a/test-data/spreadsheet/60284.xls b/test-data/spreadsheet/60284.xls
new file mode 100644 (file)
index 0000000..7e0f9fb
Binary files /dev/null and b/test-data/spreadsheet/60284.xls differ