Skip to content

Commit

Permalink
Reduce the number of unprefixed whitelisted functions in the PHAR
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 5, 2020
1 parent 0baf6de commit 70e559b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
45 changes: 45 additions & 0 deletions 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.
*/
27 changes: 27 additions & 0 deletions 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);
- }
-}
-
-}
38 changes: 38 additions & 0 deletions compiler/src/Console/CompileCommand.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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)));
}

}
2 changes: 1 addition & 1 deletion compiler/tests/Console/CompileCommandTest.php
Expand Up @@ -48,7 +48,7 @@ public function testCommand(): void
'command' => $command->getName(),
]);

self::assertSame('', $commandTester->getDisplay());
self::assertStringContainsString('Patching failed', $commandTester->getDisplay());
}

}

0 comments on commit 70e559b

Please sign in to comment.