Validate that OLE file contains a workbook object (ie. isn't a doc or a ppt file)

This commit is contained in:
MarkBaker
2023-04-04 00:03:16 +02:00
parent 5da2f6ec42
commit ee1075dc09
4 changed files with 33 additions and 3 deletions
+5 -2
View File
@@ -430,7 +430,7 @@ class Xls extends BaseReader
*/
public function canRead(string $filename): bool
{
if (!File::testFileNoThrow($filename)) {
if (File::testFileNoThrow($filename) === false) {
return false;
}
@@ -440,6 +440,9 @@ class Xls extends BaseReader
// get excel data
$ole->read($filename);
if ($ole->wrkbook === null) {
throw new Exception('The filename ' . $filename . ' is not recognised as a Spreadsheet file');
}
return true;
} catch (PhpSpreadsheetException $e) {
@@ -449,7 +452,7 @@ class Xls extends BaseReader
public function setCodepage(string $codepage): void
{
if (!CodePage::validate($codepage)) {
if (CodePage::validate($codepage) === false) {
throw new PhpSpreadsheetException('Unknown codepage: ' . $codepage);
}
+1 -1
View File
@@ -134,7 +134,7 @@ class OLERead
$bbdBlocks = $this->numBigBlockDepotBlocks;
if ($this->numExtensionBlocks != 0) {
if ($this->numExtensionBlocks !== 0) {
$bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS) / 4;
}
@@ -108,6 +108,33 @@ class IOFactoryTest extends TestCase
];
}
public function testIdentifyInvalid(): void
{
$file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc';
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('Unable to identify a reader for this file');
IOFactory::identify($file);
}
public function testCreateInvalid(): void
{
$file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc';
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('Unable to identify a reader for this file');
IOFactory::createReaderForFile($file);
}
public function testLoadInvalid(): void
{
$file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc';
$this->expectException(ReaderException::class);
$this->expectExceptionMessage('Unable to identify a reader for this file');
IOFactory::load($file);
}
public function testFormatAsExpected(): void
{
$fileName = 'samples/templates/30template.xls';
Binary file not shown.