From: Nick Burch Date: Wed, 9 Mar 2016 10:57:52 +0000 (+0000) Subject: More helpful exception message if POIFS is given a raw XML file (eg an Office 2003... X-Git-Tag: REL_3_15_BETA2~473 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a20e6a38d34973a2977172d5bcdeb447b79397ae;p=poi.git More helpful exception message if POIFS is given a raw XML file (eg an Office 2003 XML file), plus tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734215 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/poifs/common/POIFSConstants.java b/src/java/org/apache/poi/poifs/common/POIFSConstants.java index b732db45d9..1cc5d038d6 100644 --- a/src/java/org/apache/poi/poifs/common/POIFSConstants.java +++ b/src/java/org/apache/poi/poifs/common/POIFSConstants.java @@ -61,4 +61,7 @@ public interface POIFSConstants /** The first 4 bytes of an OOXML file, used in detection */ public static final byte[] OOXML_FILE_HEADER = new byte[] { 0x50, 0x4b, 0x03, 0x04 }; + /** The first 5 bytes of a raw XML file, used in detection */ + public static final byte[] RAW_XML_FILE_HEADER = + new byte[] { 0x3c, 0x3f, 0x78, 0x6d, 0x6c }; } // end public interface POIFSConstants; diff --git a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java index 49e491c5ad..20613896bf 100644 --- a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java +++ b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java @@ -128,6 +128,15 @@ public final class HeaderBlock implements HeaderBlockConstants { throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)"); } + byte[] RAW_XML_FILE_HEADER = POIFSConstants.RAW_XML_FILE_HEADER; + if (_data[0] == RAW_XML_FILE_HEADER[0] && + _data[1] == RAW_XML_FILE_HEADER[1] && + _data[2] == RAW_XML_FILE_HEADER[2] && + _data[3] == RAW_XML_FILE_HEADER[3] && + _data[4] == RAW_XML_FILE_HEADER[4]) { + throw new NotOLE2FileException("The supplied data appears to be a raw XML file. Formats such as Office 2003 XML are not supported"); + } + if (_data[0] == 0x09 && _data[1] == 0x00 && // sid=0x0009 _data[2] == 0x04 && _data[3] == 0x00 && // size=0x0004 _data[4] == 0x00 && _data[5] == 0x00 && // unused diff --git a/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java b/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java index 364aa1a6e2..999f2de1be 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java @@ -32,7 +32,7 @@ import org.junit.runners.Suite; , TestDocumentNode.class , TestDocumentOutputStream.class , TestEmptyDocument.class - , TestOffice2007XMLException.class + , TestOfficeXMLException.class , TestPOIFSDocumentPath.class , TestPOIFSFileSystem.class , TestNPOIFSFileSystem.class diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java b/src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java deleted file mode 100644 index d5d92bd6bb..0000000000 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java +++ /dev/null @@ -1,114 +0,0 @@ -/* ==================================================================== - 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.poifs.filesystem; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.PushbackInputStream; -import java.util.Arrays; - -import org.apache.poi.hssf.HSSFTestDataSamples; - -import junit.framework.TestCase; - -/** - * Class to test that POIFS complains when given an Office 2007 XML document - * - * @author Marc Johnson - */ -public class TestOffice2007XMLException extends TestCase { - - private static final InputStream openSampleStream(String sampleFileName) { - return HSSFTestDataSamples.openSampleFileStream(sampleFileName); - } - public void testXMLException() throws IOException - { - InputStream in = openSampleStream("sample.xlsx"); - - try { - new POIFSFileSystem(in).close(); - fail("expected exception was not thrown"); - } catch(OfficeXmlFileException e) { - // expected during successful test - assertTrue(e.getMessage().indexOf("The supplied data appears to be in the Office 2007+ XML") > -1); - assertTrue(e.getMessage().indexOf("You are calling the part of POI that deals with OLE2 Office Documents") > -1); - } - } - - public void testDetectAsPOIFS() throws IOException { - - // ooxml file isn't - confirmIsPOIFS("SampleSS.xlsx", false); - - // xls file is - confirmIsPOIFS("SampleSS.xls", true); - - // text file isn't - confirmIsPOIFS("SampleSS.txt", false); - } - private void confirmIsPOIFS(String sampleFileName, boolean expectedResult) throws IOException { - InputStream in = new PushbackInputStream(openSampleStream(sampleFileName), 10); - try { - boolean actualResult; - try { - actualResult = POIFSFileSystem.hasPOIFSHeader(in); - } catch (IOException e) { - throw new RuntimeException(e); - } - assertEquals(expectedResult, actualResult); - } finally { - in.close(); - } - } - - public void testFileCorruption() throws Exception { - - // create test InputStream - byte[] testData = { (byte)1, (byte)2, (byte)3 }; - InputStream testInput = new ByteArrayInputStream(testData); - - // detect header - InputStream in = new PushbackInputStream(testInput, 10); - assertFalse(POIFSFileSystem.hasPOIFSHeader(in)); - - // check if InputStream is still intact - byte[] test = new byte[3]; - in.read(test); - assertTrue(Arrays.equals(testData, test)); - assertEquals(-1, in.read()); - } - - - public void testFileCorruptionOPOIFS() throws Exception { - - // create test InputStream - byte[] testData = { (byte)1, (byte)2, (byte)3 }; - InputStream testInput = new ByteArrayInputStream(testData); - - // detect header - InputStream in = new PushbackInputStream(testInput, 10); - assertFalse(OPOIFSFileSystem.hasPOIFSHeader(in)); - - // check if InputStream is still intact - byte[] test = new byte[3]; - in.read(test); - assertTrue(Arrays.equals(testData, test)); - assertEquals(-1, in.read()); - } -} diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java b/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java new file mode 100644 index 0000000000..087fbb2bd3 --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java @@ -0,0 +1,128 @@ +/* ==================================================================== + 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.poifs.filesystem; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; +import java.util.Arrays; + +import org.apache.poi.hssf.HSSFTestDataSamples; + +import junit.framework.TestCase; + +/** + * Class to test that POIFS complains when given an Office 2003 XML + * of Office Open XML (OOXML, 2007+) document + */ +public class TestOfficeXMLException extends TestCase { + + private static final InputStream openSampleStream(String sampleFileName) { + return HSSFTestDataSamples.openSampleFileStream(sampleFileName); + } + public void testOOXMLException() throws IOException + { + InputStream in = openSampleStream("sample.xlsx"); + + try { + new POIFSFileSystem(in).close(); + fail("expected exception was not thrown"); + } catch(OfficeXmlFileException e) { + // expected during successful test + assertTrue(e.getMessage().indexOf("The supplied data appears to be in the Office 2007+ XML") > -1); + assertTrue(e.getMessage().indexOf("You are calling the part of POI that deals with OLE2 Office Documents") > -1); + } + } + public void test2003XMLException() throws IOException + { + InputStream in = openSampleStream("SampleSS.xml"); + + try { + new POIFSFileSystem(in).close(); + fail("expected exception was not thrown"); + } catch(NotOLE2FileException e) { + // expected during successful test + assertTrue(e.getMessage().indexOf("The supplied data appears to be a raw XML file") > -1); + assertTrue(e.getMessage().indexOf("Formats such as Office 2003 XML") > -1); + } + } + + public void testDetectAsPOIFS() throws IOException { + // ooxml file isn't + confirmIsPOIFS("SampleSS.xlsx", false); + + // 2003 xml file isn't + confirmIsPOIFS("SampleSS.xml", false); + + // xls file is + confirmIsPOIFS("SampleSS.xls", true); + + // text file isn't + confirmIsPOIFS("SampleSS.txt", false); + } + private void confirmIsPOIFS(String sampleFileName, boolean expectedResult) throws IOException { + InputStream in = new PushbackInputStream(openSampleStream(sampleFileName), 10); + try { + boolean actualResult; + try { + actualResult = POIFSFileSystem.hasPOIFSHeader(in); + } catch (IOException e) { + throw new RuntimeException(e); + } + assertEquals(expectedResult, actualResult); + } finally { + in.close(); + } + } + + public void testFileCorruption() throws Exception { + + // create test InputStream + byte[] testData = { (byte)1, (byte)2, (byte)3 }; + InputStream testInput = new ByteArrayInputStream(testData); + + // detect header + InputStream in = new PushbackInputStream(testInput, 10); + assertFalse(POIFSFileSystem.hasPOIFSHeader(in)); + + // check if InputStream is still intact + byte[] test = new byte[3]; + in.read(test); + assertTrue(Arrays.equals(testData, test)); + assertEquals(-1, in.read()); + } + + + public void testFileCorruptionOPOIFS() throws Exception { + + // create test InputStream + byte[] testData = { (byte)1, (byte)2, (byte)3 }; + InputStream testInput = new ByteArrayInputStream(testData); + + // detect header + InputStream in = new PushbackInputStream(testInput, 10); + assertFalse(OPOIFSFileSystem.hasPOIFSHeader(in)); + + // check if InputStream is still intact + byte[] test = new byte[3]; + in.read(test); + assertTrue(Arrays.equals(testData, test)); + assertEquals(-1, in.read()); + } +} diff --git a/test-data/spreadsheet/SampleSS.xml b/test-data/spreadsheet/SampleSS.xml new file mode 100644 index 0000000000..45cd58cec1 --- /dev/null +++ b/test-data/spreadsheet/SampleSS.xml @@ -0,0 +1,142 @@ + + + + + Sample Spreadsheet + Spreadsheet for testing + Nick Burch + Testing Sample Formulas + This is a sample spreadsheet, for use when testing things + Nick Burch + 2008-01-04T11:51:36Z + 2008-01-04T11:56:04Z + 14.00 + + + + + + 5580 + 11295 + 360 + 60 + 1 + False + False + + + + + + + + + + Test spreadsheet + + + 2nd row + 2nd row 2nd column + + + This one is red + +
+ + +
+