From 69c3311a98bad230ab3173645fc41875c012972c Mon Sep 17 00:00:00 2001 From: Tim Allison Date: Mon, 20 Mar 2017 20:47:15 +0000 Subject: [PATCH] 60881 and 60891 -- on further look, no need to throw an exception for an encrypted xlsb. On the second, let's fix readFully to read fully. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1787846 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/stress/XSSFBFileHandler.java | 2 +- .../poi/util/LittleEndianInputStream.java | 21 +++++++++- .../poi/poifs/crypt/TestSecureTempZip.java | 37 ++++++++++++++++++ test-data/spreadsheet/protected_passtika.xlsb | Bin 0 -> 14336 bytes 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 test-data/spreadsheet/protected_passtika.xlsb diff --git a/src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java b/src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java index 02c5193d4..0d71de7e8 100644 --- a/src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java @@ -33,7 +33,7 @@ public class XSSFBFileHandler extends AbstractFileHandler { static { //add expected failures here: -// AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.add("spreadsheet/Simple.xlsb"); + AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.add("spreadsheet/protected_passtika.xlsb"); } @Override diff --git a/src/java/org/apache/poi/util/LittleEndianInputStream.java b/src/java/org/apache/poi/util/LittleEndianInputStream.java index 3109f88b2..428b598d7 100644 --- a/src/java/org/apache/poi/util/LittleEndianInputStream.java +++ b/src/java/org/apache/poi/util/LittleEndianInputStream.java @@ -28,6 +28,9 @@ import java.io.InputStream; * by this class is consistent with that of the inner stream. */ public class LittleEndianInputStream extends FilterInputStream implements LittleEndianInput { + + private static final int EOF = -1; + public LittleEndianInputStream(InputStream is) { super(is); } @@ -128,12 +131,28 @@ public class LittleEndianInputStream extends FilterInputStream implements Little @Override public void readFully(byte[] buf, int off, int len) { try { - checkEOF(read(buf, off, len), len); + checkEOF(_read(buf, off, len), len); } catch (IOException e) { throw new RuntimeException(e); } } + //Makes repeated calls to super.read() until length is read or EOF is reached + private int _read(byte[] buffer, int offset, int length) throws IOException { + //lifted directly from org.apache.commons.io.IOUtils 2.4 + int remaining = length; + while (remaining > 0) { + int location = length - remaining; + int count = read(buffer, offset + location, remaining); + if (EOF == count) { // EOF + break; + } + remaining -= count; + } + + return length - remaining; + } + @Override public void readPlain(byte[] buf, int off, int len) { readFully(buf, off, len); diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java index e6224adf4..9fb149dce 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java @@ -32,6 +32,7 @@ import org.apache.poi.openxml4j.util.ZipEntrySource; import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor; import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.xmlbeans.XmlException; @@ -79,4 +80,40 @@ public class TestSecureTempZip { poifs.close(); fis.close(); } + + /** + * Test case for #59841 - this is an example on how to use encrypted temp files, + * which are streamed into POI opposed to having everything in memory + */ + @Test + public void protectedXLSBZip() throws IOException, GeneralSecurityException, XmlException, OpenXML4JException { + File tikaProt = XSSFTestDataSamples.getSampleFile("protected_passtika.xlsb"); + FileInputStream fis = new FileInputStream(tikaProt); + POIFSFileSystem poifs = new POIFSFileSystem(fis); + EncryptionInfo ei = new EncryptionInfo(poifs); + Decryptor dec = ei.getDecryptor(); + boolean passOk = dec.verifyPassword("tika"); + assertTrue(passOk); + + // extract encrypted ooxml file and write to custom encrypted zip file + InputStream is = dec.getDataStream(poifs); + + // provide ZipEntrySource to poi which decrypts on the fly + ZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(is); + + // test the source + OPCPackage opc = OPCPackage.open(source); + String expected = "You can't see me"; + + XSSFBEventBasedExcelExtractor extractor = new XSSFBEventBasedExcelExtractor(opc); + extractor.setIncludeSheetNames(false); + String txt = extractor.getText(); + assertEquals(expected, txt.trim()); + + extractor.close(); + opc.close(); + poifs.close(); + fis.close(); + } + } diff --git a/test-data/spreadsheet/protected_passtika.xlsb b/test-data/spreadsheet/protected_passtika.xlsb new file mode 100644 index 0000000000000000000000000000000000000000..63405f1b105235d83a0278a36a22c52a764d5aa6 GIT binary patch literal 14336 zcmeHtWmKG7vS?$$6D(MOK;!Q2?(PJFHr{AMaCb;AzlUi9f$xB0JnFjfP23iM}YR> ze!XJ>0Pw%P-`?Hb{TfDwc2J-Q9r_RaFVMgZ^o)p5d%rNFuKIQwb1T=;D49~Gysm!9|S-S zkOnwFTNi*Q)LMTY@Sz^_OC*?|{NG@O9$PpkcM1-cM*?7p0tzs$=0CVPLAiqf763JX zDRjgR`m}<+|1$tWv(PX8u=by=hs~ox5rypM%6kAQC?^OMH33dPID-JLKd1iGlMtE_ zfANRK(LVyQZnPhAlW$=BXn(&(2P&frl%oyQ3J$-Y_>+As06g3;{(n|}7-^;7=)>|k zEJk5AfwjN#z^^H;{Op8NuWpyMHtQh!2b*l2tn zjRqEg+WnUSc2|JyxBjh#5g>=+Kg$ao&^4Mr@)ay@ ztp0BO&uIE%WJ5Ro9sJ_|XXt4`wO|V6<_gu3BeV~O($a$>?EJ8J@&?F2fBXP(04KC% zgFcx7+yEZv*<_$SE@+Sa$Cw0ElTy$b*sM5ooE@rdcIXHjfEU2=W1TNR1xoMF+U0L` z^;7nLbk<*Xg0;Wkk2+F@%INfK`k%JM(EJ0-9k6=?%%lJ52IvCVz3~@^Kk}D{Y8a;9 zKL;cLxF7ismal$)SN;8NC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