Skip to content

Commit

Permalink
Prevented fatal errors when opening corrupt files or "doc" files (PHP…
Browse files Browse the repository at this point in the history
…Office#2626)

* Prevented fatal errors when opening corrupt files or "doc" files

* Ran php-cs-fixer

* Fixed phpstan errors

* Updated the change log
  • Loading branch information
mmcev106 authored Aug 8, 2024
1 parent b99230a commit 761280b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- bug: TemplateProcessor fix multiline values [@gimler](https://github.com/gimler) fixing [#268](https://github.com/PHPOffice/PHPWord/issues/268), [#2323](https://github.com/PHPOffice/PHPWord/issues/2323) and [#2486](https://github.com/PHPOffice/PHPWord/issues/2486) in [#2522](https://github.com/PHPOffice/PHPWord/pull/2522)
- 32-bit Problem in PasswordEncoder [@oleibman](https://github.com/oleibman) fixing [#2550](https://github.com/PHPOffice/PHPWord/issues/2550) in [#2551](https://github.com/PHPOffice/PHPWord/pull/2551)
- Typo : Fix hardcoded macro chars in TemplateProcessor method [@glafarge](https://github.com/glafarge) in [#2618](https://github.com/PHPOffice/PHPWord/pull/2618)
- XML Reader : Prevent fatal errors when opening corrupt files or "doc" files [@mmcev106](https://github.com/mmcev106) in [#2626](https://github.com/PHPOffice/PHPWord/pull/2626)

### Miscellaneous

Expand Down
10 changes: 9 additions & 1 deletion src/PhpWord/Shared/XMLReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ public function getDomFromZip($zipFile, $xmlFile)
}

$zip = new ZipArchive();
$zip->open($zipFile);
$openStatus = $zip->open($zipFile);
if ($openStatus !== true) {
/**
* Throw an exception since making further calls on the ZipArchive would cause a fatal error.
* This prevents fatal errors on corrupt archives and attempts to open old "doc" files.
*/
throw new Exception("The archive failed to load with the following error code: $openStatus");
}

$content = $zip->getFromName(ltrim($xmlFile, '/'));
$zip->close();

Expand Down
27 changes: 27 additions & 0 deletions tests/PhpWordTests/Shared/XMLReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ public function testThrowsExceptionOnNonExistingArchive(): void
$reader->getDomFromZip($archiveFile, 'test.xml');
}

/**
* Test that read from invalid archive throws exception.
*/
public function testThrowsExceptionOnZipArchiveOpenErrors(): void
{
/**
* @var string
*/
$tempPath = tempnam(sys_get_temp_dir(), 'PhpWord');

// Simulate a corrupt archive
file_put_contents($tempPath, mt_rand());

$exceptionMessage = null;

try {
$reader = new XMLReader();
$reader->getDomFromZip($tempPath, 'test.xml');
} catch (Exception $e) {
$exceptionMessage = $e->getMessage();
}

self::assertNotNull($exceptionMessage);

unlink($tempPath);
}

/**
* Test elements count.
*/
Expand Down

0 comments on commit 761280b

Please sign in to comment.