Skip to content

Commit 7c9daa4

Browse files
authored
Prevent ENV var tokens from beeing leaked by commited DI containers
1 parent 5af6526 commit 7c9daa4

6 files changed

Lines changed: 43 additions & 13 deletions

File tree

src/Command/CommandHelper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
use function error_get_last;
4949
use function get_class;
5050
use function getcwd;
51-
use function getenv;
5251
use function gettype;
5352
use function implode;
5453
use function ini_get;
@@ -270,7 +269,7 @@ public static function begin(
270269
$defaultParameters = [
271270
'rootDir' => $containerFactory->getRootDirectory(),
272271
'currentWorkingDirectory' => $containerFactory->getCurrentWorkingDirectory(),
273-
'env' => getenv(),
272+
'env' => Environment::getCleanedArray(),
274273
];
275274

276275
if (isset($projectConfig['parameters']['tmpDir'])) {

src/Command/Environment.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Command;
4+
5+
use function getenv;
6+
use function in_array;
7+
8+
final class Environment
9+
{
10+
11+
private const SENSITIVE_ENV_VARIABLES = [
12+
'GITHUB_TOKEN',
13+
'CI_JOB_TOKEN', // gitlab
14+
'PRIVATE-TOKEN', // gitlab
15+
'TIDEWAYS_APIKEY',
16+
];
17+
18+
/**
19+
* Prevents known sensitive env vars from being leaked, e.g. when container files committed in repositories
20+
*
21+
* @return array<string, string>
22+
*/
23+
public static function getCleanedArray(): array
24+
{
25+
$env = getenv();
26+
$cleanedArray = [];
27+
foreach ($env as $name => $value) {
28+
if (in_array($name, self::SENSITIVE_ENV_VARIABLES, true)) {
29+
continue;
30+
}
31+
$cleanedArray[$name] = $value;
32+
}
33+
return $cleanedArray;
34+
}
35+
36+
}

src/Command/FixerApplication.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
use function defined;
4949
use function escapeshellarg;
5050
use function get_class;
51-
use function getenv;
5251
use function http_build_query;
5352
use function ini_get;
5453
use function is_file;
@@ -269,7 +268,7 @@ private function getFixerProcess(OutputInterface $output, int $serverPort): Proc
269268
throw new FixerProcessException();
270269
}
271270

272-
$env = getenv();
271+
$env = Environment::getCleanedArray();
273272
$env['PHPSTAN_PRO_TMP_DIR'] = $this->proTmpDir;
274273
$forcedPort = $_SERVER['PHPSTAN_PRO_WEB_PORT'] ?? null;
275274
if ($forcedPort !== null) {

src/DependencyInjection/Configurator.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ public function loadContainer(): string
9696
unset($staticParameters['env']['SHELL_VERBOSITY']);
9797
// make sure invocations via blackfire use the same container
9898
unset($staticParameters['env']['BLACKFIRE_AGENT_SOCKET']);
99-
// prevent known sensitive parameter from being leaked, when container files committed in repositories
100-
unset($staticParameters['env']['GITHUB_TOKEN']);
101-
unset($staticParameters['env']['CI_JOB_TOKEN']); // gitlab
102-
unset($staticParameters['env']['PRIVATE-TOKEN']); // gitlab
10399

104100
$containerKey = [
105101
$staticParameters,

src/DependencyInjection/ContainerFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPStan\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber;
2424
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
2525
use PHPStan\Command\CommandHelper;
26+
use PHPStan\Command\Environment;
2627
use PHPStan\File\FileHelper;
2728
use PHPStan\Node\Printer\Printer;
2829
use PHPStan\Php\PhpVersion;
@@ -42,7 +43,6 @@
4243
use function count;
4344
use function dirname;
4445
use function extension_loaded;
45-
use function getenv;
4646
use function implode;
4747
use function ini_get;
4848
use function is_array;
@@ -118,7 +118,7 @@ public function create(
118118
[
119119
'rootDir' => $this->rootDirectory,
120120
'currentWorkingDirectory' => $this->currentWorkingDirectory,
121-
'env' => getenv(),
121+
'env' => Environment::getCleanedArray(),
122122
],
123123
);
124124

@@ -146,7 +146,7 @@ public function create(
146146
'generateBaselineFile' => $generateBaselineFile,
147147
'usedLevel' => $usedLevel,
148148
'cliAutoloadFile' => $cliAutoloadFile,
149-
'env' => getenv(),
149+
'env' => Environment::getCleanedArray(),
150150
], $additionalParameters));
151151
$configurator->addDynamicParameters([
152152
'singleReflectionFile' => $singleReflectionFile,

src/DependencyInjection/LoaderFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace PHPStan\DependencyInjection;
44

55
use Nette\DI\Config\Loader;
6+
use PHPStan\Command\Environment;
67
use PHPStan\File\FileHelper;
7-
use function getenv;
88

99
final class LoaderFactory
1010
{
@@ -32,7 +32,7 @@ public function createLoader(): Loader
3232
$loader->setParameters([
3333
'rootDir' => $this->rootDir,
3434
'currentWorkingDirectory' => $this->currentWorkingDirectory,
35-
'env' => getenv(),
35+
'env' => Environment::getCleanedArray(),
3636
]);
3737

3838
return $loader;

0 commit comments

Comments
 (0)