From 70e559bec1878885efb433e07d0d1425b5128091 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 5 May 2020 08:24:52 +0200 Subject: [PATCH] Reduce the number of unprefixed whitelisted functions in the PHAR --- compiler/patches/Consistency.diff | 45 +++++++++++++++++++ compiler/patches/Wrapper.diff | 27 +++++++++++ compiler/src/Console/CompileCommand.php | 38 ++++++++++++++++ compiler/tests/Console/CompileCommandTest.php | 2 +- 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 compiler/patches/Consistency.diff create mode 100644 compiler/patches/Wrapper.diff diff --git a/compiler/patches/Consistency.diff b/compiler/patches/Consistency.diff new file mode 100644 index 0000000000..7b279857ed --- /dev/null +++ b/compiler/patches/Consistency.diff @@ -0,0 +1,45 @@ +--- ../vendor/hoa/consistency/Consistency.php 2017-05-02 14:18:12.000000000 +0200 ++++ ../vendor/hoa/consistency/Consistency2.php 2020-05-05 08:28:35.000000000 +0200 +@@ -319,42 +319,6 @@ + $define('STREAM_CRYPTO_METHOD_ANY_CLIENT', 63); + } + +-if (!function_exists('curry')) { +- /** +- * Curry. +- * Example: +- * $c = curry('str_replace', …, …, 'foobar'); +- * var_dump($c('foo', 'baz')); // bazbar +- * $c = curry('str_replace', 'foo', 'baz', …); +- * var_dump($c('foobarbaz')); // bazbarbaz +- * Nested curries also work: +- * $c1 = curry('str_replace', …, …, 'foobar'); +- * $c2 = curry($c1, 'foo', …); +- * var_dump($c2('baz')); // bazbar +- * Obviously, as the first argument is a callable, we can combine this with +- * \Hoa\Consistency\Xcallable ;-). +- * The “…” character is the HORIZONTAL ELLIPSIS Unicode character (Unicode: +- * 2026, UTF-8: E2 80 A6). +- * +- * @param mixed $callable Callable (two parts). +- * @param ... ... Arguments. +- * @return \Closure +- */ +- function curry($callable) +- { +- $arguments = func_get_args(); +- array_shift($arguments); +- $ii = array_keys($arguments, …, true); +- +- return function () use ($callable, $arguments, $ii) { +- return call_user_func_array( +- $callable, +- array_replace($arguments, array_combine($ii, func_get_args())) +- ); +- }; +- } +-} +- + /** + * Flex entity. + */ diff --git a/compiler/patches/Wrapper.diff b/compiler/patches/Wrapper.diff new file mode 100644 index 0000000000..b07c9b0b3b --- /dev/null +++ b/compiler/patches/Wrapper.diff @@ -0,0 +1,27 @@ +--- ../vendor/hoa/protocol/Wrapper.php 2017-01-14 13:26:10.000000000 +0100 ++++ ../vendor/hoa/protocol/Wrapper2.php 2020-05-05 08:39:18.000000000 +0200 +@@ -582,24 +582,3 @@ + stream_wrapper_register('hoa', Wrapper::class); + + } +- +-namespace +-{ +- +-/** +- * Alias of `Hoa\Protocol::resolve` method. +- * +- * @param string $path Path to resolve. +- * @param bool $exists If `true`, try to find the first that exists, +- * else return the first solution. +- * @param bool $unfold Return all solutions instead of one. +- * @return mixed +- */ +-if (!function_exists('resolve')) { +- function resolve($path, $exists = true, $unfold = false) +- { +- return Hoa\Protocol::getInstance()->resolve($path, $exists, $unfold); +- } +-} +- +-} diff --git a/compiler/src/Console/CompileCommand.php b/compiler/src/Console/CompileCommand.php index f4037352e6..39329f5acb 100644 --- a/compiler/src/Console/CompileCommand.php +++ b/compiler/src/Console/CompileCommand.php @@ -7,6 +7,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use function escapeshellarg; final class CompileCommand extends Command { @@ -48,6 +49,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->processFactory->setOutput($output); $this->buildPreloadScript(); + $this->deleteUnnecessaryVendorCode(); + $this->patchFile( + $output, + 'vendor/hoa/consistency/Consistency.php', + 'compiler/patches/Consistency.diff' + ); + $this->patchFile( + $output, + 'vendor/hoa/protocol/Wrapper.php', + 'compiler/patches/Wrapper.diff' + ); $this->fixComposerJson($this->buildDir); $this->renamePhpStormStubs(); @@ -145,4 +157,30 @@ private function buildPreloadScript(): void file_put_contents($preloadScript, sprintf($template, $output)); } + private function deleteUnnecessaryVendorCode(): void + { + $vendorDir = $this->buildDir . '/vendor'; + if (!is_dir($vendorDir . '/nikic/php-parser')) { + return; + } + + @unlink($vendorDir . '/nikic/php-parser/grammar/rebuildParsers.php'); + @unlink($vendorDir . '/nikic/php-parser/bin/php-parse'); + } + + private function patchFile(OutputInterface $output, string $originalFile, string $patchFile): void + { + exec(sprintf( + 'patch -d %s %s %s', + escapeshellarg(realpath(__DIR__ . '/../../..')), + escapeshellarg($originalFile), + escapeshellarg($patchFile) + ), $outputLines, $exitCode); + if ($exitCode === 0) { + return; + } + + $output->writeln(sprintf('Patching failed: %s', implode("\n", $outputLines))); + } + } diff --git a/compiler/tests/Console/CompileCommandTest.php b/compiler/tests/Console/CompileCommandTest.php index f76d673322..085ca91014 100644 --- a/compiler/tests/Console/CompileCommandTest.php +++ b/compiler/tests/Console/CompileCommandTest.php @@ -48,7 +48,7 @@ public function testCommand(): void 'command' => $command->getName(), ]); - self::assertSame('', $commandTester->getDisplay()); + self::assertStringContainsString('Patching failed', $commandTester->getDisplay()); } }