File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
tests/PhpWordTests/Shared Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,15 @@ public function getDomFromZip($zipFile, $xmlFile)
61
61
}
62
62
63
63
$ 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
+
65
73
$ content = $ zip ->getFromName (ltrim ($ xmlFile , '/ ' ));
66
74
$ zip ->close ();
67
75
Original file line number Diff line number Diff line change @@ -85,6 +85,30 @@ public function testThrowsExceptionOnNonExistingArchive(): void
85
85
$ reader ->getDomFromZip ($ archiveFile , 'test.xml ' );
86
86
}
87
87
88
+ /**
89
+ * Test that read from invalid archive throws exception.
90
+ */
91
+ public function testThrowsExceptionOnZipArchiveOpenErrors (): void
92
+ {
93
+ $ tempPath = tempnam (sys_get_temp_dir (), 'PhpWord ' );
94
+
95
+ // Simulate a corrupt archive
96
+ file_put_contents ($ tempPath , rand ());
97
+
98
+ $ exceptionMessage = null ;
99
+ try {
100
+ $ reader = new XMLReader ();
101
+ $ reader ->getDomFromZip ($ tempPath , 'test.xml ' );
102
+ }
103
+ catch (\Exception $ e ){
104
+ $ exceptionMessage = $ e ->getMessage ();
105
+ }
106
+
107
+ $ this ->assertNotNull ($ exceptionMessage );
108
+
109
+ unlink ($ tempPath );
110
+ }
111
+
88
112
/**
89
113
* Test elements count.
90
114
*/
You can’t perform that action at this time.
0 commit comments