Skip to content
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

fix: Allow to extract PHARs which have a pubkey #967

Merged
merged 1 commit into from
Mar 31, 2023
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
Binary file added fixtures/extract/incorrect-key-openssl.phar
Binary file not shown.
Empty file.
Binary file added fixtures/extract/openssl-no-pubkey.phar
Binary file not shown.
Binary file added fixtures/extract/openssl.phar
Binary file not shown.
4 changes: 4 additions & 0 deletions fixtures/extract/openssl.phar.pubkey
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKuZkrHT54KtuBCTrR36+4tibd+2un9b
aLFs3X+RHc/jDCXL8pJATz049ckfcfd2ZCMIzH1PHew8H+EMhy4CbSECAwEAAQ==
-----END PUBLIC KEY-----
8 changes: 8 additions & 0 deletions src/Console/Command/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
24 changes: 23 additions & 1 deletion tests/Console/Command/ExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ private static function pharProvider(): iterable
PHP,
],
];

yield 'OpenSSL signed PHAR' => [
self::FIXTURES.'/openssl.phar',
[
'index.php' => <<<'PHP'
<?php echo "Hello, world!\n";

PHP,
],
];
}

/**
Expand Down Expand Up @@ -155,11 +165,23 @@ public static function invalidPharPath(): iterable
'/^Could not create a Phar or PharData instance for the file .+$/',
];

yield 'corrupted PHAR (was valid; got tempered with' => [
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 .+$/',
];
}

/**
Expand Down