BUG-60284 -- throw EncryptedDocumentException for password protected OldExcel files

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1765829 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim Allison 2016-10-20 15:13:16 +00:00
parent 5232c0e1a6
commit 126c6c93a8
3 changed files with 24 additions and 2 deletions

View File

@ -28,6 +28,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.OldExcelFormatException; import org.apache.poi.hssf.OldExcelFormatException;
import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.CodepageRecord; import org.apache.poi.hssf.record.CodepageRecord;
@ -57,6 +58,9 @@ import org.apache.poi.util.IOUtils;
* </p> * </p>
*/ */
public class OldExcelExtractor implements Closeable { public class OldExcelExtractor implements Closeable {
private final static int FILE_PASS_RECORD_SID = 0x2f;
private RecordInputStream ris; private RecordInputStream ris;
// sometimes we hold the stream here and thus need to ensure it is closed at some point // 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(); ris.nextRecord();
switch (sid) { 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: case OldSheetRecord.sid:
OldSheetRecord shr = new OldSheetRecord(ris); OldSheetRecord shr = new OldSheetRecord(ris);
shr.setCodePage(codepage); shr.setCodePage(codepage);

View File

@ -32,6 +32,7 @@ import java.io.InputStream;
import java.io.PrintStream; import java.io.PrintStream;
import org.apache.poi.EmptyFileException; import org.apache.poi.EmptyFileException;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
@ -345,10 +346,25 @@ public final class TestOldExcelExtractor {
out.close(); out.close();
} }
String string = new String(out.toByteArray(), "UTF-8"); String string = new String(out.toByteArray(), "UTF-8");
assertTrue("Had: " + string, assertTrue("Had: " + string,
string.contains("Table C-13--Lemons")); string.contains("Table C-13--Lemons"));
} finally { } finally {
System.setOut(save); 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);
}
}
} }

Binary file not shown.