Skip to content

Commit 761280b

Browse files
authored
Prevented fatal errors when opening corrupt files or "doc" files (#2626)
* Prevented fatal errors when opening corrupt files or "doc" files * Ran php-cs-fixer * Fixed phpstan errors * Updated the change log
1 parent b99230a commit 761280b

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

docs/changes/2.x/2.0.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- 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)
1818
- 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)
1919
- Typo : Fix hardcoded macro chars in TemplateProcessor method [@glafarge](https://github.com/glafarge) in [#2618](https://github.com/PHPOffice/PHPWord/pull/2618)
20+
- 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)
2021

2122
### Miscellaneous
2223

src/PhpWord/Shared/XMLReader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ public function getDomFromZip($zipFile, $xmlFile)
6161
}
6262

6363
$zip = new ZipArchive();
64-
$zip->open($zipFile);
64+
$openStatus = $zip->open($zipFile);
65+
if ($openStatus !== true) {
66+
/**
67+
* Throw an exception since making further calls on the ZipArchive would cause a fatal error.
68+
* This prevents fatal errors on corrupt archives and attempts to open old "doc" files.
69+
*/
70+
throw new Exception("The archive failed to load with the following error code: $openStatus");
71+
}
72+
6573
$content = $zip->getFromName(ltrim($xmlFile, '/'));
6674
$zip->close();
6775

tests/PhpWordTests/Shared/XMLReaderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,33 @@ public function testThrowsExceptionOnNonExistingArchive(): void
8585
$reader->getDomFromZip($archiveFile, 'test.xml');
8686
}
8787

88+
/**
89+
* Test that read from invalid archive throws exception.
90+
*/
91+
public function testThrowsExceptionOnZipArchiveOpenErrors(): void
92+
{
93+
/**
94+
* @var string
95+
*/
96+
$tempPath = tempnam(sys_get_temp_dir(), 'PhpWord');
97+
98+
// Simulate a corrupt archive
99+
file_put_contents($tempPath, mt_rand());
100+
101+
$exceptionMessage = null;
102+
103+
try {
104+
$reader = new XMLReader();
105+
$reader->getDomFromZip($tempPath, 'test.xml');
106+
} catch (Exception $e) {
107+
$exceptionMessage = $e->getMessage();
108+
}
109+
110+
self::assertNotNull($exceptionMessage);
111+
112+
unlink($tempPath);
113+
}
114+
88115
/**
89116
* Test elements count.
90117
*/

0 commit comments

Comments
 (0)