Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into refactor/safe-phar
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Oct 8, 2023
2 parents 74bfa5e + 398f62a commit 9981314
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 94 deletions.
9 changes: 0 additions & 9 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use KevinGH\Box\Compactor\PhpScoper;
use KevinGH\Box\Compactor\Placeholder;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\PhpScoper\NullScoper;
use KevinGH\Box\PhpScoper\Scoper;
use Phar;
Expand Down Expand Up @@ -96,14 +95,6 @@ public static function create(string $pharFilePath, int $pharFlags = 0, ?string
);
}

public static function createFromPharaoh(SafePhar $pharaoh): self
{
return new self(
$pharaoh->getPhar(),
$pharaoh->getFile(),
);
}

public function startBuffering(): void
{
Assert::false($this->buffering, 'The buffering must be ended before starting it again');
Expand Down
10 changes: 6 additions & 4 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use KevinGH\Box\Console\PharInfoRenderer;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\PharDiff;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\Phar\PharInfo;
use PharFileInfo;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down Expand Up @@ -234,7 +236,7 @@ private function compareContents(PharDiff $diff, IO $io): int
/**
* @param list<non-empty-string> $paths
*/
private static function renderPaths(string $symbol, SafePhar $phar, array $paths, IO $io): void
private static function renderPaths(string $symbol, PharInfo $pharInfo, array $paths, IO $io): void
{
$bufferedOutput = new BufferedOutput(
$io->getVerbosity(),
Expand All @@ -244,7 +246,7 @@ private static function renderPaths(string $symbol, SafePhar $phar, array $paths

PharInfoRenderer::renderContent(
$bufferedOutput,
$phar,
$pharInfo,
false,
false,
);
Expand All @@ -260,7 +262,7 @@ private static function renderPaths(string $symbol, SafePhar $phar, array $paths
$io->writeln($lines);
}

private static function renderArchive(string $fileName, SafePhar $pharInfo, IO $io): void
private static function renderArchive(string $fileName, PharInfo $pharInfo, IO $io): void
{
$io->writeln(
sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static function dumpPhar(string $file, string $tmpDir): string
$pubKeyContent = FS::getFileContents($pubKey);
}

$phar = PharFactory::create($tmpFile);
$phar = PharFactory::create($tmpFile, $file);
$pharMeta = PharMeta::fromPhar($phar, $pubKeyContent);

$phar->extractTo($tmpDir);
Expand Down
8 changes: 4 additions & 4 deletions src/Console/Command/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use KevinGH\Box\Console\PharInfoRenderer;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\Phar\PharInfo;
use Phar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -130,7 +130,7 @@ public static function showInfo(string $file, IO $io): int
$mode = $io->getOption(self::MODE_OPT)->asStringChoice(self::MODES);

try {
$pharInfo = new SafePhar($file);
$pharInfo = new PharInfo($file);

return self::showPharInfo(
$pharInfo,
Expand Down Expand Up @@ -188,7 +188,7 @@ private static function showGlobalInfo(IO $io): int
}

private static function showPharInfo(
SafePhar $pharInfo,
PharInfo $pharInfo,
bool $content,
int|false $maxDepth,
bool $indent,
Expand All @@ -210,7 +210,7 @@ private static function showPharInfo(
return ExitCode::SUCCESS;
}

private static function showPharMeta(SafePhar $pharInfo, IO $io): void
private static function showPharMeta(PharInfo $pharInfo, IO $io): void
{
$io->writeln(
sprintf(
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Command/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Fidry\Console\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\Phar\PharInfo;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Filesystem\Path;
use Throwable;
Expand Down Expand Up @@ -117,10 +117,10 @@ private static function verifyPhar(string $pharFilePath): array
$throwable = null;

try {
$phar = new SafePhar($pharFilePath);
$pharInfo = new PharInfo($pharFilePath);

$verified = true;
$signature = $phar->getSignature();
$signature = $pharInfo->getSignature();
} catch (Throwable $throwable) {
// Continue
}
Expand Down
12 changes: 6 additions & 6 deletions src/Console/PharInfoRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Fidry\Console\Input\IO;
use KevinGH\Box\NotInstantiable;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\SafePhar;
use KevinGH\Box\Phar\PharInfo;
use SplFileInfo;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Path;
Expand All @@ -43,7 +43,7 @@ final class PharInfoRenderer

private const INDENT_SIZE = 2;

public static function renderCompression(SafePhar $pharInfo, IO $io): void
public static function renderCompression(PharInfo $pharInfo, IO $io): void
{
$io->writeln(
sprintf(
Expand Down Expand Up @@ -94,7 +94,7 @@ public static function renderCompression(SafePhar $pharInfo, IO $io): void
}
}

public static function renderSignature(SafePhar $pharInfo, IO $io): void
public static function renderSignature(PharInfo $pharInfo, IO $io): void
{
$signature = $pharInfo->getSignature();

Expand All @@ -118,7 +118,7 @@ public static function renderSignature(SafePhar $pharInfo, IO $io): void
);
}

public static function renderMetadata(SafePhar $pharInfo, IO $io): void
public static function renderMetadata(PharInfo $pharInfo, IO $io): void
{
$metadata = $pharInfo->getNormalizedMetadata();

Expand All @@ -130,7 +130,7 @@ public static function renderMetadata(SafePhar $pharInfo, IO $io): void
}
}

public static function renderContentsSummary(SafePhar $pharInfo, IO $io): void
public static function renderContentsSummary(PharInfo $pharInfo, IO $io): void
{
$count = array_filter($pharInfo->getFilesCompressionCount());
$totalCount = array_sum($count);
Expand All @@ -152,7 +152,7 @@ public static function renderContentsSummary(SafePhar $pharInfo, IO $io): void
*/
public static function renderContent(
OutputInterface $output,
SafePhar $pharInfo,
PharInfo $pharInfo,
int|false $maxDepth,
bool $indent,
): void {
Expand Down
93 changes: 58 additions & 35 deletions src/Phar/InvalidPhar.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,40 @@

final class InvalidPhar extends PharError
{
public static function fileNotLocal(string $file): self
{
public static function fileNotLocal(
string $file,
?string $originalFile = null,
): self {
// Covers:
// https://github.com/php/php-src/blob/930db2b2d315b2acc917706cf76bed8b09f94b79/ext/phar/phar.c#L1328
return new self(
sprintf(
'Could not create a Phar or PharData instance for the file path "%s". PHAR objects can only be created from local files.',
'Could not create a Phar or PharData instance for the file path "%s"%s. PHAR objects can only be created from local files.',
$file,
null === $originalFile
? ''
: sprintf(
' (of the original file "%s")',
$originalFile,
),
),
);
}

public static function fileNotFound(string $file): self
{
public static function fileNotFound(
string $file,
?string $originalFile = null,
): self {
return new self(
sprintf(
'Could not find the file "%s".',
'Could not find the file "%s"%s.',
$file,
null === $originalFile
? ''
: sprintf(
' (of the original file "%s")',
$originalFile,
),
),
);
}
Expand All @@ -57,32 +73,42 @@ public static function fileNotReadable(string $file): self
);
}

public static function forPhar(string $file, ?Throwable $previous): self
{
public static function forPhar(
string $file,
?string $originalFile,
?Throwable $previous,
): self {
return new self(
self::mapThrowableToErrorMessage($file, $previous, false),
self::mapThrowableToErrorMessage($file, $originalFile, $previous, false),
previous: $previous,
);
}

public static function forPharData(string $file, ?Throwable $previous): self
{
public static function forPharData(
string $file,
?string $originalFile,
?Throwable $previous,
): self {
return new self(
self::mapThrowableToErrorMessage($file, $previous, true),
self::mapThrowableToErrorMessage($file, $originalFile, $previous, true),
previous: $previous,
);
}

public static function forPharAndPharData(string $file, ?Throwable $previous): self
{
public static function forPharAndPharData(
string $file,
?string $originalFile,
?Throwable $previous,
): self {
return new self(
self::mapThrowableToErrorMessage($file, $previous, null),
self::mapThrowableToErrorMessage($file, $originalFile, $previous, null),
previous: $previous,
);
}

private static function mapThrowableToErrorMessage(
string $file,
?string $originalFile,
?Throwable $throwable,
?bool $isPharData,
): string {
Expand All @@ -92,15 +118,24 @@ private static function mapThrowableToErrorMessage(
$pharObject = $isPharData ? 'PharData' : 'Phar';
}

$message = $throwable->getMessage();
$errorMessageStart = sprintf(
'Could not create a %s instance for the file "%s"%s',
$pharObject,
$file,
null === $originalFile
? ''
: sprintf(
' (of the original file "%s")',
$originalFile,
),
);
$message = $throwable?->getMessage() ?? '';

if ($throwable instanceof UnexpectedValueException) {
// https://github.com/php/php-src/blob/930db2b2d315b2acc917706cf76bed8b09f94b79/ext/phar/phar.c#L1330
if (str_ends_with($message, 'file extension (or combination) not recognised or the directory does not exist')) {
return sprintf(
'Could not create a %s instance for the file "%s". The file must have the extension "%s".',
$pharObject,
$file,
$errorMessageStart.'. The file must have the extension "%s".',
$isPharData ? '.zip", ".tar", ".tar.bz2" or ".tar.gz' : '.phar',
);
}
Expand All @@ -111,9 +146,7 @@ private static function mapThrowableToErrorMessage(
preg_match('/^internal corruption of phar \".+\" \((?<reason>.+)\)$/', $message, $matches);

return sprintf(
'Could not create a %s instance for the file "%s". The archive is corrupted: %s.',
$pharObject,
$file,
$errorMessageStart.'. The archive is corrupted: %s.',
ucfirst($matches['reason']),
);
}
Expand All @@ -122,11 +155,7 @@ private static function mapThrowableToErrorMessage(
// https://github.com/php/php-src/blob/930db2b2d315b2acc917706cf76bed8b09f94b79/ext/phar/phar.c#L892
// https://github.com/php/php-src/blob/930db2b2d315b2acc917706cf76bed8b09f94b79/ext/phar/phar.c#L903
if (str_contains($message, ' openssl signature ')) {
return sprintf(
'Could not create a %s instance for the file "%s". The OpenSSL signature could not be read or verified.',
$pharObject,
$file,
);
return $errorMessageStart.'. The OpenSSL signature could not be read or verified.';
}

// https://github.com/php/php-src/blob/930db2b2d315b2acc917706cf76bed8b09f94b79/ext/phar/phar.c#L1002
Expand All @@ -137,18 +166,12 @@ private static function mapThrowableToErrorMessage(
|| str_contains($message, ' signature could not be verified')
|| str_contains($message, ' has a broken or unsupported signature')
) {
return sprintf(
'Could not create a %s instance for the file "%s". The archive signature is broken.',
$pharObject,
$file,
);
return $errorMessageStart.'. The archive signature is broken.';
}
}

return sprintf(
'Could not create a %s instance for the file "%s": %s',
$pharObject,
$file,
$errorMessageStart.': %s',
$message,
);
}
Expand Down
Loading

0 comments on commit 9981314

Please sign in to comment.