diff --git a/fixtures/extract/incorrect-key-openssl.phar b/fixtures/extract/incorrect-key-openssl.phar new file mode 100644 index 000000000..a2cd9b8d0 Binary files /dev/null and b/fixtures/extract/incorrect-key-openssl.phar differ diff --git a/fixtures/extract/incorrect-key-openssl.phar.pubkey b/fixtures/extract/incorrect-key-openssl.phar.pubkey new file mode 100644 index 000000000..e69de29bb diff --git a/fixtures/extract/openssl-no-pubkey.phar b/fixtures/extract/openssl-no-pubkey.phar new file mode 100644 index 000000000..a2cd9b8d0 Binary files /dev/null and b/fixtures/extract/openssl-no-pubkey.phar differ diff --git a/fixtures/extract/openssl.phar b/fixtures/extract/openssl.phar new file mode 100644 index 000000000..a2cd9b8d0 Binary files /dev/null and b/fixtures/extract/openssl.phar differ diff --git a/fixtures/extract/openssl.phar.pubkey b/fixtures/extract/openssl.phar.pubkey new file mode 100644 index 000000000..811450e49 --- /dev/null +++ b/fixtures/extract/openssl.phar.pubkey @@ -0,0 +1,4 @@ +-----BEGIN PUBLIC KEY----- +MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKuZkrHT54KtuBCTrR36+4tibd+2un9b +aLFs3X+RHc/jDCXL8pJATz049ckfcfd2ZCMIzH1PHew8H+EMhy4CbSECAwEAAQ== +-----END PUBLIC KEY----- diff --git a/src/Console/Command/Extract.php b/src/Console/Command/Extract.php index b147e50fb..ea075f829 100644 --- a/src/Console/Command/Extract.php +++ b/src/Console/Command/Extract.php @@ -101,21 +101,29 @@ private static function dumpPhar(string $file, string $tmpDir): string // missing in which case we would not be able to create a Phar instance // as it requires the .phar extension. $tmpFile = $tmpDir.DIRECTORY_SEPARATOR.$alias; + $pubkey = $file.'.pubkey'; + $intermediatePubkey = $tmpFile.'.pubkey'; try { copy($file, $tmpFile, true); + if (file_exists($pubkey)) { + copy($pubkey, $intermediatePubkey, true); + } + $phar = self::createPhar($file, $tmpFile); $phar->extractTo($tmpDir); } catch (Throwable $throwable) { remove($tmpFile); + remove($intermediatePubkey); throw $throwable; } // Cleanup the temporary PHAR. remove($tmpFile); + remove($intermediatePubkey); return $tmpDir; } diff --git a/tests/Console/Command/ExtractTest.php b/tests/Console/Command/ExtractTest.php index f7fdf0710..3a50a0c33 100644 --- a/tests/Console/Command/ExtractTest.php +++ b/tests/Console/Command/ExtractTest.php @@ -105,6 +105,16 @@ private static function pharProvider(): iterable PHP, ], ]; + + yield 'OpenSSL signed PHAR' => [ + self::FIXTURES.'/openssl.phar', + [ + 'index.php' => <<<'PHP' + [ + yield 'corrupted PHAR (was valid; got tempered with)' => [ self::FIXTURES.'/corrupted.phar', InvalidPhar::class, '/^Could not create a Phar or PharData instance for the file .+$/', ]; + + yield 'OpenSSL signed PHAR without a pubkey' => [ + self::FIXTURES.'/openssl-no-pubkey.phar', + InvalidPhar::class, + '/^Could not create a Phar or PharData instance for the file .+$/', + ]; + + yield 'OpenSSL signed PHAR with incorrect pubkey' => [ + self::FIXTURES.'/incorrect-key-openssl.phar', + InvalidPhar::class, + '/^Could not create a Phar or PharData instance for the file .+$/', + ]; } /**