mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-16 13:06:31 +00:00
Cover encryption and OLE error paths
This commit is contained in:
@@ -43,7 +43,7 @@ final class AgileEncryption
|
||||
throw new Exception('Unsupported XLSX encryption profile.');
|
||||
}
|
||||
|
||||
$xml = simplexml_load_string(substr($encryptionInfo, 8));
|
||||
$xml = @simplexml_load_string(substr($encryptionInfo, 8));
|
||||
if (!$xml instanceof SimpleXMLElement) {
|
||||
throw new Exception('Malformed XLSX encryption information.');
|
||||
}
|
||||
|
||||
@@ -25,6 +25,12 @@ class AgileEncryptionTest extends TestCase
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testCanReadEncryptedWorkbook(): void
|
||||
{
|
||||
self::assertTrue((new Xlsx())->canRead(self::FIXTURE));
|
||||
self::assertFalse((new Xlsx())->canRead('tests/data/Reader/XLS/sample.xls'));
|
||||
}
|
||||
|
||||
public function testListWorksheetMetadataForEncryptedWorkbook(): void
|
||||
{
|
||||
$reader = (new Xlsx())->setEncryptionPassword('open');
|
||||
@@ -68,6 +74,36 @@ class AgileEncryptionTest extends TestCase
|
||||
AgileEncryption::decrypt(AgileEncryption::parse($package['encryptionInfo']), $package['encryptedPackage'], 'password');
|
||||
}
|
||||
|
||||
public function testRejectsMalformedEncryptedPackage(): void
|
||||
{
|
||||
$package = AgileEncryption::encrypt('package', 'password', 128, 'SHA-1', 10);
|
||||
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('Malformed encrypted XLSX package');
|
||||
AgileEncryption::decrypt(AgileEncryption::parse($package['encryptionInfo']), substr($package['encryptedPackage'], 0, 7), 'password');
|
||||
}
|
||||
|
||||
public function testRejectsMalformedEncryptionInfo(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('Malformed XLSX encryption information');
|
||||
AgileEncryption::parse("\x04\x00\x04\x00\x40\x00\x00\x00not XML");
|
||||
}
|
||||
|
||||
public function testEncryptRequiresSupportedProfileAndPassword(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('XLSX encryption password required');
|
||||
AgileEncryption::encrypt('package', '');
|
||||
}
|
||||
|
||||
public function testEncryptRejectsUnsupportedProfile(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('Unsupported XLSX encryption profile');
|
||||
AgileEncryption::encrypt('package', 'password', 64, 'SHA512', 10);
|
||||
}
|
||||
|
||||
public function testRejectsUnsupportedEncryptionInfo(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
@@ -94,4 +130,12 @@ class AgileEncryptionTest extends TestCase
|
||||
self::assertSame('profile test', AgileEncryption::decrypt($info, $package['encryptedPackage'], 'password'));
|
||||
}
|
||||
}
|
||||
|
||||
public function testDecryptsMultipleEncryptedSegments(): void
|
||||
{
|
||||
$plain = str_repeat('x', 4097);
|
||||
$package = AgileEncryption::encrypt($plain, 'password', 128, 'SHA-1', 10);
|
||||
|
||||
self::assertSame($plain, AgileEncryption::decrypt(AgileEncryption::parse($package['encryptionInfo']), $package['encryptedPackage'], 'password'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\OLE;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\Root;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@@ -58,4 +60,46 @@ class OLEPhpunit10Test extends TestCase
|
||||
$ole->stream_open('whatever', 'r', STREAM_REPORT_ERRORS, $openedPath);
|
||||
self::assertSame('OLE stream not found', self::$errorString);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $bbat
|
||||
* @param mixed[] $sbat
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('invalidChainProvider')]
|
||||
public function testChainedStreamRejectsInvalidAllocationChains(array $bbat, array $sbat, int $blockId, ?int $size, string $message): void
|
||||
{
|
||||
$fileHandle = tmpfile();
|
||||
self::assertNotFalse($fileHandle);
|
||||
$ole = new OLE();
|
||||
$ole->_file_handle = $fileHandle;
|
||||
$ole->bbat = $bbat;
|
||||
$ole->sbat = $sbat;
|
||||
$ole->bigBlockSize = 512;
|
||||
$ole->smallBlockSize = 64;
|
||||
$ole->bigBlockThreshold = 4096;
|
||||
$ole->root = new Root(null, null, []);
|
||||
$ole->root->startBlock = 0;
|
||||
$GLOBALS['_OLE_INSTANCES'] = [$ole];
|
||||
$path = 'ole-chainedblockstream://oleInstanceId=0&blockId=' . $blockId . ($size === null ? '' : '&size=' . $size);
|
||||
|
||||
try {
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
(new OLE\ChainedBlockStream())->stream_open($path, 'r', 0, $openedPath);
|
||||
} finally {
|
||||
fclose($fileHandle);
|
||||
$GLOBALS['_OLE_INSTANCES'] = [];
|
||||
}
|
||||
}
|
||||
|
||||
/** @return array<string, array{array<mixed>, array<mixed>, int, ?int, string}> */
|
||||
public static function invalidChainProvider(): array
|
||||
{
|
||||
return [
|
||||
'cyclic root mini-stream chain' => [[0 => 0], [1 => -2], 1, 1, 'Invalid OLE root mini-stream chain.'],
|
||||
'cyclic mini-stream chain' => [[0 => -2], [1 => 1], 1, 1, 'Invalid OLE mini-stream chain.'],
|
||||
'cyclic regular stream chain' => [[0 => 0], [], 0, null, 'Invalid OLE stream chain.'],
|
||||
'missing regular stream allocation' => [[], [], 0, null, 'Invalid OLE stream chain.'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,4 +90,14 @@ class OLETest extends TestCase
|
||||
self::assertSame('0400040040000000', bin2hex(substr($ole->getDataByName('EncryptionInfo'), 0, 8)));
|
||||
self::assertSame(8952, strlen($ole->getDataByName('EncryptedPackage')));
|
||||
}
|
||||
|
||||
public function testNamedStreamMustExist(): void
|
||||
{
|
||||
$ole = new OLE();
|
||||
$ole->read('tests/data/Reader/XLSX/agile-encrypted-excel.xlsx');
|
||||
|
||||
$this->expectException(ReaderException::class);
|
||||
$this->expectExceptionMessage("OLE stream 'Missing' was not found.");
|
||||
$ole->getDataByName('Missing');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user