From a46c659ea4d440667f21ea7950ab223f8783279e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 20 Jun 2023 10:57:47 +0200 Subject: [PATCH] [FrameworkBundle] Fix secrets:list not displaying local vars --- .../FrameworkBundle/Command/SecretsListCommand.php | 2 +- .../Bundle/FrameworkBundle/Secrets/DotenvVault.php | 8 ++++---- .../Tests/Command/SecretsListCommandTest.php | 14 ++++++++++---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/SecretsListCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/SecretsListCommand.php index abfe2064f8222..8422d2c91a023 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/SecretsListCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/SecretsListCommand.php @@ -86,7 +86,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } foreach ($localSecrets ?? [] as $name => $value) { - if (isset($rows[$name]) && !\in_array($value, ['', false, null], true)) { + if (isset($rows[$name])) { $rows[$name][] = $dump($value); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Secrets/DotenvVault.php b/src/Symfony/Bundle/FrameworkBundle/Secrets/DotenvVault.php index db4891c34b337..994b31d18be59 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Secrets/DotenvVault.php +++ b/src/Symfony/Bundle/FrameworkBundle/Secrets/DotenvVault.php @@ -52,9 +52,9 @@ public function reveal(string $name): ?string { $this->lastMessage = null; $this->validateName($name); - $v = \is_string($_SERVER[$name] ?? null) && !str_starts_with($name, 'HTTP_') ? $_SERVER[$name] : ($_ENV[$name] ?? null); + $v = $_ENV[$name] ?? (str_starts_with($name, 'HTTP_') ? null : ($_SERVER[$name] ?? null)); - if (null === $v) { + if ('' === ($v ?? '')) { $this->lastMessage = sprintf('Secret "%s" not found in "%s".', $name, $this->getPrettyPath($this->dotenvFile)); return null; @@ -89,13 +89,13 @@ public function list(bool $reveal = false): array $secrets = []; foreach ($_ENV as $k => $v) { - if (preg_match('/^\w+$/D', $k)) { + if ('' !== ($v ?? '') && preg_match('/^\w+$/D', $k)) { $secrets[$k] = $reveal ? $v : null; } } foreach ($_SERVER as $k => $v) { - if (\is_string($v) && preg_match('/^\w+$/D', $k)) { + if ('' !== ($v ?? '') && preg_match('/^\w+$/D', $k)) { $secrets[$k] = $reveal ? $v : null; } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsListCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsListCommandTest.php index 933cafcf7c74c..12d3ab2e8ac28 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsListCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/SecretsListCommandTest.php @@ -14,16 +14,22 @@ use PHPUnit\Framework\TestCase; use Symfony\Bundle\FrameworkBundle\Command\SecretsListCommand; use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault; +use Symfony\Bundle\FrameworkBundle\Secrets\DotenvVault; use Symfony\Component\Console\Tester\CommandTester; class SecretsListCommandTest extends TestCase { + /** + * @backupGlobals enabled + */ public function testExecute() { $vault = $this->createMock(AbstractVault::class); $vault->method('list')->willReturn(['A' => 'a', 'B' => 'b', 'C' => null, 'D' => null, 'E' => null]); - $localVault = $this->createMock(AbstractVault::class); - $localVault->method('list')->willReturn(['A' => '', 'B' => 'A', 'C' => '', 'D' => false, 'E' => null]); + + $_ENV = ['A' => '', 'B' => 'A', 'C' => '', 'D' => false, 'E' => null]; + $localVault = new DotenvVault('/not/a/path'); + $command = new SecretsListCommand($vault, $localVault); $tester = new CommandTester($command); $this->assertSame(0, $tester->execute([])); @@ -37,9 +43,9 @@ public function testExecute() Secret Value Local Value -------- -------- ------------- A "a" - B "b" "A" + B "b" ****** C ****** - D ****** + D ****** ****** E ****** -------- -------- -------------