Skip to content

Prevented fatal errors when opening corrupt files or "doc" files #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading