ZipPasswordTest.php 16 KB

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