ZipPasswordTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PhpZip\Constants\ZipCompressionMethod;
  4. use PhpZip\Constants\ZipEncryptionMethod;
  5. use PhpZip\Exception\InvalidArgumentException;
  6. use PhpZip\Exception\RuntimeException;
  7. use PhpZip\Exception\ZipAuthenticationException;
  8. use PhpZip\Exception\ZipEntryNotFoundException;
  9. use PhpZip\Exception\ZipException;
  10. use PhpZip\Model\ZipInfo;
  11. use PhpZip\ZipFile;
  12. /**
  13. * Tests with zip password.
  14. *
  15. * @internal
  16. *
  17. * @small
  18. */
  19. class ZipPasswordTest extends ZipFileSetTestCase
  20. {
  21. /**
  22. * Test archive password.
  23. *
  24. * @throws ZipException
  25. * @throws \Exception
  26. * @noinspection PhpRedundantCatchClauseInspection
  27. */
  28. public function testSetPassword()
  29. {
  30. if (\PHP_INT_SIZE === 4) { // php 32 bit
  31. $this->setExpectedException(
  32. RuntimeException::class,
  33. 'Traditional PKWARE Encryption is not supported in 32-bit PHP.'
  34. );
  35. }
  36. $password = base64_encode(random_bytes(100));
  37. $badPassword = 'bad password';
  38. // create encryption password with Traditional PKWARE encryption
  39. $zipFile = new ZipFile();
  40. $zipFile->addDir(__DIR__);
  41. $zipFile->setPassword($password, ZipEncryptionMethod::PKWARE);
  42. $zipFile->saveAsFile($this->outputFilename);
  43. $zipFile->close();
  44. static::assertCorrectZipArchive($this->outputFilename, $password);
  45. $zipFile->openFile($this->outputFilename);
  46. // check bad password for Traditional PKWARE encryption
  47. $zipFile->setReadPassword($badPassword);
  48. foreach ($zipFile->getListFiles() as $entryName) {
  49. try {
  50. $zipFile[$entryName];
  51. static::fail('Expected Exception has not been raised.');
  52. } catch (ZipException $e) {
  53. }
  54. }
  55. // check correct password for Traditional PKWARE encryption
  56. $zipFile->setReadPassword($password);
  57. foreach ($zipFile->getAllInfo() as $info) {
  58. static::assertTrue($info->isEncrypted());
  59. static::assertContains('Traditional PKWARE encryption', $info->getEncryptionMethodName());
  60. $decryptContent = $zipFile[$info->getName()];
  61. static::assertNotEmpty($decryptContent);
  62. static::assertContains('<?php', $decryptContent);
  63. }
  64. // change encryption method to WinZip Aes and update file
  65. $zipFile->setPassword($password, ZipEncryptionMethod::WINZIP_AES_256);
  66. $zipFile->saveAsFile($this->outputFilename);
  67. $zipFile->close();
  68. /** @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/ WinZip 99-character limit */
  69. static::assertCorrectZipArchive($this->outputFilename, substr($password, 0, 99));
  70. // check from WinZip AES encryption
  71. $zipFile->openFile($this->outputFilename);
  72. // set bad password WinZip AES
  73. $zipFile->setReadPassword($badPassword);
  74. foreach ($zipFile->getListFiles() as $entryName) {
  75. try {
  76. $zipFile[$entryName];
  77. static::fail('Expected Exception has not been raised.');
  78. } catch (ZipAuthenticationException $ae) {
  79. static::assertNotNull($ae);
  80. }
  81. }
  82. // set correct password WinZip AES
  83. $zipFile->setReadPassword($password);
  84. foreach ($zipFile->getAllInfo() as $info) {
  85. static::assertTrue($info->isEncrypted());
  86. static::assertContains('Deflated', $info->getMethodName());
  87. static::assertContains('WinZip AES-256', $info->getEncryptionMethodName());
  88. $decryptContent = $zipFile[$info->getName()];
  89. static::assertNotEmpty($decryptContent);
  90. static::assertContains('<?php', $decryptContent);
  91. }
  92. // clear password
  93. $zipFile->addFromString('file1', '');
  94. $zipFile->disableEncryption();
  95. $zipFile->addFromString('file2', '');
  96. $zipFile->saveAsFile($this->outputFilename);
  97. $zipFile->close();
  98. static::assertCorrectZipArchive($this->outputFilename);
  99. // check remove password
  100. $zipFile->openFile($this->outputFilename);
  101. foreach ($zipFile->getAllInfo() as $info) {
  102. static::assertFalse($info->isEncrypted());
  103. }
  104. $zipFile->close();
  105. }
  106. /**
  107. * @throws ZipException
  108. * @throws \Exception
  109. */
  110. public function testTraditionalEncryption()
  111. {
  112. if (\PHP_INT_SIZE === 4) { // php 32 bit
  113. $this->setExpectedException(
  114. RuntimeException::class,
  115. 'Traditional PKWARE Encryption is not supported in 32-bit PHP.'
  116. );
  117. }
  118. $password = md5(random_bytes(50));
  119. $zip = new ZipFile();
  120. $zip->addDirRecursive($this->outputDirname);
  121. $zip->setPassword($password, ZipEncryptionMethod::PKWARE);
  122. $zip->saveAsFile($this->outputFilename);
  123. $zip->close();
  124. static::assertCorrectZipArchive($this->outputFilename, $password);
  125. $zip->openFile($this->outputFilename);
  126. $zip->setReadPassword($password);
  127. static::assertFilesResult($zip, array_keys(self::$files));
  128. foreach ($zip->getAllInfo() as $info) {
  129. if (!$info->isFolder()) {
  130. static::assertTrue($info->isEncrypted());
  131. static::assertContains('Traditional PKWARE encryption', $info->getEncryptionMethodName());
  132. }
  133. }
  134. $zip->close();
  135. }
  136. /**
  137. * @dataProvider winZipKeyStrengthProvider
  138. *
  139. * @param int $encryptionMethod
  140. * @param int $bitSize
  141. *
  142. * @throws ZipException
  143. * @throws \Exception
  144. */
  145. public function testWinZipAesEncryption($encryptionMethod, $bitSize)
  146. {
  147. $password = base64_encode(random_bytes(50));
  148. $zip = new ZipFile();
  149. $zip->addDirRecursive($this->outputDirname);
  150. $zip->setPassword($password, $encryptionMethod);
  151. $zip->saveAsFile($this->outputFilename);
  152. $zip->close();
  153. static::assertCorrectZipArchive($this->outputFilename, $password);
  154. $zip->openFile($this->outputFilename);
  155. $zip->setReadPassword($password);
  156. static::assertFilesResult($zip, array_keys(self::$files));
  157. foreach ($zip->getAllInfo() as $info) {
  158. if (!$info->isFolder()) {
  159. static::assertTrue($info->isEncrypted());
  160. static::assertSame($info->getEncryptionMethod(), $encryptionMethod);
  161. static::assertContains('WinZip AES-' . $bitSize, $info->getEncryptionMethodName());
  162. }
  163. }
  164. $zip->close();
  165. }
  166. /**
  167. * @return array
  168. */
  169. public function winZipKeyStrengthProvider()
  170. {
  171. return [
  172. [ZipEncryptionMethod::WINZIP_AES_128, 128],
  173. [ZipEncryptionMethod::WINZIP_AES_192, 192],
  174. [ZipEncryptionMethod::WINZIP_AES_256, 256],
  175. ];
  176. }
  177. /**
  178. * @throws ZipEntryNotFoundException
  179. * @throws ZipException
  180. */
  181. public function testEncryptionEntries()
  182. {
  183. if (\PHP_INT_SIZE === 4) { // php 32 bit
  184. $this->setExpectedException(
  185. RuntimeException::class,
  186. 'Traditional PKWARE Encryption is not supported in 32-bit PHP.'
  187. );
  188. }
  189. $password1 = '353442434235424234';
  190. $password2 = 'adgerhvrwjhqqehtqhkbqrgewg';
  191. $zip = new ZipFile();
  192. $zip->addDir($this->outputDirname);
  193. $zip->setPasswordEntry('.hidden', $password1, ZipEncryptionMethod::PKWARE);
  194. $zip->setPasswordEntry('text file.txt', $password2, ZipEncryptionMethod::WINZIP_AES_256);
  195. $zip->saveAsFile($this->outputFilename);
  196. $zip->close();
  197. $zip->openFile($this->outputFilename);
  198. $zip->setReadPasswordEntry('.hidden', $password1);
  199. $zip->setReadPasswordEntry('text file.txt', $password2);
  200. static::assertFilesResult(
  201. $zip,
  202. [
  203. '.hidden',
  204. 'text file.txt',
  205. 'Текстовый документ.txt',
  206. 'empty dir/',
  207. 'LoremIpsum.txt',
  208. ]
  209. );
  210. $info = $zip->getEntryInfo('.hidden');
  211. static::assertTrue($info->isEncrypted());
  212. static::assertContains('Traditional PKWARE encryption', $info->getEncryptionMethodName());
  213. $info = $zip->getEntryInfo('text file.txt');
  214. static::assertTrue($info->isEncrypted());
  215. static::assertContains('WinZip AES', $info->getEncryptionMethodName());
  216. static::assertFalse($zip->getEntryInfo('Текстовый документ.txt')->isEncrypted());
  217. static::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
  218. $zip->close();
  219. }
  220. /**
  221. * @throws ZipEntryNotFoundException
  222. * @throws ZipException
  223. */
  224. public function testEncryptionEntriesWithDefaultPassword()
  225. {
  226. if (\PHP_INT_SIZE === 4) { // php 32 bit
  227. $this->setExpectedException(
  228. RuntimeException::class,
  229. 'Traditional PKWARE Encryption is not supported in 32-bit PHP.'
  230. );
  231. }
  232. $password1 = '353442434235424234';
  233. $password2 = 'adgerhvrwjhqqehtqhkbqrgewg';
  234. $defaultPassword = ' f f f f f ffff f5 ';
  235. $zip = new ZipFile();
  236. $zip->addDir($this->outputDirname);
  237. $zip->setPassword($defaultPassword);
  238. $zip->setPasswordEntry('.hidden', $password1, ZipEncryptionMethod::PKWARE);
  239. $zip->setPasswordEntry('text file.txt', $password2, ZipEncryptionMethod::WINZIP_AES_256);
  240. $zip->saveAsFile($this->outputFilename);
  241. $zip->close();
  242. $zip->openFile($this->outputFilename);
  243. $zip->setReadPassword($defaultPassword);
  244. $zip->setReadPasswordEntry('.hidden', $password1);
  245. $zip->setReadPasswordEntry('text file.txt', $password2);
  246. static::assertFilesResult(
  247. $zip,
  248. [
  249. '.hidden',
  250. 'text file.txt',
  251. 'Текстовый документ.txt',
  252. 'empty dir/',
  253. 'LoremIpsum.txt',
  254. ]
  255. );
  256. $info = $zip->getEntryInfo('.hidden');
  257. static::assertTrue($info->isEncrypted());
  258. static::assertContains('Traditional PKWARE encryption', $info->getEncryptionMethodName());
  259. $info = $zip->getEntryInfo('text file.txt');
  260. static::assertTrue($info->isEncrypted());
  261. static::assertContains('WinZip AES', $info->getEncryptionMethodName());
  262. $info = $zip->getEntryInfo('Текстовый документ.txt');
  263. static::assertTrue($info->isEncrypted());
  264. static::assertContains('WinZip AES', $info->getEncryptionMethodName());
  265. static::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
  266. $zip->close();
  267. }
  268. /**
  269. * @throws ZipException
  270. */
  271. public function testSetEncryptionMethodInvalid()
  272. {
  273. $this->setExpectedException(InvalidArgumentException::class, 'Encryption method 9999 is not supported.');
  274. $zipFile = new ZipFile();
  275. $encryptionMethod = 9999;
  276. $zipFile['entry'] = 'content';
  277. $zipFile->setPassword('pass', $encryptionMethod);
  278. $zipFile->outputAsString();
  279. }
  280. /**
  281. * @throws ZipEntryNotFoundException
  282. * @throws ZipException
  283. */
  284. public function testEntryPassword()
  285. {
  286. $zipFile = new ZipFile();
  287. $zipFile->setPassword('pass');
  288. $zipFile['file'] = 'content';
  289. static::assertFalse($zipFile->getEntryInfo('file')->isEncrypted());
  290. for ($i = 1; $i <= 10; $i++) {
  291. $zipFile['file' . $i] = 'content';
  292. if ($i < 6) {
  293. $zipFile->setPasswordEntry('file' . $i, 'pass');
  294. static::assertTrue($zipFile->getEntryInfo('file' . $i)->isEncrypted());
  295. } else {
  296. static::assertFalse($zipFile->getEntryInfo('file' . $i)->isEncrypted());
  297. }
  298. }
  299. $zipFile->disableEncryptionEntry('file3');
  300. static::assertFalse($zipFile->getEntryInfo('file3')->isEncrypted());
  301. static::assertTrue($zipFile->getEntryInfo('file2')->isEncrypted());
  302. $zipFile->disableEncryption();
  303. $infoList = $zipFile->getAllInfo();
  304. array_walk(
  305. $infoList,
  306. function (ZipInfo $zipInfo) {
  307. $this->assertFalse($zipInfo->isEncrypted());
  308. }
  309. );
  310. $zipFile->close();
  311. }
  312. /**
  313. * @throws ZipException
  314. */
  315. public function testInvalidEncryptionMethodEntry()
  316. {
  317. $this->setExpectedException(InvalidArgumentException::class, 'Encryption method 99 is not supported.');
  318. $zipFile = new ZipFile();
  319. $zipFile->addFromString('file', 'content', ZipCompressionMethod::STORED);
  320. $zipFile->setPasswordEntry('file', 'pass', ZipCompressionMethod::WINZIP_AES);
  321. }
  322. /**
  323. * @throws ZipEntryNotFoundException
  324. * @throws ZipException
  325. */
  326. public function testArchivePasswordUpdateWithoutSetReadPassword()
  327. {
  328. $zipFile = new ZipFile();
  329. $zipFile['file1'] = 'content';
  330. $zipFile['file2'] = 'content';
  331. $zipFile['file3'] = 'content';
  332. $zipFile->setPassword('password');
  333. $zipFile->saveAsFile($this->outputFilename);
  334. $zipFile->close();
  335. static::assertCorrectZipArchive($this->outputFilename, 'password');
  336. $zipFile->openFile($this->outputFilename);
  337. static::assertCount(3, $zipFile);
  338. foreach ($zipFile->getAllInfo() as $info) {
  339. static::assertTrue($info->isEncrypted());
  340. }
  341. unset($zipFile['file3']);
  342. $zipFile['file4'] = 'content';
  343. $zipFile->rewrite();
  344. static::assertCorrectZipArchive($this->outputFilename, 'password');
  345. static::assertCount(3, $zipFile);
  346. static::assertFalse(isset($zipFile['file3']));
  347. static::assertTrue(isset($zipFile['file4']));
  348. static::assertTrue($zipFile->getEntryInfo('file1')->isEncrypted());
  349. static::assertTrue($zipFile->getEntryInfo('file2')->isEncrypted());
  350. static::assertFalse($zipFile->getEntryInfo('file4')->isEncrypted());
  351. static::assertSame($zipFile['file4'], 'content');
  352. $zipFile->extractTo($this->outputDirname, ['file4']);
  353. static::assertFileExists($this->outputDirname . \DIRECTORY_SEPARATOR . 'file4');
  354. static::assertStringEqualsFile($this->outputDirname . \DIRECTORY_SEPARATOR . 'file4', $zipFile['file4']);
  355. $zipFile->close();
  356. }
  357. /**
  358. * @see https://github.com/Ne-Lexa/php-zip/issues/9
  359. *
  360. * @throws ZipException
  361. * @throws \Exception
  362. */
  363. public function testIssues9()
  364. {
  365. $contents = str_pad('', 1000, 'test;test2;test3' . \PHP_EOL, \STR_PAD_RIGHT);
  366. $password = base64_encode(random_bytes(20));
  367. $zipFile = new ZipFile();
  368. $zipFile
  369. ->addFromString('codes.csv', $contents, ZipCompressionMethod::DEFLATED)
  370. ->setPassword($password, ZipEncryptionMethod::WINZIP_AES_256)
  371. ->saveAsFile($this->outputFilename)
  372. ->close()
  373. ;
  374. static::assertCorrectZipArchive($this->outputFilename, $password);
  375. $zipFile->openFile($this->outputFilename);
  376. $zipFile->setReadPassword($password);
  377. static::assertSame($zipFile['codes.csv'], $contents);
  378. $zipFile->close();
  379. }
  380. /**
  381. * @throws ZipEntryNotFoundException
  382. * @throws ZipException
  383. */
  384. public function testReadAesEncryptedAndRewriteArchive()
  385. {
  386. $file = __DIR__ . '/resources/aes_password_archive.zip';
  387. $password = '1234567890';
  388. $zipFile = new ZipFile();
  389. $zipFile->openFile($file);
  390. $zipFile->setReadPassword($password);
  391. $zipFile->setPassword($password);
  392. $zipFile->setEntryComment('contents.txt', 'comment'); // change entry, but not changed contents
  393. $zipFile->saveAsFile($this->outputFilename);
  394. $zipFile2 = new ZipFile();
  395. $zipFile2->openFile($this->outputFilename);
  396. $zipFile2->setReadPassword($password);
  397. static::assertSame($zipFile2->getListFiles(), $zipFile->getListFiles());
  398. foreach ($zipFile as $name => $contents) {
  399. static::assertNotEmpty($name);
  400. static::assertNotEmpty($contents);
  401. static::assertContains('test contents', $contents);
  402. static::assertSame($zipFile2[$name], $contents);
  403. }
  404. $zipFile2->close();
  405. $zipFile->close();
  406. }
  407. }